gptq.py 10.6 KB
Newer Older
1
# SPDX-License-Identifier: Apache-2.0
2
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
3

CHU Tianxiang's avatar
CHU Tianxiang committed
4
5
import enum
from enum import Enum
6
from fractions import Fraction
7
from typing import Any, Optional, Union
CHU Tianxiang's avatar
CHU Tianxiang committed
8
9
10
11

import torch
from torch.nn.parameter import Parameter

12
from vllm import _custom_ops as ops
13
from vllm.model_executor.layers.linear import LinearMethodBase
14
from vllm.model_executor.layers.quantization import QuantizationMethods
CHU Tianxiang's avatar
CHU Tianxiang committed
15
16
from vllm.model_executor.layers.quantization.base_config import (
    QuantizationConfig)
17
18
from vllm.model_executor.layers.quantization.utils.gptq_utils import (
    get_linear_quant_method)
19
20
21
22
23
from vllm.model_executor.parameter import (ChannelQuantScaleParameter,
                                           GroupQuantScaleParameter,
                                           PackedColumnParameter,
                                           PackedvLLMParameter,
                                           RowvLLMParameter)
CHU Tianxiang's avatar
CHU Tianxiang committed
24
25
26
27
28
29
30
31
32
33
34
35
36


class GPTQConfig(QuantizationConfig):
    """Config class for GPTQ.

    Reference: https://arxiv.org/abs/2210.17323
    """

    def __init__(
        self,
        weight_bits: int,
        group_size: int,
        desc_act: bool,
37
        lm_head_quantized: bool,
38
        dynamic: dict[str, dict[str, Union[int, bool]]],
CHU Tianxiang's avatar
CHU Tianxiang committed
39
    ) -> None:
40
41
        # GPTQModel use `dynamic` config property to allow per module
        # quantization config so each module can be individually optimized.
42
        # Format is dict[str, dict] where key is a regex string that can
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
        # perform both positive ("+:" prefixed) or negative ("-:" prefixed)
        # matching of a module.
        # Default to positive match, override base quant config mode, if no
        # prefix is used. Value is in dict format of field key and override
        # value.
        # Negative matching will skip quantization init for this module
        # entirely:
        # non-quantized inference. More details and quantization examples can be
        # found at: https://github.com/ModelCloud/GPTQModel
        # Example:
        #  # last 1/2 of the layers 10-21 has 8bit vs 4bit for 0-9
        #  # last 1/4 of the layers 16-21 has 8bit and group_size 64
        # dynamic = {
        #  #`.*\.` matches the layers_node prefix
        #  # positive match layer 10-15
        #  r"+:.*\.(?:1[0-5])\..*": {"bits": 8,},
        #  # positive match layer 16-21
        #  r"+:.*\.(?:1[6-9]|20|21)\..*": {"bits": 8, "group_size": 64,},
        #  r"-:.*\.moe\..*": {}, # negative match (skip) all `moe` layers
        # }
63
        super().__init__()
64
65
        self.dynamic = dynamic

CHU Tianxiang's avatar
CHU Tianxiang committed
66
67
68
        self.weight_bits = weight_bits
        self.group_size = group_size
        self.desc_act = desc_act
69
        self.lm_head_quantized = lm_head_quantized
70
71
        self.pack_factor = Fraction(32, self.weight_bits)
        if self.weight_bits not in [2, 3, 4, 8]:
CHU Tianxiang's avatar
CHU Tianxiang committed
72
            raise ValueError(
73
74
                "Currently, only 2/3/4/8-bit weight quantization is "
                f"supported for GPTQ, but got {self.weight_bits} bits.")
CHU Tianxiang's avatar
CHU Tianxiang committed
75
76
77
78

    def __repr__(self) -> str:
        return (f"GPTQConfig(weight_bits={self.weight_bits}, "
                f"group_size={self.group_size}, "
79
                f"desc_act={self.desc_act}), "
80
81
                f"lm_head_quantized={self.lm_head_quantized}), "
                f"dynamic={self.dynamic}")
CHU Tianxiang's avatar
CHU Tianxiang committed
82
83

    @classmethod
84
    def get_name(cls) -> QuantizationMethods:
CHU Tianxiang's avatar
CHU Tianxiang committed
85
86
87
        return "gptq"

    @classmethod
