|
| 1 | +# |
| 2 | +# Copyright (c) 2025 Huawei Technologies Co., Ltd. All Rights Reserved. |
| 3 | +# This file is a part of the vllm-ascend project. |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | +# |
| 17 | + |
| 18 | +from typing import Optional, Tuple, Union |
| 19 | + |
| 20 | +import torch |
| 21 | +import torch_npu |
| 22 | +from vllm.logger import logger |
| 23 | +from vllm.model_executor.layers.layernorm import RMSNorm |
| 24 | +from vllm.model_executor.layers.linear import UnquantizedLinearMethod |
| 25 | + |
| 26 | + |
| 27 | +# func refers to RMSNorm.__init__ |
| 28 | +def wrapper_rmsnorm_init(func): |
| 29 | + |
| 30 | + def init(self, hidden_size: int, **extra_args) -> None: |
| 31 | + func(self, hidden_size, **extra_args) |
| 32 | + self.ignore_anti = True |
| 33 | + self.bias = torch.nn.Parameter(torch.zeros(hidden_size), |
| 34 | + requires_grad=False) |
| 35 | + |
| 36 | + return init |
| 37 | + |
| 38 | + |
| 39 | +# func refers to RMSNorm.forward_oot |
| 40 | +def wrapper_rmsnorm_forward_oot(func): |
| 41 | + |
| 42 | + def _rmsnorm_forward_oot( |
| 43 | + self, |
| 44 | + x: torch.Tensor, |
| 45 | + residual: Optional[torch.Tensor] = None, |
| 46 | + ) -> Union[torch.Tensor, Tuple[torch.Tensor, torch.Tensor]]: |
| 47 | + if not self.ignore_anti: |
| 48 | + if residual is not None: |
| 49 | + residual += x |
| 50 | + out = torch_npu._npu_quant_rms_norm( |
| 51 | + residual, |
| 52 | + self.weight, |
| 53 | + self.bias, |
| 54 | + self.input_scale, |
| 55 | + self.input_offset, |
| 56 | + self.variance_epsilon, |
| 57 | + ) |
| 58 | + return out, residual |
| 59 | + out = torch_npu._npu_quant_rms_norm( |
| 60 | + x, |
| 61 | + self.weight, |
| 62 | + self.bias, |
| 63 | + self.input_scale, |
| 64 | + self.input_offset, |
| 65 | + self.variance_epsilon, |
| 66 | + ) |
| 67 | + return out |
| 68 | + |
| 69 | + if residual is not None: |
| 70 | + x, residual = func(self, x, residual) |
| 71 | + return x.add_(self.bias), residual |
| 72 | + |
| 73 | + return func(self, x).add_(self.bias) |
| 74 | + |
| 75 | + return _rmsnorm_forward_oot |
| 76 | + |
| 77 | + |
| 78 | +MODEL_LAYER_MAPPING = { |
| 79 | + "LlamaModel": { |
| 80 | + "attn": { |
| 81 | + "layer_attr": "self_attn", |
| 82 | + "proj_attr": "qkv_proj", |
| 83 | + "norm_attr": "input_layernorm", |
| 84 | + "unquantized_type": UnquantizedLinearMethod, |
| 85 | + }, |
| 86 | + "mlp": { |
| 87 | + "layer_attr": "mlp", |
| 88 | + "proj_attr": "gate_up_proj", |
| 89 | + "norm_attr": "post_attention_layernorm", |
| 90 | + "unquantized_type": UnquantizedLinearMethod, |
| 91 | + }, |
| 92 | + }, |
| 93 | +} |
| 94 | + |
| 95 | + |
| 96 | +def wrapper_load_model(func): |
| 97 | + |
| 98 | + def postprocess_loading(self) -> None: |
| 99 | + func(self) |
| 100 | + |
| 101 | + def process_layer(layer, idx, mapping): |
| 102 | + |
| 103 | + def process_module(module_cfg, layer_obj): |
| 104 | + if module_cfg is None: |
| 105 | + return |
| 106 | + |
| 107 | + module_obj = getattr(layer_obj, module_cfg["layer_attr"], None) |
| 108 | + if module_obj is None: |
| 109 | + return |
| 110 | + |
| 111 | + proj_attr = module_cfg["proj_attr"] |
| 112 | + if callable(proj_attr): |
| 113 | + proj = proj_attr(module_obj, idx) |
| 114 | + else: |
| 115 | + proj = getattr(module_obj, proj_attr, None) |
| 116 | + |
| 117 | + norm = getattr(layer_obj, module_cfg["norm_attr"], None) |
| 118 | + |
| 119 | + if proj is None or norm is None: |
| 120 | + return |
| 121 | + |
| 122 | + norm.ignore_anti = isinstance(proj.quant_method, |
| 123 | + module_cfg["unquantized_type"]) |
| 124 | + if not norm.ignore_anti: |
| 125 | + for param_name in ["input_scale", "input_offset"]: |
| 126 | + if hasattr(proj, param_name): |
| 127 | + param = getattr(proj, param_name) |
| 128 | + norm.register_parameter( |
| 129 | + param_name, |
| 130 | + torch.nn.Parameter(param.clone(), |
| 131 | + requires_grad=False)) |
| 132 | + |
| 133 | + process_module(mapping.get("attn"), layer) |
| 134 | + process_module(mapping.get("mlp"), layer) |
| 135 | + |
| 136 | + model_type = self.model.model.__class__.__name__ |
| 137 | + mapping = MODEL_LAYER_MAPPING.get(model_type) |
| 138 | + |
| 139 | + if not mapping: |
| 140 | + logger.info( |
| 141 | + f"Warning: Model type '{model_type}' not found in MODEL_LAYER_MAPPING. Skipping layer mapping." |
| 142 | + ) |
| 143 | + return |
| 144 | + |
| 145 | + for idx, layer in enumerate(self.model.model.layers): |
| 146 | + process_layer(layer, idx, mapping) |
| 147 | + |
| 148 | + if isinstance(self.model.model.norm, RMSNorm): |
| 149 | + self.model.model.norm.ignore_anti = True |
| 150 | + |
| 151 | + return postprocess_loading |
0 commit comments