tenferro_einsum/execution/
backend.rs

1use tenferro_algebra::Semiring;
2use tenferro_prims::{TensorSemiringCore, TensorSemiringFastPath};
3
4/// Context type used by public einsum APIs for a backend/algebra pair.
5///
6/// # Examples
7///
8/// ```ignore
9/// use tenferro_algebra::Standard;
10/// use tenferro_einsum::BackendContext;
11/// use tenferro_prims::{CpuBackend, CpuContext};
12///
13/// let mut ctx: BackendContext<Standard<f64>, CpuBackend> = CpuContext::new(1);
14/// let _ = &mut ctx;
15/// ```
16pub type BackendContext<Alg, Backend> = <Backend as TensorSemiringCore<Alg>>::Context;
17pub(crate) type BackendPlan<Alg, Backend> = <Backend as TensorSemiringCore<Alg>>::Plan;
18
19/// Public backend contract accepted by `tenferro-einsum`.
20///
21/// Backends must provide the semiring core operations needed for all einsum
22/// execution paths, plus the optional semiring fast paths used for optimized
23/// contraction and elementwise multiplication.
24///
25/// # Examples
26///
27/// ```ignore
28/// use tenferro_algebra::Standard;
29/// use tenferro_einsum::EinsumBackend;
30/// use tenferro_prims::CpuBackend;
31///
32/// fn assert_backend<B: EinsumBackend<Standard<f64>>>() {}
33/// assert_backend::<CpuBackend>();
34/// ```
35pub trait EinsumBackend<Alg: Semiring>:
36    TensorSemiringCore<Alg>
37    + TensorSemiringFastPath<
38        Alg,
39        Plan = <Self as TensorSemiringCore<Alg>>::Plan,
40        Context = <Self as TensorSemiringCore<Alg>>::Context,
41    >
42{
43}
44
45impl<Alg, Backend> EinsumBackend<Alg> for Backend
46where
47    Alg: Semiring,
48    Backend: TensorSemiringCore<Alg>
49        + TensorSemiringFastPath<
50            Alg,
51            Plan = <Backend as TensorSemiringCore<Alg>>::Plan,
52            Context = <Backend as TensorSemiringCore<Alg>>::Context,
53        >,
54{
55}