MatWithArgmax

Struct MatWithArgmax 

Source
pub struct MatWithArgmax<S: TropicalWithArgmax> {
    pub values: Mat<S>,
    pub argmax: Vec<u32>,
}
Expand description

Result of matrix multiplication with argmax tracking.

Fields§

§values: Mat<S>

The result matrix values.

§argmax: Vec<u32>

The argmax indices (which k produced each C[i,j]).

Implementations§

Source§

impl<S: TropicalWithArgmax<Index = u32>> MatWithArgmax<S>

Source

pub fn get(&self, i: usize, j: usize) -> S

Get the value at position (i, j).

Source

pub fn get_value(&self, i: usize, j: usize) -> S::Scalar

Get the scalar value at position (i, j).

This is a convenience method that extracts the underlying scalar without requiring a trait import.

Source

pub fn get_argmax(&self, i: usize, j: usize) -> u32

Get the argmax index at position (i, j).

Source

pub fn nrows(&self) -> usize

Number of rows.

Source

pub fn ncols(&self) -> usize

Number of columns.

Source

pub fn argmax_slice(&self) -> &[u32]

Get the argmax indices as a slice.

This is useful for backward pass computation.

Source

pub fn backward_a<G>(&self, grad_c: &Mat<G>, k: usize) -> Mat<G>

Compute gradient with respect to matrix A.

Given the upstream gradient dL/dC, computes dL/dA using the argmax indices from the forward pass.

For C = A ⊗ B where C[i,j] = ⊕_k (A[i,k] ⊗ B[k,j]): dL/dA[i,k] = Σ_j { dL/dC[i,j] if argmax[i,j] == k }

§Arguments
  • grad_c - Gradient of the loss with respect to C, dimensions m×n
  • k - Number of columns in A (the inner dimension)
§Returns

Gradient of the loss with respect to A, dimensions m×k

§Example
use tropical_gemm::{Mat, MaxPlus, TropicalMaxPlus};

let a = Mat::<MaxPlus<f64>>::from_row_major(&[1.0, 2.0, 3.0, 4.0, 5.0, 6.0], 2, 3);
let b = Mat::<MaxPlus<f64>>::from_row_major(&[1.0, 2.0, 3.0, 4.0, 5.0, 6.0], 3, 2);

// Forward pass with argmax
let result = a.matmul_argmax(&b);

// Backward pass: grad_c is upstream gradient (e.g., all ones)
let grad_c = Mat::<MaxPlus<f64>>::from_fn(2, 2, |_, _| TropicalMaxPlus(1.0));
let grad_a = result.backward_a(&grad_c, 3); // k=3 (columns in A)

assert_eq!(grad_a.nrows(), 2);
assert_eq!(grad_a.ncols(), 3);
Source

pub fn backward_b<G>(&self, grad_c: &Mat<G>, k: usize) -> Mat<G>

Compute gradient with respect to matrix B.

Given the upstream gradient dL/dC, computes dL/dB using the argmax indices from the forward pass.

For C = A ⊗ B where C[i,j] = ⊕_k (A[i,k] ⊗ B[k,j]): dL/dB[k,j] = Σ_i { dL/dC[i,j] if argmax[i,j] == k }

§Arguments
  • grad_c - Gradient of the loss with respect to C, dimensions m×n
  • k - Number of rows in B (the inner dimension)
§Returns

Gradient of the loss with respect to B, dimensions k×n

§Example
use tropical_gemm::{Mat, MaxPlus, TropicalMaxPlus};

let a = Mat::<MaxPlus<f64>>::from_row_major(&[1.0, 2.0, 3.0, 4.0, 5.0, 6.0], 2, 3);
let b = Mat::<MaxPlus<f64>>::from_row_major(&[1.0, 2.0, 3.0, 4.0, 5.0, 6.0], 3, 2);

// Forward pass with argmax
let result = a.matmul_argmax(&b);

// Backward pass: grad_c is upstream gradient
let grad_c = Mat::<MaxPlus<f64>>::from_fn(2, 2, |_, _| TropicalMaxPlus(1.0));
let grad_b = result.backward_b(&grad_c, 3); // k=3 (rows in B)

assert_eq!(grad_b.nrows(), 3);
assert_eq!(grad_b.ncols(), 2);

Auto Trait Implementations§

§

impl<S> Freeze for MatWithArgmax<S>

§

impl<S> RefUnwindSafe for MatWithArgmax<S>
where S: RefUnwindSafe,

§

impl<S> Send for MatWithArgmax<S>

§

impl<S> Sync for MatWithArgmax<S>

§

impl<S> Unpin for MatWithArgmax<S>
where S: Unpin,

§

impl<S> UnwindSafe for MatWithArgmax<S>
where S: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
§

impl<T> Pointable for T

§

const ALIGN: usize

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.