88
    def get_supported_act_dtypes(cls) -> list[torch.dtype]:
CHU Tianxiang's avatar
CHU Tianxiang committed
89
90
91
92
93
94
95
96
        return [torch.half]

    @classmethod
    # Need to figure it out
    def get_min_capability(cls) -> int:
        return 60

    @classmethod
97
    def get_config_filenames(cls) -> list[str]:
CHU Tianxiang's avatar
CHU Tianxiang committed
98
99
100
        return ["quantize_config.json"]

    @classmethod
101
    def from_config(cls, config: dict[str, Any]) -> "GPTQConfig":
102
103
104
        dynamic = cls.get_from_keys_or(config, ["dynamic"], default={})
        dynamic = {} if dynamic is None else dynamic

CHU Tianxiang's avatar
CHU Tianxiang committed
105
106
107
        weight_bits = cls.get_from_keys(config, ["bits"])
        group_size = cls.get_from_keys(config, ["group_size"])
        desc_act = cls.get_from_keys(config, ["desc_act"])
108
109
        lm_head_quantized = cls.get_from_keys_or(config, ["lm_head"],
                                                 default=False)
110
111
        return cls(weight_bits, group_size, desc_act, lm_head_quantized,
                   dynamic)
CHU Tianxiang's avatar
CHU Tianxiang committed
112

113
114
    def get_quant_method(self, layer: torch.nn.Module,
                         prefix: str) -> Optional["GPTQLinearMethod"]:
115
        return get_linear_quant_method(self, layer, prefix, GPTQLinearMethod)
CHU Tianxiang's avatar
CHU Tianxiang committed
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136


class ExllamaState(Enum):

    UNUSED = enum.auto()
    UNINITIALIZED = enum.auto()
    READY = enum.auto()


class GPTQLinearMethod(LinearMethodBase):
    """Linear method for GPTQ.

    Args:
        quant_config: The GPTQ quantization config.
    """

    def __init__(self, quant_config: GPTQConfig):
        self.quant_config = quant_config

    def create_weights(
        self,
137
        layer: torch.nn.Module,
CHU Tianxiang's avatar
CHU Tianxiang committed
138
        input_size_per_partition: int,
139
        output_partition_sizes: list[int],
CHU Tianxiang's avatar
CHU Tianxiang committed
140
141
142
        input_size: int,
        output_size: int,
        params_dtype: torch.dtype,
143
144
        **extra_weight_attrs,
    ):
CHU Tianxiang's avatar
CHU Tianxiang committed
145
        del output_size  # Unused.
146
        weight_loader = extra_weight_attrs.get("weight_loader")
CHU Tianxiang's avatar
CHU Tianxiang committed
147
148
149
150
151
        if input_size_per_partition % self.quant_config.group_size != 0:
            raise ValueError(
                "The input size is not aligned with the quantized "
                "weight shape. This can be caused by too large "
                "tensor parallel size.")
James Fleming's avatar
James Fleming committed
152
        output_size_per_partition = sum(output_partition_sizes)
153
154
        if (output_size_per_partition % self.quant_config.pack_factor.numerator
                != 0):
CHU Tianxiang's avatar
CHU Tianxiang committed
155
156
157
158
159
160
161
162
163
164
165
166
            raise ValueError(
                "The output size is not aligned with the quantized "
                "weight shape. This can be caused by too large "
                "tensor parallel size.")

        if self.quant_config.group_size != -1:
            group_size = self.quant_config.group_size
        else:
            group_size = input_size
        exllama_state = ExllamaState.UNINITIALIZED
        scale_and_zero_size = input_size // group_size
        scale_and_zero_input_dim = None
167
168
        if (input_size != input_size_per_partition
                and self.quant_config.group_size != -1):
CHU Tianxiang's avatar
CHU Tianxiang committed
169
170
171
172
173
174
175
176
            # For act-order models, we cannot use Exllama for row parallel layer
            if self.quant_config.desc_act:
                exllama_state = ExllamaState.UNUSED
            else:
                # we need to partition qzeros and scales for exllama kernel
                scale_and_zero_size = input_size_per_partition // group_size
                scale_and_zero_input_dim = 0

