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.

fake_quant.py 1.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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 .. import functional as F
  9. from .._internal.dtype import _metadata_dict
  10. from ..module import Module
  11. from .observer import Round
  12. class FakeQuantize(Module):
  13. r"""
  14. A module to do quant and dequant according to observer's scale and zero_point.
  15. """
  16. def __init__(self, dtype: str, enable: bool = True):
  17. super().__init__()
  18. if not dtype in _metadata_dict.keys():
  19. raise ValueError(
  20. "unknown dtype: {}, only support {}".format(
  21. dtype, _metadata_dict.keys()
  22. )
  23. )
  24. self.dtype = dtype
  25. self.qmin = _metadata_dict[dtype].qmin
  26. self.qmax = _metadata_dict[dtype].qmax
  27. self.enabled = enable
  28. def enable(self):
  29. self.enabled = True
  30. def disable(self):
  31. self.enabled = False
  32. def forward(self, inp, scale, zero_point):
  33. if self.enabled:
  34. # Quant
  35. oup = Round()(inp / scale) + zero_point
  36. # clip
  37. oup = F.minimum(F.maximum(oup, self.qmin), self.qmax)
  38. # DeQuant
  39. oup = (oup - zero_point) * scale
  40. return oup
  41. return inp

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