|
| 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 | + |
| 19 | +from typing import Any, Callable, Dict, Optional, Tuple, Union |
| 20 | + |
| 21 | +import torch |
| 22 | +import torch_npu |
| 23 | +from vllm.config import get_current_vllm_config |
| 24 | + |
| 25 | +from vllm_ascend.ascend_config import get_ascend_config |
| 26 | +from vllm_ascend.distributed.parallel_state import get_mc2_group |
| 27 | +from vllm_ascend.ops.fused_moe.experts_selector import select_experts |
| 28 | + |
| 29 | + |
| 30 | + |
| 31 | +class AscendW8A8MXFP8DynamicLinearMethod: |
| 32 | + """Linear method for Ascend W8A8_DYNAMIC. |
| 33 | + """ |
| 34 | + model_dtype = None |
| 35 | + |
| 36 | + |
| 37 | + def __init__(self): |
| 38 | + vllm_config = get_current_vllm_config() |
| 39 | + self.group_size = vllm_config.quant_config.quant_description.get( |
| 40 | + "group_size", 32) |
| 41 | + |
| 42 | + @staticmethod |
| 43 | + def get_weight(input_size: int, output_size: int, |
| 44 | + params_dtype: torch.dtype) -> Dict[str, Any]: |
| 45 | + params_dict = { |
| 46 | + "weight": torch.empty(output_size, input_size, dtype=torch.float8_e4m3fn) |
| 47 | + } |
| 48 | + return params_dict |
| 49 | + |
| 50 | + @staticmethod |
| 51 | + def get_pertensor_param(params_dtype: torch.dtype) -> Dict[str, Any]: |
| 52 | + return {} |
| 53 | + |
| 54 | + @staticmethod |
| 55 | + def get_perchannel_param( |
| 56 | + output_size: int, |
| 57 | + params_dtype: torch.dtype, |
| 58 | + ) -> Dict[str, Any]: |
| 59 | + return {} |
| 60 | + |
| 61 | + def get_pergroup_param(self, input_size: int, output_size: int, |
| 62 | + params_dtype: torch.dtype, layer_type: Optional[str] = None) -> Dict[str, Any]: |
| 63 | + params_dict = {} |
| 64 | + params_dict["weight_scale"] = torch.empty( |
| 65 | + output_size, input_size // self.group_size, dtype=torch.uint8) |
| 66 | + return params_dict |
| 67 | + |
| 68 | + def apply( |
| 69 | + self, |
| 70 | + layer: torch.nn.Module, |
| 71 | + x: Union[torch.Tensor, Tuple[torch.Tensor, torch.Tensor]], |
| 72 | + bias: Optional[torch.Tensor] = None, |
| 73 | + tp_rank: Optional[int] = 0, |
| 74 | + ) -> torch.Tensor: |
| 75 | + |
| 76 | + quantized_x, dynamic_scale = torch_npu.npu_dynamic_mx_quant(x, dst_type=torch.float8_e4m3fn) |
| 77 | + pertoken_scale = dynamic_scale |
| 78 | + output_dtype = x.dtype |
| 79 | + |
| 80 | + output = torch_npu.npu_quant_matmul( |
| 81 | + quantized_x, |
| 82 | + layer.weight, |
| 83 | + layer.weight_scale, |
| 84 | + scale_dtype=torch_npu.float8_e8m0fnu, |
| 85 | + pertoken_scale=pertoken_scale, |
| 86 | + pertoken_scale_dtype=torch_npu.float8_e8m0fnu, |
| 87 | + bias=bias, |
| 88 | + output_dtype=output_dtype, |
| 89 | + group_sizes=[1, 1, self.group_size] |
| 90 | + ) |
| 91 | + |
| 92 | + return output |
| 93 | + |
| 94 | + def process_weights_after_loading(self, layer): |
| 95 | + n_dim, k_dim = layer.weight_scale.data.shape |
| 96 | + layer.weight_scale.data = layer.weight_scale.data.reshape(n_dim, k_dim//2, 2) |
| 97 | + layer.weight.data = layer.weight.data.transpose(0, 1) |
| 98 | + layer.weight_scale.data = layer.weight_scale.data.transpose(0, 1) |
0 commit comments