Fast tropical matrix multiplication with automatic differentiation support
Project description
tropical-gemm
Fast tropical matrix multiplication with automatic differentiation support.
Installation
# From PyPI
pip install tropical-gemm
# With PyTorch support (for automatic differentiation)
pip install tropical-gemm[torch]
# For GPU support (requires CUDA toolkit)
pip install maturin
git clone https://github.com/TensorBFS/tropical-gemm
cd tropical-gemm/crates/tropical-gemm-python
maturin develop --features cuda
Quick Start
import numpy as np
import tropical_gemm
# Create matrices
a = np.array([[1.0, 2.0, 3.0],
[4.0, 5.0, 6.0]], dtype=np.float32)
b = np.array([[1.0, 2.0],
[3.0, 4.0],
[5.0, 6.0]], dtype=np.float32)
# MaxPlus tropical matmul: C[i,j] = max_k(A[i,k] + B[k,j])
c = tropical_gemm.maxplus_matmul(a, b)
print("MaxPlus result:", c)
# MinPlus tropical matmul: C[i,j] = min_k(A[i,k] + B[k,j])
c = tropical_gemm.minplus_matmul(a, b)
print("MinPlus result:", c)
# MaxMul tropical matmul: C[i,j] = max_k(A[i,k] * B[k,j])
c = tropical_gemm.maxmul_matmul(a, b)
print("MaxMul result:", c)
# With argmax for backpropagation
c, argmax = tropical_gemm.maxplus_matmul_with_argmax(a, b)
print("Result:", c)
print("Argmax:", argmax)
# GPU acceleration (if compiled with CUDA)
if tropical_gemm.cuda_available():
c = tropical_gemm.maxplus_matmul_gpu(a, b)
c = tropical_gemm.minplus_matmul_gpu(a, b)
c = tropical_gemm.maxmul_matmul_gpu(a, b)
PyTorch Integration
The package includes a pytorch submodule with pre-built autograd functions:
import torch
from tropical_gemm.pytorch import (
# CPU operations
tropical_maxplus_matmul,
tropical_minplus_matmul,
tropical_maxmul_matmul,
# GPU operations (requires CUDA)
tropical_maxplus_matmul_gpu,
tropical_minplus_matmul_gpu,
tropical_maxmul_matmul_gpu,
GPU_AVAILABLE,
)
# Create tensors with gradient tracking
a = torch.randn(100, 50, requires_grad=True)
b = torch.randn(50, 80, requires_grad=True)
# Forward pass
c = tropical_maxplus_matmul(a, b)
# Backward pass - gradients computed automatically
loss = c.sum()
loss.backward()
print(f"grad_a shape: {a.grad.shape}") # (100, 50)
print(f"grad_b shape: {b.grad.shape}") # (50, 80)
# Use GPU for larger matrices
if GPU_AVAILABLE:
c = tropical_maxplus_matmul_gpu(a, b)
Gradient Semantics
The gradient computation depends on the semiring type:
MaxPlus/MinPlus (additive rule):
grad_A[i,k] = grad_C[i,j]ifk == argmax[i,j]grad_B[k,j] = grad_C[i,j]ifk == argmax[i,j]
MaxMul (multiplicative rule):
grad_A[i,k] = grad_C[i,j] * B[k,j]ifk == argmax[i,j]grad_B[k,j] = grad_C[i,j] * A[i,k]ifk == argmax[i,j]
API Reference
Core Functions
| Function | Description |
|---|---|
maxplus_matmul(a, b) |
MaxPlus: C[i,j] = max_k(A[i,k] + B[k,j]) |
minplus_matmul(a, b) |
MinPlus: C[i,j] = min_k(A[i,k] + B[k,j]) |
maxmul_matmul(a, b) |
MaxMul: C[i,j] = max_k(A[i,k] * B[k,j]) |
*_with_argmax(a, b) |
Returns (result, argmax) for backpropagation |
backward_a(grad_c, argmax, k) |
Gradient w.r.t. A (additive rule) |
backward_b(grad_c, argmax, k) |
Gradient w.r.t. B (additive rule) |
maxmul_backward_a(grad_c, argmax, b) |
Gradient w.r.t. A (multiplicative rule) |
maxmul_backward_b(grad_c, argmax, a) |
Gradient w.r.t. B (multiplicative rule) |
GPU Functions (requires CUDA)
| Function | Description |
|---|---|
cuda_available() |
Check if CUDA support is available |
maxplus_matmul_gpu(a, b) |
GPU MaxPlus matmul |
minplus_matmul_gpu(a, b) |
GPU MinPlus matmul |
maxmul_matmul_gpu(a, b) |
GPU MaxMul matmul |
*_gpu_with_argmax(a, b) |
GPU matmul with argmax tracking |
Data Types
All functions support:
f32(default):maxplus_matmul, etc.f64:maxplus_matmul_f64, etc.i32:maxplus_matmul_i32, etc.i64:maxplus_matmul_i64, etc.
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distributions
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file tropical_gemm-0.3.0-cp312-cp312-win_amd64.whl.
File metadata
- Download URL: tropical_gemm-0.3.0-cp312-cp312-win_amd64.whl
- Upload date:
- Size: 240.0 kB
- Tags: CPython 3.12, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
01d8f8814db2267f2a68eb4d7e0e85b5be4415a9ee5835c91476e36f02d376c7
|
|
| MD5 |
d2452a352b9fd57f96c0e1fb269c98d4
|
|
| BLAKE2b-256 |
06d8ced97ee4f3fc802acebc18a458e562a127f2aa287603aaa72b52fd9b13fd
|
File details
Details for the file tropical_gemm-0.3.0-cp312-cp312-manylinux_2_34_x86_64.whl.
File metadata
- Download URL: tropical_gemm-0.3.0-cp312-cp312-manylinux_2_34_x86_64.whl
- Upload date:
- Size: 385.2 kB
- Tags: CPython 3.12, manylinux: glibc 2.34+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5ddd4630bf06128c828a8c142366a48bf3c966824de029ba6ffa42366288398e
|
|
| MD5 |
e0db5372c7b08c117f58424baaf57173
|
|
| BLAKE2b-256 |
f65dfd3551f6c0bf6bd0801bd7a844ff8fd4303e2fbbba66fe387a67557049eb
|
File details
Details for the file tropical_gemm-0.3.0-cp312-cp312-macosx_11_0_arm64.whl.
File metadata
- Download URL: tropical_gemm-0.3.0-cp312-cp312-macosx_11_0_arm64.whl
- Upload date:
- Size: 320.5 kB
- Tags: CPython 3.12, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
47a06df089b07790457d98020fc13df4c2dd38ba90796fc94d207e33258bcebc
|
|
| MD5 |
9ddd8c41562f6eb0415719559d765c06
|
|
| BLAKE2b-256 |
961b4d8ad0f99d999baacd46bb270776c47d21020fae6a31aff9cfd0c9e44a33
|
File details
Details for the file tropical_gemm-0.3.0-cp311-cp311-win_amd64.whl.
File metadata
- Download URL: tropical_gemm-0.3.0-cp311-cp311-win_amd64.whl
- Upload date:
- Size: 240.6 kB
- Tags: CPython 3.11, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4d8695dd92bf7d05e8068b9ed6fe13857a082e37121cbfaeec4434bd62163d28
|
|
| MD5 |
35dc7fe5054e990658b2e2f17c220aec
|
|
| BLAKE2b-256 |
c0d6ac34813aed859ade9963075a671ccd68ff3a192938a25612d00961a4c687
|
File details
Details for the file tropical_gemm-0.3.0-cp311-cp311-manylinux_2_34_x86_64.whl.
File metadata
- Download URL: tropical_gemm-0.3.0-cp311-cp311-manylinux_2_34_x86_64.whl
- Upload date:
- Size: 386.7 kB
- Tags: CPython 3.11, manylinux: glibc 2.34+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b73768fc37437329d44b1d6fbce5a5b14294d7d9350ac07347d56d810eadadce
|
|
| MD5 |
59362e3f2ca9d2866ee89b5b3fc05f6c
|
|
| BLAKE2b-256 |
cf27fac199e6cd3a17e1b1f1ee27071c5b16d3c9d5a33048c69f04d6d3ca1fc3
|
File details
Details for the file tropical_gemm-0.3.0-cp311-cp311-macosx_11_0_arm64.whl.
File metadata
- Download URL: tropical_gemm-0.3.0-cp311-cp311-macosx_11_0_arm64.whl
- Upload date:
- Size: 321.2 kB
- Tags: CPython 3.11, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8e4dfb99ea557844c8d8e5ed8c13b75debd7e85d74d00b319bb443673ff6d7cf
|
|
| MD5 |
92a8f9b86f9e5bbc4a70df41a7212f70
|
|
| BLAKE2b-256 |
414bc0f0a9acda21c4a23bed0879677106518bda1083b410ea5eb40ce995c8fa
|
File details
Details for the file tropical_gemm-0.3.0-cp310-cp310-win_amd64.whl.
File metadata
- Download URL: tropical_gemm-0.3.0-cp310-cp310-win_amd64.whl
- Upload date:
- Size: 240.7 kB
- Tags: CPython 3.10, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
384ea8e036eaa66919e5937927f88bdd32227985df01951d7128ef4827a16d84
|
|
| MD5 |
bae02183360e3c12b95edce9f1efc03e
|
|
| BLAKE2b-256 |
56f41407ca725207bf6db791f2b3a6b98dec73e219acc6e4239ee81d7ad506d6
|
File details
Details for the file tropical_gemm-0.3.0-cp310-cp310-manylinux_2_34_x86_64.whl.
File metadata
- Download URL: tropical_gemm-0.3.0-cp310-cp310-manylinux_2_34_x86_64.whl
- Upload date:
- Size: 387.1 kB
- Tags: CPython 3.10, manylinux: glibc 2.34+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
be6c33b12c90cc5796753ab7d3d44de497bb06ea37d6463697728ffbc3b826a8
|
|
| MD5 |
ab8064ea12856d6f006fdc2e53417da5
|
|
| BLAKE2b-256 |
0ef0c827a91ef736f1d06c4c2c05ed177e8b60eb1cde4f41376465d431496d32
|
File details
Details for the file tropical_gemm-0.3.0-cp310-cp310-macosx_11_0_arm64.whl.
File metadata
- Download URL: tropical_gemm-0.3.0-cp310-cp310-macosx_11_0_arm64.whl
- Upload date:
- Size: 321.5 kB
- Tags: CPython 3.10, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4e0e8377dea90044e5edeb5de61cd929307d0f00b90514c63f34d65708c4e998
|
|
| MD5 |
4d805525d7e92d94f9cb250f46083566
|
|
| BLAKE2b-256 |
db170fe8775d5223460809c6ccb269adb55a361bb6c861f60bc74c9bcb2a51b2
|
File details
Details for the file tropical_gemm-0.3.0-cp39-cp39-win_amd64.whl.
File metadata
- Download URL: tropical_gemm-0.3.0-cp39-cp39-win_amd64.whl
- Upload date:
- Size: 243.8 kB
- Tags: CPython 3.9, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d1fbf49d65ad56cb5a819a261a40feace4305aff60613edb8e8434585445cc32
|
|
| MD5 |
638cf62b47bfcd8f0f067577ed01d8f2
|
|
| BLAKE2b-256 |
f2ee0d582090551e0fbb9a87fc44584fc7d99876ad8078ba3752a5deeec9cde4
|
File details
Details for the file tropical_gemm-0.3.0-cp39-cp39-manylinux_2_34_x86_64.whl.
File metadata
- Download URL: tropical_gemm-0.3.0-cp39-cp39-manylinux_2_34_x86_64.whl
- Upload date:
- Size: 391.1 kB
- Tags: CPython 3.9, manylinux: glibc 2.34+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0455bae41a2432fa378b2415db8586b9158ebe953576845662365aed38ee1120
|
|
| MD5 |
7410d3a1ef8a896c5ed6a65200add238
|
|
| BLAKE2b-256 |
2a53cb6155df16590c41104a9ad9c36505fe902d6ed21136f61e2518415d031e
|
File details
Details for the file tropical_gemm-0.3.0-cp39-cp39-macosx_11_0_arm64.whl.
File metadata
- Download URL: tropical_gemm-0.3.0-cp39-cp39-macosx_11_0_arm64.whl
- Upload date:
- Size: 323.9 kB
- Tags: CPython 3.9, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a7abf4cc2d97c5d7e72e5a48af242ea74bb6e84191f2d1c5ed3cf4c92c655853
|
|
| MD5 |
2b4b8cff5e74635f24d499591602dfb9
|
|
| BLAKE2b-256 |
e77ac33bd5643c87a2c47abdbaadaca59d5621c48217d7b74350cddfe1fcd03b
|