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.

utils.py 4.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 enum import Enum
  9. from functools import partial, update_wrapper, wraps
  10. from typing import Dict
  11. import numpy as np
  12. from .. import functional as F
  13. from ..core._imperative_rt.core2 import apply
  14. from ..core.autodiff.grad import Function
  15. from ..core.ops import builtin
  16. from ..core.tensor import megbrain_graph
  17. from ..core.tensor.dtype import _metadata_dict
  18. from ..tensor import Tensor
  19. class Round(Function):
  20. """
  21. The functional round have no grad and can not use for quantization-aware-training.
  22. We use Function and STE(Straight-Through Estimator) to implement backward propagation.
  23. """
  24. def forward(self, x):
  25. return F.round(x)
  26. def backward(self, output_grads):
  27. return output_grads
  28. def tqt_forward(qmin, qmax, inp, scale):
  29. op = builtin.TQT(qmin=qmin, qmax=qmax)
  30. (output,) = apply(op, inp, scale)
  31. return output
  32. def register_method_to_class(cls):
  33. def decorator(func):
  34. @wraps(func)
  35. def wrapper(self, *args, **kwargs):
  36. return func(self, *args, **kwargs)
  37. if isinstance(func, partial):
  38. update_wrapper(func, func.func)
  39. setattr(cls, func.__name__, wrapper)
  40. return func
  41. return decorator
  42. class QuantMode(Enum):
  43. """
  44. Quantization mode enumerate class.
  45. """
  46. SYMMERTIC = 1
  47. ASYMMERTIC = 2
  48. qparam_dict = {
  49. QuantMode.SYMMERTIC: {"mode": QuantMode.SYMMERTIC, "scale": None},
  50. QuantMode.ASYMMERTIC: {
  51. "mode": QuantMode.ASYMMERTIC,
  52. "scale": None,
  53. "zero_point": None,
  54. },
  55. }
  56. def get_qparam_dict(mode: QuantMode):
  57. """
  58. Return the quantization parameters dictionary according to the mode.
  59. """
  60. return qparam_dict.get(mode, None)
  61. def fake_quant_tensor(inp: Tensor, qmin: int, qmax: int, q_dict: Dict) -> Tensor:
  62. """
  63. Apply fake quantization to the inp tensor.
  64. :param inp: the input tensor which need to be faked.
  65. :param qmin: the minimum value which the integer limit to.
  66. :param qmax: the maximum value which the integer limit to.
  67. :param q_dict: the quantization parameter dict.
  68. """
  69. scale = q_dict["scale"]
  70. zero_point = Tensor([0.0], dtype=np.float32)
  71. if q_dict["mode"] == QuantMode.ASYMMERTIC:
  72. zero_point = q_dict["zero_point"]
  73. assert isinstance(inp, (Tensor, megbrain_graph.VarNode)), "inp must be Tensor type"
  74. assert isinstance(
  75. scale, (Tensor, megbrain_graph.VarNode)
  76. ), "scale must be Tensor type"
  77. assert isinstance(
  78. zero_point, (Tensor, megbrain_graph.VarNode)
  79. ), "zero point must be Tensor type"
  80. op = builtin.FakeQuant(qmin=qmin, qmax=qmax)
  81. return apply(op, inp, scale, zero_point)[0]
  82. def fake_quant_bias(bias: Tensor, inp: Tensor, w_qat: Tensor) -> Tensor:
  83. """
  84. Apply fake quantization to bias, with the special scale from input tensor
  85. and weight tensor, the quantized type set to qint32 also.
  86. :param bias: the bias tensor which need to be faked.
  87. :param inp: the input tensor which contain the quantization parameters.
  88. :param qmax: the weight tensor which contain the quantization parameters.
  89. .. warning::
  90. Only work for symmetric quantization method now.
  91. """
  92. b_qat = bias
  93. if hasattr(inp, "q_dict") and b_qat is not None:
  94. if inp.q_dict["scale"] is not None and w_qat.q_dict["scale"] is not None:
  95. # use the same mode with weight.
  96. b_dict = get_qparam_dict(w_qat.q_dict["mode"])
  97. b_dict["scale"] = inp.q_dict["scale"] * w_qat.q_dict["scale"]
  98. # TODO: add zero_point for ASYMMERTIC mode.
  99. qmax = _metadata_dict["qint32"].qmax
  100. qmin = _metadata_dict["qint32"].qmin
  101. b_qat = fake_quant_tensor(b_qat, qmin, qmax, b_dict)
  102. b_qat.q_dict.update(b_dict)
  103. return b_qat

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