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

16 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"""Linear Family retaining loaded parameters and selecting existing Dense adapters.""" 

16 

17from __future__ import annotations 

18 

19from collections.abc import Mapping 

20from typing import Any 

21 

22from torch import nn # pylint: disable=forbidden-backend-import 

23 

24from hyper_parallel.components.modules.family import ModuleFamily, SelectorSpec, VariantRule, register_module_family 

25from hyper_parallel.models.replacement import module_replacement 

26 

27 

28@module_replacement 

29class Linear(nn.Linear): 

30 """Baseline Dense replacement using ordinary torch linear computation.""" 

31 

32 def __init__(self, *, module: nn.Linear, module_fqn: str = "", 

33 context: Mapping[str, Any] | None = None) -> None: 

34 """Reuse parameters without allocating or initializing a second weight set. 

35 

36 Args: 

37 module: Source Linear with loaded weight and optional bias. 

38 module_fqn: Replacement path; unused by baseline computation. 

39 context: Replacement context; unused by baseline computation. 

40 """ 

41 del module_fqn, context 

42 # Like LowPrecisionLinear.from_linear, reuse parameters without nn.Linear allocating temporary weights. 

43 nn.Module.__init__(self) # pylint: disable=non-parent-init-called 

44 self.in_features, self.out_features = module.in_features, module.out_features 

45 self.register_parameter("weight", module.weight) 

46 self.register_parameter("bias", module.bias) 

47 self.train(module.training) 

48 

49 

50register_module_family(ModuleFamily( 

51 name="linear", 

52 baseline=Linear, 

53 variants={"mxfp8": "hyper_parallel.components.modules.low_precision_linear.replace_mxfp8_linear"}, 

54 selector_specs={"weight_format": SelectorSpec(str, ""), "act_format": SelectorSpec(str, ""), 

55 "is_fake_quantize": SelectorSpec(bool, False), "fused": SelectorSpec(bool, False)}, 

56 variant_rules=(VariantRule( 

57 {"weight_format": "mxfp8", "act_format": "mxfp8", "is_fake_quantize": False, "fused": False}, "mxfp8"),), 

58))