Sparse Johnson-Lindenstrauss Transform with CUDA acceleration
Project description
Sparse Johnson-Lindenstrauss Transform CUDA Kernel
This is a simple repository for Sparse Johnson-Lindenstrauss Transform with CUDA acceleration for PyTorch.
Features
- GPU-accelerated sparse random projections
- Supports float16, float32, float64, and bfloat16 data types
- Optimized CUDA kernels for high performance
- Easy integration with PyTorch workflows
Installation
Requirements
- Python >= 3.8
- PyTorch >= 1.9.0 with CUDA support
- CUDA Toolkit (version compatible with your PyTorch installation)
- C++ compiler (GCC 7-11 recommended)
Install from PyPI
To build the CUDA SJLT CUDA kernel, you will need to make sure that nvcc -V and torch.version.cuda gives the same CUDA version. Then, you can install sjlt via:
pip install sjlt
[!IMPORTANT] The installation will fail if CUDA toolkit is not available. This is intentional to prevent silent failures. If you want to install without CUDA (for testing only), use:
SJLT_SKIP_CUDA_BUILD=1 pip install sjltNote: The package will not work at runtime without the CUDA extension.
Install from Source
git clone https://github.com/TRAIS-Lab/sjlt
cd sjlt
pip install -e .
Quick Start
Our SJLT implementation accepts the following parameters:
original_dim: input dimensionproj_dim: output dimensionc: sparsity parameter, i.e., non-zeros per column (default:1)threads: CUDA threads per block (default:1024)fixed_blocks: CUDA blocks to use (default:84)
[!Note] The input is supposed to have
batch_dim, i.e.,input.shape()should be(batch_size, original_dim)andoutput.shape()will be(batch_size, proj_dim).
The following is a simple snippet of using our SJLT CUDA kernel:
import torch
from sjlt import SJLTProjection
# Create projection: 1024 -> 128 dimensions with sparsity 4
proj = SJLTProjection(original_dim=1024, proj_dim=128, c=4)
# Project some data
x = torch.randn(100, 1024, device='cuda')
y = proj(x) # Shape: [100, 128]
Profile Example
To profile the performance of the SJLT CUDA kernel, you can use the provided profile notebook. This benchmarks the projection speed for different input sizes and sparsity levels.
Troubleshooting
Installing a CUDA kernel can be tricky. Here, I have gathered several common errors I have encountered, with their corresponding solutions.
pip install sjltdoesn't work: In this case, please try to build it in your environment. This means to trypip install sjlt --no-cache-dir --force-reinstall- CUDA version mismatch: Please make sure that you have a compatible CUDA version of
nvccas well as the CUDA version that is used to build PyTorch. If this is the case, but you still encounter the following errors:RuntimeError: The detected CUDA version (11.8) mismatches the version that was used to compile PyTorch (12.6). Please make sure to use the same CUDA versions.
This is because the default isolation building behavior ofpip install(this applies to both "Install from PyPI" and "Install from Source"), even ifnvcc -Vandtorch.version.cudagive the same CUDA version. In this case, using--no-build-isolationto force pip to build using your current virtual environment. With 1., please trypip install sjlt --no-build-isolation --no-cache-dir --force-reinstall - Unsupported GNU version: With 1. and 2., if you then encounter the following:
error -- unsupported GNU version! gcc versions later than 11 are not supported! The nvcc flag '-allow-unsupported-compiler' can be used to override this version check; however, using an unsupported host compiler may cause compilation failure or incorrect run time execution. Use at your own risk.
This means your GCC version is too new for your CUDA version. Please ensure that you have the correct GCC version installed, and once you have done so, you can use the corresponding GCC version by setting the environment variables such asCC=gcc-11 CXX=g++-11 pip install sjlt --no-build-isolation --no-cache-dir --force-reinstallin the case ofCUDA=11.8.
In summary:
- Ensure CUDA toolkit is installed and
nvccis inPATH - Check PyTorch CUDA compatibility:
python -c "import torch; print(torch.cuda.is_available())" - Try clean reinstalling:
pip install sjlt --no-build-isolation --no-cache-dir --force-reinstall - If GCC version is too new, install compatible version and set
CCandCXXenvironment variables accordingly.
Reference
- A Sparse Johnson-Lindenstrauss Transform
- Sparser Johnson-Lindenstrauss Transforms
- GraSS: Scalable Data Attribution with Gradient Sparsification and Sparse Projection
Citation
If you find this repository valuable, please give it a star! Got any questions or feedback? Feel free to open an issue. Using this in your work? Please reference us using the provided citation:
@inproceedings{hu2025grass,
author = {Pingbang Hu and Joseph Melkonian and Weijing Tang and Han Zhao and Jiaqi W. Ma},
title = {GraSS: Scalable Data Attribution with Gradient Sparsification and Sparse Projection},
booktitle = {Advances in Neural Information Processing Systems},
year = {2025}
}
As this repository is an effort from the GraSS project.
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 sjlt-0.1.5.1.tar.gz.
File metadata
- Download URL: sjlt-0.1.5.1.tar.gz
- Upload date:
- Size: 160.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.10.18
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cc34d759faf7e75e4f18931fc4abd31d8e0e5688d44552bfef1a0f8fb1edb013
|
|
| MD5 |
42df17318490508b80778efe37fe3145
|
|
| BLAKE2b-256 |
caaefe5c27d109d2bfc96e145aabf667ef17e24f6c4ae6e5252c23bae6657005
|