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.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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.scale = None
  23. self.weight_observer = None # type: Observer
  24. self.act_observer = None # type: Observer
  25. self.weight_fake_quant = None # type: FakeQuantize
  26. self.act_fake_quant = None # type: FakeQuantize
  27. def set_qconfig(self, qconfig: QConfig):
  28. r"""
  29. Set quantization related configs with ``qconfig``, including
  30. observer and fake_quant for weight and activation.
  31. """
  32. self.weight_observer = qconfig.weight_observer()
  33. self.act_observer = qconfig.act_observer()
  34. if qconfig.fake_quant is None:
  35. self.weight_fake_quant = None
  36. self.act_fake_quant = None
  37. else:
  38. self.weight_fake_quant = qconfig.fake_quant(self.weight_observer.dtype)
  39. self.act_fake_quant = qconfig.fake_quant(self.act_observer.dtype)
  40. def _apply_fakequant_with_observer(
  41. self, target: Tensor, fake_quant: FakeQuantize, observer: Observer
  42. ):
  43. oup = observer(target)
  44. if fake_quant is None:
  45. return oup
  46. else:
  47. q_dict = observer.get_qparams()
  48. return fake_quant(oup, q_dict)
  49. def apply_quant_weight(self, target: Tensor):
  50. r"""
  51. Apply weight's observer and fake_quant from ``qconfig`` on ``target``.
  52. """
  53. return self._apply_fakequant_with_observer(
  54. target, self.weight_fake_quant, self.weight_observer
  55. )
  56. def apply_quant_activation(self, target: Tensor):
  57. r"""
  58. Apply weight's observer and fake_quant from ``qconfig`` on ``target``.
  59. """
  60. return self._apply_fakequant_with_observer(
  61. target, self.act_fake_quant, self.act_observer
  62. )
  63. def get_weight_dtype(self):
  64. r"""
  65. Get weight's quantization dtype as the method from ``qconfig``.
  66. """
  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. return self.act_observer.get_dtype()
  73. @classmethod
  74. @abstractmethod
  75. def from_float_module(cls, float_module: Module):
  76. r"""
  77. Return a :class:`~.QATModule` instance converted from
  78. a float :class:`~.Module` instance.
  79. """

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