(self, config_path: str, graph: BaseGraph)
| 62 | 所有未写入 min, max 的变量将保持 fp32(fp16) 精度执行。 |
| 63 | """ |
| 64 | def export_quantization_config(self, config_path: str, graph: BaseGraph): |
| 65 | act_quant_info = {} |
| 66 | for op in graph.topological_sort(): |
| 67 | if not isinstance(op, QuantableOperation): continue |
| 68 | if op.type in {"Gather", "Unsqueeze", "Concat", "Reshape", "Squeeze"}: continue |
| 69 | |
| 70 | for cfg, var in op.config_with_variable: |
| 71 | if not cfg.can_export(export_overlapped=True): continue |
| 72 | if var.is_parameter: continue |
| 73 | |
| 74 | if cfg.policy != QuantizationPolicy( |
| 75 | QuantizationProperty.LINEAR + |
| 76 | QuantizationProperty.SYMMETRICAL + |
| 77 | QuantizationProperty.PER_TENSOR): |
| 78 | ppq_warning(f'Can not export quantization config on variable {var.name}, ' |
| 79 | 'Quantization Policy is invalid.') |
| 80 | continue |
| 81 | |
| 82 | if cfg.num_of_bits != 8 or cfg.quant_max != 127 or cfg.quant_min != -128: |
| 83 | ppq_warning(f'Can not export quantization config on variable {var.name}, ' |
| 84 | 'Tensor Quantization Config has unexpected setting.') |
| 85 | continue |
| 86 | |
| 87 | act_quant_info[var.name] = cfg.scale.item() * 127 |
| 88 | |
| 89 | json_qparams_str = json.dumps({'act_quant_info': act_quant_info}, indent=4) |
| 90 | with open(config_path, "w") as json_file: |
| 91 | json_file.write(json_qparams_str) |
| 92 | |
| 93 | def export_weights(self, graph: BaseGraph, config_path: str = None): |
| 94 | topo_order = graph.topological_sort() |
no test coverage detected