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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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 .. import _internal as mgb
  11. from ..core import Tensor, wrap_io_tensor
  12. from ..utils.types import _pair, _pair_nonzero
  13. from .debug_param import get_conv_execution_strategy
  14. @wrap_io_tensor
  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: The feature map of the convolution operation
  30. :param weight: The convolution kernel
  31. :param bias: The 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
  34. spatial dimensions. Only zero-padding is supported. Default: 0
  35. :param dilation: Dilation of the 2D convolution operation. Default: 1
  36. :param groups: number of groups to divide input and output channels into,
  37. so as to perform a "grouped convolution". When ``groups`` is not 1,
  38. ``in_channels`` and ``out_channels`` must be divisible by ``groups``,
  39. and the shape of weight should be ``(groups, out_channel // groups,
  40. in_channels // groups, height, width)``.
  41. :type conv_mode: string or :class:`mgb.opr_param_defs.Convolution.Mode`
  42. :param conv_mode: Supports 'CROSS_CORRELATION' or 'CONVOLUTION'. Default:
  43. 'CROSS_CORRELATION'.
  44. :param dtype: Support for np.dtype, Default:
  45. np.int8.
  46. :param scale: scale if use quantization, Default:
  47. 0.0.
  48. :param zero_point: scale if use quantization quint8, Default:
  49. 0.0.
  50. :type compute_mode: string or
  51. :class:`mgb.opr_param_defs.Convolution.ComputeMode`
  52. :param compute_mode: When set to 'DEFAULT', no special requirements will be
  53. placed on the precision of intermediate results. When set to 'FLOAT32',
  54. Float32 would be used for accumulator and intermediate result, but only
  55. effective when input and output are of Float16 dtype.
  56. """
  57. ph, pw = _pair(padding)
  58. sh, sw = _pair_nonzero(stride)
  59. dh, dw = _pair_nonzero(dilation)
  60. sparse_type = "DENSE" if groups == 1 else "GROUP"
  61. res = mgb.opr.conv_bias_activation(
  62. inp,
  63. weight,
  64. bias,
  65. compute_mode=compute_mode,
  66. dtype=dtype,
  67. strategy=get_conv_execution_strategy(),
  68. nonlineMode=nonlinear_mode,
  69. sparse=sparse_type,
  70. format="NCHW",
  71. pad_h=ph,
  72. pad_w=pw,
  73. stride_h=sh,
  74. stride_w=sw,
  75. dilate_h=dh,
  76. dilate_w=dw,
  77. mode=conv_mode,
  78. )
  79. return res

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