torch-lap-cuda


Nametorch-lap-cuda JSON
Version 0.0.2 PyPI version JSON
download
home_pagehttps://github.com/dkobylianskii/torch-lap-cuda
SummaryPyTorch wrapper for HyLAC CUDA library for solving linear assignment problems.
upload_time2025-07-08 14:21:49
maintainerNone
docs_urlNone
authorDmitrii Kobylianskii
requires_python>=3.9
licenseMIT License HyLAC Copyright (c) 2024 Samiran Kawtikwar torch_lap_cuda Copyright (c) 2025 Dmitrii Kobylianskii Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
keywords machine learning linear assignment problem lap cuda torch
VCS
bugtrack_url
requirements torch
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # CUDA LAP Solver
[![PyPI version](https://badge.fury.io/py/torch-lap-cuda.svg)](https://badge.fury.io/py/torch-lap-cuda)
[![Downloads](https://static.pepy.tech/badge/torch-lap-cuda)](https://pepy.tech/project/torch-lap-cuda)
[![License](https://img.shields.io/badge/MIT-blue.svg)](https://opensource.org/licenses/MIT)

<h4 align="left">
    <p>
        <a href="#Installation">Installation</a> |
        <a href="#Usage">Usage</a> |
        <a href="#Benchmarks">Benchmarks</a>
    <p>
</h4>

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
- Currently supports `torch.float32` and `torch.int32` data types.

## Requirements

- Python >= 3.9
- CUDA >= 10.0
- PyTorch
- NVIDIA GPU with compute capability >= 7.5

## Installation

To install the package, you can use pip:

```bash
pip install torch-lap-cuda --no-build-isolation
```

You can install the package directly from source:

```bash
git clone https://github.com/dkobylianskii/torch-lap-cuda.git
cd torch-lap-cuda
pip install .
```

## Usage

Here's a simple example of how to use the LAP solver:

```python
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:

```python
# Single cost matrix (N x N)
cost_matrix = torch.randn((size, size), device="cuda")
assignments = solve_lap(cost_matrix)  # Shape: (size,)
```

## 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 both torch.float32 and torch.int32 dtypes

## 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:

```bash
python tests/benchmark.py
```

### Benchmark for uniform random distribution:

![Benchmark results for uniform random cost matrices](figs/benchmark_uniform.png)

### Benchmark for normal random distribution:

![Benchmark results for normal random cost matrices](figs/benchmark_normal.png)

### Benchmark for integer random distribution:

![Benchmark results for integer random cost matrices](figs/benchmark_integer.png)

## Testing

To run the test suite:

```bash
pytest tests/
```

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/dkobylianskii/torch-lap-cuda",
    "name": "torch-lap-cuda",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.9",
    "maintainer_email": null,
    "keywords": "machine learning, linear assignment problem, lap, cuda, torch",
    "author": "Dmitrii Kobylianskii",
    "author_email": "Dmitrii Kobylianskii <kobad2@gmail.com>",
    "download_url": "https://files.pythonhosted.org/packages/e8/41/c910cba0faac53955d059d0bd242092d4da64e2c601053bbed7277e85b6f/torch_lap_cuda-0.0.2.tar.gz",
    "platform": null,
    "description": "# CUDA LAP Solver\n[![PyPI version](https://badge.fury.io/py/torch-lap-cuda.svg)](https://badge.fury.io/py/torch-lap-cuda)\n[![Downloads](https://static.pepy.tech/badge/torch-lap-cuda)](https://pepy.tech/project/torch-lap-cuda)\n[![License](https://img.shields.io/badge/MIT-blue.svg)](https://opensource.org/licenses/MIT)\n\n<h4 align=\"left\">\n    <p>\n        <a href=\"#Installation\">Installation</a> |\n        <a href=\"#Usage\">Usage</a> |\n        <a href=\"#Benchmarks\">Benchmarks</a>\n    <p>\n</h4>\n\nA 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.\n\nBased on the HyLAC code https://github.com/Nagi-Research-Group/HyLAC/tree/Block-LAP\nPlease cite the original work if you use this code in your research:  https://doi.org/10.1016/j.jpdc.2024.104838 \n\n## Features\n\n- Fast CUDA-based implementation of the LAP solver \n- Batched processing support for multiple cost matrices\n- Seamless integration with PyTorch\n- Currently supports `torch.float32` and `torch.int32` data types.\n\n## Requirements\n\n- Python >= 3.9\n- CUDA >= 10.0\n- PyTorch\n- NVIDIA GPU with compute capability >= 7.5\n\n## Installation\n\nTo install the package, you can use pip:\n\n```bash\npip install torch-lap-cuda --no-build-isolation\n```\n\nYou can install the package directly from source:\n\n```bash\ngit clone https://github.com/dkobylianskii/torch-lap-cuda.git\ncd torch-lap-cuda\npip install .\n```\n\n## Usage\n\nHere's a simple example of how to use the LAP solver:\n\n```python\nimport torch\nfrom torch_lap_cuda import solve_lap\n\n# Create a random cost matrix (batch_size x N x N)\nbatch_size = 128\nsize = 256\ncost_matrix = torch.randn((batch_size, size, size), device=\"cuda\")\n\n# Solve the assignment problem\n# assignments shape will be (batch_size, size)\n# Each batch element contains the column indices for optimal assignment\nassignments = solve_lap(cost_matrix)\n\n# Calculate total costs\nbatch_idxs = torch.arange(batch_size, device=assignments.device).unsqueeze(1)\nrow_idxs = torch.arange(size, device=assignments.device).unsqueeze(0)\ntotal_cost = cost_matrix[batch_idxs, row_idxs, assignments].sum()\n```\n\nThe solver also supports 2D inputs for single matrices:\n\n```python\n# Single cost matrix (N x N)\ncost_matrix = torch.randn((size, size), device=\"cuda\")\nassignments = solve_lap(cost_matrix)  # Shape: (size,)\n```\n\n## Input Requirements\n\n- Cost matrices must be on a CUDA device\n- Input can be either 2D (N x N) or 3D (batch_size x N x N) \n- Matrices must be square\n- Supports both torch.float32 and torch.int32 dtypes\n\n## Benchmarks\n\nTests were performed on an INTEL(R) XEON(R) GOLD 6530 and NVIDIA A6000 Ada GPU with CUDA 12.5 and PyTorch 2.6.0.\n\n`Scipy (MP)` means multiprocessing version, `Scipy (MT)` means multithreading version, both used 32 processes/threads.\n\nTo run the benchmarks, execute:\n\n```bash\npython tests/benchmark.py\n```\n\n### Benchmark for uniform random distribution:\n\n![Benchmark results for uniform random cost matrices](figs/benchmark_uniform.png)\n\n### Benchmark for normal random distribution:\n\n![Benchmark results for normal random cost matrices](figs/benchmark_normal.png)\n\n### Benchmark for integer random distribution:\n\n![Benchmark results for integer random cost matrices](figs/benchmark_integer.png)\n\n## Testing\n\nTo run the test suite:\n\n```bash\npytest tests/\n```\n",
    "bugtrack_url": null,
    "license": "MIT License\n        \n        HyLAC Copyright (c) 2024 Samiran Kawtikwar\n        torch_lap_cuda Copyright (c) 2025 Dmitrii Kobylianskii\n        \n        Permission is hereby granted, free of charge, to any person obtaining a copy\n        of this software and associated documentation files (the \"Software\"), to deal\n        in the Software without restriction, including without limitation the rights\n        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n        copies of the Software, and to permit persons to whom the Software is\n        furnished to do so, subject to the following conditions:\n        \n        The above copyright notice and this permission notice shall be included in all\n        copies or substantial portions of the Software.\n        \n        THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n        SOFTWARE.",
    "summary": "PyTorch wrapper for HyLAC CUDA library for solving linear assignment problems.",
    "version": "0.0.2",
    "project_urls": {
        "Homepage": "https://github.com/dkobylianskii/torch-lap-cuda",
        "Issues": "https://github.com/dkobylianskii/torch-lap-cuda/issues"
    },
    "split_keywords": [
        "machine learning",
        " linear assignment problem",
        " lap",
        " cuda",
        " torch"
    ],
    "urls": [
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "e841c910cba0faac53955d059d0bd242092d4da64e2c601053bbed7277e85b6f",
                "md5": "46e12def6903fc9f8460b23ceaa40630",
                "sha256": "bd05bd2c9c84cad5c1689b8008293e515187d7ad7faf76f564ad676e3dab32de"
            },
            "downloads": -1,
            "filename": "torch_lap_cuda-0.0.2.tar.gz",
            "has_sig": false,
            "md5_digest": "46e12def6903fc9f8460b23ceaa40630",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.9",
            "size": 17189,
            "upload_time": "2025-07-08T14:21:49",
            "upload_time_iso_8601": "2025-07-08T14:21:49.681118Z",
            "url": "https://files.pythonhosted.org/packages/e8/41/c910cba0faac53955d059d0bd242092d4da64e2c601053bbed7277e85b6f/torch_lap_cuda-0.0.2.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2025-07-08 14:21:49",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "dkobylianskii",
    "github_project": "torch-lap-cuda",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "requirements": [
        {
            "name": "torch",
            "specs": [
                [
                    ">=",
                    "1.12.0"
                ]
            ]
        }
    ],
    "lcname": "torch-lap-cuda"
}
        
Elapsed time: 1.62589s