Coverage for  / home / jenkins / .local / lib / python3.10 / site-packages / hyper_parallel / components / ops / auto_policy.py: 100%

44 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"""Default backend orders for the built-in operator catalog.""" 

16 

17from .impl.attention import ( 

18 GROUPED_QUERY_ATTENTION, MULTI_LATENT_ATTENTION, MULTI_LATENT_ATTENTION_WITH_STATS, VISION_ATTENTION, 

19) 

20from .impl.causal_conv1d import CAUSAL_CONV1D 

21from .impl.dsa_indexer import DSA_INDEXER, DSA_PACKED_INDEXER 

22from .impl.dsa_indexer_kl import DSA_DENSE_KL, DSA_INDEXER_KL, DSA_PACKED_KL 

23from .impl.deepseek_sparse_attention import DSA_ABSORBED, DSA_ABSORBED_SINK, DSA_COMPRESSED_BANKS 

24from .impl.gdn import GDN 

25from .impl.grouped_matmul import GROUPED_MATMUL 

26from .impl.grouped_swiglu import GROUPED_SWIGLU 

27from .impl.kda import KDA 

28from .impl.mhc_post import MHC_POST 

29from .impl.moe_router import MOE_ROUTER 

30from .impl.moe_token_reorder import MOE_TOKEN_REORDER, MOE_TOKEN_RESTORE 

31from .impl.mome import MOME 

32from .impl.rms_norm import RMS_NORM, RMS_NORM_FP32_AFFINE 

33from .impl.rope import ROPE, ROPE_INTERLEAVED 

34from .impl.sinkhorn import SINKHORN 

35from .impl.swiglu import SWIGLU, SWIGLU_CLAMPED 

36from .registry import OpRegistry 

37 

38 

39def apply_auto_policy(registry: OpRegistry) -> None: 

40 """Set device-specific priorities independently of implementation registration.""" 

41 for spec in (RMS_NORM, RMS_NORM_FP32_AFFINE, SWIGLU, SWIGLU_CLAMPED): 

42 registry.set_auto_order(spec, device="npu", candidates=("torch_npu",)) 

43 registry.set_auto_order(spec, device="cuda", candidates=("native",)) 

44 registry.set_auto_order(spec, device="cpu", candidates=("native",)) 

45 registry.set_auto_order(MHC_POST, device="npu", candidates=("cann", "omni", "native")) 

46 registry.set_auto_order(MHC_POST, device="cuda", candidates=("native",)) 

47 registry.set_auto_order(MHC_POST, device="cpu", candidates=("native",)) 

48 registry.set_auto_order(GROUPED_MATMUL, device="npu", candidates=("torch_npu",)) 

49 registry.set_auto_order(GROUPED_MATMUL, device="cuda", candidates=("torch_grouped_mm", "native")) 

50 registry.set_auto_order(GROUPED_MATMUL, device="cpu", candidates=("native",)) 

51 registry.set_auto_order(GROUPED_SWIGLU, device="npu", candidates=("torch_npu", "native")) 

52 registry.set_auto_order(GROUPED_SWIGLU, device="cuda", candidates=("torch_grouped_mm", "native")) 

53 registry.set_auto_order(GROUPED_SWIGLU, device="cpu", candidates=("native",)) 

54 for spec in (CAUSAL_CONV1D, GDN, KDA): 

55 registry.set_auto_order(spec, device="npu", candidates=("triton_ascend", "ascendc", "native")) 

56 registry.set_auto_order(spec, device="cuda", candidates=("native",)) 

57 registry.set_auto_order(spec, device="cpu", candidates=("native",)) 

58 for spec in (VISION_ATTENTION, GROUPED_QUERY_ATTENTION, MULTI_LATENT_ATTENTION, MULTI_LATENT_ATTENTION_WITH_STATS): 

59 registry.set_auto_order(spec, device="npu", candidates=("torch_npu", "native")) 

60 for device in ("cuda", "cpu"): 

61 registry.set_auto_order(spec, device=device, candidates=("sdpa", "native")) 

62 for spec, fused in ( 

63 (MOME, "omni"), 

64 (ROPE, "torch_npu"), (ROPE_INTERLEAVED, "torch_npu"), 

65 (SINKHORN, "omni"), (MOE_ROUTER, "megagate"), 

66 (MOE_TOKEN_REORDER, "torch_npu"), (MOE_TOKEN_RESTORE, "torch_npu"), 

67 (DSA_COMPRESSED_BANKS, "cann"), (DSA_INDEXER, "cann"), (DSA_INDEXER_KL, "cann"), 

68 (DSA_PACKED_INDEXER, "cann"), (DSA_ABSORBED, "cann"), (DSA_ABSORBED_SINK, "omni"), 

69 (DSA_PACKED_KL, "cann"), (DSA_DENSE_KL, "cann"), 

70 ): 

71 registry.set_auto_order(spec, device="npu", candidates=(fused, "native")) 

72 registry.set_auto_order(spec, device="cuda", candidates=("native",)) 

73 registry.set_auto_order(spec, device="cpu", candidates=("native",))