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)
| 50 | |
| 51 | |
| 52 | def 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 | |
| 84 | def quantize_qat( |