Implementation:Rapidsai Cuml LARS Solver
| Knowledge Sources | |
|---|---|
| Domains | Machine_Learning, Linear_Models |
| Last Updated | 2026-02-08 12:00 GMT |
Overview
Provides GPU-accelerated Least Angle Regression (LARS) for training and predicting with sparse linear regression models, computing the full regularization path of coefficients.
Description
The lars.hpp header declares the LARS (Least Angle Regression) solver within the ML::Solver::Lars namespace. LARS is an algorithm for fitting linear regression models that efficiently computes the full regularization path by iteratively adding the most correlated feature to the active set.
Two templated functions are provided:
larsFit: Trains a LARS regression model. The function accepts a column-major training matrixX(expected to be normalized with zero mean and unit variance per column), targetsy(expected to have zero mean), and produces regression coefficients (beta), active variable indices (active_idx), and the maximum correlations along the regularization path (alphas). It optionally accepts a precomputed Gram matrix and can output the full coefficient path. Note that the columns of X may be permuted if no Gram matrix is provided.
larsPredict: Makes predictions using a fitted LARS model. Only the active columns of X (identified byactive_idx) are used for prediction, combined with the learned intercept.
Both functions are templated on math_t (float/double) and idx_t (index type).
Usage
Use LARS when you need a sparse linear model that selects features along the regularization path, similar to Lasso but with a different algorithmic approach. LARS is particularly effective when the number of features is much larger than the number of samples, or when you need to understand the order in which features become relevant. The GPU acceleration makes this practical for larger datasets.
Code Reference
Source Location
- Repository: Rapidsai_Cuml
- File:
cpp/include/cuml/solvers/lars.hpp
Signature
namespace ML {
namespace Solver {
namespace Lars {
template <typename math_t, typename idx_t>
void larsFit(const raft::handle_t& handle,
math_t* X,
idx_t n_rows,
idx_t n_cols,
const math_t* y,
math_t* beta,
idx_t* active_idx,
math_t* alphas,
idx_t* n_active,
math_t* Gram,
int max_iter,
math_t* coef_path,
rapids_logger::level_enum verbosity,
idx_t ld_X,
idx_t ld_G,
math_t eps);
template <typename math_t, typename idx_t>
void larsPredict(const raft::handle_t& handle,
const math_t* X,
idx_t n_rows,
idx_t n_cols,
idx_t ld_X,
const math_t* beta,
idx_t n_active,
idx_t* active_idx,
math_t intercept,
math_t* preds);
} // namespace Lars
} // namespace Solver
} // namespace ML
Import
#include <cuml/solvers/lars.hpp>
I/O Contract
Inputs
larsFit
| Name | Type | Required | Description |
|---|---|---|---|
| handle | const raft::handle_t& | Yes | RAFT handle for GPU resources |
| X | math_t* | Yes | Device array of training data in column-major format [n_rows x n_cols]; columns may be permuted |
| n_rows | idx_t | Yes | Number of training samples |
| n_cols | idx_t | Yes | Number of feature columns |
| y | const math_t* | Yes | Device array of regression targets [n_rows], should have zero mean |
| Gram | math_t* | No | Device array for precomputed Gram matrix (X^T * X) [n_cols x ld_G], or nullptr |
| max_iter | int | Yes | Maximum number of iterations (and maximum coefficients returned); must be <= n_cols |
| coef_path | math_t* | No | Device array for coefficient path [max_iter x max_iter], or nullptr |
| verbosity | rapids_logger::level_enum | Yes | Verbosity level for logging |
| ld_X | idx_t | Yes | Leading dimension of X (stride of columns, >= n_rows) |
| ld_G | idx_t | Yes | Leading dimension of Gram matrix (>= n_cols) |
| eps | math_t | Yes | Numeric parameter for Cholesky rank-one update stability |
larsPredict
| Name | Type | Required | Description |
|---|---|---|---|
| handle | const raft::handle_t& | Yes | RAFT handle |
| X | const math_t* | Yes | Device array of input data in column-major format [n_rows x n_cols] |
| n_rows | idx_t | Yes | Number of samples |
| n_cols | idx_t | Yes | Number of feature columns |
| ld_X | idx_t | Yes | Leading dimension of X |
| beta | const math_t* | Yes | Device array of regression coefficients [n_active] |
| n_active | idx_t | Yes | Number of active (non-zero) regression coefficients |
| active_idx | idx_t* | Yes | Device array of active variable indices [n_active] |
| intercept | math_t | Yes | Intercept value |
Outputs
| Name | Type | Description |
|---|---|---|
| beta (fit) | math_t* | Device array of learned regression coefficients [max_iter] |
| active_idx (fit) | idx_t* | Device array of active variable indices [max_iter] |
| alphas (fit) | math_t* | Device array of maximum correlations along the regularization path [max_iter] |
| n_active (fit) | idx_t* | Host pointer returning the number of active elements |
| coef_path (fit) | math_t* | Optional coefficient path array [max_iter x max_iter] |
| preds (predict) | math_t* | Device array of predictions [n_rows] |
Usage Examples
#include <cuml/solvers/lars.hpp>
raft::handle_t handle;
int n_rows = 1000;
int n_cols = 200;
int max_iter = 100;
// Device arrays (pre-allocated)
float* d_X; // column-major [n_rows x n_cols]
float* d_y; // [n_rows]
float* d_beta; // [max_iter]
int* d_active_idx; // [max_iter]
float* d_alphas; // [max_iter]
int n_active;
// Fit LARS model
ML::Solver::Lars::larsFit<float, int>(
handle, d_X, n_rows, n_cols, d_y,
d_beta, d_active_idx, d_alphas, &n_active,
nullptr, // no precomputed Gram matrix
max_iter,
nullptr, // no coefficient path output
rapids_logger::level_enum::info,
n_rows, // ld_X
n_cols, // ld_G
1e-12f); // eps
// Predict
float* d_preds; // [n_rows]
ML::Solver::Lars::larsPredict<float, int>(
handle, d_X, n_rows, n_cols, n_rows,
d_beta, n_active, d_active_idx, 0.0f, d_preds);