为你的算子创建一个默认量化信息 对于一个 Onnx 算子而言,它总是会有几个输入和输出 Variable 你需要为每一个相连的 Variable 初始化量化信息 TensorQuantConfig 这个函数就是用来帮你初始化这些信息的。 一个麻烦的问题是: 对于很多 onnx 算子而言,他们的部分输入都是不需要量化的: 如 Clip 算子的三个输入 value, min, max, 大
(
op: Operation,
num_of_bits: int = 8,
quant_min: Union[int, float] = -127,
quant_max: Union[int, float] = 128,
observer_algorithm: str = 'percentile',
policy: QuantizationPolicy =
QuantizationPolicy(
QuantizationProperty.PER_TENSOR +
QuantizationProperty.LINEAR +
QuantizationProperty.SYMMETRICAL),
rounding: RoundingPolicy = RoundingPolicy.ROUND_HALF_EVEN,
exponent_bits: int = 0,
)
| 105 | |
| 106 | @ staticmethod |
| 107 | def create_default_quant_config( |
| 108 | op: Operation, |
| 109 | num_of_bits: int = 8, |
| 110 | quant_min: Union[int, float] = -127, |
| 111 | quant_max: Union[int, float] = 128, |
| 112 | observer_algorithm: str = 'percentile', |
| 113 | policy: QuantizationPolicy = |
| 114 | QuantizationPolicy( |
| 115 | QuantizationProperty.PER_TENSOR + |
| 116 | QuantizationProperty.LINEAR + |
| 117 | QuantizationProperty.SYMMETRICAL), |
| 118 | rounding: RoundingPolicy = RoundingPolicy.ROUND_HALF_EVEN, |
| 119 | exponent_bits: int = 0, |
| 120 | ) -> OperationQuantizationConfig: |
| 121 | """ |
| 122 | 为你的算子创建一个默认量化信息 |
| 123 | |
| 124 | 对于一个 Onnx 算子而言,它总是会有几个输入和输出 Variable |
| 125 | 你需要为每一个相连的 Variable 初始化量化信息 TensorQuantConfig |
| 126 | |
| 127 | 这个函数就是用来帮你初始化这些信息的。 |
| 128 | |
| 129 | 一个麻烦的问题是: |
| 130 | |
| 131 | 对于很多 onnx 算子而言,他们的部分输入都是不需要量化的: |
| 132 | |
| 133 | 如 Clip 算子的三个输入 value, min, max, 大部分框架不要求量化 min, max |
| 134 | 如 Reshape 算子的两个输入 value, shape, 其中 shape 不能够被量化 |
| 135 | |
| 136 | PPQ 的算子接线器中记录了这些信息 |
| 137 | |
| 138 | 算子接线器中记录了所有标准 onnx 的默认量化策略 |
| 139 | 该函数将使用预定义的算子量化策略初始化量化信息 |
| 140 | |
| 141 | 你可以在 Quantizer 中对默认量化策略进行进一步修改 |
| 142 | |
| 143 | Create a default quantization configuration for given op. |
| 144 | For each onnx op, there will be some input and output variables. |
| 145 | |
| 146 | You are required to create tensor quantization config for every |
| 147 | input and output variables. |
| 148 | |
| 149 | This function is designed for creating a default quantization config for you. |
| 150 | |
| 151 | The created OQC(Op Quantization Config) is based on OpSocket. |
| 152 | |
| 153 | In fact, there are some rules or templates when creating the OQC: |
| 154 | For Clip Op which has 3 input variable, namely value, min and max |
| 155 | most framework does not require a quantization config for min and max. |
| 156 | For Reshape Op which has 2 input variable, namely value and shape |
| 157 | the input shape can never be quantized. |
| 158 | |
| 159 | Those rules are pre-defined within OpSocket, thus ppq will create |
| 160 | OQC based on underlying OpSocket of your Op. |
| 161 | |
| 162 | After the default OQC got created, you can overwrite its state in quantizer. |
| 163 | """ |
| 164 | assert isinstance(op, Operation), ( |
no test coverage detected