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

13 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 reuses CP transport and applies its mandatory gate to restored tokens.""" 

16 

17from __future__ import annotations 

18 

19from typing import Any 

20 

21from torch import nn 

22 

23from ..grouped_query_attention.cp_grouped_query_attention import ( 

24 CPGroupedQueryAttentionModule, RingCPGroupedQueryAttentionModule, 

25) 

26from ..grouped_query_attention.grouped_query_attention_family import _gqa_cp_options 

27from .gated_grouped_query_attention import GatedGroupedQueryAttentionModule 

28 

29 

30class CPGatedGroupedQueryAttentionModule(CPGroupedQueryAttentionModule, GatedGroupedQueryAttentionModule): 

31 """Combine CP operand stages with the gated member's projection and output math. 

32 

33 The Q/gate projection is already fused: there is no independent gate GEMM 

34 to schedule as in Kimi MLA. The gate stays token-local and is applied after 

35 output restoration; CP communication scheduling is shared with ordinary GQA. 

36 """ 

37 

38 

39class RingCPGatedGroupedQueryAttentionModule( 

40 RingCPGroupedQueryAttentionModule, GatedGroupedQueryAttentionModule, 

41): 

42 """Gated GQA Ring member reusing Ring scheduling and gated projection math.""" 

43 

44 

45def build_cp_gated_grouped_query_attention(*, module: nn.Module | None = None, 

46 config: Any = None, module_fqn: str = "", 

47 context: Any = None) -> nn.Module: 

48 """Construct the ordinary or Ring gated GQA CP member from CP factors.""" 

49 attention_config = config if config is not None else getattr(module, "config", None) 

50 options = _gqa_cp_options(attention_config) 

51 member = (RingCPGatedGroupedQueryAttentionModule 

52 if options["ring_cp_size"] > 1 else CPGatedGroupedQueryAttentionModule) 

53 return member(config=config, module=module, module_fqn=module_fqn, context=context)