Coverage for  / home / jenkins / .local / lib / python3.10 / site-packages / hyper_parallel / components / modules / mlp / mlp_family.py: 100%

11 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"""Dense MLP replacement entry points and family declarations.""" 

16 

17from __future__ import annotations 

18 

19from collections.abc import Mapping 

20from typing import Any 

21 

22from torch import nn 

23 

24from hyper_parallel.components.modules.module_family import ( 

25 ModuleFamily, 

26 SelectorSpec, 

27 VariantRule, 

28 build_family, 

29 register_module_family, 

30) 

31from hyper_parallel.models.replacement import module_replacement 

32 

33 

34@module_replacement 

35def SwiGLUMLP( # pylint: disable=invalid-name 

36 *, config: Any = None, module: nn.Module | None = None, 

37 module_fqn: str = "", context: Mapping[str, Any] | None = None, mesh: Any = None, 

38 activation_dtype: str | None = None, swiglu_limit: float | None = None, 

39 state_layout: str = "linear_fc", 

40) -> nn.Module: 

41 """Construct a tracked MLP from complete config or separate source projections.""" 

42 return build_family(SwiGLUMLP, config=config, module=module, module_fqn=module_fqn, 

43 context=context, mesh=mesh, activation_dtype=activation_dtype, 

44 swiglu_limit=swiglu_limit, state_layout=state_layout) 

45 

46 

47register_module_family(ModuleFamily( 

48 name="swiglu_mlp", 

49 baseline=SwiGLUMLP, 

50 variants={ 

51 "local": "hyper_parallel.components.modules.mlp.swiglu_mlp.SwiGLUMLPModule", 

52 "w1_w2": "hyper_parallel.components.modules.mlp.swiglu_mlp.W1W2SwiGLUMLPModule", 

53 "separate": "hyper_parallel.components.modules.mlp.swiglu_mlp.SeparateSwiGLUMLPModule", 

54 }, 

55 selector_specs={"state_layout": SelectorSpec(str, "linear_fc")}, 

56 variant_rules=(VariantRule({"state_layout": "w1_w2", "parallel": False}, "w1_w2"), 

57 VariantRule({"state_layout": "separate", "parallel": False}, "separate")), 

58 default_variant="local", 

59)) 

60 

61__all__ = ["SwiGLUMLP"]