Optimal Transport
TorchMorph provides a differentiable Sinkhorn solver that runs on CPU or CUDA.
CUDA float32 inputs automatically use the fused implementation.
torchmorph.build_cost_matrix
Build pairwise distances between points on a spatial grid
The grid contains d = prod(shape) points in row-major order. Flatten
grid-shaped distributions to (n, d) before passing this matrix to
:class:SinkhornSolver.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
shape
|
sequence[int]
|
Spatial grid shape. |
required |
p
|
float
|
Norm degree passed to :func: |
2
|
device
|
device or str
|
Device for the returned matrix. |
None
|
Returns:
| Type | Description |
|---|---|
Tensor
|
torch.Tensor: Symmetric |
torchmorph.SinkhornSolver
Bases: Module
Solve entropy-regularized balanced optimal transport
The module accepts batches of flattened histograms and returns transport
costs that are differentiable with respect to both marginals. CUDA
float32 inputs use fused kernels; other device and dtype combinations
use PyTorch operations. Set log_space=True for better stability at small
regularization values.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
epsilon
|
float
|
Positive entropy-regularization strength. |
1.0
|
max_iter
|
int
|
Positive maximum number of Sinkhorn iterations. |
100
|
threshold
|
float
|
Nonnegative early-stopping tolerance checked by the
PyTorch implementation. Fused CUDA kernels always run |
0.0
|
p
|
float
|
Norm used by the default one-dimensional cost matrix. |
2
|
log_space
|
bool
|
Run Sinkhorn iterations in the log domain. |
False
|
Example
data_preprocess
Validate and normalize batched transport marginals
Negative values are clamped to zero and every row is normalized to unit
mass. These operations remain in the autograd graph. If no cost matrix
is supplied, the d bins are treated as points on a line.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source
|
Tensor
|
Floating-point source marginals with shape
|
required |
target
|
Tensor
|
Floating-point target marginals with the same
shape as |
required |
cost_matrix
|
Tensor
|
Shared pairwise cost matrix
with shape |
None
|
Returns:
| Type | Description |
|---|---|
Tensor
|
tuple[torch.Tensor, torch.Tensor, torch.Tensor]: Normalized source, |
Tensor
|
normalized target, and contiguous cost matrix on the source device |
Tensor
|
and with the source dtype. |
forward
Compute transport costs between corresponding marginal rows
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source
|
Tensor
|
Floating-point source marginals with shape
|
required |
target
|
Tensor
|
Floating-point target marginals with shape
|
required |
cost_matrix
|
Tensor
|
Shared cost matrix with shape
|
None
|
Returns:
| Type | Description |
|---|---|
Tensor
|
torch.Tensor: Differentiable transport costs with shape |
plan
Reconstruct transport plans in the log domain
This method runs without gradient tracking.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source
|
Tensor
|
Floating-point source marginals with shape
|
required |
target
|
Tensor
|
Floating-point target marginals with shape
|
required |
cost_matrix
|
Tensor
|
Shared cost matrix with shape
|
None
|
Returns:
| Type | Description |
|---|---|
Tensor
|
torch.Tensor: Transport plans with shape |
potentials
Compute centered dual potentials for both marginals
The potentials are the envelope-theorem gradients of the entropic transport cost with respect to normalized marginals. This method runs without gradient tracking.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source
|
Tensor
|
Floating-point source marginals with shape
|
required |
target
|
Tensor
|
Floating-point target marginals with shape
|
required |
cost_matrix
|
Tensor
|
Shared cost matrix with shape
|
None
|
Returns:
| Type | Description |
|---|---|
Tensor
|
tuple[torch.Tensor, torch.Tensor]: Source and target potentials, |
Tensor
|
each with shape |