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

Method quantize_highbit

deepspeed/runtime/quantize.py:78–100  ·  view source on GitHub ↗
(self, inputs, num_bits)

Source from the content-addressed store, hash-verified

76 self.qsteps += 1
77
78 def quantize_highbit(self, inputs, num_bits):
79
80 q_range = 2**num_bits
81 input_flat = inputs.reshape(self.q_groups, -1)
82 g_min = input_flat.amin(dim=-1, keepdim=True)
83 g_max = input_flat.amax(dim=-1, keepdim=True)
84
85 # Random number generator (Uniform)
86 if self.q_rounding == 'nearest':
87 p = 0.
88 else:
89 p = input_flat.new(input_flat.shape).uniform_(-0.5, 0.5)
90
91 if self.q_type == 'symmetric':
92 scale = 2 * torch.max(torch.abs(g_min), torch.abs(g_max)) / q_range
93 zero_point = 0.
94 input_flat = (input_flat / scale + p).round().clamp(-(q_range >> 1), (q_range >> 1) - 1) * scale
95 elif self.q_type == 'asymmetric':
96 scale = (g_max - g_min) / q_range
97 zero_point = (g_min / scale).round() * scale
98 input_flat = ((input_flat - zero_point) / scale + p).round().clamp(0, (q_range - 1)) * scale + zero_point
99 output = input_flat.reshape(inputs.shape).contiguous()
100 return output
101
102 def quantize_tenary(self, inputs):
103 input_flat = inputs.reshape(self.q_groups, -1)

Callers 1

compute_quantizationMethod · 0.95

Calls 2

contiguousMethod · 0.80
reshapeMethod · 0.45

Tested by

no test coverage detected