Skip to main content

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] if k == argmax[i,j]
  • grad_B[k,j] = grad_C[i,j] if k == argmax[i,j]

MaxMul (multiplicative rule):

  • grad_A[i,k] = grad_C[i,j] * B[k,j] if k == argmax[i,j]
  • grad_B[k,j] = grad_C[i,j] * A[i,k] if k == 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

Metadata

Release files for tropical-gemm 0.4.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Built distributions (wheels)

Table of built distributions (wheels) for tropical-gemm 0.4.0
File
tropical_gemm-0.4.0-cp312-cp312-win_amd64.whl CPython 3.12 CPython 3.12 Windows x86-64 Details
tropical_gemm-0.4.0-cp312-cp312-manylinux_2_34_x86_64.whl CPython 3.12 CPython 3.12 Linux glibc 2.34+ x86-64 Details
tropical_gemm-0.4.0-cp312-cp312-macosx_11_0_arm64.whl CPython 3.12 CPython 3.12 macOS 11.0+ ARM64 Details
tropical_gemm-0.4.0-cp311-cp311-win_amd64.whl CPython 3.11 CPython 3.11 Windows x86-64 Details
tropical_gemm-0.4.0-cp311-cp311-manylinux_2_34_x86_64.whl CPython 3.11 CPython 3.11 Linux glibc 2.34+ x86-64 Details
tropical_gemm-0.4.0-cp311-cp311-macosx_11_0_arm64.whl CPython 3.11 CPython 3.11 macOS 11.0+ ARM64 Details
tropical_gemm-0.4.0-cp310-cp310-win_amd64.whl CPython 3.10 CPython 3.10 Windows x86-64 Details
tropical_gemm-0.4.0-cp310-cp310-manylinux_2_34_x86_64.whl CPython 3.10 CPython 3.10 Linux glibc 2.34+ x86-64 Details
tropical_gemm-0.4.0-cp310-cp310-macosx_11_0_arm64.whl CPython 3.10 CPython 3.10 macOS 11.0+ ARM64 Details
tropical_gemm-0.4.0-cp39-cp39-win_amd64.whl CPython 3.9 CPython 3.9 Windows x86-64 Details
tropical_gemm-0.4.0-cp39-cp39-manylinux_2_34_x86_64.whl CPython 3.9 CPython 3.9 Linux glibc 2.34+ x86-64 Details
tropical_gemm-0.4.0-cp39-cp39-macosx_11_0_arm64.whl CPython 3.9 CPython 3.9 macOS 11.0+ ARM64 Details

Total release size: 5.0 MB

Release files / tropical_gemm-0.4.0-cp312-cp312-win_amd64.whl

Download URL tropical_gemm-0.4.0-cp312-cp312-win_amd64.whl
Size 338.5 kB
Tags CPython 3.12 Windows x86-64
SHA-256 checksum
How to use checksums
d56e4913cdc79fc32d7f6052a4d97b1d90844b8da30a3d109dd3fc0282e759c2
BLAKE2b-256 checksum
How to use checksums
0c155d8ca30cc694ca1de7bf0002dd4cd2d02b3ab6bb46ad36b73046b053b218
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.3

Release files / tropical_gemm-0.4.0-cp312-cp312-manylinux_2_34_x86_64.whl

Download URL tropical_gemm-0.4.0-cp312-cp312-manylinux_2_34_x86_64.whl
Size 495.6 kB
Tags CPython 3.12 Linux glibc 2.34+ x86-64
SHA-256 checksum
How to use checksums
5d374863e03fcddadf5c0ea0240c982c529c8f6f790838a03abce567021c8b5b
BLAKE2b-256 checksum
How to use checksums
c19071a5bcedc48067d671589bac9932ac6d334bfe8f016577de03f0bd511a33
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.3

Release files / tropical_gemm-0.4.0-cp312-cp312-macosx_11_0_arm64.whl

Download URL tropical_gemm-0.4.0-cp312-cp312-macosx_11_0_arm64.whl
Size 401.6 kB
Tags CPython 3.12 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
3c172cd437601f45160bb800b8d0e426c501db2f0a0aaf60eb87a265b5367229
BLAKE2b-256 checksum
How to use checksums
9e3a0cf73405b0864964e4246bfd157e34bdd1d1f2a5bf2845c84eed7fc7e410
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.3

Release files / tropical_gemm-0.4.0-cp311-cp311-win_amd64.whl

Download URL tropical_gemm-0.4.0-cp311-cp311-win_amd64.whl
Size 339.2 kB
Tags CPython 3.11 Windows x86-64
SHA-256 checksum
How to use checksums
be86adda3af6013cafaf20643ba2c2f871553dbda164b75528108006ef707db8
BLAKE2b-256 checksum
How to use checksums
0d8c99a9852de530cd4fa7d7f52be40140bfd8dfb8d11fb722191ecf73344fe3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.3

