(self, config_path: str, graph: BaseGraph)
| 178 | |
| 179 | class SNPECaffeExporter(CaffeExporter): |
| 180 | def export_quantization_config(self, config_path: str, graph: BaseGraph): |
| 181 | var_quant_info_recorder = {} |
| 182 | for operation in graph.operations.values(): |
| 183 | if not isinstance(operation, QuantableOperation): continue |
| 184 | for config, var in operation.config_with_variable: |
| 185 | if not QuantizationStates.can_export(config.state): |
| 186 | raise PermissionError( |
| 187 | 'Can not export quant config cause not all quantization configurations ' |
| 188 | 'have been correctly initialized(or some of them has been deactivated). ' |
| 189 | f'Operation {operation.name} has an invalid quantization config({config.state}) ' |
| 190 | f'at variable {var.name}.') |
| 191 | |
| 192 | # PATCH 2021.11.25 |
| 193 | # REMOVE BIAS FROM CONFIGURATION |
| 194 | if config.num_of_bits > 8: |
| 195 | continue |
| 196 | |
| 197 | if config.state in { |
| 198 | QuantizationStates.FP32, |
| 199 | QuantizationStates.SOI |
| 200 | }: continue |
| 201 | # Simply override recorder is acceptable here, |
| 202 | # we do not support mix precision quantization for CUDA backend now. |
| 203 | # All configurations for this variable should keep identical towards each other. |
| 204 | if config.state == QuantizationStates.PASSIVE and var.name in var_quant_info_recorder: continue |
| 205 | var_quant_info_recorder[var.name] = config |
| 206 | |
| 207 | # ready to render config to json. |
| 208 | for var in var_quant_info_recorder: |
| 209 | config = var_quant_info_recorder[var] |
| 210 | assert isinstance(config, TensorQuantizationConfig) |
| 211 | tensorwise = config.policy.has_property(QuantizationProperty.PER_TENSOR) |
| 212 | var_quant_info_recorder[var] = { |
| 213 | 'tensor_min' : convert_value(config.scale * (config.quant_min - config.offset), tensorwise, DataType.FP32), |
| 214 | 'tensor_max' : convert_value(config.scale * (config.quant_max - config.offset), tensorwise, DataType.FP32) |
| 215 | } |
| 216 | |
| 217 | with open(file=config_path, mode='w') as file: |
| 218 | json.dump(var_quant_info_recorder, file, indent=4) |
| 219 | |
| 220 | def export(self, file_path: str, graph: BaseGraph, config_path: str = None, input_shapes: List[List[int]] = [[1, 3, 224, 224]]): |
| 221 | # dump config |
no test coverage detected