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.

conv_bn_relu.py 3.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 typing import Tuple, Union
  10. import megengine._internal as mgb
  11. from ... import module as Float
  12. from ...core import Parameter
  13. from ...functional import conv_bias_activation
  14. from ...module import Conv2d
  15. from ...quantization.utils import register_method_to_class
  16. class _ConvBnActivation2d(Conv2d):
  17. r"""Applies a 2D convolution over an quantized input tensor, inference only.
  18. The parameter is same with :class: `~.Conv2d`
  19. """
  20. def __init__(
  21. self,
  22. in_channels: int,
  23. out_channels: int,
  24. kernel_size: Union[int, Tuple[int, int]],
  25. stride: Union[int, Tuple[int, int]] = 1,
  26. padding: Union[int, Tuple[int, int]] = 0,
  27. dilation: Union[int, Tuple[int, int]] = 1,
  28. groups: int = 1,
  29. conv_mode: str = "CROSS_CORRELATION",
  30. compute_mode: str = "DEFAULT",
  31. ):
  32. super().__init__(
  33. in_channels,
  34. out_channels,
  35. kernel_size,
  36. stride,
  37. padding,
  38. dilation,
  39. groups,
  40. True,
  41. conv_mode,
  42. compute_mode,
  43. )
  44. self.scale = 1.0
  45. self.zero_point = 0.0
  46. self.output_dtype = mgb.dtype.qint8(self.scale)
  47. self.weight = self.weight.astype(self.output_dtype)
  48. self.bias = self.bias.astype(mgb.dtype.qint32(self.scale))
  49. def calc_conv_quantized(self, inp, nonlinear_mode="IDENTITY"):
  50. inp_scale = mgb.dtype.get_scale(inp.dtype)
  51. w_scale = mgb.dtype.get_scale(self.weight.dtype)
  52. bias_scale = inp_scale * w_scale
  53. return conv_bias_activation(
  54. inp,
  55. self.weight,
  56. self.bias.astype(mgb.dtype.qint32(bias_scale)),
  57. self.output_dtype,
  58. self.stride,
  59. self.padding,
  60. self.dilation,
  61. self.groups,
  62. conv_mode=self.conv_mode,
  63. compute_mode=self.compute_mode,
  64. nonlinear_mode=nonlinear_mode,
  65. )
  66. class ConvBn2d(_ConvBnActivation2d):
  67. def forward(self, inp):
  68. if self.training:
  69. raise ValueError("quantized module only support inference.")
  70. return self.calc_conv_quantized(inp, nonlinear_mode="IDENTITY")
  71. class ConvBnRelu2d(_ConvBnActivation2d):
  72. def forward(self, inp):
  73. if self.training:
  74. raise ValueError("quantized module only support inference.")
  75. return self.calc_conv_quantized(inp, nonlinear_mode="RELU")
  76. def to_quantized(quantized_class, float_module):
  77. qconv = quantized_class(
  78. float_module.conv.in_channels,
  79. float_module.conv.out_channels,
  80. float_module.conv.kernel_size,
  81. float_module.conv.stride,
  82. float_module.conv.padding,
  83. float_module.conv.dilation,
  84. float_module.conv.groups,
  85. )
  86. w_fold, b_fold = float_module.fold_weight_bias(
  87. float_module.bn.running_mean, float_module.bn.running_var
  88. )
  89. weight = w_fold.astype(float_module.weight_observer.get_dtype())
  90. qconv.output_dtype = float_module.act_observer.get_dtype()
  91. qconv.weight = Parameter(weight.numpy())
  92. qconv.bias = Parameter(b_fold.numpy())
  93. qconv.scale, qconv.zero_point = float_module.act_observer.get_qparams()
  94. return qconv
  95. # replace :class:`~.module.QATModule`'s ``to_quantized`` method.
  96. # implemented here to avoid circular import.
  97. register_method_to_class(Float.ConvBn2d)(partial(to_quantized, ConvBn2d))
  98. register_method_to_class(Float.ConvBnRelu2d)(partial(to_quantized, ConvBnRelu2d))

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