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
//! Semiring matrix multiplication trait.
use crate::error::{Error, Result};
use crate::ops::semiring::SemiringOp;
use crate::runtime::Runtime;
use crate::tensor::Tensor;
/// Generalized matrix multiplication over arbitrary semirings.
///
/// Standard matmul computes `` `C[i,j] = Σ_k A[i,k] * B[k,j]` `` using the (+, ×) semiring.
/// This trait generalizes to `` `C[i,j] = ⊕_k (A[i,k] ⊗ B[k,j])` `` for any semiring (⊕, ⊗).
///
/// # Supported Semirings
///
/// | Variant | Reduce (⊕) | Combine (⊗) | Application |
/// |-----------|-----------|------------|-------------|
/// | MinPlus | min | + | Shortest paths |
/// | MaxPlus | max | + | Longest paths |
/// | MaxMin | max | min | Bottleneck/capacity |
/// | MinMax | min | max | Fuzzy relations |
/// | OrAnd | OR | AND | Transitive closure |
/// | PlusMax | + | max | DP formulations |
///
/// # DType Support
///
/// - **F32, F64, I32, I64**: All semiring ops except OrAnd
/// - **F16, BF16**: All semiring ops except OrAnd (with `f16` feature)
/// - **Bool**: OrAnd only
///
/// # Batched Operation
///
/// Supports batched matmul with the same broadcasting rules as standard matmul.
pub trait SemiringMatmulOps<R: Runtime> {
/// Generalized semiring matrix multiplication.
///
/// Computes `` `C[i,j] = ⊕_k (A[i,k] ⊗ B[k,j])` `` where ⊕ and ⊗ are defined by `op`.
///
/// # Arguments
///
/// * `a` - Input tensor of shape `` `[..., M, K]` ``
/// * `b` - Input tensor of shape `` `[..., K, N]` ``
/// * `op` - The semiring operation to use
///
/// # Returns
///
/// Output tensor of shape `` `[..., M, N]` ``
fn semiring_matmul(&self, a: &Tensor<R>, b: &Tensor<R>, op: SemiringOp) -> Result<Tensor<R>> {
let _ = (a, b, op);
Err(Error::NotImplemented {
feature: "SemiringMatmulOps::semiring_matmul",
})
}
}