PyTorch wrapper for HyLAC CUDA library for solving linear assignment problems.
Project description
CUDA LAP Solver
Installation | Usage | Benchmarks
A fast CUDA implementation of the Linear Assignment Problem (LAP) solver for PyTorch. This project provides GPU-accelerated HyLAC algorithm implementation that can efficiently handle batched inputs.
Based on the HyLAC code https://github.com/Nagi-Research-Group/HyLAC/tree/Block-LAP Please cite the original work if you use this code in your research: https://doi.org/10.1016/j.jpdc.2024.104838
Features
- Fast CUDA-based implementation of the LAP solver
- Batched processing support for multiple cost matrices
- Seamless integration with PyTorch
- Supports single and double precision types:
torch.int32, torch.int64, torch.float32, torch.float64
Requirements
- Python >= 3.9
- CUDA >= 10.0
- PyTorch
- NVIDIA GPU with compute capability >= 7.5
Installation
To install the package, you can use pip:
pip install torch-lap-cuda --no-build-isolation
You can install the package directly from source:
git clone https://github.com/dkobylianskii/torch-lap-cuda.git
cd torch-lap-cuda
pip install . --no-build-isolation
Usage
Here's a simple example of how to use the LAP solver:
import torch
from torch_lap_cuda import solve_lap
# Create a random cost matrix (batch_size x N x N)
batch_size = 128
size = 256
cost_matrix = torch.randn((batch_size, size, size), device="cuda")
# Solve the assignment problem
# assignments shape will be (batch_size, size)
# Each batch element contains the column indices for optimal assignment
assignments = solve_lap(cost_matrix)
# Calculate total costs
batch_idxs = torch.arange(batch_size, device=assignments.device).unsqueeze(1)
row_idxs = torch.arange(size, device=assignments.device).unsqueeze(0)
total_cost = cost_matrix[batch_idxs, row_idxs, assignments].sum()
The solver also supports 2D inputs for single matrices:
# Single cost matrix (N x N)
cost_matrix = torch.randn((size, size), device="cuda")
assignments = solve_lap(cost_matrix) # Shape: (size,)
In case of having multiple GPUs, you can specify the device for lap solver using the device argument:
cost_matrix = torch.randn((batch_size, size, size), device="cuda:0")
assignments = solve_lap(cost_matrix, device="cuda:1") # assignments will be on cuda:0
Input Requirements
- Cost matrices must be on a CUDA device
- Input can be either 2D (N x N) or 3D (batch_size x N x N)
- Matrices must be square
- Supports single and double precision types:
torch.int32, torch.int64, torch.float32, torch.float64
Benchmarks
Tests were performed on an INTEL(R) XEON(R) GOLD 6530 and NVIDIA A6000 Ada GPU with CUDA 12.5 and PyTorch 2.6.0.
Scipy (MP) means multiprocessing version, Scipy (MT) means multithreading version, both used 32 processes/threads.
To run the benchmarks, execute:
python tests/benchmark.py
Benchmark for uniform random distribution:
Benchmark for normal random distribution:
Benchmark for integer random distribution:
Testing
To run the test suite:
pytest tests/
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 Distribution
File details
Details for the file torch_lap_cuda-0.0.3.tar.gz.
File metadata
- Download URL: torch_lap_cuda-0.0.3.tar.gz
- Upload date:
- Size: 1.2 MB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
00496a1d53a4dd0d89bcd953e45ac5cfe7b83118f9ca9e758018982590bbebbd
|
|
| MD5 |
7283cd98be94cc2ddb06c7839a7fbf77
|
|
| BLAKE2b-256 |
dfcbb7e24f81886295291282908f4d21a7abd274f3a9771e346554df4de4fc09
|
Provenance
The following attestation bundles were made for torch_lap_cuda-0.0.3.tar.gz:
Publisher:
publish-to-pypi.yml on dkobylianskii/torch-lap-cuda
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
torch_lap_cuda-0.0.3.tar.gz -
Subject digest:
00496a1d53a4dd0d89bcd953e45ac5cfe7b83118f9ca9e758018982590bbebbd - Sigstore transparency entry: 394143051
- Sigstore integration time:
-
Permalink:
dkobylianskii/torch-lap-cuda@9533f44987c2a5843f8c242a2443fd08aed2cfee -
Branch / Tag:
refs/tags/v0.0.3 - Owner: https://github.com/dkobylianskii
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-to-pypi.yml@9533f44987c2a5843f8c242a2443fd08aed2cfee -
Trigger Event:
release
-
Statement type: