Coverage for / home / jenkins / .local / lib / python3.10 / site-packages / hyper_parallel / components / modules / mhc / mhc_family.py: 100%
24 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"""mHC replacement entry points and family declarations."""
17from __future__ import annotations
19from collections.abc import Mapping
20from typing import Any
22from torch import nn
23from hyper_parallel.components.modules.module_family import ModuleFamily, build_family, register_module_family
24from hyper_parallel.components.modules.family import SelectorSpec, VariantRule
25from hyper_parallel.models.replacement import module_replacement
26from ._utils import _validate_pipelined, pipelined_mhc_sharding
29@module_replacement
30def MHCPre( # pylint: disable=invalid-name
31 *, config: Any = None, module: nn.Module | None = None,
32 module_fqn: str = "", context: Mapping[str, Any] | None = None, mesh: Any = None,
33) -> nn.Module:
34 """Construct a tracked mhc_pre member without changing its execution contract."""
35 return build_family(MHCPre, config=config, module=module, module_fqn=module_fqn,
36 context=context, mesh=mesh)
39register_module_family(ModuleFamily(
40 name="mhc_pre", baseline=MHCPre,
41 variants={"local": "hyper_parallel.components.modules.mhc.mhc_pre.MhcPreModule"}, default_variant="local",
42))
45@module_replacement
46def MHCPost( # pylint: disable=invalid-name
47 *, config: Any = None, module: nn.Module | None = None,
48 module_fqn: str = "", context: Mapping[str, Any] | None = None, mesh: Any = None,
49) -> nn.Module:
50 """Construct a tracked mhc_post member without changing its execution contract."""
51 return build_family(MHCPost, config=config, module=module, module_fqn=module_fqn,
52 context=context, mesh=mesh)
55register_module_family(ModuleFamily(
56 name="mhc_post", baseline=MHCPost,
57 variants={"local": "hyper_parallel.components.modules.mhc.mhc_post.MhcPostModule"}, default_variant="local",
58))
61@module_replacement
62def PipelinedMhc( # pylint: disable=invalid-name
63 *, config: Any = None, module: nn.Module | None = None,
64 module_fqn: str = "", context: Mapping[str, Any] | None = None, mesh: Any = None,
65 shifted: bool = False, megakernel: bool = False, token_tile: int = 32,
66) -> nn.Module:
67 """Construct a shifted native MegaMhc with megakernel=True, or a local member."""
68 return build_family(PipelinedMhc, config=config, module=module, module_fqn=module_fqn,
69 context=context, mesh=mesh, shifted=shifted, megakernel=megakernel, token_tile=token_tile)
72register_module_family(ModuleFamily(
73 name="pipelined_mhc", baseline=PipelinedMhc,
74 variants={"local": "hyper_parallel.components.modules.mhc.pipelined_mhc.PipelinedMhcModule",
75 "shifted": "hyper_parallel.components.modules.mhc.pipelined_mhc.ShiftedPipelinedMhcModule",
76 "shifted_npu": "hyper_parallel.components.modules.mhc.pipelined_mhc.NativeShiftedPipelinedMhcModule"},
77 default_variant="local", selector_specs={"shifted": SelectorSpec(bool, False),
78 "megakernel": SelectorSpec(bool, False)},
79 variant_rules=(VariantRule({"shifted": True, "megakernel": False, "parallel": False}, "shifted"),
80 VariantRule({"shifted": True, "megakernel": True, "parallel": False}, "shifted_npu")),
81 validate=_validate_pipelined, sharding=pipelined_mhc_sharding,
82))
85@module_replacement
86def MegaMhc( # pylint: disable=invalid-name
87 *, config: Any = None, module: nn.Module | None = None, module_fqn: str = "",
88 context: Mapping[str, Any] | None = None, mesh: Any = None, backend: str = "torch",
89) -> nn.Module:
90 """Build a complete shifted boundary with an explicit execution backend."""
91 return build_family(MegaMhc, config=config, module=module, module_fqn=module_fqn,
92 context=context, mesh=mesh, backend=backend)
95register_module_family(ModuleFamily(
96 name="mega_mhc", baseline=MegaMhc,
97 variants={"local": "hyper_parallel.components.modules.mhc.mega_mhc.MegaMhcModule",
98 "fused": "hyper_parallel.components.modules.mhc.mega_mhc.FusedMegaMhcModule"},
99 selector_specs={"backend": SelectorSpec(str, "torch")},
100 default_variant="local",
101 variant_rules=(VariantRule({"backend": "fused", "parallel": False}, "fused"),),
102))