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.

module.py 3.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 abc import abstractmethod
  9. from ...core import Tensor
  10. from ...quantization import FakeQuantize, Observer, QConfig
  11. from ..module import Module
  12. class QATModule(Module):
  13. r"""
  14. Base class of quantized-float related Module, basically for QAT and Calibration.
  15. Use :meth:`~.QATModule.from_float_module` to generate a instance from float :class:`~.Module`.
  16. Or use :func:`~.quantize.quantize_qat` to do it recursively and automatically.
  17. Can also be converted to :class:`~.QuantizedModule` for deployment using
  18. :func:`~.quantize.quantize` further.
  19. """
  20. def __init__(self):
  21. super().__init__()
  22. self.weight_observer = None # type: Observer
  23. self.act_observer = None # type: Observer
  24. self.weight_fake_quant = None # type: FakeQuantize
  25. self.act_fake_quant = None # type: FakeQuantize
  26. def set_qconfig(self, qconfig: QConfig):
  27. r"""
  28. Set quantization related configs with ``qconfig``, including
  29. observer and fake_quant for weight and activation.
  30. """
  31. def safe_call(func):
  32. return func() if func is not None else None
  33. self.weight_observer = safe_call(qconfig.weight_observer)
  34. self.act_observer = safe_call(qconfig.act_observer)
  35. self.weight_fake_quant = safe_call(qconfig.weight_fake_quant)
  36. self.act_fake_quant = safe_call(qconfig.act_fake_quant)
  37. def _apply_fakequant_with_observer(
  38. self, target: Tensor, fake_quant: FakeQuantize, observer: Observer
  39. ):
  40. oup = observer(target)
  41. if fake_quant is None:
  42. return oup
  43. else:
  44. q_dict = observer.get_qparams()
  45. return fake_quant(oup, q_dict)
  46. def apply_quant_weight(self, target: Tensor):
  47. r"""
  48. Apply weight's observer and fake_quant from ``qconfig`` on ``target``.
  49. """
  50. return self._apply_fakequant_with_observer(
  51. target, self.weight_fake_quant, self.weight_observer
  52. )
  53. def apply_quant_activation(self, target: Tensor):
  54. r"""
  55. Apply weight's observer and fake_quant from ``qconfig`` on ``target``.
  56. """
  57. return self._apply_fakequant_with_observer(
  58. target, self.act_fake_quant, self.act_observer
  59. )
  60. def get_weight_dtype(self):
  61. r"""
  62. Get weight's quantization dtype as the method from ``qconfig``.
  63. """
  64. if hasattr(self.act_fake_quant, "get_dtype"):
  65. return self.weight_fake_quant.get_dtype()
  66. else:
  67. return self.weight_observer.get_dtype()
  68. def get_activation_dtype(self):
  69. r"""
  70. Get activation's quantization dtype as the method from ``qconfig``.
  71. """
  72. if hasattr(self.act_fake_quant, "get_dtype"):
  73. return self.act_fake_quant.get_dtype()
  74. else:
  75. return self.act_observer.get_dtype()
  76. @classmethod
  77. @abstractmethod
  78. def from_float_module(cls, float_module: Module):
  79. r"""
  80. Return a :class:`~.QATModule` instance converted from
  81. a float :class:`~.Module` instance.
  82. """

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