Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Rapidsai Cuml Pairwise Kernels

From Leeroopedia


Knowledge Sources
Domains Machine_Learning, Kernel_Methods, Similarity
Last Updated 2026-02-08 12:00 GMT

Overview

Provides GPU-accelerated pairwise kernel computation between feature arrays, supporting built-in kernels (linear, polynomial, RBF, sigmoid, laplacian, cosine, chi-squared) and user-defined Numba device kernels.

Description

The pairwise_kernels.py module computes kernel matrices K where K[i, j] is the kernel value between the i-th row of X and the j-th row of Y.

Built-in kernels:

  • linear_kernel -- dot product: X . Y^T
  • polynomial_kernel -- (gamma * X . Y^T + coef0) ^ degree
  • sigmoid_kernel -- tanh(gamma * X . Y^T + coef0)
  • rbf_kernel -- exp(-gamma * ||X - Y||^2)
  • laplacian_kernel -- exp(-gamma * ||X - Y||_1)
  • cosine_similarity -- 1 - cosine_distance(X, Y)
  • additive_chi2_kernel -- additive chi-squared kernel via Numba device function
  • chi2_kernel -- exp(gamma * additive_chi2(X, Y))

Custom kernels: Users can pass a Numba @cuda.jit(device=True) function as the metric parameter. The custom_kernel function launches a CUDA kernel that evaluates the user function elementwise and caches the compiled kernel for reuse.

The main entry point is the pairwise_kernels function, which dispatches to the appropriate kernel implementation based on the metric string or callable.

Usage

Use this function when you need to compute kernel matrices for kernel-based methods such as SVM, Kernel Ridge Regression, Gaussian Processes, or any algorithm that operates in a reproducing kernel Hilbert space. It replaces scikit-learn's sklearn.metrics.pairwise.pairwise_kernels with GPU acceleration.

Code Reference

Source Location

  • Repository: Rapidsai_Cuml
  • File: python/cuml/cuml/metrics/pairwise_kernels.py

Signature

def pairwise_kernels(
    X,
    Y=None,
    metric="linear",
    *,
    filter_params=False,
    convert_dtype=True,
    **kwds,
)

Import

from cuml.metrics import pairwise_kernels

I/O Contract

Inputs

Name Type Required Description
X Dense matrix (device or host) of shape (n_samples_X, n_features) Yes First feature array. Accepts cuDF DataFrame, NumPy ndarray, Numba device ndarray, CuPy array.
Y Dense matrix (device or host) of shape (n_samples_Y, n_features) No Second feature array. If None, Y = X (compute self-kernel). Default None.
metric str or callable (Numba device function) No Kernel to compute. Built-in options: "linear", "polynomial" / "poly", "rbf", "sigmoid", "laplacian", "cosine", "additive_chi2", "chi2", "precomputed". Or a Numba @cuda.jit(device=True) callable. Default "linear".
filter_params bool No If True, silently ignore invalid keyword arguments. Default False.
convert_dtype bool No Automatically convert Y to match X dtype if they differ. Default True.
**kwds keyword arguments No Additional parameters passed to the kernel function (e.g. gamma, degree, coef0).

Outputs

Name Type Description
K ndarray of shape (n_samples_X, n_samples_Y) Kernel matrix where K[i, j] is the kernel between the i-th vector of X and the j-th vector of Y.

Usage Examples

import cupy as cp
from cuml.metrics import pairwise_kernels
from numba import cuda
import math

X = cp.array([[2, 3], [3, 5], [5, 8]], dtype=cp.float64)
Y = cp.array([[1, 0], [2, 1]], dtype=cp.float64)

# Linear kernel
K_linear = pairwise_kernels(X, Y, metric='linear')
print(K_linear)
# [[ 2,  7],
#  [ 3, 11],
#  [ 5, 18]]

# RBF kernel
K_rbf = pairwise_kernels(X, Y, metric='rbf', gamma=0.5)
print(K_rbf)

# Custom Numba device kernel
@cuda.jit(device=True)
def custom_rbf(x, y, gamma=0.5):
    s = 0.0
    for i in range(len(x)):
        s += (x[i] - y[i]) ** 2
    return math.exp(-gamma * s)

K_custom = pairwise_kernels(X, Y, metric=custom_rbf)
print(K_custom)

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment