RAFT Python Library (CUDA 12)
pylibraft-cu12 is the Python binding for RAFT (Reusable Algorithms Functions and other Tools), a core component of the NVIDIA RAPIDS ecosystem providing a collection of GPU-accelerated primitives and algorithms for data science. It is designed for optimal performance on NVIDIA GPUs with CUDA 12.x. The current version is 26.4.0, following a monthly release cadence aligned with the broader RAPIDS project.
Common errors
-
ModuleNotFoundError: No module named 'raft'
cause The `pylibraft-cu12` package is not installed or not available in the current Python environment.fixInstall the package using `pip install pylibraft-cu12` (and `cupy-cuda12x` for practical use). -
RuntimeError: CUDA driver version is insufficient for CUDA runtime version
cause Your installed NVIDIA CUDA driver is older than what `pylibraft-cu12` (which requires CUDA 12.2+) expects, or there's a mismatch between your system's CUDA Toolkit and driver.fixUpdate your NVIDIA GPU drivers to the latest version. Ensure your system's CUDA Toolkit version (if installed) is 12.2 or higher, matching the `cu12` suffix in `pylibraft-cu12`. -
AttributeError: module 'raft.neighbors' has no attribute 'OldAlgorithmName'
cause A specific algorithm, class, or function within a RAFT module was removed or renamed in a newer version due to API restructuring (e.g., in v26.02.00 or v26.04.00).fixConsult the `raft` Python API documentation for your installed version or the release notes on GitHub for renamed or removed features. You may need to update your code to use an alternative or refactored API. -
TypeError: __init__() got an unexpected keyword argument 'old_parameter'
cause The constructor or method signature of a RAFT class or function has changed, often due to a breaking change in a newer version.fixReview the `raft` Python API documentation for the specific class/function and your installed `pylibraft-cu12` version to identify the correct parameters and their usage. Update your code accordingly.
Warnings
- breaking As of v25.12.00, `pylibraft-cu12` (and all `raft` variants) explicitly require CUDA 12.2 or newer. Using older CUDA versions will result in runtime errors or compilation failures.
- breaking In v26.02.00, significant internal C++ API restructuring occurred, including the removal of certain specific `neighbors/`, `cluster/`, `distance/`, `spatial/`, and `sparse/neighbors/` APIs. While core Python modules like `raft.neighbors` persist, specific less common algorithms, classes, or parameters within these modules may have been removed or changed.
- breaking The `lanczos` solver API was renamed in v26.04.00. If you were using this specific linear algebra component, your code will break.
- gotcha Installing `pylibraft-cu12` only provides the RAFT bindings. You will almost certainly need `cupy-cuda12x` (or another GPU array library like `cudf` for dataframes) to effectively work with GPU arrays required by RAFT algorithms.
- gotcha RAPIDS libraries, including RAFT, follow a monthly release cycle. This means breaking changes can occur frequently between minor version updates, requiring regular attention to release notes when upgrading.
Install
-
pip install pylibraft-cu12 cupy-cuda12x -
pip install pylibraft-cu12
Imports
- NearestNeighbors
from raft.neighbors import NearestNeighbors
- KMeans
from raft.cluster import KMeans
- get_device_info
from pylibraft.common.cuda_utils import get_device_info
from raft.common.cuda_utils import get_device_info
- LinearRegression
from raft.linear_model import LinearRegression
Quickstart
import cupy as cp
from raft.neighbors import NearestNeighbors
import sys
# Ensure CuPy is installed for GPU array handling and a CUDA device is available
try:
if cp.cuda.runtime.getDeviceCount() == 0:
print("Error: No CUDA device found. RAFT requires a CUDA-enabled GPU.")
sys.exit(1)
except cp.cuda.runtime.CUDARuntimeError as e:
print(f"Error initializing CuPy or CUDA runtime: {e}")
print("Please ensure CuPy is installed correctly (e.g., `cupy-cuda12x`) and your CUDA environment is set up.")
sys.exit(1)
# Generate some random GPU data using CuPy
n_samples = 100
n_features = 10
X = cp.random.rand(n_samples, n_features, dtype=cp.float32)
# Create a NearestNeighbors model
nn = NearestNeighbors(n_neighbors=5, metric='euclidean', output_type='cupy')
# Fit the model to the data
nn.fit(X)
# Query for nearest neighbors to the same data
distances, indices = nn.kneighbors(X)
print("Distances shape:", distances.shape)
print("Indices shape:", indices.shape)
print("\nFirst 5 distances (sample 0):\n", distances[0, :5])
print("\nFirst 5 indices (sample 0):\n", indices[0, :5])