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.

quantized.py 3.2 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. # pylint: disable=too-many-lines
  9. from typing import Tuple, Union
  10. from ..core.ops import builtin
  11. from ..core.tensor.core import apply
  12. from ..tensor import Tensor
  13. from .debug_param import get_conv_execution_strategy
  14. from .types import _pair, _pair_nonzero
  15. def conv_bias_activation(
  16. inp: Tensor,
  17. weight: Tensor,
  18. bias: Tensor,
  19. dtype=None,
  20. stride: Union[int, Tuple[int, int]] = 1,
  21. padding: Union[int, Tuple[int, int]] = 0,
  22. dilation: Union[int, Tuple[int, int]] = 1,
  23. groups: int = 1,
  24. nonlinear_mode="IDENTITY",
  25. conv_mode="CROSS_CORRELATION",
  26. compute_mode="DEFAULT",
  27. ) -> Tensor:
  28. """Convolution bias with activation operation, only for inference.
  29. :param inp: feature map of the convolution operation.
  30. :param weight: convolution kernel.
  31. :param bias: bias added to the result of convolution
  32. :param stride: stride of the 2D convolution operation. Default: 1
  33. :param padding: size of the paddings added to the input on both sides of its spatial dimensions. Only zero-padding is supported. Default: 0
  34. :param dilation: dilation of the 2D convolution operation. Default: 1
  35. :param groups: number of groups into which the input and output channels are divided, so as to perform a "grouped convolution". When ``groups`` is not 1,
  36. ``in_channels`` and ``out_channels`` must be divisible by ``groups``,
  37. and the shape of weight should be `(groups, out_channel // groups,
  38. in_channels // groups, height, width)`.
  39. :type conv_mode: string or :class:`P.Convolution.Mode`.
  40. :param conv_mode: supports 'CROSS_CORRELATION' or 'CONVOLUTION'. Default:
  41. 'CROSS_CORRELATION'
  42. :param dtype: support for ``np.dtype``, Default: np.int8
  43. :param scale: scale if use quantization, Default: 0.0
  44. :param zero_point: scale if use quantization quint8, Default: 0.0
  45. :type compute_mode: string or
  46. :class:`P.Convolution.ComputeMode`.
  47. :param compute_mode: when set to "DEFAULT", no special requirements will be
  48. placed on the precision of intermediate results. When set to "FLOAT32",
  49. "Float32" would be used for accumulator and intermediate result, but only effective when input and output are of Float16 dtype.
  50. """
  51. ph, pw = _pair(padding)
  52. sh, sw = _pair_nonzero(stride)
  53. dh, dw = _pair_nonzero(dilation)
  54. sparse_type = "DENSE" if groups == 1 else "GROUP"
  55. op = builtin.ConvBiasForward(
  56. stride_h=sh,
  57. stride_w=sw,
  58. pad_h=ph,
  59. pad_w=pw,
  60. dilate_h=dh,
  61. dilate_w=dw,
  62. dtype=dtype,
  63. format="NCHW",
  64. strategy=get_conv_execution_strategy(),
  65. nonlineMode=nonlinear_mode,
  66. mode=conv_mode,
  67. compute_mode=compute_mode,
  68. sparse=sparse_type,
  69. )
  70. (outputs,) = apply(op, inp, weight, bias)
  71. return outputs

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