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

Function quantize

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

r"""Recursively convert :class:`~.QATModule` to :class:`~.QuantizedModule` through :meth:`~.Module.apply`. Args: module: root module to do convert recursively. inplace: whether to convert submodules in-place. mapping: a dict indicating how to convert custom modules f

(module: Module, inplace: bool = True, mapping: dict = None)

Source from the content-addressed store, hash-verified

50
51
52def quantize(module: Module, inplace: bool = True, mapping: dict = None):
53 r"""Recursively convert :class:`~.QATModule` to :class:`~.QuantizedModule`
54 through :meth:`~.Module.apply`.
55
56 Args:
57 module: root module to do convert recursively.
58 inplace: whether to convert submodules in-place.
59 mapping: a dict indicating how to convert custom modules from QATModule to
60 QuantizedModule. Will be combined with internal default convert mapping dict.
61 """
62
63 if not inplace:
64 module = deepcopy(module)
65
66 convert_dict = copy(_qat2quantized_dict)
67 if mapping is not None:
68 convert_dict.update(mapping)
69 qat_modules = tuple(convert_dict.keys())
70
71 def is_qat(mod: Module):
72 return isinstance(mod, qat_modules)
73
74 # must use list to avoid replacement influencing successor modules
75 for key, submodule, parent in list(
76 module._flatten(with_key=True, with_parent=True, predicate=is_qat)
77 ):
78 new_mod = convert_dict[type(submodule)].from_qat_module(submodule)
79 set_expand_structure(module, key, new_mod)
80
81 return module
82
83
84def quantize_qat(

Calls 7

listFunction · 0.85
set_expand_structureFunction · 0.85
keysMethod · 0.80
_flattenMethod · 0.80
copyFunction · 0.50
updateMethod · 0.45
from_qat_moduleMethod · 0.45