MCPcopy Create free account
hub / github.com/deepspeedai/DeepSpeed / DeQuantizer

Class DeQuantizer

deepspeed/inference/quantization/utils.py:101–162  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

99
100
101class DeQuantizer:
102
103 def __init__(self, config: Dict, dtype: torch.dtype) -> None:
104 self.config = config
105 self.dtype = dtype
106 assert self.config['num_bits'] == 4 or self.config[
107 'num_bits'] == 8, 'Only INT4 and INT8 quantization is supported.'
108 assert self.config['symmetric'] == False, 'Only asymmetric quantization is supported at this moment.'
109
110 def dequantize(self, tensor: Tensor, quant_scale: Tensor, quant_min: Tensor) -> Tensor:
111 # Use customized CUDA quantization kernel if possible.
112 if self.config['group_size'] % 8 == 0 and \
113 (self.config['num_bits'] == 4 or self.config['num_bits'] == 8) and \
114 self.config['group_dim'] == len(tensor.shape) - 1 and \
115 self.dtype == torch.float16 and _get_device() == get_accelerator().device_name():
116
117 last_dimension_size = self.config['group_size']
118 if self.config['num_bits'] == 4:
119 last_dimension_size = last_dimension_size // 2
120 quantized_tensor = get_quantizer_module().dequantize_int4_to_half_experimental(
121 tensor.reshape(-1, last_dimension_size), quant_scale, quant_min,
122 tensor.numel() // last_dimension_size, self.config['group_size'])
123 shape = list(tensor.shape)
124 shape[-1] = shape[-1] * 2
125 elif self.config['num_bits'] == 8:
126 quantized_tensor = get_quantizer_module().dequantize_int8_to_half_experimental(
127 tensor.reshape(-1, last_dimension_size), quant_scale, quant_min,
128 tensor.numel() // last_dimension_size, self.config['group_size'])
129 shape = list(tensor.shape)
130
131 return quantized_tensor.reshape(shape)
132
133 if self.config['num_bits'] == 4:
134 tensor = self._decompress_uint4_to_uint8(tensor)
135 elif self.config['num_bits'] != 8:
136 assert False, 'Unsupported quantization bits {}'.format(self.config['num_bits'])
137
138 shape = tensor.shape
139 num_groups = shape[self.config['group_dim']] // self.config['group_size']
140 new_shape = (shape[:self.config['group_dim']] + (num_groups, self.config['group_size']) +
141 shape[self.config['group_dim'] + 1:])
142 tensor = tensor.view(new_shape)
143
144 dequantized_tensor = self._dequantize_int8(tensor, quant_scale, quant_min).view(shape)
145 return dequantized_tensor
146
147 def _dequantize_int8(self, tensor: Tensor, quant_scale: Tensor, quant_min: Tensor) -> Tensor:
148 assert tensor.dtype == torch.uint8
149 data = torch.zeros_like(tensor, dtype=self.dtype, device=tensor.device)
150 data = data.copy_(tensor)
151 data = data.div_(quant_scale).add_(quant_min)
152
153 return data
154
155 def _decompress_uint4_to_uint8(self, tensor: Tensor) -> Tensor:
156 new_data_shape = list(tensor.shape)
157 new_data_shape[-1] = new_data_shape[-1] * 2
158 data = torch.empty(new_data_shape, dtype=torch.uint8, device=tensor.device)

Callers 4

quantization_test_helperFunction · 0.90
__init__Method · 0.85
__init__Method · 0.85
_quantize_paramFunction · 0.85

Calls

no outgoing calls

Tested by 1

quantization_test_helperFunction · 0.72