1
0

gptq.py 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. import enum
  2. from enum import Enum
  3. from typing import Any, Dict, List, Optional
  4. from fractions import Fraction
  5. import torch
  6. from torch.nn.parameter import Parameter
  7. from aphrodite._C import ops
  8. from aphrodite.modeling.layers.linear import (LinearMethodBase,
  9. set_weight_attrs)
  10. from aphrodite.modeling.layers.quantization.base_config import (
  11. QuantizationConfig)
  12. class GPTQConfig(QuantizationConfig):
  13. """Config class for GPTQ.
  14. Reference: https://arxiv.org/abs/2210.17323
  15. """
  16. def __init__(
  17. self,
  18. weight_bits: int,
  19. group_size: int,
  20. desc_act: bool,
  21. ) -> None:
  22. self.weight_bits = weight_bits
  23. self.group_size = group_size
  24. self.desc_act = desc_act
  25. self.pack_factor = Fraction(32, self.weight_bits)
  26. if self.weight_bits not in [2, 3, 4, 8]:
  27. raise ValueError(
  28. "Currently, only 2/3/4/8-bit weight quantization is supported "
  29. f"for GPTQ, but got {self.weight_bits} bits.")
  30. def __repr__(self) -> str:
  31. return (f"GPTQConfig(weight_bits={self.weight_bits}, "
  32. f"group_size={self.group_size}, "
  33. f"desc_act={self.desc_act})")
  34. @classmethod
  35. def get_name(cls) -> str:
  36. return "gptq"
  37. @classmethod
  38. def get_supported_act_dtypes(cls) -> List[torch.dtype]:
  39. return [torch.half]
  40. @classmethod
  41. # Need to figure it out
  42. def get_min_capability(cls) -> int:
  43. return 60
  44. @classmethod
  45. def get_config_filenames(cls) -> List[str]:
  46. return ["quantize_config.json"]
  47. @classmethod
  48. def from_config(cls, config: Dict[str, Any]) -> "GPTQConfig":
  49. weight_bits = cls.get_from_keys(config, ["bits"])
  50. group_size = cls.get_from_keys(config, ["group_size"])
  51. desc_act = cls.get_from_keys(config, ["desc_act"])
  52. return cls(weight_bits, group_size, desc_act)
  53. def get_linear_method(self) -> "GPTQLinearMethod":
  54. return GPTQLinearMethod(self)
  55. def get_scaled_act_names(self) -> List[str]:
  56. return []
  57. def merge_weight(self) -> bool:
  58. return True
  59. def rope_style(self) -> Optional[bool]:
  60. return None
  61. class ExllamaState(Enum):
  62. UNUSED = enum.auto()
  63. UNINITIALIZED = enum.auto()
  64. READY = enum.auto()
  65. class GPTQLinearMethod(LinearMethodBase):
  66. """Linear method for GPTQ.
  67. Args:
  68. quant_config: The GPTQ quantization config.
  69. """
  70. def __init__(self, quant_config: GPTQConfig):
  71. self.quant_config = quant_config
  72. def create_weights(
  73. self,
  74. input_size_per_partition: int,
  75. output_partition_sizes: List[int],
  76. input_size: int,
  77. output_size: int,
  78. params_dtype: torch.dtype,
  79. ) -> Dict[str, Any]:
  80. del output_size # Unused.
  81. if input_size_per_partition % self.quant_config.group_size != 0:
  82. raise ValueError(
  83. "The input size is not aligned with the quantized "
  84. "weight shape. This can be caused by too large "
  85. "tensor parallel size.")
  86. output_size_per_partition = sum(output_partition_sizes)
  87. if (output_size_per_partition % self.quant_config.pack_factor.numerator
  88. != 0):
  89. raise ValueError(
  90. "The output size is not aligned with the quantized "
  91. "weight shape. This can be caused by too large "
  92. "tensor parallel size.")
  93. if self.quant_config.group_size != -1:
  94. group_size = self.quant_config.group_size
  95. else:
  96. group_size = input_size
  97. exllama_state = ExllamaState.UNINITIALIZED
  98. scale_and_zero_size = input_size // group_size
  99. scale_and_zero_input_dim = None
  100. if (input_size != input_size_per_partition
  101. and self.quant_config.group_size != -1):
  102. # For act-order models, we cannot use Exllama for row parallel layer
  103. if self.quant_config.desc_act:
  104. exllama_state = ExllamaState.UNUSED
  105. else:
  106. # we need to partition qzeros and scales for exllama kernel
  107. scale_and_zero_size = input_size_per_partition // group_size
  108. scale_and_zero_input_dim = 0
  109. qweight = Parameter(
  110. torch.empty(
  111. input_size_per_partition // self.quant_config.pack_factor,
  112. output_size_per_partition,
  113. dtype=torch.int32,
  114. ),
  115. requires_grad=False,
  116. )
  117. set_weight_attrs(
  118. qweight, {
  119. "input_dim": 0,
  120. "output_dim": 1,
  121. "packed_dim": 0,
  122. "pack_factor": self.quant_config.pack_factor,
  123. })
  124. g_idx = Parameter(
  125. torch.tensor(
  126. [
  127. i // self.quant_config.group_size
  128. for i in range(input_size_per_partition)
  129. ],
  130. dtype=torch.int32,
  131. ),
  132. requires_grad=False,
  133. )
  134. # Ignore warning from fused linear layers such as QKVParallelLinear.
  135. set_weight_attrs(g_idx, {"input_dim": 0, "ignore_warning": True})
  136. qzeros = Parameter(
  137. torch.empty(
  138. scale_and_zero_size,
  139. output_size_per_partition // self.quant_config.pack_factor,
  140. dtype=torch.int32,
  141. ),
  142. requires_grad=False,
  143. )
  144. set_weight_attrs(
  145. qzeros, {
  146. "input_dim": scale_and_zero_input_dim,
  147. "output_dim": 1,
  148. "packed_dim": 1,
  149. "pack_factor": self.quant_config.pack_factor,
  150. })
  151. scales = Parameter(
  152. torch.empty(
  153. scale_and_zero_size,
  154. output_size_per_partition,
  155. dtype=params_dtype,
  156. ),
  157. requires_grad=False,
  158. )
  159. set_weight_attrs(scales, {
  160. "input_dim": scale_and_zero_input_dim,
  161. "output_dim": 1,
  162. })
  163. return {
  164. "qweight": qweight,
  165. "g_idx": g_idx,
  166. "qzeros": qzeros,
  167. "scales": scales,
  168. "exllama_state": exllama_state,
  169. }
  170. def apply_weights(self,
  171. weights: Dict[str, Any],
  172. x: torch.Tensor,
  173. bias: Optional[torch.Tensor] = None) -> torch.Tensor:
  174. qweight = weights["qweight"]
  175. out_shape = x.shape[:-1] + (qweight.shape[-1], )
  176. reshaped_x = x.reshape(-1, x.shape[-1])
  177. # exllama needs to shuffle the weight after the weight is loaded
  178. # here we do the shuffle on first forward pass
  179. if weights["exllama_state"] == ExllamaState.UNINITIALIZED:
  180. if self.quant_config.desc_act:
  181. weights["g_idx"] = torch.argsort(weights["g_idx"]).to(
  182. torch.int)
  183. else:
  184. weights["g_idx"] = torch.empty((1, 1), device="meta")
  185. weights["exllama_state"] = ExllamaState.READY
  186. ops.gptq_shuffle(weights["qweight"], weights["g_idx"],
  187. self.quant_config.weight_bits)
  188. output = ops.gptq_gemm(reshaped_x, weights["qweight"],
  189. weights["qzeros"], weights["scales"],
  190. weights["g_idx"],
  191. weights["exllama_state"] == ExllamaState.READY,
  192. self.quant_config.weight_bits)
  193. if bias is not None:
  194. output = output + bias
  195. return output.reshape(out_shape)