1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
//! Core execution traits for TensorLogic engines.
use tensorlogic_ir::EinsumGraph;
use crate::ops::{ElemOp, ReduceOp};
/// Core tensor execution interface.
///
/// Implementations provide the fundamental tensor operations required
/// for executing compiled TensorLogic programs.
pub trait TlExecutor {
type Tensor;
type Error;
/// Execute an einsum operation on input tensors.
fn einsum(&mut self, spec: &str, inputs: &[Self::Tensor]) -> Result<Self::Tensor, Self::Error>;
/// Apply an element-wise unary operation.
fn elem_op(&mut self, op: ElemOp, x: &Self::Tensor) -> Result<Self::Tensor, Self::Error>;
/// Apply an element-wise binary operation.
fn elem_op_binary(
&mut self,
op: ElemOp,
x: &Self::Tensor,
y: &Self::Tensor,
) -> Result<Self::Tensor, Self::Error>;
/// Reduce a tensor along specified axes.
fn reduce(
&mut self,
op: ReduceOp,
x: &Self::Tensor,
axes: &[usize],
) -> Result<Self::Tensor, Self::Error>;
}
/// Automatic differentiation interface.
///
/// Extends `TlExecutor` with forward/backward pass capabilities for training.
pub trait TlAutodiff: TlExecutor {
type Tape;
/// Execute forward pass on an EinsumGraph.
fn forward(&mut self, graph: &EinsumGraph) -> Result<Self::Tensor, Self::Error>;
/// Execute backward pass to compute gradients.
fn backward(
&mut self,
graph: &EinsumGraph,
loss: &Self::Tensor,
) -> Result<Self::Tape, Self::Error>;
}