Coverage for  / home / jenkins / .local / lib / python3.10 / site-packages / hyper_parallel / components / quantization / functional / linear_strategy_factory.py: 91%

22 statements  

« prev     ^ index     » next       coverage.py v7.13.1, created at 2026-11-15 07:18 +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"""Build an independent Dense Linear strategy from resolved policy data.""" 

16 

17from typing import Optional 

18 

19from hyper_parallel.components.quantization.config import ( 

20 LowPrecisionConfig, 

21 LowPrecisionDtypeScheme, 

22 MX_BLOCK_SIZE_32, 

23) 

24from hyper_parallel.components.quantization.functional.base_linear_func import ( 

25 LinearStrategy, 

26) 

27from hyper_parallel.components.quantization.functional.hifloat8_linear_func import ( 

28 HiFloat8LinearStrategy, 

29) 

30from hyper_parallel.components.quantization.functional.mxfp8_linear_func import ( 

31 MXFP8LinearStrategy, 

32) 

33from hyper_parallel.components.quantization.ops.npu_hifloat8 import ( 

34 validate_hifloat8_runtime, 

35) 

36from hyper_parallel.components.quantization.ops.npu_mxfp8 import validate_npu_runtime 

37 

38 

39def build_linear_strategy( 

40 config: Optional[LowPrecisionDtypeScheme | LowPrecisionConfig], 

41 *, 

42 in_features: int, 

43 out_features: int, 

44) -> LinearStrategy: 

45 """Build one Dense strategy without consulting grouped-expert state. 

46 

47 Args: 

48 config: Resolved dtype scheme, legacy global config, or ``None`` for 

49 the legacy MXFP8 default. 

50 in_features: Dense input feature count. 

51 out_features: Dense output feature count. 

52 

53 Returns: 

54 A new strategy instance owned by one replacement Linear. 

55 

56 Raises: 

57 ValueError: If the MXFP8 weight shape is not tile aligned. 

58 NotImplementedError: If the selected Dense policy is not implemented. 

59 """ 

60 

61 if isinstance(config, LowPrecisionConfig): 

62 config = config.resolve_dtype_scheme() 

63 if config is None: 

64 config = LowPrecisionDtypeScheme() 

65 

66 if ( 

67 not config.is_fake_quantize 

68 and config.weight_format == "mxfp8" 

69 and config.act_format == "mxfp8" 

70 ): 

71 if in_features % MX_BLOCK_SIZE_32 or out_features % MX_BLOCK_SIZE_32: 

72 raise ValueError( 

73 "MXFP8 Linear is not tile aligned: " 

74 f"shape=({out_features}, {in_features}) requires multiples of " 

75 f"{MX_BLOCK_SIZE_32}." 

76 ) 

77 validate_npu_runtime() 

78 return MXFP8LinearStrategy() 

79 

80 if ( 

81 not config.is_fake_quantize 

82 and config.weight_format == "hif8" 

83 and config.act_format == "hif8" 

84 ): 

85 validate_hifloat8_runtime() 

86 return HiFloat8LinearStrategy() 

87 

88 raise NotImplementedError( 

89 "Dense Linear policy is not implemented: " 

90 f"fake={config.is_fake_quantize}, weight_format={config.weight_format!r}, " 

91 f"act_format={config.act_format!r}." 

92 ) 

93 

94 

95__all__ = ["build_linear_strategy"]