(self, input, index=0, factor=1)
| 127 | return input_q |
| 128 | |
| 129 | def compute_quantization(self, input, index=0, factor=1): |
| 130 | # fixing the quantization bits based on the training steps |
| 131 | # when reducing 1 bit at each period, we increase the period |
| 132 | # to go slowly toward the target quantization bits |
| 133 | # the period and starting bit can be configured |
| 134 | |
| 135 | if input.start_bits != input.target_bits: |
| 136 | if self.qsteps >= input.q_period: |
| 137 | self.quantize_real_ratio = 1.0 |
| 138 | input.q_period <<= 1 |
| 139 | input.q_period *= factor |
| 140 | input.start_bits -= 1 |
| 141 | if self.q_verbose: |
| 142 | logger.info( |
| 143 | f'Quantization settings: current bit-precision = {input.start_bits}, step = {self.qsteps}, quantization period = {input.q_period}, index = {index}' |
| 144 | ) |
| 145 | assert (input.start_bits >= input.target_bits), \ |
| 146 | 'Quantization bit is lower than target precision bits!' |
| 147 | |
| 148 | if self.use_quantizer_kernel: |
| 149 | if input.start_bits <= 2: |
| 150 | raise ValueError('Quantization bit is too low, please do it without quantization kernel!') |
| 151 | input_q = ds_quantizer(input.data.clone(), |
| 152 | self.q_groups, |
| 153 | input.start_bits, |
| 154 | asym=False if self.q_type == 'symmetric' else True, |
| 155 | sr=False if self.q_rounding == 'nearest_neighbor' else True) |
| 156 | else: |
| 157 | if input.start_bits >= 3: |
| 158 | input_flat = self.quantize_highbit(input.data, input.start_bits) |
| 159 | elif input.start_bits == 2: |
| 160 | assert self.q_type == 'symmetric', 'Quantization type is not symmetric!' |
| 161 | assert self.q_rounding == 'nearest', 'Quantization rounding is not nearest_neighbor!' |
| 162 | input_flat = self.quantize_tenary(input.data) |
| 163 | elif input.start_bits == 1: |
| 164 | assert self.q_type == 'symmetric', 'Quantization type is not symmetric!' |
| 165 | assert self.q_rounding == 'nearest', 'Quantization rounding is not nearest_neighbor!' |
| 166 | input_flat = self.quantize_binary(input.data) |
| 167 | if self.use_quantizer_kernel: |
| 168 | return self.mixed_fp16_quantize(input.data, input_q, index) |
| 169 | else: |
| 170 | if self.q_mixed_fp16 and input.start_bits >= input.target_bits - 1: |
| 171 | input_flat = self.quantize_real_ratio * input.data + \ |
| 172 | (1 - self.quantize_real_ratio) * input_flat |
| 173 | return input_flat |
| 174 | |
| 175 | def update_fp16_ratio(self): |
| 176 | if self.q_mixed_fp16: |
no test coverage detected