NVIDIA cuSPARSE (CUDA 12)
The `nvidia-cusparse-cu12` package provides the native runtime libraries for NVIDIA's cuSPARSE, a GPU-accelerated library for sparse matrix computations, specifically compatible with CUDA Toolkit 12.x. It offers highly optimized basic linear algebra subroutines for sparse matrices, enabling faster computations than CPU-only alternatives in fields like machine learning, AI, and scientific computing. This package is part of a series of NVIDIA-provided Python wheels that make CUDA runtime components available via PyPI, with the current version being 12.5.10.65. New versions are released in alignment with CUDA Toolkit updates.
Warnings
- breaking Mismatch between `nvidia-cusparse-cu12` version and the installed CUDA Toolkit or GPU driver can lead to `ImportError: undefined symbol` errors. This commonly occurs when `torch` or `tensorflow` dependencies request a specific `nvidia-cusparse-cu12` version that doesn't align with your system's CUDA setup. [18, 20, 21, 23]
- gotcha The `nvidia-cusparse-cu12` package provides native C++ runtime libraries and does not expose a direct Python API for `cusparse` functions. Attempting to `import cusparse` directly or find Python bindings within this package will fail. [13, 15]
- gotcha For CUDA 12.4 and later, the `cusparseSpMV` routine might cause invalid memory accesses if the output vector is not 16-byte aligned. This can lead to crashes or incorrect results. [24]
- gotcha This library is distributed under an NVIDIA Proprietary Software License (LicenseRef-NVIDIA-Proprietary), which may have different terms and conditions compared to open-source licenses. [2]
Install
-
pip install nvidia-cusparse-cu12
Imports
- N/A (runtime library)
import cupy.sparse # Example using a higher-level library
Quickstart
import os
try:
import cupy as cp
import cupy.sparse as cps
import numpy as np
# Check for CUDA device
if cp.cuda.is_available():
print(f"CUDA is available. CuPy version: {cp.__version__}")
print(f"CUDA Device Name: {cp.cuda.Device().name}")
# Create a sparse matrix on CPU (SciPy format)
row = np.array([0, 1, 2, 0])
col = np.array([0, 1, 2, 1])
data = np.array([1, 2, 3, 4])
shape = (3, 3)
sparse_cpu = cps.csr_matrix((data, (row, col)), shape=shape)
print("\nCPU Sparse Matrix:\n", sparse_cpu.toarray())
# Transfer to GPU and perform a sparse matrix-vector multiplication
sparse_gpu = cps.csr_matrix(sparse_cpu, dtype=cp.float32) # cuSPARSE generally works with float32/64
vector_gpu = cp.array([1.0, 2.0, 3.0], dtype=cp.float32)
result_gpu = sparse_gpu @ vector_gpu
print("\nGPU Sparse Matrix-Vector Product (using cuSPARSE via CuPy):\n", result_gpu)
else:
print("CUDA is not available. Please ensure a compatible NVIDIA GPU and driver are installed.")
print("You may need to install cupy-cuda12x manually if using a specific CUDA version.")
except ImportError:
print("CuPy is not installed. To run this example, install CuPy compatible with CUDA 12:")
print("pip install cupy-cuda12x")
except Exception as e:
print(f"An error occurred: {e}")