Transport reference
Input rules, surface overview, and graph-capture notes for torchmatch.transport ops.
Signatures, parameter types, and per-op descriptions are auto-generated from the source and browseable in the API reference →.
This page covers cross-cutting rules that apply to both transport surfaces.
Two surfaces
The torchmatch.transport namespace exposes two sub-packages that
solve closely related problems with different input conventions and
different runtime requirements:
torchmatch.transport.matrixtakes a precomputed cost matrix (shape(N, M)or(B, N, M)) and returns a transport plan in log space or a scalar divergence. Backed by Python custom ops that run iterative log-sum-exp loops on CPU and CUDA;EXACT_EMDuses a network-simplex algorithm (CPU only).torchmatch.transport.samplestakes two raw point clouds (shapes(N, D)and(M, D)) and returns a scalar OT loss or debiased divergence. Pairwise squared distances are computed on the fly inside Triton kernels — no N×M matrix is materialised. CUDA only.
Both surfaces register their ops under torch.ops.transport.*; the
Python-side dispatchers (transport.matrix.solve,
transport.samples.loss) are the recommended entry points.
Input semantics
transport.matrix
NaNcells are rejected with aRuntimeError.-infcells are rejected with aRuntimeError. A cost of-infwould break the solver's numerical assumptions; use a large negative finite value if you need to strongly favour a particular pair.+infcells are forbidden edges. The dispatcher passes them through to the LSE solver, where they cancel automatically; the network simplex handles them by withholding the edge from the graph.mask(optionalbooltensor of the same shape ascost):Falsecells are rewritten to+infbefore the solver runs.aandb(optionalfloattensors): source and target marginals. Both default to uniform; either can be supplied on its own. Both must be non-negative.
transport.matrix.solve raises ValueError for an out-of-range
ndim, a non-float dtype, a non-CPU / non-CUDA device, a NaN or
-inf cell, a non-positive reg, or an unknown backend string.
transport.samples
The samples surface is CUDA-only; loss(...) raises RuntimeError
when called with CPU tensors. Triton kernels live under
sources/torchmatch/transport/samples/kernels/ and load lazily on
first call.
Tracing & graph capture
Both surfaces are compatible with torch.compile and support autograd.
They can be used inside compiled model code or as differentiable loss
functions.
See also
- Operations: the assignment ops, for comparison.
- Building: wheel vs JIT paths and environment overrides.
Algorithms
The optimal transport problem, Sinkhorn, debiasing, unbalanced OT, and the network simplex — plus how the matrix-face Python LSE loops and the samples-face Triton streaming kernel implement them.
Choosing
A decision guide for transport.matrix and transport.samples backends, organised by cost type, differentiability requirements, and problem scale.