Skip to main content

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 backprop
  • minplus_matmul_with_argmax(a, b) - MinPlus with argmax indices for backprop
  • backward_a(grad_c, argmax, k) - Compute gradient w.r.t. A
  • backward_b(grad_c, argmax, k) - Compute gradient w.r.t. B

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

tropical_gemm-0.1.0-cp312-cp312-win_amd64.whl (208.3 kB view details)

Uploaded CPython 3.12Windows x86-64

tropical_gemm-0.1.0-cp312-cp312-manylinux_2_34_x86_64.whl (361.4 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.34+ x86-64

tropical_gemm-0.1.0-cp312-cp312-macosx_11_0_arm64.whl (298.6 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

tropical_gemm-0.1.0-cp311-cp311-win_amd64.whl (208.2 kB view details)

Uploaded CPython 3.11Windows x86-64

tropical_gemm-0.1.0-cp311-cp311-manylinux_2_34_x86_64.whl (361.9 kB view details)

Uploaded CPython 3.11manylinux: glibc 2.34+ x86-64

tropical_gemm-0.1.0-cp311-cp311-macosx_11_0_arm64.whl (301.5 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

tropical_gemm-0.1.0-cp310-cp310-win_amd64.whl (208.8 kB view details)

Uploaded CPython 3.10Windows x86-64

tropical_gemm-0.1.0-cp310-cp310-manylinux_2_34_x86_64.whl (362.6 kB view details)

Uploaded CPython 3.10manylinux: glibc 2.34+ x86-64

tropical_gemm-0.1.0-cp310-cp310-macosx_11_0_arm64.whl (301.5 kB view details)

Uploaded CPython 3.10macOS 11.0+ ARM64

tropical_gemm-0.1.0-cp39-cp39-win_amd64.whl (209.4 kB view details)

Uploaded CPython 3.9Windows x86-64

tropical_gemm-0.1.0-cp39-cp39-manylinux_2_34_x86_64.whl (363.5 kB view details)

Uploaded CPython 3.9manylinux: glibc 2.34+ x86-64

tropical_gemm-0.1.0-cp39-cp39-macosx_11_0_arm64.whl (302.8 kB view details)

Uploaded CPython 3.9macOS 11.0+ ARM64

File details

Details for the file tropical_gemm-0.1.0-cp312-cp312-win_amd64.whl.

File metadata

File hashes

Hashes for tropical_gemm-0.1.0-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 40dc2e8b563927d3dd4882195ccf0509e2e6ca8f4fc8a549b1a3bec45e64a8aa
MD5 20f38351d1af8a6f8787b8253ea06feb
BLAKE2b-256 4080d1cf0480017d35a7a55a73fd899cfa6ae9eb6223dc592de3caec40af3c3c

See more details on using hashes here.

File details

Details for the file tropical_gemm-0.1.0-cp312-cp312-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for tropical_gemm-0.1.0-cp312-cp312-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 70b812d185224a3d7bf706da2c7a45eab5d78cef4d1fdfc87ece6c9e5e9564c1
MD5 1304284fec2f98d44d5318d7656559db
BLAKE2b-256 ba8bb698f0e6b329a320038871dd707f13d4c8e398a2c8a070a4bb89e49e2234

See more details on using hashes here.

File details

Details for the file tropical_gemm-0.1.0-cp312-cp312-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for tropical_gemm-0.1.0-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 53348214061371f28b4f299ccd729d38bc47ebde852f2485228be5204cc68c8f
MD5 246ede958a0c5d8d82de3cff135f93a7
BLAKE2b-256 6fe57f8acf3c4db76af59e5939e0ef24f0964f28c2f0e5f104fa01d7f2294026

See more details on using hashes here.

File details

Details for the file tropical_gemm-0.1.0-cp311-cp311-win_amd64.whl.

File metadata

File hashes

Hashes for tropical_gemm-0.1.0-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 c27306fc353330fcfb0eeee422d1ac545768c7200f725f8bb7391fa0f7606b54
MD5 a2efaf47a290badd90969038e821d7ce
BLAKE2b-256 48197def2f19654576eb27e9f538bcbb56af158195dac35366ecaca613048de8

See more details on using hashes here.

File details

Details for the file tropical_gemm-0.1.0-cp311-cp311-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for tropical_gemm-0.1.0-cp311-cp311-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 4407b647e6a42bb9cc5270b3aa46bd8f5b6b570f01635e2520ca6b8c8819bca6
MD5 03d95e2f3798c3c187b909132988931d
BLAKE2b-256 c18cc06a4cf33f83e137161d6c74097d33bdbe697e85761f2aa3b94d41a8e904

See more details on using hashes here.

File details

Details for the file tropical_gemm-0.1.0-cp311-cp311-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for tropical_gemm-0.1.0-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 c2ccc810451820eca8b42f9d183e2039a8c48c123bb6f3e5b230a5fab16acc57
MD5 87fb133c342a5df13d6e9999beeed24b
BLAKE2b-256 749e1f30b82557dfafe6f0bceb87e75b32fa031e1227868b068f2abd0eb4b546

See more details on using hashes here.

File details

Details for the file tropical_gemm-0.1.0-cp310-cp310-win_amd64.whl.

File metadata

File hashes

Hashes for tropical_gemm-0.1.0-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 452f2130986c4cae4ac73a3058f11a0ba8ca3f275e937b1ea7e15f8fcfa003db
MD5 1d78d0fddbb9f6e4c14bac4d97b5af5d
BLAKE2b-256 1fa22284a4ddbaec043007f07847311d9cf9dff1977546abb6021711d76fdb5b

See more details on using hashes here.

File details

Details for the file tropical_gemm-0.1.0-cp310-cp310-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for tropical_gemm-0.1.0-cp310-cp310-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 ce0de28274c153c77dbe1c5c9da28cc19c7f1e8293ba4455d2ebe94b5fdcd99c
MD5 3b6419694d72b274fa8a9db612036155
BLAKE2b-256 5f6424ef1c22c998eeae2ef6c1616ac3031cbc149b1db16f223bf47ea3d73fb3

See more details on using hashes here.

File details

Details for the file tropical_gemm-0.1.0-cp310-cp310-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for tropical_gemm-0.1.0-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 1e07c12e1613021465fa1f7de5d963f905552e8cd5f1154a82e3a4d9b9081299
MD5 4c05b649fc3131e724dd19422379ff32
BLAKE2b-256 108e9734aff2bef2829e4600308daa26b6fd5e38f4b056124ea87c735286fea5

See more details on using hashes here.

File details

Details for the file tropical_gemm-0.1.0-cp39-cp39-win_amd64.whl.

File metadata

File hashes

Hashes for tropical_gemm-0.1.0-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 c6043ed2b01a3387877d33cfce37332c6a43631fe9f0f096157ddb064c3e211d
MD5 f443dcc83061713fe405b9c603628317
BLAKE2b-256 49f49f06808a44e03435279fd283eb594d471afb085b00e5fbeb118929149c37

See more details on using hashes here.

File details

Details for the file tropical_gemm-0.1.0-cp39-cp39-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for tropical_gemm-0.1.0-cp39-cp39-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 5183548c7f9f3cef058b63d36f0a7f6f4e5797c9efbc53fc203c00cc38fbc834
MD5 ed650248dac32cffa119f0eda2a195ba
BLAKE2b-256 e039084b7d932052afc57d56014be6ba3bc589db9558e760a79e6e03a2523d9a

See more details on using hashes here.

File details

Details for the file tropical_gemm-0.1.0-cp39-cp39-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for tropical_gemm-0.1.0-cp39-cp39-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 d7d0e61f4f5d8e282c5215828133e1dcba781f1a81375767c112c03992048400
MD5 43ea7120948980f49c04f1d9096b5df9
BLAKE2b-256 91392bee8a11a166d60be47f008b5129b0f78af59c4b0cc683bb8fff7bfe7220

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page