Coverage for / home / jenkins / .local / lib / python3.10 / site-packages / hyper_parallel / components / ops / impl / sinkhorn.py: 78%
18 statements
« prev ^ index » next coverage.py v7.13.1, created at 2026-12-05 05:12 +0800
« prev ^ index » next coverage.py v7.13.1, created at 2026-12-05 05:12 +0800
1# Copyright 2026 Huawei Technologies Co., Ltd
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14# ============================================================================
15"""Sinkhorn normalization of mHC residual mixing logits."""
17from typing import Protocol
19import torch
21from hyper_parallel.components.functional.sinkhorn import sinkhorn, sinkhorn_knopps
23from ..registry import OpRegistry
24from ..spec import OpSpec
25from .._dependency import require_package, require_symbol
28class SinkhornFn(Protocol):
29 """Normalize square mixing matrices without changing their shape."""
31 def __call__(self, x: torch.Tensor, sinkhorn_iters: int = 20, eps: float = 1e-6) -> torch.Tensor:
32 """Return the normalized matrix."""
35SINKHORN = OpSpec[SinkhornFn](config_key="sinkhorn")
38def _load_omni() -> SinkhornFn:
39 require_package("omni_training_custom_ops")
40 require_symbol(torch.ops.custom, "npu_sinkhorn")
41 require_symbol(torch.ops.custom, "npu_sinkhorn_grad")
42 return sinkhorn
45def register_sinkhorn(registry: OpRegistry) -> None:
46 """Register basic Torch normalization and Omni's fused implementation."""
47 registry.register_family("sinkhorn", specs=(SINKHORN,))
48 registry.register_impl(SINKHORN, name="native", devices=("cpu", "cuda", "npu"), load=lambda: sinkhorn_knopps)
49 registry.register_impl(SINKHORN, name="omni", devices=("npu",), load=_load_omni)