quant weights and remove weights input quantize_linear node. for example: `weight -> quant -> dequant -> conv2d` will be frozen into `weight -> dequant -> conv2d`, and weight will be scaled offline. Args: scope(paddle.Scope): scope is used to get the weight tensor values.
| 3165 | |
| 3166 | |
| 3167 | class QuantWeightPass: |
| 3168 | """ |
| 3169 | quant weights and remove weights input quantize_linear node. for example: |
| 3170 | `weight -> quant -> dequant -> conv2d` will be frozen into `weight -> dequant -> conv2d`, |
| 3171 | and weight will be scaled offline. |
| 3172 | |
| 3173 | Args: |
| 3174 | scope(paddle.Scope): scope is used to get the weight tensor values. |
| 3175 | place(paddle.CPUPlace|paddle.CUDAPlace|str): place is used to restore the weight tensors. |
| 3176 | If it's string, It can be ``cpu``, and ``gpu:x``, where ``x`` is the index of the GPUs. |
| 3177 | bias_correction(bool): whether use bias correction for post-training quantization. |
| 3178 | https://arxiv.org/abs/1810.05723. |
| 3179 | quant_bits(int, optional): quantization bit number for weight. Default is 8. |
| 3180 | save_int_weight(bool, optional): Whether the type saving the weight is int. Default is True. |
| 3181 | |
| 3182 | Examples: |
| 3183 | .. code-block:: pycon |
| 3184 | |
| 3185 | >>> # The original graph will be rewrite. |
| 3186 | >>> import paddle |
| 3187 | >>> import paddle.static as static |
| 3188 | >>> from paddle.static.quantization import QuantWeightPass |
| 3189 | >>> from paddle.base.framework import IrGraph |
| 3190 | >>> from paddle.framework import core |
| 3191 | |
| 3192 | >>> graph = IrGraph(core.Graph(paddle.static.Program().desc), for_test=False) |
| 3193 | >>> place = paddle.CPUPlace() |
| 3194 | >>> scope = paddle.static.global_scope() |
| 3195 | >>> quant_weight_pass = QuantWeightPass(scope, place) |
| 3196 | >>> quant_weight_pass.apply(graph) |
| 3197 | """ |
| 3198 | |
| 3199 | def __init__( |
| 3200 | self, |
| 3201 | scope, |
| 3202 | place, |
| 3203 | bias_correction=False, |
| 3204 | quant_bits=8, |
| 3205 | save_int_weight=True, |
| 3206 | ): |
| 3207 | self._place = _get_paddle_place(place) |
| 3208 | self._scope = scope |
| 3209 | self._bias_correction = bias_correction |
| 3210 | self._quant_bits = quant_bits |
| 3211 | self._save_int_weight = save_int_weight |
| 3212 | assert self._scope is not None, "scope must not be None." |
| 3213 | assert self._place is not None, "place must not be None." |
| 3214 | self._quantized_ops = set() |
| 3215 | |
| 3216 | def apply(self, graph): |
| 3217 | assert isinstance(graph, IrGraph), ( |
| 3218 | 'graph must be the instance of IrGraph.' |
| 3219 | ) |
| 3220 | fake_quant_ops_for_weight = [] |
| 3221 | |
| 3222 | fake_quant_ops = [ |
| 3223 | op for op in graph.all_op_nodes() if op.name() == "quantize_linear" |
| 3224 | ] |
no outgoing calls
no test coverage detected