pub trait Algebra: Semiring {
type Index: Copy + Clone + Send + Sync + Default + Debug + 'static;
// Required methods
fn add_with_argmax(
self,
self_idx: Self::Index,
rhs: Self,
rhs_idx: Self::Index,
) -> (Self, Self::Index);
fn add_backward(
self,
rhs: Self,
grad_out: Self::Scalar,
winner_idx: Option<Self::Index>,
) -> (Self::Scalar, Self::Scalar);
fn mul_backward(
self,
rhs: Self,
grad_out: Self::Scalar,
) -> (Self::Scalar, Self::Scalar);
fn is_better(&self, other: &Self) -> bool;
// Provided method
fn needs_argmax() -> bool { ... }
}Expand description
Extended semiring operations for automatic differentiation.
This trait adds argmax tracking needed for tropical backpropagation.
Required Associated Types§
Required Methods§
Sourcefn add_with_argmax(
self,
self_idx: Self::Index,
rhs: Self,
rhs_idx: Self::Index,
) -> (Self, Self::Index)
fn add_with_argmax( self, self_idx: Self::Index, rhs: Self, rhs_idx: Self::Index, ) -> (Self, Self::Index)
Addition with argmax tracking.
Returns (result, winner_index) where winner_index indicates which operand “won” the addition (relevant for tropical max/min).
Sourcefn add_backward(
self,
rhs: Self,
grad_out: Self::Scalar,
winner_idx: Option<Self::Index>,
) -> (Self::Scalar, Self::Scalar)
fn add_backward( self, rhs: Self, grad_out: Self::Scalar, winner_idx: Option<Self::Index>, ) -> (Self::Scalar, Self::Scalar)
Backward pass for addition.
Given output gradient grad_out, compute gradients for inputs.
For standard arithmetic: both inputs get grad_out.
For tropical: only the winner gets grad_out.
Sourcefn mul_backward(
self,
rhs: Self,
grad_out: Self::Scalar,
) -> (Self::Scalar, Self::Scalar)
fn mul_backward( self, rhs: Self, grad_out: Self::Scalar, ) -> (Self::Scalar, Self::Scalar)
Backward pass for multiplication.
Given output gradient grad_out, compute gradients for inputs.
Standard: grad_a = grad_out × b, grad_b = grad_out × a
Tropical (add): grad_a = grad_out, grad_b = grad_out
Provided Methods§
Sourcefn needs_argmax() -> bool
fn needs_argmax() -> bool
Whether this algebra requires argmax tracking for backprop.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.