Implementation:Rapidsai Cuml GLM API
| Knowledge Sources | |
|---|---|
| Domains | Machine_Learning, Linear_Models |
| Last Updated | 2026-02-08 12:00 GMT |
Overview
Provides the C++ API for GPU-accelerated Generalized Linear Model (GLM) operations including OLS, Ridge regression, and quasi-Newton (QN) based GLM fitting and prediction.
Description
This header defines the core GLM functions in the ML::GLM namespace:
- olsFit: Fits an Ordinary Least Squares (OLS) regression model. Supports SVD (algo=0), Eigendecomposition (algo=1), and QR-decomposition (algo=2) solvers, with optional sample weights and intercept fitting. Overloaded for float and double.
- ridgeFit: Fits a Ridge regression model (L2 regularized least squares). Accepts one or more regularization parameters (
alpha), with configurable solver and optional sample weights. Overloaded for float and double.
- gemmPredict: Makes predictions using a fitted OLS or Ridge model via matrix-vector multiplication (GEMM). Computes
predictions = X * coef + intercept. Overloaded for float and double.
- qnFit: Fits a GLM using quasi-Newton (L-BFGS) optimization for logistic regression, softmax, and SVR. Supports dense feature matrices. Template function parameterized on data type
Tand index typeI.
- qnFitSparse: Same as
qnFitbut accepts sparse CSR-format feature matrices.
- qnDecisionFunction: Computes confidence scores (decision function values) for a fitted QN model. Both dense and sparse variants.
- qnPredict: Makes class predictions using a fitted QN model. Both dense and sparse variants.
Usage
Use these functions for GPU-accelerated linear model training and inference. Choose olsFit/ridgeFit for standard linear regression problems, and the qn* family for logistic regression, multinomial classification, or support vector regression. All functions require a RAFT handle and operate on device memory.
Code Reference
Source Location
- Repository: Rapidsai_Cuml
- File:
cpp/include/cuml/linear_model/glm.hpp
Signature
namespace ML {
namespace GLM {
// OLS
void olsFit(const raft::handle_t& handle,
float* input, size_t n_rows, size_t n_cols,
float* labels, float* coef, float* intercept,
bool fit_intercept, int algo = 0,
float* sample_weight = nullptr);
void olsFit(const raft::handle_t& handle,
double* input, size_t n_rows, size_t n_cols,
double* labels, double* coef, double* intercept,
bool fit_intercept, int algo = 0,
double* sample_weight = nullptr);
// Ridge
void ridgeFit(const raft::handle_t& handle,
float* input, size_t n_rows, size_t n_cols,
float* labels, float* alpha, int n_alpha,
float* coef, float* intercept,
bool fit_intercept, int algo = 0,
float* sample_weight = nullptr);
void ridgeFit(const raft::handle_t& handle,
double* input, size_t n_rows, size_t n_cols,
double* labels, double* alpha, int n_alpha,
double* coef, double* intercept,
bool fit_intercept, int algo = 0,
double* sample_weight = nullptr);
// Predict
void gemmPredict(const raft::handle_t& handle,
const float* input, size_t n_rows, size_t n_cols,
const float* coef, float intercept, float* preds);
void gemmPredict(const raft::handle_t& handle,
const double* input, size_t n_rows, size_t n_cols,
const double* coef, double intercept, double* preds);
// Quasi-Newton GLM
template <typename T, typename I = int>
void qnFit(const raft::handle_t& cuml_handle, const qn_params& params,
T* X, bool X_col_major, T* y, I N, I D, I C,
T* w0, T* f, int* num_iters,
T* sample_weight = nullptr, T svr_eps = 0);
template <typename T, typename I = int>
void qnFitSparse(const raft::handle_t& cuml_handle, const qn_params& params,
T* X_values, I* X_cols, I* X_row_ids, I X_nnz,
T* y, I N, I D, I C,
T* w0, T* f, int* num_iters,
T* sample_weight = nullptr, T svr_eps = 0);
template <typename T, typename I = int>
void qnDecisionFunction(const raft::handle_t& cuml_handle,
const qn_params& params,
T* X, bool X_col_major, I N, I D, I C,
T* coefs, T* scores);
template <typename T, typename I = int>
void qnDecisionFunctionSparse(const raft::handle_t& cuml_handle,
const qn_params& params,
T* X_values, I* X_cols, I* X_row_ids, I X_nnz,
I N, I D, I C, T* coefs, T* scores);
template <typename T, typename I = int>
void qnPredict(const raft::handle_t& cuml_handle, const qn_params& params,
T* X, bool X_col_major, I N, I D, I C,
T* coefs, T* preds);
template <typename T, typename I = int>
void qnPredictSparse(const raft::handle_t& cuml_handle,
const qn_params& params,
T* X_values, I* X_cols, I* X_row_ids, I X_nnz,
I N, I D, I C, T* coefs, T* preds);
} // namespace GLM
} // namespace ML
Import
#include <cuml/linear_model/glm.hpp>
I/O Contract
Inputs (olsFit)
| Name | Type | Required | Description |
|---|---|---|---|
| handle | const raft::handle_t& | Yes | cuML handle for GPU resource management |
| input | float*/double* | Yes | Device pointer to feature matrix [n_rows x n_cols] |
| n_rows | size_t | Yes | Number of rows in the feature matrix |
| n_cols | size_t | Yes | Number of columns in the feature matrix |
| labels | float*/double* | Yes | Device pointer to label vector [n_rows] |
| fit_intercept | bool | Yes | Whether to fit an intercept term |
| algo | int | No (default 0) | Solver: 0=SVD, 1=Eigendecomposition, 2=QR |
| sample_weight | float*/double* | No (default nullptr) | Device pointer to sample weights [n_rows]; nullptr for uniform weights |
Inputs (ridgeFit, additional)
| Name | Type | Required | Description |
|---|---|---|---|
| alpha | float*/double* | Yes | Host pointer to L2 regularization parameter(s) |
| n_alpha | int | Yes | Number of regularization parameters |
Inputs (qnFit)
| Name | Type | Required | Description |
|---|---|---|---|
| params | const qn_params& | Yes | Model parameters (loss type, regularization, etc.) |
| X | T* | Yes | Device pointer to feature matrix [N x D] |
| X_col_major | bool | Yes | Whether X is stored in column-major layout |
| y | T* | Yes | Device pointer to label vector [N] |
| N | I | Yes | Number of examples |
| D | I | Yes | Number of features |
| C | I | Yes | Number of classes (or 1 for regression) |
| w0 | T* | Yes (inout) | Device pointer to initial weights, overwritten with final result |
| sample_weight | T* | No (default nullptr) | Device pointer to sample weights [N] |
| svr_eps | T | No (default 0) | Epsilon for SVR loss |
Outputs
| Name | Type | Description |
|---|---|---|
| coef | float*/double* | Device pointer to fitted model coefficients [n_cols] |
| intercept | float*/double* | Host pointer to the fitted intercept value |
| preds | float*/double* | Device pointer to predictions [n_rows] |
| w0 (qnFit) | T* | Device pointer to final model weights (overwritten in-place) |
| f (qnFit) | T* | Host pointer to final objective function value |
| num_iters (qnFit) | int* | Host pointer to actual number of iterations taken |
| scores (qnDecisionFunction) | T* | Device pointer to confidence scores [N] |
Usage Examples
#include <cuml/linear_model/glm.hpp>
#include <raft/core/handle.hpp>
void fit_ols_model() {
raft::handle_t handle;
size_t n_rows = 1000;
size_t n_cols = 10;
// Allocate device memory
float* X; // feature matrix
float* y; // labels
float* coef; // coefficients
float intercept;
cudaMalloc(&X, n_rows * n_cols * sizeof(float));
cudaMalloc(&y, n_rows * sizeof(float));
cudaMalloc(&coef, n_cols * sizeof(float));
// Load data into X and y...
// Fit OLS model using SVD solver
ML::GLM::olsFit(handle, X, n_rows, n_cols, y, coef, &intercept,
true, // fit_intercept
0); // algo = SVD
handle.sync_stream();
// Make predictions
float* preds;
cudaMalloc(&preds, n_rows * sizeof(float));
ML::GLM::gemmPredict(handle, X, n_rows, n_cols, coef, intercept, preds);
handle.sync_stream();
cudaFree(X);
cudaFree(y);
cudaFree(coef);
cudaFree(preds);
}