Coverage for  / home / jenkins / .local / lib / python3.10 / site-packages / hyper_parallel / components / ops / impl / mhc_post.py: 77%

31 statements  

« 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"""Backends for residual mixing after an mHC-wrapped sublayer.""" 

16 

17from functools import partial 

18from typing import Protocol 

19 

20import torch 

21 

22from hyper_parallel.components.functional.mhc_post import mhc_post 

23 

24from .._dependency import require_package, require_symbol 

25from ..registry import OpRegistry 

26from ..spec import OpSpec 

27 

28 

29class MhcPostFn(Protocol): 

30 """Combine residual and transformed streams and flatten the result.""" 

31 

32 def __call__( 

33 self, x: torch.Tensor, residual: torch.Tensor, h_post: torch.Tensor, h_res: torch.Tensor, num_stream: int, 

34 ) -> torch.Tensor: 

35 """Return mixed streams with the stream dimension flattened.""" 

36 

37 

38MHC_POST = OpSpec[MhcPostFn](config_key="mhc_post") 

39 

40 

41def _native( 

42 x: torch.Tensor, residual: torch.Tensor, h_post: torch.Tensor, h_res: torch.Tensor, num_stream: int, 

43) -> torch.Tensor: 

44 streams = residual.reshape(x.shape[0], x.shape[1], num_stream, x.shape[-1]) 

45 transformed = h_post.unsqueeze(-1) * x.unsqueeze(-2) 

46 mixed = (h_res.unsqueeze(-1) * streams.unsqueeze(-2)).sum(dim=2) 

47 return (transformed + mixed).flatten(2).to(x.dtype) 

48 

49 

50def _load_native() -> MhcPostFn: 

51 return _native 

52 

53 

54def _load_omni() -> MhcPostFn: 

55 require_package("omni_training_custom_ops") 

56 require_symbol(torch.ops.custom, "npu_ai_infra_manifold_constrained_hyper_connection_post") 

57 require_symbol(torch.ops.custom, "npu_ai_infra_mhc_post_grad") 

58 return partial(mhc_post, backend="omni") 

59 

60 

61def _load_cann() -> MhcPostFn: 

62 require_package("cann_ops_transformer") 

63 require_symbol(require_package("cann_ops_transformer.ops"), "mhc_post") 

64 return partial(mhc_post, backend="cann") 

65 

66 

67def register_mhc_post(registry: OpRegistry) -> None: 

68 """Register Torch residual mixing and the optional CANN and Omni kernels.""" 

69 registry.register_family("mhc_post", specs=(MHC_POST,)) 

70 registry.register_impl(MHC_POST, name="native", devices=("cuda", "cpu", "npu"), load=_load_native) 

71 registry.register_impl(MHC_POST, name="cann", devices=("npu",), load=_load_cann) 

72 registry.register_impl(MHC_POST, name="omni", devices=("npu",), load=_load_omni)