Args: inputs (`torch.FloatTensor`) The input which needs to be quantized num_bits (int) Dummy variable min_value/max_value (torch.FloatTensor) Used for static activation quantization; for now they are dummy
(ctx, input, num_bits, min_value=None, max_value=None, num_groups=1)
| 152 | |
| 153 | @staticmethod |
| 154 | def forward(ctx, input, num_bits, min_value=None, max_value=None, num_groups=1): |
| 155 | """ |
| 156 | Args: |
| 157 | inputs (`torch.FloatTensor`) |
| 158 | The input which needs to be quantized |
| 159 | num_bits (int) |
| 160 | Dummy variable |
| 161 | min_value/max_value (torch.FloatTensor) |
| 162 | Used for static activation quantization; for now they are dummy variable |
| 163 | num_groups (int) |
| 164 | How many groups to partition the quantization into |
| 165 | Returns: |
| 166 | quantized_input (`torch.FloatTensor`) |
| 167 | Quantized input |
| 168 | """ |
| 169 | |
| 170 | assert (min_value is None and max_value is None) |
| 171 | input_flat = input.reshape(num_groups, -1) |
| 172 | n = input_flat.shape[1] |
| 173 | m = input_flat.norm(p=1, dim=1).div(n) |
| 174 | thres = (0.7 * m).view(-1, 1) |
| 175 | pos = (input_flat > thres).type(input.type()) |
| 176 | neg = (input_flat < -thres).type(input.type()) |
| 177 | mask = (input_flat.abs() > thres).type(input.type()) |
| 178 | alpha = ((mask * input_flat).abs().sum(dim=1) / mask.sum(dim=1)).view(-1, 1) |
| 179 | output = alpha * pos - alpha * neg |
| 180 | output = output.reshape(input.shape).contiguous() |
| 181 | return output |
| 182 | |
| 183 | @staticmethod |
| 184 | def backward(ctx, grad_output): |