|
| 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 Any, Dict, Optional |
| 19 | + |
| 20 | +import torch |
| 21 | +import torch_npu |
| 22 | +from vllm.config import get_current_vllm_config |
| 23 | + |
| 24 | + |
| 25 | +class AscendW4A8DynamicLinearMethod: |
| 26 | + """Linear method for Ascend W4A8_DYNAMIC |
| 27 | + """ |
| 28 | + |
| 29 | + def __init__(self): |
| 30 | + self.transpose_weight = True |
| 31 | + try: |
| 32 | + self.group_size = get_current_vllm_config( |
| 33 | + ).quant_config.quant_description.get("group_size", 256) |
| 34 | + except AttributeError: |
| 35 | + self.group_size = 256 |
| 36 | + |
| 37 | + @staticmethod |
| 38 | + def get_weight(input_size: int, output_size: int, |
| 39 | + params_dtype: torch.dtype) -> Dict[str, Any]: |
| 40 | + params_dict = { |
| 41 | + "weight": torch.empty(output_size, input_size, dtype=torch.int8) |
| 42 | + } |
| 43 | + return params_dict |
| 44 | + |
| 45 | + @staticmethod |
| 46 | + def get_pertensor_param(params_dtype: torch.dtype) -> Dict[str, Any]: |
| 47 | + return {} |
| 48 | + |
| 49 | + @staticmethod |
| 50 | + def get_perchannel_param(output_size: int, |
| 51 | + params_dtype: torch.dtype) -> Dict[str, Any]: |
| 52 | + return {} |
| 53 | + |
| 54 | + def get_pergroup_param(self, input_size: int, output_size: int, |
| 55 | + params_dtype: torch.dtype) -> Dict[str, Any]: |
| 56 | + params_dict = {} |
| 57 | + params_dict["weight_scale"] = torch.empty(output_size, |
| 58 | + 1, |
| 59 | + dtype=params_dtype) |
| 60 | + params_dict["weight_offset"] = torch.empty(output_size, |
| 61 | + 1, |
| 62 | + dtype=params_dtype) |
| 63 | + params_dict["weight_scale_second"] = torch.empty(output_size, |
| 64 | + input_size // |
| 65 | + self.group_size, |
| 66 | + dtype=params_dtype) |
| 67 | + params_dict["weight_offset_second"] = torch.empty(output_size, |
| 68 | + input_size // |
| 69 | + self.group_size, |
| 70 | + dtype=params_dtype) |
| 71 | + return params_dict |
| 72 | + |
| 73 | + @staticmethod |
| 74 | + def process_scale_second(weight: torch.Tensor, scale: torch.Tensor, |
| 75 | + per_group_scale: torch.Tensor): |
| 76 | + k, n = weight.shape |
| 77 | + group_num, n = per_group_scale.shape |
| 78 | + weight_high = weight.to(torch.float32).reshape( |
| 79 | + group_num, -1, n) * per_group_scale.reshape(group_num, 1, n) |
| 80 | + weight_high = weight_high.reshape(k, n) |
| 81 | + bias = 8 * (weight_high.to(torch.float32) * scale).sum(dim=0) |
| 82 | + antiquant_scale = (scale * per_group_scale).reshape(group_num, n) |
| 83 | + return antiquant_scale.npu(), bias |
| 84 | + |
| 85 | + def apply( |
| 86 | + self, |
| 87 | + layer: torch.nn.Module, |
| 88 | + x: torch.Tensor, |
| 89 | + bias: Optional[torch.Tensor] = None, |
| 90 | + tp_rank: Optional[int] = None, |
| 91 | + ) -> torch.Tensor: |
| 92 | + return torch_npu.npu_weight_quant_batchmatmul( |
| 93 | + x, |
| 94 | + layer.weight, |
| 95 | + antiquant_scale=layer.weight_scale_second.to(x.dtype), |
| 96 | + antiquant_group_size=self.group_size, |
| 97 | + ) |
| 98 | + |
| 99 | + def process_weights_after_loading(self, layer: torch.nn.Module): |
| 100 | + if self.transpose_weight: |
| 101 | + layer.weight.data = layer.weight.data.transpose(0, 1).contiguous() |
| 102 | + layer.weight_scale.data = layer.weight_scale.data.flatten().to( |
| 103 | + torch.float32) |
| 104 | + layer.weight_offset.data = layer.weight_offset.data.flatten() |
| 105 | + layer.weight_scale_second.data, scale_bias = self.process_scale_second( |
| 106 | + layer.weight.data, |
| 107 | + layer.weight_scale.data, |
| 108 | + layer.weight_scale_second.data.transpose(0, 1).contiguous(), |
| 109 | + ) |
| 110 | + param = torch.nn.Parameter(scale_bias, requires_grad=False) |
| 111 | + layer.register_parameter("weight_scale_bias", param) |
| 112 | + layer.weight.data = torch_npu.npu_convert_weight_to_int4pack( |
| 113 | + layer.weight.data.to(torch.int32)) |
0 commit comments