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§
Sourcefn from_slice(&self, data: &[f32], shape: &Shape) -> Result<Storage>
fn from_slice(&self, data: &[f32], shape: &Shape) -> Result<Storage>
Creates a tensor from a slice of f32 values.
Sourcefn sub(&self, lhs: &Storage, rhs: &Storage) -> Result<Storage>
fn sub(&self, lhs: &Storage, rhs: &Storage) -> Result<Storage>
Performs element-wise subtraction.
Sourcefn mul(&self, lhs: &Storage, rhs: &Storage) -> Result<Storage>
fn mul(&self, lhs: &Storage, rhs: &Storage) -> Result<Storage>
Performs element-wise multiplication.
Sourcefn sum(
&self,
storage: &Storage,
shape: &Shape,
axis: Option<usize>,
) -> Result<Storage>
fn sum( &self, storage: &Storage, shape: &Shape, axis: Option<usize>, ) -> Result<Storage>
Computes the sum of elements along an optional axis.
Sourcefn mean(
&self,
storage: &Storage,
shape: &Shape,
axis: Option<usize>,
) -> Result<Storage>
fn mean( &self, storage: &Storage, shape: &Shape, axis: Option<usize>, ) -> Result<Storage>
Computes the mean of elements along an optional axis.
Sourcefn transpose(&self, storage: &Storage, shape: &Shape) -> Result<Storage>
fn transpose(&self, storage: &Storage, shape: &Shape) -> Result<Storage>
Transposes a 2D tensor.
Sourcefn to_vec_f32(&self, storage: &Storage) -> Result<Vec<f32>>
fn to_vec_f32(&self, storage: &Storage) -> Result<Vec<f32>>
Converts the storage to a vector of f32 values.
Sourcefn matmul(
&self,
lhs: &Storage,
rhs: &Storage,
lhs_shape: &Shape,
rhs_shape: &Shape,
) -> Result<Storage>
fn matmul( &self, lhs: &Storage, rhs: &Storage, lhs_shape: &Shape, rhs_shape: &Shape, ) -> Result<Storage>
Matrix multiplication for 2D tensors.
Sourcefn bmm(
&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>
Batched matrix multiplication for 3D tensors.
Provided Methods§
Sourcefn is_available(&self) -> bool
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).