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 3.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. def quantize_qat(module: Module, qconfig: QConfig = ema_fakequant_qconfig):
  30. r"""
  31. Recursively convert `module` to `qat` mode through :meth:`~.Module.apply`
  32. and set qconfig relatively.
  33. :param module: root module to do convert recursively.
  34. :param qconfig: a instance of :class:`~.QConfig` to be set as submodules' qconfig.
  35. default is :any:`~.qconfig.ema_fakequant_qconfig`.
  36. """
  37. def fn(mod: Module):
  38. if isinstance(mod, QATModule):
  39. mod.set_qat_mode(QATModule.QATMode.QAT)
  40. mod.set_qconfig(qconfig)
  41. module.apply(fn)
  42. def disable_fake_quant(module: Module):
  43. r"""
  44. Recursively disable `module` fake quantization in QATModule through :meth:`~.Module.apply`
  45. :param module: root module to do disable fake quantization recursively.
  46. """
  47. def fn(mod):
  48. if isinstance(mod, QATModule):
  49. mod.act_fake_quant.disable()
  50. mod.weight_fake_quant.disable()
  51. module.apply(fn)
  52. def disable_observer(module: Module):
  53. r"""
  54. Recursively disable `module` observer in QATModule through :meth:`~.Module.apply`
  55. :param module: root module to do disable observer recursively.
  56. """
  57. def fn(mod):
  58. if isinstance(mod, QATModule):
  59. mod.act_observer.disable()
  60. mod.weight_observer.disable()
  61. module.apply(fn)
  62. def enable_fake_quant(module: Module):
  63. r"""
  64. Recursively enable `module` fake quantization in QATModule through :meth:`~.Module.apply`
  65. :param module: root module to do enable fake quantization recursively.
  66. """
  67. def fn(mod):
  68. if isinstance(mod, QATModule):
  69. mod.act_fake_quant.enable()
  70. mod.weight_fake_quant.enable()
  71. module.apply(fn)
  72. def enable_observer(module: Module):
  73. r"""
  74. Recursively enable `module` observer in QATModule through :meth:`~.Module.apply`
  75. :param module: root module to do enable observer recursively.
  76. """
  77. def fn(mod):
  78. if isinstance(mod, QATModule):
  79. mod.act_observer.enable()
  80. mod.weight_observer.enable()
  81. module.apply(fn)

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