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

Method quantize

deepspeed/inference/quantization/utils.py:56–76  ·  view source on GitHub ↗
(self, tensor: Tensor)

Source from the content-addressed store, hash-verified

54 assert self.config['symmetric'] == False, 'Only asymmetric quantization is supported at this moment.'
55
56 def quantize(self, tensor: Tensor) -> Tuple[Tensor, Tensor, Tensor]:
57 assert tensor.shape[self.config['group_dim']] % self.config['group_size'] == 0 \
58 , f'Tensor shape: {tensor.shape} quantization config {self.config}'
59
60 tensor = torch.clone(tensor)
61
62 shape = tensor.shape
63 num_groups = shape[self.config['group_dim']] // self.config['group_size']
64 new_shape = (shape[:self.config['group_dim']] + (num_groups, self.config['group_size']) +
65 shape[self.config['group_dim'] + 1:])
66 tensor = tensor.view(new_shape)
67
68 quantized_tensor, scale, min_value = self._quantize_int8(tensor)
69 quantized_tensor = quantized_tensor.view(shape)
70
71 if self.config['num_bits'] == 4:
72 return self._compress_uint8_to_uint4(quantized_tensor), scale, min_value
73 if self.config['num_bits'] == 8:
74 return quantized_tensor, scale, min_value
75
76 assert False, 'Unsupported quantization bits {}'.format(self.config['num_bits'])
77
78 def _quantize_int8(self, tensor: Tensor) -> Tuple[Tensor, Tensor, Tensor]:
79 q_range = 2**self.config['num_bits'] - 1

Callers 3

_quantize_paramFunction · 0.95
funcFunction · 0.45
wrapperFunction · 0.45

Calls 2

_quantize_int8Method · 0.95

Tested by

no test coverage detected