177
178
        qweight = PackedvLLMParameter(
            data=torch.empty(
CHU Tianxiang's avatar
CHU Tianxiang committed
179
180
181
182
                input_size_per_partition // self.quant_config.pack_factor,
                output_size_per_partition,
                dtype=torch.int32,
            ),
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
            input_dim=0,
            output_dim=1,
            packed_dim=0,
            packed_factor=self.quant_config.pack_factor,
            weight_loader=weight_loader)

        g_idx = RowvLLMParameter(data=torch.tensor(
            [
                i // self.quant_config.group_size
                for i in range(input_size_per_partition)
            ],
            dtype=torch.int32,
        ),
                                 input_dim=0,
                                 weight_loader=weight_loader)
        qzeros_args = {
            "data":
CHU Tianxiang's avatar
CHU Tianxiang committed
200
201
202
203
204
            torch.empty(
                scale_and_zero_size,
                output_size_per_partition // self.quant_config.pack_factor,
                dtype=torch.int32,
            ),
205
206
207
208
209
            "weight_loader":
            weight_loader
        }
        weight_scale_args = {
            "data":
CHU Tianxiang's avatar
CHU Tianxiang committed
210
211
212
213
214
            torch.empty(
                scale_and_zero_size,
                output_size_per_partition,
                dtype=params_dtype,
            ),
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
            "weight_loader":
            weight_loader
        }
        if scale_and_zero_input_dim is None:
            scales = ChannelQuantScaleParameter(output_dim=1,
                                                **weight_scale_args)
            qzeros = PackedColumnParameter(
                output_dim=1,
                packed_dim=1,
                packed_factor=self.quant_config.pack_factor,
                **qzeros_args)

        else:
            scales = GroupQuantScaleParameter(output_dim=1,
                                              input_dim=0,
                                              **weight_scale_args)
            qzeros = PackedvLLMParameter(
                input_dim=0,
                output_dim=1,
                packed_dim=1,
                packed_factor=self.quant_config.pack_factor,
                **qzeros_args)
237
238
239
240
241
242
243

        layer.register_parameter("qweight", qweight)
        layer.register_parameter("g_idx", g_idx)
        layer.register_parameter("qzeros", qzeros)
        layer.register_parameter("scales", scales)

        layer.exllama_state = exllama_state
CHU Tianxiang's avatar
CHU Tianxiang committed
244

245
    def process_weights_after_loading(self, layer: torch.nn.Module) -> None:
246
247
248
249
        # for torch.compile
        layer.qzeros = Parameter(layer.qzeros.data, requires_grad=False)
        layer.qweight = Parameter(layer.qweight.data, requires_grad=False)
        layer.g_idx = Parameter(layer.g_idx.data, requires_grad=False)
250
        layer.scales = Parameter(layer.scales.data, requires_grad=False)
251

CHU Tianxiang's avatar
CHU Tianxiang committed
252
253
        # exllama needs to shuffle the weight after the weight is loaded
        # here we do the shuffle on first forward pass
254
        if layer.exllama_state == ExllamaState.UNINITIALIZED:
CHU Tianxiang's avatar
CHU Tianxiang committed
255
            if self.quant_config.desc_act:
256
                layer.g_idx.data = torch.argsort(layer.g_idx).to(torch.int)
CHU Tianxiang's avatar
CHU Tianxiang committed
257
            else:
258
                layer.g_idx.data = torch.empty((0, ),
259
                                               dtype=torch.int,
260
261
262
                                               device=layer.g_idx.device)
            layer.exllama_state = ExllamaState.READY
            ops.gptq_shuffle(layer.qweight, layer.g_idx,
263
                             self.quant_config.weight_bits)
264
265
266
267
268
269
270
271

    def apply(self,
              layer: torch.nn.Module,
              x: torch.Tensor,
              bias: Optional[torch.Tensor] = None) -> torch.Tensor:
        out_shape = x.shape[:-1] + (layer.qweight.shape[-1], )
        reshaped_x = x.reshape(-1, x.shape[-1])

272
273
274
        output = ops.gptq_gemm(reshaped_x, layer.qweight, layer.qzeros,
                               layer.scales, layer.g_idx,
                               layer.exllama_state == ExllamaState.READY,
275
                               self.quant_config.weight_bits)
CHU Tianxiang's avatar
CHU Tianxiang committed
276
        if bias is not None:
277
            output.add_(bias)
CHU Tianxiang's avatar
CHU Tianxiang committed
278
        return output.reshape(out_shape)