Release files / tropical_gemm-0.4.0-cp311-cp311-manylinux_2_34_x86_64.whl

Download URL tropical_gemm-0.4.0-cp311-cp311-manylinux_2_34_x86_64.whl
Size 496.9 kB
Tags CPython 3.11 Linux glibc 2.34+ x86-64
SHA-256 checksum
How to use checksums
0457bda91e609dce1a36773dee511c81b9b4efb49cb0135f553315f077a47ab4
BLAKE2b-256 checksum
How to use checksums
0425f17eb065bb8355e494c1f6ec16b0a1219cafbaeca2d4e180284bd6d738dc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.3

Release files / tropical_gemm-0.4.0-cp311-cp311-macosx_11_0_arm64.whl

Download URL tropical_gemm-0.4.0-cp311-cp311-macosx_11_0_arm64.whl
Size 404.0 kB
Tags CPython 3.11 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
0a160c56d551db7efb7eabbea90a9dda92b9a7abecb334c29ec94033122e3717
BLAKE2b-256 checksum
How to use checksums
993565fdefbd56488038d4d659e60d831da4c61afd3d3afa660711f43f28ad3c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.3

Release files / tropical_gemm-0.4.0-cp310-cp310-win_amd64.whl

Download URL tropical_gemm-0.4.0-cp310-cp310-win_amd64.whl
Size 339.2 kB
Tags CPython 3.10 Windows x86-64
SHA-256 checksum
How to use checksums
5212bd8d8112b588675eb18ae79b515d523ad60dd9ff2bcfbe47968ceeb201e4
BLAKE2b-256 checksum
How to use checksums
bbdf4d17a8e30d610e9e1cf78af72e320f028c2213599e1c4707a947154f0fdf
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.3

Release files / tropical_gemm-0.4.0-cp310-cp310-manylinux_2_34_x86_64.whl

Download URL tropical_gemm-0.4.0-cp310-cp310-manylinux_2_34_x86_64.whl
Size 496.8 kB
Tags CPython 3.10 Linux glibc 2.34+ x86-64
SHA-256 checksum
How to use checksums
ad96a3e34c1fd4af352efda323bcd0d6e556b8204e8cdb07b8a1b2cfa4448c57
BLAKE2b-256 checksum
How to use checksums
e7aa8e7c00d5bde8a41c81f2200b7d7cddb1fe9d0e26bee168ab35a0e9656f11
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.3

Release files / tropical_gemm-0.4.0-cp310-cp310-macosx_11_0_arm64.whl

Download URL tropical_gemm-0.4.0-cp310-cp310-macosx_11_0_arm64.whl
Size 403.9 kB
Tags CPython 3.10 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
dd7c8c4ab8f548cc5118550f4fbe8c5d7c217a1541660774fbfdf427b0b987a3
BLAKE2b-256 checksum
How to use checksums
d34d33f476bbcfd98689f9e177868c656895d7a45e2f951171c9edfc5c001031
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.3

Release files / tropical_gemm-0.4.0-cp39-cp39-win_amd64.whl

Download URL tropical_gemm-0.4.0-cp39-cp39-win_amd64.whl
Size 341.7 kB
Tags CPython 3.9 Windows x86-64
SHA-256 checksum
How to use checksums
b2586825e0d18dbe59192afd1475136a364a0836f4d7fca65a13c1cb379f8878
BLAKE2b-256 checksum
How to use checksums
f4c4c32c427a4a66ab5441e10fb45b61e0bbee2aefcac2e95961d7bcf8c5c1fd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.3

Release files / tropical_gemm-0.4.0-cp39-cp39-manylinux_2_34_x86_64.whl

Download URL tropical_gemm-0.4.0-cp39-cp39-manylinux_2_34_x86_64.whl
Size 499.7 kB
Tags CPython 3.9 Linux glibc 2.34+ x86-64
SHA-256 checksum
How to use checksums
7185e117ba6dfa4ce09f85fcc58f41fa99957ee4e3f43eb8156410fb14a35cd9
BLAKE2b-256 checksum
How to use checksums
fb4a19b5fa9977b38f9c207759550606e64c8afce2b2b7c20ed58ce45db5285e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.3

Release files / tropical_gemm-0.4.0-cp39-cp39-macosx_11_0_arm64.whl

Download URL tropical_gemm-0.4.0-cp39-cp39-macosx_11_0_arm64.whl
Size 406.4 kB
Tags CPython 3.9 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
4e670b78b57eb577f3425f2c66f3fdb051b0bf62c2f978f7b5597d4719790699
BLAKE2b-256 checksum
How to use checksums
74d04be125c0f41fcedfa128abff2f038b7947dd94081928b191914a8dd3432c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.3

Release history Release notifications | RSS feed

This release

0.4.0 This release

12 release files

0.3.0

12 release files

0.1.0

12 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page