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)
23from hyper_parallel.components.quantization.functional.base_linear_func import (
24 LinearStrategy,
25)
26from hyper_parallel.components.quantization.functional.hifloat8_linear_func import (
27 HiFloat8LinearStrategy,
28)
29from hyper_parallel.components.quantization.functional.mxfp8_linear_func import (
30 MXFP8LinearStrategy,
31)
32from hyper_parallel.components.quantization.ops.npu_hifloat8 import (
33 validate_hifloat8_runtime,
34)
35from hyper_parallel.components.quantization.ops.npu_mxfp8 import validate_npu_runtime
36
37
38def build_linear_strategy(
39 config: Optional[LowPrecisionDtypeScheme | LowPrecisionConfig],
40 *,
41 in_features: int,
42 out_features: int,
43) -> LinearStrategy:
44 """Build one Dense strategy without consulting grouped-expert state.
45
46 Args:
47 config: Resolved dtype scheme, legacy global config, or ``None`` for
48 the legacy MXFP8 default.
49 in_features: Dense input feature count.
50 out_features: Dense output feature count.
51
52 Returns:
53 A new strategy instance owned by one replacement Linear.
54
55 Raises:
56 ValueError: If the MXFP8 weight shape is not tile aligned.
57 NotImplementedError: If the selected Dense policy is not implemented.
58 """
59
60 if isinstance(config, LowPrecisionConfig):
61 config = config.resolve_dtype_scheme()
62 if config is None:
63 config = LowPrecisionDtypeScheme()
64
65 if (
66 not config.is_fake_quantize
67 and config.weight_format == "mxfp8"
68 and config.act_format == "mxfp8"
69 ):
70 if in_features % 32 or out_features % 32:
71 raise ValueError(
72 "MXFP8 Linear is not tile aligned: "
73 f"shape=({out_features}, {in_features}) requires multiples of 32."
74 )
75 validate_npu_runtime()
76 return MXFP8LinearStrategy()
77
78 if (
79 not config.is_fake_quantize
80 and config.weight_format == "hif8"
81 and config.act_format == "hif8"
82 ):
83 validate_hifloat8_runtime()
84 return HiFloat8LinearStrategy()
85
86 raise NotImplementedError(
87 "Dense Linear policy is not implemented: "
88 f"fake={config.is_fake_quantize}, weight_format={config.weight_format!r}, "
89 f"act_format={config.act_format!r}."
90 )
91
92
93__all__ = ["build_linear_strategy"]