Coverage for  / home / jenkins / .local / lib / python3.10 / site-packages / hyper_parallel / components / modules / moe / shared_expert_overlap.py: 90%

10 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"""Bind shared-expert overlap without rewriting model forwards or routing interfaces.""" 

16 

17from __future__ import annotations 

18 

19from typing import TYPE_CHECKING 

20 

21from torch import nn 

22 

23if TYPE_CHECKING: 

24 from hyper_parallel.distributed.expert_parallel.ep_context import EPContext 

25 

26 

27def bind_shared_expert_overlap(model: nn.Module, expert_fqn: str, context: EPContext) -> None: 

28 """Bind the containing MoE's shared family member before activation/FSDP wrapping. 

29 

30 Args: 

31 model: Final module tree after replacement and layout. 

32 expert_fqn: Routed expert path in the parallel plan. 

33 context: Ordinary or MoonEP context controlling shared-stage scheduling. 

34 

35 Native shared modules without the split-stage/cache protocol remain serial. 

36 The owner must feed the same activation to routed and shared branches. 

37 """ 

38 owner = model.get_submodule(expert_fqn.rpartition(".")[0]) 

39 shared = [child for child in owner.children() 

40 if getattr(child, "split_forward_supported", False) and callable(getattr(child, "bind_ep_context", None))] 

41 if len(shared) > 1: 

42 raise ValueError(f"MoE owner of {expert_fqn!r} has multiple shared-expert candidates") 

43 if shared: 

44 context.bind_shared_expert(owner, shared[0], model.get_submodule(expert_fqn))