MCPcopy Index your code
hub / github.com/OpenPPL/ppq / export_quantization_config

Method export_quantization_config

ppq/parser/tensorRT.py:64–91  ·  view source on GitHub ↗
(self, config_path: str, graph: BaseGraph)

Source from the content-addressed store, hash-verified

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()

Callers 1

exportMethod · 0.95

Calls 4

QuantizationPolicyClass · 0.90
ppq_warningFunction · 0.90
topological_sortMethod · 0.80
can_exportMethod · 0.45

Tested by

no test coverage detected