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

19 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"""Gated NoPE MLA family with a mandatory output gate.""" 

16 

17from __future__ import annotations 

18 

19from importlib import import_module 

20from typing import Any 

21 

22from torch import nn 

23 

24from hyper_parallel.components.modules.module_family import ( 

25 ModuleFamily, VariantRule, build_family, register_module_family, 

26) 

27from hyper_parallel.components.modules.attention.multi_latent_attention.multi_latent_attention_family import ( 

28 mla_local_parallel_spec, mla_sharding, validate_mla_family, 

29) 

30from hyper_parallel.models.replacement import module_replacement 

31 

32 

33def validate_gated_mla_family(*, config: Any, module: nn.Module | None, context: Any, member: Any) -> None: 

34 """Keep gated MLA's TP restriction independent of the model API adapter's family name.""" 

35 if member.axis_sizes["tp"] != 1: 

36 raise ValueError("Gated MLA currently supports TP1 only") 

37 validate_mla_family(config=config, module=module, context=context, member=member) 

38 

39 

40def _build_local( 

41 *, config: Any = None, module: nn.Module | None = None, module_fqn: str = "", context: Any = None, 

42 strategy: str = "expanded_ulysses", overlap: bool = False, 

43 ulysses_degree: int | None = None, kv_exchange: str = "all_gather", 

44) -> nn.Module: 

45 """Consume family-only options; the saved request retains them for CP preparation.""" 

46 # Resolve after family registration; concrete members import this module's layout validators. 

47 implementation = import_module('.gated_multi_latent_attention', __package__).GatedMultiLatentAttentionModule 

48 

49 del module_fqn, context, strategy, overlap, ulysses_degree, kv_exchange 

50 return implementation(config=config, module=module) 

51 

52 

53@module_replacement 

54def GatedMultiLatentAttention( # pylint: disable=invalid-name 

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

56 module_fqn: str = "", context: Any = None, mesh: Any = None, 

57 strategy: str = "expanded_ulysses", overlap: bool = False, 

58 ulysses_degree: int | None = None, kv_exchange: str = "all_gather", 

59) -> nn.Module: 

60 """Build the local gated NoPE MLA member. 

61 

62 Args: 

63 config: Full model dimensions for direct construction, with a mandatory output gate. 

64 module: Optional supported NoPE source; exclusive with config. 

65 module_fqn: Source path supplied by model replacement. 

66 context: Logical build constraints. Topology selects the local or CP implementation. 

67 mesh: Optional existing job mesh used by the shared layout planner. 

68 strategy: Expanded QKV Ulysses or shared latent-KV communication. 

69 overlap: Enable existing forward and supported backward scheduling. 

70 ulysses_degree: CP head-parallel factor; the remaining factor partitions queries. 

71 kv_exchange: Full AllGather or selective causal-document AllToAllV. 

72 

73 Returns: 

74 Gated MLA with its own NoPE math, gate Parameters and family provenance. 

75 """ 

76 return build_family(GatedMultiLatentAttention, config=config, module=module, 

77 module_fqn=module_fqn, context=context, mesh=mesh, 

78 strategy=strategy, overlap=overlap, ulysses_degree=ulysses_degree, kv_exchange=kv_exchange) 

79 

80 

81register_module_family(ModuleFamily( 

82 name="gated_multi_latent_attention", baseline=GatedMultiLatentAttention, 

83 variants={"local": ( 

84 "hyper_parallel.components.modules.attention.gated_multi_latent_attention." 

85 "gated_multi_latent_attention_family._build_local" 

86 ), "cp": ( 

87 "hyper_parallel.components.modules.attention.gated_multi_latent_attention." 

88 "cp_gated_multi_latent_attention.CPGatedMultiLatentAttentionModule" 

89 )}, 

90 default_variant="local", parallel_axis="cp", 

91 variant_rules=(VariantRule({"parallel": True}, "cp"),), 

92 sharding=mla_sharding, validate=validate_gated_mla_family, parallel_spec=mla_local_parallel_spec, 

93))