Implementation:Rapidsai Cuml SVM C API
| Knowledge Sources | |
|---|---|
| Domains | Machine_Learning, Support_Vector_Machines |
| Last Updated | 2026-02-08 12:00 GMT |
Overview
Provides a C-compatible API for GPU-accelerated Support Vector Machine classification (SVC), enabling SVM fit and predict operations through C-linkage functions for use with foreign function interfaces.
Description
The svm_api.h header declares C-linkage wrappers around the cuML C++ SVM implementation. It provides both single-precision (Sp) and double-precision (Dp) variants for fitting and predicting with Support Vector Classifiers.
Type Definitions:
cumlSvmKernelType: An enum defining the supported kernel types:LINEAR,POLYNOMIAL,RBF, andTANH.
Fit Functions:
cumlSpSvcFit/cumlDpSvcFit: Train an SVM classifier on dense column-major input data. The output device buffers for support vectors, dual coefficients, support indices, and unique labels are allocated internally and returned as output pointers. Host scalar outputs include the number of support vectors (n_support), the number of classes (n_classes), and the bias term (b).
Predict Functions:
cumlSpSvcPredict/cumlDpSvcPredict: Predict class labels or decision function values using a previously trained SVM model. The outputpredsarray must be pre-allocated.
All functions return cumlError_t for error handling.
Usage
Use this C API when integrating cuML SVM functionality into non-C++ environments such as Python bindings, C libraries, or other language FFIs. For pure C++ usage, prefer the svc.hpp C++ API which provides a richer type-safe interface.
Code Reference
Source Location
- Repository: Rapidsai_Cuml
- File:
cpp/include/cuml/svm/svm_api.h
Signature
typedef enum cumlSvmKernelType { LINEAR, POLYNOMIAL, RBF, TANH } cumlSvmKernelType;
cumlError_t cumlSpSvcFit(cumlHandle_t handle,
float* input, int n_rows, int n_cols, float* labels,
float C, float cache_size, int max_iter, int nochange_steps,
float tol, int verbosity, cumlSvmKernelType kernel,
int degree, float gamma, float coef0,
int* n_support, float* b, float** dual_coefs,
float** x_support, int** support_idx,
int* n_classes, float** unique_labels);
cumlError_t cumlDpSvcFit(cumlHandle_t handle,
double* input, int n_rows, int n_cols, double* labels,
double C, double cache_size, int max_iter, int nochange_steps,
double tol, int verbosity, cumlSvmKernelType kernel,
int degree, double gamma, double coef0,
int* n_support, double* b, double** dual_coefs,
double** x_support, int** support_idx,
int* n_classes, double** unique_labels);
cumlError_t cumlSpSvcPredict(cumlHandle_t handle,
float* input, int n_rows, int n_cols,
cumlSvmKernelType kernel, int degree, float gamma, float coef0,
int n_support, float b, float* dual_coefs,
float* x_support, int n_classes, float* unique_labels,
float* preds, float buffer_size, int predict_class);
cumlError_t cumlDpSvcPredict(cumlHandle_t handle,
double* input, int n_rows, int n_cols,
cumlSvmKernelType kernel, int degree, double gamma, double coef0,
int n_support, double b, double* dual_coefs,
double* x_support, int n_classes, double* unique_labels,
double* preds, double buffer_size, int predict_class);
Import
#include <cuml/svm/svm_api.h>
I/O Contract
Inputs
cumlSpSvcFit / cumlDpSvcFit
| Name | Type | Required | Description |
|---|---|---|---|
| handle | cumlHandle_t | Yes | cuML handle for GPU resources |
| input | T* | Yes | Device pointer to input data [n_rows x n_cols], column-major |
| n_rows | int | Yes | Number of training samples |
| n_cols | int | Yes | Number of features |
| labels | T* | Yes | Device pointer to class labels [n_rows] |
| C | T | Yes | Penalty parameter |
| cache_size | T | Yes | Size of kernel cache in device memory (MiB) |
| max_iter | int | Yes | Maximum number of SMO outer iterations |
| nochange_steps | int | Yes | Max outer iterations without convergence change |
| tol | T | Yes | Tolerance to stop fitting |
| verbosity | int | Yes | Verbosity level for logging |
| kernel | cumlSvmKernelType | Yes | Kernel type (LINEAR, POLYNOMIAL, RBF, or TANH) |
| degree | int | Yes | Degree for polynomial kernel (ignored by other kernels) |
| gamma | T | Yes | Gamma multiplier for RBF, POLYNOMIAL, and TANH kernels |
| coef0 | T | Yes | Additive constant for POLYNOMIAL and TANH kernels |
cumlSpSvcPredict / cumlDpSvcPredict
| Name | Type | Required | Description |
|---|---|---|---|
| handle | cumlHandle_t | Yes | cuML handle |
| input | T* | Yes | Device pointer to input data [n_rows x n_cols] |
| n_rows | int | Yes | Number of samples |
| n_cols | int | Yes | Number of features |
| kernel | cumlSvmKernelType | Yes | Kernel type |
| degree | int | Yes | Polynomial degree |
| gamma | T | Yes | Gamma parameter |
| coef0 | T | Yes | Coef0 parameter |
| n_support | int | Yes | Number of support vectors |
| b | T | Yes | Bias term from training |
| dual_coefs | T* | Yes | Device pointer to dual coefficients [n_support] |
| x_support | T* | Yes | Device pointer to support vectors [n_support x n_cols] |
| n_classes | int | Yes | Number of classes |
| unique_labels | T* | Yes | Device pointer to unique class labels [n_classes] |
| buffer_size | T | Yes | Temporary buffer size in MiB |
| predict_class | int | Yes | 1 to predict class labels, 0 for decision function values |
Outputs
| Name | Type | Description |
|---|---|---|
| n_support (fit) | int* | Host pointer returning the number of support vectors |
| b (fit) | T* | Host pointer returning the bias term |
| dual_coefs (fit) | T** | Device pointer to allocated dual coefficients array [n_support] |
| x_support (fit) | T** | Device pointer to allocated support vectors [n_support x n_cols] |
| support_idx (fit) | int** | Device pointer to allocated support indices [n_support] |
| n_classes (fit) | int* | Host pointer returning the number of classes |
| unique_labels (fit) | T** | Device pointer to allocated unique labels [n_classes] |
| preds (predict) | T* | Device array of predictions [n_rows] |
| return value | cumlError_t | CUML_SUCCESS on success, error flag on failure |
Usage Examples
#include <cuml/svm/svm_api.h>
cumlHandle_t handle; // assume initialized
// Training data on device
float* d_input; // [1000 x 50], column-major
float* d_labels; // [1000]
int n_rows = 1000, n_cols = 50;
// Output variables
int n_support, n_classes;
float b;
float* dual_coefs = nullptr;
float* x_support = nullptr;
int* support_idx = nullptr;
float* unique_labels = nullptr;
// Fit SVM with RBF kernel
cumlError_t status = cumlSpSvcFit(handle, d_input, n_rows, n_cols, d_labels,
1.0f, // C
200.0f, // cache_size
-1, // max_iter
1000, // nochange_steps
1e-3f, // tol
0, // verbosity
RBF, // kernel
3, // degree
0.02f, // gamma
0.0f, // coef0
&n_support, &b, &dual_coefs,
&x_support, &support_idx,
&n_classes, &unique_labels);
// Predict
float* d_preds; // pre-allocated [n_rows]
cumlSpSvcPredict(handle, d_input, n_rows, n_cols,
RBF, 3, 0.02f, 0.0f,
n_support, b, dual_coefs, x_support,
n_classes, unique_labels,
d_preds, 200.0f, 1);