|
| 1 | +# Copyright (c) 2025 Intel Corporation |
| 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 | +# SPDX-License-Identifier: Apache-2.0 |
| 16 | +from typing import Callable, Optional |
| 17 | + |
| 18 | +import torch |
| 19 | +import vllm.envs as envs |
| 20 | +from torch.nn.parameter import Parameter |
| 21 | +from vllm.logger import init_logger |
| 22 | +from vllm.model_executor.parameter import GroupQuantScaleParameter, ModelWeightParameter, PerTensorScaleParameter |
| 23 | +from vllm.platforms import current_platform |
| 24 | + |
| 25 | +from auto_round_extension.vllm_ext.mxfp4_qdq_utils import ( |
| 26 | + dequant_mxfp4_to_fp8, |
| 27 | + mxfp4_gemm_with_unpacked_weight, |
| 28 | + run_mxfp4_emulations, |
| 29 | +) |
| 30 | + |
| 31 | +logger = init_logger(__name__) |
| 32 | + |
| 33 | +__all__ = ["AutoRoundMXFP4LinearImpl"] |
| 34 | + |
| 35 | +from auto_round_extension.vllm_ext.quant_impl import AutoRoundQuantImpl |
| 36 | + |
| 37 | + |
| 38 | +class AutoRoundMXFP4LinearImpl(AutoRoundQuantImpl): |
| 39 | + def __init__(self, quant_scheme): |
| 40 | + self.quant_scheme = quant_scheme |
| 41 | + self.group_size = 32 |
| 42 | + |
| 43 | + @classmethod |
| 44 | + def get_min_capability(cls) -> int: |
| 45 | + if envs.VLLM_USE_MXFP4_CT_EMULATIONS: |
| 46 | + return 80 |
| 47 | + return 100 |
| 48 | + |
| 49 | + def create_weights( |
| 50 | + self, |
| 51 | + layer: torch.nn.Module, |
| 52 | + output_partition_sizes: list[int], |
| 53 | + input_size_per_partition: int, |
| 54 | + params_dtype: torch.dtype, |
| 55 | + weight_loader: Callable, |
| 56 | + **kwargs, |
| 57 | + ): |
| 58 | + output_size_per_partition = sum(output_partition_sizes) |
| 59 | + layer.logical_widths = output_partition_sizes |
| 60 | + layer.input_size_per_partition = input_size_per_partition |
| 61 | + layer.output_size_per_partition = output_size_per_partition |
| 62 | + |
| 63 | + # Weight |
| 64 | + weight = ModelWeightParameter( |
| 65 | + data=torch.empty(sum(output_partition_sizes), input_size_per_partition // 2, dtype=torch.uint8), |
| 66 | + input_dim=1, |
| 67 | + output_dim=0, |
| 68 | + weight_loader=weight_loader, |
| 69 | + ) |
| 70 | + layer.register_parameter("weight_packed", weight) |
| 71 | + |
| 72 | + # Per Group Weight Scale |
| 73 | + weight_scale = GroupQuantScaleParameter( |
| 74 | + data=torch.empty( |
| 75 | + sum(output_partition_sizes), |
| 76 | + input_size_per_partition // self.group_size, |
| 77 | + # dtype=torch.uint8, |
| 78 | + dtype=torch.uint8, |
| 79 | + ), |
| 80 | + input_dim=1, |
| 81 | + output_dim=0, |
| 82 | + weight_loader=weight_loader, |
| 83 | + ) |
| 84 | + |
| 85 | + layer.register_parameter("weight_scale", weight_scale) |
| 86 | + |
| 87 | + def process_weights_after_loading(self, layer) -> None: |
| 88 | + # FIXME: may dequant to bf16 |
| 89 | + if envs.VLLM_MXFP4_PRE_UNPACK_WEIGHTS: |
| 90 | + |
| 91 | + weight_fp8, scale_bf16 = dequant_mxfp4_to_fp8( |
| 92 | + data_lp=layer.weight_packed, |
| 93 | + scale_e8m0=layer.weight_scale, |
| 94 | + ) |
| 95 | + del layer.weight_packed |
| 96 | + del layer.weight_scale |
| 97 | + layer.weight_packed = None |
| 98 | + layer.weight_scale = None |
| 99 | + layer.register_parameter( |
| 100 | + "weight_unpacked_fp8", |
| 101 | + torch.nn.Parameter( |
| 102 | + weight_fp8, |
| 103 | + requires_grad=False, |
| 104 | + ), |
| 105 | + ) |
| 106 | + layer.register_parameter( |
| 107 | + "weight_scale_bf16", |
| 108 | + torch.nn.Parameter( |
| 109 | + scale_bf16, |
| 110 | + requires_grad=False, |
| 111 | + ), |
| 112 | + ) |
| 113 | + |
| 114 | + def apply_weights( |
| 115 | + self, layer: torch.nn.Module, x: torch.Tensor, bias: Optional[torch.Tensor] = None |
| 116 | + ) -> torch.Tensor: |
| 117 | + if not envs.VLLM_MXFP4_PRE_UNPACK_WEIGHTS: |
| 118 | + out = run_mxfp4_emulations(x=x, weight=layer.weight_packed, weight_scale=layer.weight_scale) |
| 119 | + if bias is not None: |
| 120 | + out = out + bias |
| 121 | + return out |
| 122 | + else: |
| 123 | + out = mxfp4_gemm_with_unpacked_weight( |
| 124 | + x=x, |
| 125 | + weight_fp8=layer.weight_unpacked_fp8, |
| 126 | + weight_scale_bf16=layer.weight_scale_bf16, |
| 127 | + bias=bias, |
| 128 | + ) |
| 129 | + return out |
0 commit comments