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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 typing import Tuple, Union
  9. import megengine._internal as mgb
  10. from ... import module as Float
  11. from ...core import Parameter
  12. from ...functional import conv_bias_activation
  13. from ..qat import conv_bn_relu as QAT
  14. from .module import QuantizedModule
  15. class _ConvBnActivation2d(Float.Conv2d, QuantizedModule):
  16. r"""Applies a 2D convolution over an quantized input tensor, inference only.
  17. The parameter is same with :class: `~.Conv2d`
  18. """
  19. def __init__(
  20. self,
  21. in_channels: int,
  22. out_channels: int,
  23. kernel_size: Union[int, Tuple[int, int]],
  24. stride: Union[int, Tuple[int, int]] = 1,
  25. padding: Union[int, Tuple[int, int]] = 0,
  26. dilation: Union[int, Tuple[int, int]] = 1,
  27. groups: int = 1,
  28. conv_mode: str = "CROSS_CORRELATION",
  29. compute_mode: str = "DEFAULT",
  30. dtype=None,
  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.output_dtype = dtype
  45. def calc_conv_quantized(self, inp, nonlinear_mode="IDENTITY"):
  46. inp_scale = mgb.dtype.get_scale(inp.dtype)
  47. w_scale = mgb.dtype.get_scale(self.weight.dtype)
  48. bias_scale = inp_scale * w_scale
  49. return conv_bias_activation(
  50. inp,
  51. self.weight,
  52. self.bias.astype(mgb.dtype.qint32(bias_scale)),
  53. self.output_dtype,
  54. self.stride,
  55. self.padding,
  56. self.dilation,
  57. self.groups,
  58. conv_mode=self.conv_mode,
  59. compute_mode=self.compute_mode,
  60. nonlinear_mode=nonlinear_mode,
  61. )
  62. @classmethod
  63. def from_qat_module(cls, qat_module: QAT._ConvBnActivation2d):
  64. r"""
  65. return a :class:`~.QuantizedModule` instance converted from a
  66. :class:`~.QATModule` instance.
  67. """
  68. output_dtype = qat_module.get_activation_dtype()
  69. qconv = cls(
  70. qat_module.conv.in_channels,
  71. qat_module.conv.out_channels,
  72. qat_module.conv.kernel_size,
  73. qat_module.conv.stride,
  74. qat_module.conv.padding,
  75. qat_module.conv.dilation,
  76. qat_module.conv.groups,
  77. dtype=output_dtype,
  78. )
  79. w_fold, b_fold = qat_module.fold_weight_bias(
  80. qat_module.bn.running_mean, qat_module.bn.running_var
  81. )
  82. weight = w_fold.astype(qat_module.get_weight_dtype())
  83. qconv.weight = Parameter(weight.numpy())
  84. qconv.bias = Parameter(b_fold.numpy())
  85. return qconv
  86. class ConvBn2d(_ConvBnActivation2d):
  87. r"""quantized version of :class:`~.qat.conv_bn_relu.ConvBn2d`."""
  88. def forward(self, inp):
  89. return self.calc_conv_quantized(inp, nonlinear_mode="IDENTITY")
  90. class ConvBnRelu2d(_ConvBnActivation2d):
  91. r"""quantized version of :class:`~.qat.conv_bn_relu.ConvBnRelu2d`."""
  92. def forward(self, inp):
  93. return self.calc_conv_quantized(inp, nonlinear_mode="RELU")

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