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

Method dequantize

deepspeed/inference/quantization/utils.py:110–145  ·  view source on GitHub ↗
(self, tensor: Tensor, quant_scale: Tensor, quant_min: Tensor)

Source from the content-addressed store, hash-verified

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

Callers 4

quantization_test_helperFunction · 0.95
forwardMethod · 0.45
forwardMethod · 0.45
wrapperFunction · 0.45

Calls 10

_dequantize_int8Method · 0.95
get_acceleratorFunction · 0.90
_get_deviceFunction · 0.85
get_quantizer_moduleFunction · 0.85
device_nameMethod · 0.45
reshapeMethod · 0.45
numelMethod · 0.45

Tested by 1

quantization_test_helperFunction · 0.76