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.

qconfig.py 4.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 functools import partial
  9. from ..module import Module
  10. from .fake_quant import TQT, FakeQuantize
  11. from .observer import (
  12. ExponentialMovingAverageObserver,
  13. HistogramObserver,
  14. MinMaxObserver,
  15. )
  16. class QConfig:
  17. r"""
  18. A config class indicating how to do quantize toward :class:`~.QATModule`'s
  19. ``activation`` and ``weight``. See :meth:`~.QATModule.set_qconfig` for detail usage.
  20. :param weight_observer: interface to instantiate an :class:`~.Observer` indicating
  21. how to collect scales and zero_point of wegiht.
  22. :param act_observer: similar to ``weight_observer`` but toward activation.
  23. :param weight_fake_quant: interface to instantiate a :class:`~.FakeQuantize` indicating
  24. how to do fake_quant calculation.
  25. :param act_observer: similar to ``weight_fake_quant`` but toward activation.
  26. Examples:
  27. .. code-block::
  28. # Default EMA QConfig for QAT.
  29. ema_fakequant_qconfig = QConfig(
  30. weight_observer=partial(MinMaxObserver, dtype="qint8", narrow_range=True),
  31. act_observer=partial(ExponentialMovingAverageObserver, dtype="qint8", narrow_range=False),
  32. weight_fake_quant=partial(FakeQuantize, dtype="qint8", narrow_range=True),
  33. act_fake_quant=partial(FakeQuantize, dtype="qint8", narrow_range=False),
  34. )
  35. Each parameter is a ``class`` rather than an instance. And we recommand using ``functools.partial``
  36. to add initialization parameters of the ``class``, so that don't need to provide parameters in
  37. :meth:`~.QATModule.set_qconfig`.
  38. Usually we set ``narrow_range`` of weight related paramters to ``True`` and of activation related
  39. parameters to ``False``. For the result of multiplication and addition as ``a * b + c * d``, if
  40. four variables are all -128 of dtype ``qint8``, then the result will be ``2^15`` and cause overflow.
  41. Weights are commonly calculated in this way, so needed to narrow the range.
  42. """
  43. def __init__(
  44. self, weight_observer, act_observer, weight_fake_quant, act_fake_quant
  45. ):
  46. if isinstance(act_observer, Module) or isinstance(weight_observer, Module):
  47. raise ValueError(
  48. "QConfig must not receive observer instance, please pass observer"
  49. " class generator using `partial(Observer, ...)` instead. Use"
  50. " partial(MyObserver, x=1) to override arguments to constructor if needed"
  51. )
  52. self.weight_observer = weight_observer
  53. self.act_observer = act_observer
  54. self.weight_fake_quant = weight_fake_quant
  55. self.act_fake_quant = act_fake_quant
  56. tqt_quant_qconfig = QConfig(
  57. weight_observer=partial(
  58. ExponentialMovingAverageObserver, dtype="qint8", narrow_range=True
  59. ),
  60. act_observer=partial(
  61. ExponentialMovingAverageObserver, dtype="qint8", narrow_range=False
  62. ),
  63. weight_fake_quant=partial(TQT, dtype="qint8", narrow_range=True),
  64. act_fake_quant=partial(TQT, dtype="qint8", narrow_range=False),
  65. )
  66. min_max_fakequant_qconfig = QConfig(
  67. weight_observer=partial(MinMaxObserver, dtype="qint8", narrow_range=True),
  68. act_observer=partial(MinMaxObserver, dtype="qint8", narrow_range=False),
  69. weight_fake_quant=partial(FakeQuantize, dtype="qint8", narrow_range=True),
  70. act_fake_quant=partial(FakeQuantize, dtype="qint8", narrow_range=False),
  71. )
  72. ema_fakequant_qconfig = QConfig(
  73. weight_observer=partial(MinMaxObserver, dtype="qint8", narrow_range=True),
  74. act_observer=partial(
  75. ExponentialMovingAverageObserver, dtype="qint8", narrow_range=False
  76. ),
  77. weight_fake_quant=partial(FakeQuantize, dtype="qint8", narrow_range=True),
  78. act_fake_quant=partial(FakeQuantize, dtype="qint8", narrow_range=False),
  79. )
  80. calibration_qconfig = QConfig(
  81. weight_observer=partial(MinMaxObserver, dtype="qint8", narrow_range=True),
  82. act_observer=partial(HistogramObserver, dtype="qint8", narrow_range=False),
  83. weight_fake_quant=None,
  84. act_fake_quant=None,
  85. )

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