Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

TorchPairwise / documentation

Comparisons and
Streamed Operators

Distances, divergences, kernels and scores for tensor samples. One comparison API, four pairing modes, shared native execution in Python and C++.

87 comparison entries13 kernels9 input familiesCPU / CUDA
Four comparison modes: all-pairs matrix, paired values, condensed upper triangle and one single-sample value. Streaming computes tiles and combines them into the requested result.

A Comparison in Either Language

The top-bar Python / C++ switch changes paired examples throughout this guide. Use the same metric name and mode in both languages.

Python
import torch
import torchpairwise as tp

X = torch.tensor([[0., 0.], [3., 4.]])  # [N=2,D=2]
Y = torch.tensor([[0., 4.], [3., 0.]])  # [M=2,D=2]

scores = tp.compare(X, Y, metric="sqeuclidean")  # [2,2]
C++ / LibTorch
#include <torchpairwise.h>
namespace tp = torchpairwise::ops;

auto X = at::tensor({0.f, 0.f, 3.f, 4.f}).reshape({2, 2});
auto Y = at::tensor({0.f, 4.f, 3.f, 0.f}).reshape({2, 2});
auto scores = tp::compare(X, Y, "sqeuclidean");  // [2,2]

Result: [[16, 9], [9, 16]].

Start with the quick start, or browse the comparison catalog. Both numeric and nonnumeric samples use ordinary dense tensors; structured distributions group their component tensors.

Explore the example gallery for kernel ridge regression, truncated kernel PCA, kernel LDA, binary SVM, landmark regression, PageRank and kernel fitting with an intercept, graph diffusion and matrix-function actions. Each pairs a streamed algorithm with a small dense correctness reference.

Streamed sources also support pair masks, entropic transport over any lazy cost, Gaussian process prediction, user-defined metrics, encoded features with one or two encoders, and optional execution backends.

This guide documents TorchPairwise 1.0. See installation for matching your PyTorch build and migration when upgrading.

Python and native callers share built-in forward and first-backward algorithms. Metric-specific domains, autocast, compilation and boundary derivatives are part of each comparison's contract. A symmetric entry is not necessarily a triangle-inequality metric, and a score need not be a positive-semidefinite kernel.