|
| 1 | +from typing import Callable, List, Optional |
| 2 | + |
| 3 | +import torch |
| 4 | +from torch.nn import Parameter |
| 5 | + |
| 6 | +from vllm import _custom_ops as ops |
| 7 | +from vllm.model_executor.layers.quantization.compressed_tensors.schemes import ( |
| 8 | + CompressedTensorsScheme) |
| 9 | +from vllm.model_executor.layers.quantization.gptq_marlin import ( |
| 10 | + GPTQ_MARLIN_MAX_PARALLEL, GPTQ_MARLIN_MIN_THREAD_N, GPTQMarlinState, |
| 11 | + marlin_permute_scales) |
| 12 | +from vllm.model_executor.utils import set_weight_attrs |
| 13 | + |
| 14 | +__all__ = ["CompressedTensorsW4A16"] |
| 15 | + |
| 16 | + |
| 17 | +class CompressedTensorsW4A16(CompressedTensorsScheme): |
| 18 | + |
| 19 | + def __init__(self, |
| 20 | + strategy: str, |
| 21 | + num_bits: int, |
| 22 | + group_size: Optional[int] = None): |
| 23 | + self.num_bits = num_bits |
| 24 | + self.strategy = strategy |
| 25 | + self.group_size = group_size |
| 26 | + |
| 27 | + if self.strategy == "group" and self.group_size is None: |
| 28 | + raise ValueError( |
| 29 | + "group_size must be given when using strategy group") |
| 30 | + |
| 31 | + def create_weights(self, layer: torch.nn.Module, input_size: int, |
| 32 | + output_partition_sizes: List[int], |
| 33 | + input_size_per_partition: int, |
| 34 | + params_dtype: torch.dtype, weight_loader: Callable, |
| 35 | + **kwargs): |
| 36 | + |
| 37 | + pack_factor = 32 // self.num_bits |
| 38 | + output_size_per_partition = sum(output_partition_sizes) |
| 39 | + |
| 40 | + if self.group_size is not None: |
| 41 | + group_size = self.group_size |
| 42 | + else: |
| 43 | + group_size = input_size |
| 44 | + |
| 45 | + weight_scale_dim = None |
| 46 | + scales_and_zp_size = input_size // group_size |
| 47 | + |
| 48 | + if (input_size != input_size_per_partition |
| 49 | + and self.group_size is not None): |
| 50 | + weight_scale_dim = 1 |
| 51 | + scales_and_zp_size = input_size_per_partition // group_size |
| 52 | + |
| 53 | + weight = Parameter( |
| 54 | + torch.empty( |
| 55 | + output_size_per_partition, |
| 56 | + input_size_per_partition // pack_factor, |
| 57 | + dtype=torch.int32, |
| 58 | + ), |
| 59 | + requires_grad=False, |
| 60 | + ) |
| 61 | + |
| 62 | + set_weight_attrs( |
| 63 | + weight, { |
| 64 | + "input_dim": 1, |
| 65 | + "output_dim": 0, |
| 66 | + "packed_dim": 1, |
| 67 | + "pack_factor": pack_factor |
| 68 | + }) |
| 69 | + set_weight_attrs(weight, {"weight_loader": weight_loader}) |
| 70 | + |
| 71 | + layer.register_parameter("weight_packed", weight) |
| 72 | + |
| 73 | + weight_scale = Parameter( |
| 74 | + torch.empty( |
| 75 | + output_size_per_partition, |
| 76 | + scales_and_zp_size, |
| 77 | + dtype=params_dtype, |
| 78 | + ), |
| 79 | + requires_grad=False, |
| 80 | + ) |
| 81 | + |
| 82 | + set_weight_attrs(weight_scale, {"weight_loader": weight_loader}) |
| 83 | + set_weight_attrs(weight_scale, { |
| 84 | + "input_dim": weight_scale_dim, |
| 85 | + "output_dim": 0 |
| 86 | + }) |
| 87 | + layer.register_parameter("weight_scale", weight_scale) |
| 88 | + |
| 89 | + # A 2D array defining the original shape of the weights |
| 90 | + # before packing |
| 91 | + weight_shape = Parameter(torch.empty(2, dtype=torch.int64), |
| 92 | + requires_grad=False) |
| 93 | + |
| 94 | + layer.register_parameter("weight_shape", weight_shape) |
| 95 | + set_weight_attrs(weight_shape, {"weight_loader": weight_loader}) |
| 96 | + |
| 97 | + layer.input_size_per_partition = input_size_per_partition |
| 98 | + layer.output_size_per_partition = output_size_per_partition |
| 99 | + |
| 100 | + layer.input_size = input_size |
| 101 | + layer.marlin_state = GPTQMarlinState.REPACK |
| 102 | + layer.is_k_full = True |
| 103 | + layer.group_size = group_size |
| 104 | + |
| 105 | + max_workspace_size = ( |
| 106 | + output_size_per_partition // |
| 107 | + GPTQ_MARLIN_MIN_THREAD_N) * GPTQ_MARLIN_MAX_PARALLEL |
| 108 | + |
| 109 | + workspace = torch.zeros(max_workspace_size, |
| 110 | + dtype=torch.int, |
| 111 | + requires_grad=False) |
| 112 | + layer.workspace = workspace |
| 113 | + |
| 114 | + def apply_weights(self, layer: torch.nn.Module, x: torch.Tensor): |
| 115 | + reshaped_x = x.reshape(-1, x.shape[-1]) |
| 116 | + |
| 117 | + size_m = reshaped_x.shape[0] |
| 118 | + part_size_n = layer.output_size_per_partition |
| 119 | + part_size_k = layer.input_size_per_partition |
| 120 | + |
| 121 | + out_shape = x.shape[:-1] + (part_size_n, ) |
| 122 | + |
| 123 | + if layer.marlin_state == GPTQMarlinState.REPACK: |
| 124 | + layer.marlin_state = GPTQMarlinState.READY |
| 125 | + |
| 126 | + # Newly generated tensors need to replace existing tensors that are |
| 127 | + # already registered as parameters by vLLM (and won't be freed) |
| 128 | + def replace_tensor(name, new_t): |
| 129 | + # It is important to use resize_() here since it ensures |
| 130 | + # the same buffer is reused |
| 131 | + getattr(layer, name).resize_(new_t.shape) |
| 132 | + getattr(layer, name).copy_(new_t) |
| 133 | + del new_t |
| 134 | + |
| 135 | + cur_device = layer.weight_packed.device |
| 136 | + |
| 137 | + # Reset g_idx related tensors |
| 138 | + layer.g_idx = Parameter(torch.empty(0, |
| 139 | + dtype=torch.int, |
| 140 | + device=cur_device), |
| 141 | + requires_grad=False) |
| 142 | + layer.g_idx_sort_indices = Parameter(torch.empty( |
| 143 | + 0, dtype=torch.int, device=cur_device), |
| 144 | + requires_grad=False) |
| 145 | + |
| 146 | + # Repack weights |
| 147 | + marlin_qweight = ops.gptq_marlin_repack( |
| 148 | + layer.weight_packed.t().contiguous(), layer.g_idx_sort_indices, |
| 149 | + part_size_k, part_size_n, self.num_bits) |
| 150 | + |
| 151 | + replace_tensor("weight_packed", marlin_qweight) |
| 152 | + |
| 153 | + # Permute scales |
| 154 | + scales_size_k = part_size_k |
| 155 | + scales_size_n = part_size_n |
| 156 | + |
| 157 | + marlin_scales = marlin_permute_scales( |
| 158 | + layer.weight_scale.squeeze().t().contiguous(), scales_size_k, |
| 159 | + scales_size_n, layer.group_size, self.num_bits) |
| 160 | + replace_tensor("weight_scale", marlin_scales) |
| 161 | + |
| 162 | + output = ops.gptq_marlin_gemm(reshaped_x, layer.weight_packed, |
| 163 | + layer.weight_scale, layer.g_idx, |
| 164 | + layer.g_idx_sort_indices, |
| 165 | + layer.workspace, self.num_bits, size_m, |
| 166 | + part_size_n, part_size_k, |
| 167 | + layer.is_k_full) |
| 168 | + return output.reshape(out_shape) |
0 commit comments