You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

quantize.py 4.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. # MegEngine is Licensed under the Apache License, Version 2.0 (the "License")
  2. #
  3. # Copyright (c) 2014-2020 Megvii Inc. All rights reserved.
  4. #
  5. # Unless required by applicable law or agreed to in writing,
  6. # software distributed under the License is distributed on an
  7. # "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  8. from copy import deepcopy
  9. from ..module import Module, QATModule, Sequential, quantized
  10. from .qconfig import QConfig, ema_fakequant_qconfig
  11. def quantize(module: Module, inplace=True):
  12. r"""
  13. Recursively convert `module` to `quantized` mode through :meth:`~.Module.apply`.
  14. :param module: root module to do convert recursively.
  15. """
  16. if not inplace:
  17. module = deepcopy(module)
  18. def is_qat_module(obj):
  19. return isinstance(obj, QATModule)
  20. # no need to pass prefix and get pure key of parent Module.
  21. for key, submodule, parent in module._flatten(
  22. with_key=True, with_parent=True, predicate=is_qat_module
  23. ):
  24. if isinstance(parent, Sequential):
  25. # cannnot use setattr to be compatible with Sequential's ``__setitem__``
  26. parent[int(key.split(".")[-1])] = submodule.to_quantized()
  27. else:
  28. setattr(parent, key.split(".")[-1], submodule.to_quantized())
  29. return module
  30. def quantize_qat(module: Module, qconfig: QConfig = ema_fakequant_qconfig):
  31. r"""
  32. Recursively convert `module` to `qat` mode through :meth:`~.Module.apply`
  33. and set qconfig relatively.
  34. :param module: root module to do convert recursively.
  35. :param qconfig: a instance of :class:`~.QConfig` to be set as submodules' qconfig.
  36. default is :any:`~.qconfig.ema_fakequant_qconfig`.
  37. """
  38. def fn(mod: Module):
  39. if isinstance(mod, QATModule):
  40. mod.set_qat_mode(QATModule.QATMode.QAT)
  41. mod.set_qconfig(qconfig)
  42. module.apply(fn)
  43. def quantize_calibration(module: Module, qconfig: QConfig = ema_fakequant_qconfig):
  44. r"""
  45. Recursively convert `module` to `calibration` mode through :meth:`~.Module.apply`
  46. and set qconfig relatively.
  47. :param module: root module to do convert recursively.
  48. :param qconfig: a instance of :class:`~.QConfig` to be set as submodules' qconfig.
  49. default is :any:`~.qconfig.ema_fakequant_qconfig`.
  50. """
  51. def fn(mod: Module):
  52. if isinstance(mod, QATModule):
  53. mod.set_qat_mode(QATModule.QATMode.CALIBRATION)
  54. mod.set_qconfig(qconfig)
  55. module.apply(fn)
  56. def disable_fake_quant(module: Module):
  57. r"""
  58. Recursively disable `module` fake quantization in QATModule through :meth:`~.Module.apply`
  59. :param module: root module to do disable fake quantization recursively.
  60. """
  61. def fn(mod):
  62. if isinstance(mod, QATModule):
  63. mod.act_fake_quant.disable()
  64. mod.weight_fake_quant.disable()
  65. module.apply(fn)
  66. def disable_observer(module: Module):
  67. r"""
  68. Recursively disable `module` observer in QATModule through :meth:`~.Module.apply`
  69. :param module: root module to do disable observer recursively.
  70. """
  71. def fn(mod):
  72. if isinstance(mod, QATModule):
  73. mod.act_observer.disable()
  74. mod.weight_observer.disable()
  75. module.apply(fn)
  76. def enable_fake_quant(module: Module):
  77. r"""
  78. Recursively enable `module` fake quantization in QATModule through :meth:`~.Module.apply`
  79. :param module: root module to do enable fake quantization recursively.
  80. """
  81. def fn(mod):
  82. if isinstance(mod, QATModule):
  83. mod.act_fake_quant.enable()
  84. mod.weight_fake_quant.enable()
  85. module.apply(fn)
  86. def enable_observer(module: Module):
  87. r"""
  88. Recursively enable `module` observer in QATModule through :meth:`~.Module.apply`
  89. :param module: root module to do enable observer recursively.
  90. """
  91. def fn(mod):
  92. if isinstance(mod, QATModule):
  93. mod.act_observer.enable()
  94. mod.weight_observer.enable()
  95. module.apply(fn)

MegEngine 安装包中集成了使用 GPU 运行代码所需的 CUDA 环境,不用区分 CPU 和 GPU 版。 如果想要运行 GPU 程序,请确保机器本身配有 GPU 硬件设备并安装好驱动。 如果你想体验在云端 GPU 算力平台进行深度学习开发的感觉,欢迎访问 MegStudio 平台