MCPcopy Create free account
hub / github.com/MegEngine/MegEngine / reset_qconfig

Function reset_qconfig

imperative/python/megengine/quantization/quantize.py:138–169  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

136
137
138def 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
172def _propagate(module: Module, func_str: str, *args, **kargs):

Callers 3

test_reset_qconfigFunction · 0.90
test_apply_easy_quantFunction · 0.90
test_apply_tqtFunction · 0.90

Calls 5

listFunction · 0.85
safe_callFunction · 0.85
_flattenMethod · 0.80
get_weight_qparamsMethod · 0.80

Tested by 3

test_reset_qconfigFunction · 0.72
test_apply_easy_quantFunction · 0.72
test_apply_tqtFunction · 0.72