r"""Reset :class:`~._FakeQuantize` and :class:`~.Observer` according to ``qconfig`` Args: module: root module to reset recursively. qconfig: an instance of :class:`~.QConfig` to be set as submodules' qconfig. inplace: whether to reset submodules in-place.
(module: Module, qconfig: QConfig, inplace: bool = True)
| 136 | |
| 137 | |
| 138 | def reset_qconfig(module: Module, qconfig: QConfig, inplace: bool = True): |
| 139 | r"""Reset :class:`~._FakeQuantize` and :class:`~.Observer` according to ``qconfig`` |
| 140 | |
| 141 | Args: |
| 142 | module: root module to reset recursively. |
| 143 | qconfig: an instance of :class:`~.QConfig` to be set as submodules' qconfig. |
| 144 | inplace: whether to reset submodules in-place. |
| 145 | """ |
| 146 | |
| 147 | if not inplace: |
| 148 | module = deepcopy(module) |
| 149 | |
| 150 | def safe_call(func, qparams): |
| 151 | inst = func() if func is not None else None |
| 152 | if inst is not None and getattr(inst, "set_qparams", None) is not None: |
| 153 | inst.set_qparams(qparams) |
| 154 | return inst |
| 155 | |
| 156 | def is_qat(mod: Module): |
| 157 | return isinstance(mod, QATModule) |
| 158 | |
| 159 | for m in list(module._flatten(predicate=is_qat)): |
| 160 | if m.with_weight: |
| 161 | weight_params = m.get_weight_qparams() |
| 162 | m.weight_observer = safe_call(qconfig.weight_observer, weight_params) |
| 163 | m.weight_fake_quant = safe_call(qconfig.weight_fake_quant, weight_params) |
| 164 | if m.with_act: |
| 165 | act_params = m.get_activation_qparams() |
| 166 | m.act_observer = safe_call(qconfig.act_observer, act_params) |
| 167 | m.act_fake_quant = safe_call(qconfig.act_fake_quant, act_params) |
| 168 | |
| 169 | return module |
| 170 | |
| 171 | |
| 172 | def _propagate(module: Module, func_str: str, *args, **kargs): |