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

15 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 GQA family with a mandatory per-query-head sigmoid gate.""" 

16 

17from __future__ import annotations 

18 

19from typing import Any 

20 

21from torch import nn 

22 

23from hyper_parallel.components.modules.module_family import ( 

24 ModuleFamily, VariantRule, build_family, register_module_family, 

25) 

26from hyper_parallel.models.replacement import module_replacement 

27 

28 

29from ..grouped_query_attention.grouped_query_attention_family import ( 

30 _store_gqa_cp_options, gqa_local_parallel_spec, gqa_sharding, validate_gqa_family, 

31) 

32from .gated_grouped_query_attention import GatedGroupedQueryAttentionModule 

33 

34 

35def _build_local( 

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

37) -> nn.Module: 

38 """Build gated math; shared layout binding handles TP/SP.""" 

39 del module_fqn, context 

40 return GatedGroupedQueryAttentionModule(config=config, module=module) 

41 

42 

43@module_replacement 

44def GatedGroupedQueryAttention( # pylint: disable=invalid-name 

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

46 context: Any = None, mesh: Any = None, 

47 ulysses_cp_size: int | None = None, ulysses_cp_strategy: str | None = None, 

48 kv_cp_size: int | None = None, kv_cp_strategy: str | None = None, 

49 ring_cp_size: int | None = None, ring_cp_strategy: str | None = None, 

50 ring_cp_load_balance: bool | str | None = None, 

51 cp_comm_overlap: bool | None = None, prune_documents: bool | None = None, 

52) -> nn.Module: 

53 """Build gated GQA; module-level CP overrides are stored on its attention config. 

54 

55 Args: 

56 config: Native dimensions/options; mutually exclusive with module. Q/K RMSNorm is opt-in via qk_norm. 

57 module: Source attention with the supported projection and checkpoint layout. 

58 module_fqn: Source module path supplied by replacement. 

59 context: Logical parallel configuration, independent of concrete process groups. 

60 mesh: Existing job mesh, borrowed by the shared execution binder. 

61 ulysses_cp_size: Number of ranks exchanging sequence for heads. 

62 ulysses_cp_strategy: Currently only ``alltoall`` is supported. 

63 kv_cp_size: Number of ranks in the original KV AllGather domain. 

64 kv_cp_strategy: Currently only ``allgather`` is supported. 

65 ring_cp_size: Number of ranks in Ring; may combine with Ulysses. 

66 ring_cp_strategy: Currently only ``p2p`` is supported. 

67 ring_cp_load_balance: Optional ``zigzag`` balancing for Ring query work. 

68 cp_comm_overlap: Submit supported CP communications asynchronously. 

69 prune_documents: Prune unused packed-document KV routes in Ring mode. 

70 

71 Returns: 

72 The local or CP member with fixed parameter names and family provenance. 

73 """ 

74 _store_gqa_cp_options(config, module, { 

75 "ulysses_cp_size": ulysses_cp_size, 

76 "ulysses_cp_strategy": ulysses_cp_strategy, 

77 "kv_cp_size": kv_cp_size, 

78 "kv_cp_strategy": kv_cp_strategy, 

79 "ring_cp_size": ring_cp_size, 

80 "ring_cp_strategy": ring_cp_strategy, 

81 "ring_cp_load_balance": ring_cp_load_balance, 

82 "cp_comm_overlap": cp_comm_overlap, 

83 "prune_documents": prune_documents, 

84 }) 

85 return build_family(GatedGroupedQueryAttention, config=config, module=module, module_fqn=module_fqn, 

86 context=context, mesh=mesh) 

87 

88 

89register_module_family(ModuleFamily( 

90 name="gated_grouped_query_attention", baseline=GatedGroupedQueryAttention, 

91 variants={"local": ( 

92 "hyper_parallel.components.modules.attention.gated_grouped_query_attention." 

93 "gated_grouped_query_attention_family._build_local" 

94 ), 

95 "cp": ( 

96 "hyper_parallel.components.modules.attention.gated_grouped_query_attention." 

97 "cp_gated_grouped_query_attention.build_cp_gated_grouped_query_attention" 

98 )}, 

99 default_variant="local", parallel_axis="cp", 

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

101 sharding=gqa_sharding, validate=validate_gqa_family, parallel_spec=gqa_local_parallel_spec, 

102))