Utility to get the quantization range for a given quantization config Adapted from phoenix/quatization/_utils.py
(n_bits: int, signed: bool, mode: str)
| 43 | |
| 44 | |
| 45 | def get_quant_range(n_bits: int, signed: bool, mode: str) -> Tuple[int, int]: |
| 46 | """ |
| 47 | Utility to get the quantization range for a given quantization config |
| 48 | Adapted from phoenix/quatization/_utils.py |
| 49 | """ |
| 50 | max_q = 2**n_bits |
| 51 | if not signed: |
| 52 | quant_min = 0 |
| 53 | quant_max = max_q - 1 |
| 54 | if mode == "LINEAR_SYMMETRIC": |
| 55 | quant_max -= 1 |
| 56 | else: |
| 57 | quant_min = -max_q / 2 |
| 58 | quant_max = max_q / 2 - 1 |
| 59 | if mode == "LINEAR_SYMMETRIC": |
| 60 | quant_min += 1 |
| 61 | return int(quant_min), int(quant_max) |
| 62 | |
| 63 | |
| 64 | def quantize_weight( |