(self, config_path: str, graph: BaseGraph)
| 89 | file.write(f"{k} {scale} {zp}\n") |
| 90 | |
| 91 | def export_quantization_config(self, config_path: str, graph: BaseGraph): |
| 92 | |
| 93 | render_buffer = {"configs": {}, "dispatchings": {}, "values": {}} |
| 94 | |
| 95 | # Render quantization config. |
| 96 | for operation in graph.operations.values(): |
| 97 | if isinstance(operation, QuantableOperation): |
| 98 | op_dict = { |
| 99 | var.name: { |
| 100 | "bit_width": config.num_of_bits, |
| 101 | "policy": config.policy.to_dict(), |
| 102 | "state": config.state.name, |
| 103 | "quant_min": config.quant_min, |
| 104 | "quant_max": config.quant_max, |
| 105 | "hash": config.__hash__(), |
| 106 | "dominator": config.dominated_by.__hash__(), |
| 107 | } |
| 108 | for config, var in operation.config_with_variable |
| 109 | } |
| 110 | |
| 111 | for config, _ in operation.config_with_variable: |
| 112 | if config.policy.has_property(QuantizationProperty.PER_CHANNEL): |
| 113 | raise PermissionError('Tengine does not support per channel quantization.') |
| 114 | |
| 115 | if config.dominated_by == config: |
| 116 | render_buffer["values"][config.__hash__()] = { |
| 117 | "scale": config.scale.item(), |
| 118 | "zero_point": config.offset.item(), |
| 119 | } |
| 120 | |
| 121 | render_buffer["configs"][operation.name] = op_dict |
| 122 | render_buffer["dispatchings"][operation.name] = operation.platform.name |
| 123 | |
| 124 | with open(file=config_path, mode="w") as file: |
| 125 | json.dump(render_buffer, file, indent=4) |
| 126 | |
| 127 | def export_operation(self, operation: Operation) -> onnx.OperatorProto: |
| 128 | if operation.type in OPERATION_EXPORTERS: |
no test coverage detected