Skip to navigation

nemo_automodel.components.models.common.sinkhorn

View as MarkdownOpen in Claude

Sinkhorn normalization of hyper-connection (mHC) mixing logits.

The torch implementation is the numerical reference. The optional CUDA path uses DeepSeek TileKernels tile_kernels.modeling.mhc.ops.sinkhorn_normalize and its low-level kernels; no TileKernels source is vendored. Upstream source: https://github.com/deepseek-ai/TileKernels/blob/36d9e45d38e204ebb87e6f6e833821eee0482fe5/tile_kernels/modeling/mhc/ops/sinkhorn.py Upstream license: MIT, copyright 2026 DeepSeek.

Module Contents

Classes

NameDescription
_TileKernelsSinkhornTileKernels Sinkhorn wrapper that accepts non-contiguous backward gradients.

Functions

NameDescription
is_tile_kernels_sinkhorn_availableReturn whether the TileKernels Sinkhorn wrapper and its forward/backward kernels are importable.
sinkhorn_normalize_tile_kernelsRun the TileKernels Sinkhorn; never falls back to the torch path.
sinkhorn_normalize_torchNormalize mixing logits into an approximately doubly stochastic matrix.

API

class nemo_automodel.components.models.common.sinkhorn._TileKernelsSinkhorn()

Bases: Function

TileKernels Sinkhorn wrapper that accepts non-contiguous backward gradients.

The upstream high-level wrapper launches the backward kernel with grad_output as-is. Hyper-connection combinations are consumed through transposed matmul sites, so autograd can provide a transposed gradient layout. The low-level TileKernels backward kernel requires contiguous row-major inputs.

nemo_automodel.components.models.common.sinkhorn._TileKernelsSinkhorn.backward(
ctx: torch.autograd.function.FunctionCtx,
grad_output: torch.Tensor
) -> tuple[torch.Tensor, None, None]
staticmethod

Run the TileKernels backward kernel on a contiguous copy of the gradient.

Parameters:

ctx
torch.autograd.function.FunctionCtx

Autograd context.

grad_output
torch.Tensor

Gradient of shape […, streams, streams], any strides.

Returns: tuple[torch.Tensor, None, None]

Input gradient with the forward input’s shape, then None for repeat and eps.

nemo_automodel.components.models.common.sinkhorn._TileKernelsSinkhorn.forward(
ctx: torch.autograd.function.FunctionCtx,
x: torch.Tensor,
repeat: int,
eps: float
) -> torch.Tensor
staticmethod

Run the TileKernels forward kernel.

Parameters:

ctx
torch.autograd.function.FunctionCtx

Autograd context.

x
torch.Tensor

CUDA logits of shape […, streams, streams], with arbitrary leading dimensions.

repeat
int

Number of normalization rounds.

eps
float

Additive stabilizer.

Returns: torch.Tensor

Tensor with the shape of x, independent of its storage.

nemo_automodel.components.models.common.sinkhorn.is_tile_kernels_sinkhorn_available() -> bool

Return whether the TileKernels Sinkhorn wrapper and its forward/backward kernels are importable.

nemo_automodel.components.models.common.sinkhorn.sinkhorn_normalize_tile_kernels(
x: torch.Tensor,
repeat: int,
eps: float
) -> torch.Tensor

Run the TileKernels Sinkhorn; never falls back to the torch path.

Parameters:

x
torch.Tensor

CUDA logits of shape […, streams, streams], with arbitrary leading dimensions.

repeat
int

Number of normalization rounds.

eps
float

Additive stabilizer.

Returns: torch.Tensor

Tensor with the shape of x.

Raises:

  • RuntimeError: If TileKernels is not importable or x is not a CUDA tensor.
nemo_automodel.components.models.common.sinkhorn.sinkhorn_normalize_torch(
x: torch.Tensor,
repeat: int,
eps: float
) -> torch.Tensor

Normalize mixing logits into an approximately doubly stochastic matrix.

Parameters:

x
torch.Tensor

Logits of shape […, streams, streams], with arbitrary leading dimensions.

repeat
int

Number of row/column normalization rounds; the first round starts from a softmax.

eps
float

Additive stabilizer applied to every normalization denominator.

Returns: torch.Tensor

Tensor with the shape and dtype of x.