Fast tropical matrix multiplication with automatic differentiation support
Project description
tropical-gemm
Fast tropical matrix multiplication with automatic differentiation support.
Installation
# From source (requires Rust toolchain)
cd crates/tropical-gemm-python
pip install maturin
maturin develop
# Or build a wheel
maturin build --release
pip install target/wheels/tropical_gemm-*.whl
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)
# With argmax for backpropagation
c, argmax = tropical_gemm.maxplus_matmul_with_argmax(a, b)
print("Result:", c)
print("Argmax:", argmax)
PyTorch Integration
See examples/pytorch_tropical.py for a complete example of using tropical GEMM with PyTorch autograd.
import torch
import tropical_gemm
class TropicalMaxPlusMatmul(torch.autograd.Function):
@staticmethod
def forward(ctx, a, b):
a_np = a.detach().numpy()
b_np = b.detach().numpy()
c_np, argmax_np = tropical_gemm.maxplus_matmul_with_argmax(a_np, b_np)
ctx.save_for_backward(torch.from_numpy(argmax_np))
ctx.k = a.shape[1]
return torch.from_numpy(c_np)
@staticmethod
def backward(ctx, grad_c):
argmax, = ctx.saved_tensors
k = ctx.k
grad_c_np = grad_c.numpy()
argmax_np = argmax.numpy()
grad_a = torch.from_numpy(tropical_gemm.backward_a(grad_c_np, argmax_np, k))
grad_b = torch.from_numpy(tropical_gemm.backward_b(grad_c_np, argmax_np, k))
return grad_a, grad_b
# Use like a regular PyTorch operation
a = torch.randn(3, 4, requires_grad=True)
b = torch.randn(4, 5, requires_grad=True)
c = TropicalMaxPlusMatmul.apply(a, b)
c.sum().backward()
API Reference
Functions
maxplus_matmul(a, b)- MaxPlus tropical matmul: C[i,j] = max_k(A[i,k] + B[k,j])minplus_matmul(a, b)- MinPlus tropical matmul: C[i,j] = min_k(A[i,k] + B[k,j])maxplus_matmul_with_argmax(a, b)- MaxPlus with argmax indices for backpropminplus_matmul_with_argmax(a, b)- MinPlus with argmax indices for backpropbackward_a(grad_c, argmax, k)- Compute gradient w.r.t. Abackward_b(grad_c, argmax, k)- Compute gradient w.r.t. B
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.1.0-cp312-cp312-win_amd64.whl.
File metadata
- Download URL: tropical_gemm-0.1.0-cp312-cp312-win_amd64.whl
- Upload date:
- Size: 208.3 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 |
40dc2e8b563927d3dd4882195ccf0509e2e6ca8f4fc8a549b1a3bec45e64a8aa
|
|
| MD5 |
20f38351d1af8a6f8787b8253ea06feb
|
|
| BLAKE2b-256 |
4080d1cf0480017d35a7a55a73fd899cfa6ae9eb6223dc592de3caec40af3c3c
|
File details
Details for the file tropical_gemm-0.1.0-cp312-cp312-manylinux_2_34_x86_64.whl.
File metadata
- Download URL: tropical_gemm-0.1.0-cp312-cp312-manylinux_2_34_x86_64.whl
- Upload date:
- Size: 361.4 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 |
70b812d185224a3d7bf706da2c7a45eab5d78cef4d1fdfc87ece6c9e5e9564c1
|
|
| MD5 |
1304284fec2f98d44d5318d7656559db
|
|
| BLAKE2b-256 |
ba8bb698f0e6b329a320038871dd707f13d4c8e398a2c8a070a4bb89e49e2234
|
File details
Details for the file tropical_gemm-0.1.0-cp312-cp312-macosx_11_0_arm64.whl.
File metadata
- Download URL: tropical_gemm-0.1.0-cp312-cp312-macosx_11_0_arm64.whl
- Upload date:
- Size: 298.6 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 |
53348214061371f28b4f299ccd729d38bc47ebde852f2485228be5204cc68c8f
|
|
| MD5 |
246ede958a0c5d8d82de3cff135f93a7
|
|
| BLAKE2b-256 |
6fe57f8acf3c4db76af59e5939e0ef24f0964f28c2f0e5f104fa01d7f2294026
|
File details
Details for the file tropical_gemm-0.1.0-cp311-cp311-win_amd64.whl.
File metadata
- Download URL: tropical_gemm-0.1.0-cp311-cp311-win_amd64.whl
- Upload date:
- Size: 208.2 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 |
c27306fc353330fcfb0eeee422d1ac545768c7200f725f8bb7391fa0f7606b54
|
|
| MD5 |
a2efaf47a290badd90969038e821d7ce
|
|
| BLAKE2b-256 |
48197def2f19654576eb27e9f538bcbb56af158195dac35366ecaca613048de8
|
File details
Details for the file tropical_gemm-0.1.0-cp311-cp311-manylinux_2_34_x86_64.whl.
File metadata
- Download URL: tropical_gemm-0.1.0-cp311-cp311-manylinux_2_34_x86_64.whl
- Upload date:
- Size: 361.9 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 |
4407b647e6a42bb9cc5270b3aa46bd8f5b6b570f01635e2520ca6b8c8819bca6
|
|
| MD5 |
03d95e2f3798c3c187b909132988931d
|
|
| BLAKE2b-256 |
c18cc06a4cf33f83e137161d6c74097d33bdbe697e85761f2aa3b94d41a8e904
|
File details
Details for the file tropical_gemm-0.1.0-cp311-cp311-macosx_11_0_arm64.whl.
File metadata
- Download URL: tropical_gemm-0.1.0-cp311-cp311-macosx_11_0_arm64.whl
- Upload date:
- Size: 301.5 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 |
c2ccc810451820eca8b42f9d183e2039a8c48c123bb6f3e5b230a5fab16acc57
|
|
| MD5 |
87fb133c342a5df13d6e9999beeed24b
|
|
| BLAKE2b-256 |
749e1f30b82557dfafe6f0bceb87e75b32fa031e1227868b068f2abd0eb4b546
|
File details
Details for the file tropical_gemm-0.1.0-cp310-cp310-win_amd64.whl.
File metadata
- Download URL: tropical_gemm-0.1.0-cp310-cp310-win_amd64.whl
- Upload date:
- Size: 208.8 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 |
452f2130986c4cae4ac73a3058f11a0ba8ca3f275e937b1ea7e15f8fcfa003db
|
|
| MD5 |
1d78d0fddbb9f6e4c14bac4d97b5af5d
|
|
| BLAKE2b-256 |
1fa22284a4ddbaec043007f07847311d9cf9dff1977546abb6021711d76fdb5b
|
File details
Details for the file tropical_gemm-0.1.0-cp310-cp310-manylinux_2_34_x86_64.whl.
File metadata
- Download URL: tropical_gemm-0.1.0-cp310-cp310-manylinux_2_34_x86_64.whl
- Upload date:
- Size: 362.6 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 |
ce0de28274c153c77dbe1c5c9da28cc19c7f1e8293ba4455d2ebe94b5fdcd99c
|
|
| MD5 |
3b6419694d72b274fa8a9db612036155
|
|
| BLAKE2b-256 |
5f6424ef1c22c998eeae2ef6c1616ac3031cbc149b1db16f223bf47ea3d73fb3
|
File details
Details for the file tropical_gemm-0.1.0-cp310-cp310-macosx_11_0_arm64.whl.
File metadata
- Download URL: tropical_gemm-0.1.0-cp310-cp310-macosx_11_0_arm64.whl
- Upload date:
- Size: 301.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 |
1e07c12e1613021465fa1f7de5d963f905552e8cd5f1154a82e3a4d9b9081299
|
|
| MD5 |
4c05b649fc3131e724dd19422379ff32
|
|
| BLAKE2b-256 |
108e9734aff2bef2829e4600308daa26b6fd5e38f4b056124ea87c735286fea5
|
File details
Details for the file tropical_gemm-0.1.0-cp39-cp39-win_amd64.whl.
File metadata
- Download URL: tropical_gemm-0.1.0-cp39-cp39-win_amd64.whl
- Upload date:
- Size: 209.4 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 |
c6043ed2b01a3387877d33cfce37332c6a43631fe9f0f096157ddb064c3e211d
|
|
| MD5 |
f443dcc83061713fe405b9c603628317
|
|
| BLAKE2b-256 |
49f49f06808a44e03435279fd283eb594d471afb085b00e5fbeb118929149c37
|
File details
Details for the file tropical_gemm-0.1.0-cp39-cp39-manylinux_2_34_x86_64.whl.
File metadata
- Download URL: tropical_gemm-0.1.0-cp39-cp39-manylinux_2_34_x86_64.whl
- Upload date:
- Size: 363.5 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 |
5183548c7f9f3cef058b63d36f0a7f6f4e5797c9efbc53fc203c00cc38fbc834
|
|
| MD5 |
ed650248dac32cffa119f0eda2a195ba
|
|
| BLAKE2b-256 |
e039084b7d932052afc57d56014be6ba3bc589db9558e760a79e6e03a2523d9a
|
File details
Details for the file tropical_gemm-0.1.0-cp39-cp39-macosx_11_0_arm64.whl.
File metadata
- Download URL: tropical_gemm-0.1.0-cp39-cp39-macosx_11_0_arm64.whl
- Upload date:
- Size: 302.8 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 |
d7d0e61f4f5d8e282c5215828133e1dcba781f1a81375767c112c03992048400
|
|
| MD5 |
43ea7120948980f49c04f1d9096b5df9
|
|
| BLAKE2b-256 |
91392bee8a11a166d60be47f008b5129b0f78af59c4b0cc683bb8fff7bfe7220
|