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 7.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. # pylint: disable=too-many-lines
  2. from typing import Tuple, Union
  3. from ..core import _config
  4. from ..core._imperative_rt.core2 import apply
  5. from ..core.ops import builtin
  6. from ..tensor import Tensor
  7. from ..utils.tuple_function import _pair, _pair_nonzero
  8. from .debug_param import get_execution_strategy
  9. def conv_bias_activation(
  10. inp: Tensor,
  11. weight: Tensor,
  12. bias: Tensor,
  13. dtype=None,
  14. stride: Union[int, Tuple[int, int]] = 1,
  15. padding: Union[int, Tuple[int, int]] = 0,
  16. dilation: Union[int, Tuple[int, int]] = 1,
  17. groups: int = 1,
  18. nonlinear_mode="identity",
  19. conv_mode="cross_correlation",
  20. compute_mode="default",
  21. ) -> Tensor:
  22. r"""Convolution bias with activation operation, only for inference.
  23. Args:
  24. inp: feature map of the convolution operation.
  25. weight: convolution kernel.
  26. bias: bias added to the result of convolution
  27. stride: stride of the 2D convolution operation. Default: 1
  28. padding: size of the paddings added to the input on both sides
  29. of its spatial dimensions. Only zero-padding is supported. Default: 0
  30. dilation: dilation of the 2D convolution operation. Default: 1
  31. groups: number of groups into which the input and output channels are divided,
  32. so as to perform a "grouped convolution". When ``groups`` is not 1,
  33. ``in_channels`` and ``out_channels`` must be divisible by ``groups``,
  34. and the shape of weight should be `(groups, out_channel // groups,
  35. in_channels // groups, height, width)`.
  36. conv_mode: supports 'cross_correlation' or 'convolution'. Default:
  37. 'cross_correlation'
  38. dtype: support for ``np.dtype``, Default: np.int8
  39. compute_mode: when set to "default", no special requirements will be
  40. placed on the precision of intermediate results. When set to "float32",
  41. "float32" would be used for accumulator and intermediate result,
  42. but only effective when input and output are of float16 dtype.
  43. """
  44. ph, pw = _pair(padding)
  45. sh, sw = _pair_nonzero(stride)
  46. dh, dw = _pair_nonzero(dilation)
  47. sparse_type = "dense" if groups == 1 else "group"
  48. compute_mode = _config._get_actual_op_param(compute_mode, _config.__compute_mode)
  49. op = builtin.ConvBias(
  50. stride_h=sh,
  51. stride_w=sw,
  52. pad_h=ph,
  53. pad_w=pw,
  54. dilate_h=dh,
  55. dilate_w=dw,
  56. dtype=dtype,
  57. strategy=get_execution_strategy(),
  58. nonlineMode=nonlinear_mode,
  59. mode=conv_mode,
  60. compute_mode=compute_mode,
  61. sparse=sparse_type,
  62. )
  63. (outputs,) = apply(op, inp, weight, bias)
  64. return outputs
  65. def batch_conv_bias_activation(
  66. inp: Tensor,
  67. weight: Tensor,
  68. bias: Tensor,
  69. dtype=None,
  70. stride: Union[int, Tuple[int, int]] = 1,
  71. padding: Union[int, Tuple[int, int]] = 0,
  72. dilation: Union[int, Tuple[int, int]] = 1,
  73. groups: int = 1,
  74. nonlinear_mode="identity",
  75. conv_mode="cross_correlation",
  76. compute_mode="default",
  77. ) -> Tensor:
  78. r"""Batch convolution bias with activation operation, only for inference.
  79. Args:
  80. inp: feature map of the convolution operation.
  81. weight: convolution kernel in batched way.
  82. bias: bias added to the result of convolution
  83. stride: stride of the 2D convolution operation. Default: 1
  84. padding: size of the paddings added to the input on both sides
  85. of its spatial dimensions. Only zero-padding is supported. Default: 0
  86. dilation: dilation of the 2D convolution operation. Default: 1
  87. groups: number of groups into which the input and output channels are divided,
  88. so as to perform a "grouped convolution". When ``groups`` is not 1,
  89. ``in_channels`` and ``out_channels`` must be divisible by ``groups``,
  90. and the shape of weight should be `(groups, out_channel // groups,
  91. in_channels // groups, height, width)`.
  92. conv_mode: supports 'cross_correlation' or 'convolution'. Default:
  93. 'cross_correlation'
  94. dtype: support for ``np.dtype``, Default: np.int8
  95. compute_mode: when set to "default", no special requirements will be
  96. placed on the precision of intermediate results. When set to "float32",
  97. "float32" would be used for accumulator and intermediate result,
  98. but only effective when input and output are of float16 dtype.
  99. """
  100. ph, pw = _pair(padding)
  101. sh, sw = _pair_nonzero(stride)
  102. dh, dw = _pair_nonzero(dilation)
  103. sparse_type = "dense" if groups == 1 else "group"
  104. compute_mode = _config._get_actual_op_param(compute_mode, _config.__compute_mode)
  105. op = builtin.BatchConvBias(
  106. stride_h=sh,
  107. stride_w=sw,
  108. pad_h=ph,
  109. pad_w=pw,
  110. dilate_h=dh,
  111. dilate_w=dw,
  112. dtype=dtype,
  113. strategy=get_execution_strategy(),
  114. nonlineMode=nonlinear_mode,
  115. mode=conv_mode,
  116. compute_mode=compute_mode,
  117. sparse=sparse_type,
  118. )
  119. (outputs,) = apply(op, inp, weight, bias)
  120. return outputs
  121. def conv_transpose2d(
  122. inp: Tensor,
  123. weight: Tensor,
  124. bias: Tensor = None,
  125. dtype=None,
  126. stride: Union[int, Tuple[int, int]] = 1,
  127. padding: Union[int, Tuple[int, int]] = 0,
  128. output_padding: Union[int, Tuple[int, int]] = 0,
  129. dilation: Union[int, Tuple[int, int]] = 1,
  130. groups: int = 1,
  131. conv_mode="cross_correlation",
  132. compute_mode="default",
  133. ) -> Tensor:
  134. assert (
  135. conv_mode.lower() == "cross_correlation"
  136. or conv_mode.name == "CROSS_CORRELATION"
  137. )
  138. assert compute_mode.lower() == "default" or compute_mode.name == "DEFAULT"
  139. if groups != 1:
  140. raise NotImplementedError(
  141. "group quantized transposed conv2d is not supported yet."
  142. )
  143. if bias is not None:
  144. raise NotImplementedError(
  145. "bias of quantized transposed conv2d is not supported yet."
  146. )
  147. pad_h, pad_w = _pair(padding)
  148. output_pad_h, output_pad_w = _pair(output_padding)
  149. stride_h, stride_w = _pair_nonzero(stride)
  150. dilate_h, dilate_w = _pair_nonzero(dilation)
  151. compute_mode = _config._get_actual_op_param(compute_mode, _config.__compute_mode)
  152. # should be replaced by Op with bias such as ConvolutionBackwardDataBias
  153. op = builtin.ConvolutionBackwardData(
  154. stride_h=stride_h,
  155. stride_w=stride_w,
  156. pad_h=pad_h,
  157. pad_w=pad_w,
  158. dilate_h=dilate_h,
  159. dilate_w=dilate_w,
  160. strategy=get_execution_strategy(),
  161. dtype=dtype,
  162. compute_mode=compute_mode,
  163. mode=conv_mode,
  164. )
  165. if output_pad_h != 0 or output_pad_h != 0:
  166. assert (
  167. output_pad_h < stride[0]
  168. ), "output_padding[0] shoule be less than stride[0]"
  169. assert (
  170. output_pad_w < stride[1]
  171. ), "output_padding[1] shoule be less than stride[1]"
  172. Hout = (
  173. (inp.shape[2] - 1) * stride[0]
  174. - 2 * padding[0]
  175. + dilation[0] * (weight.shape[2] - 1)
  176. + output_pad_h
  177. + 1
  178. )
  179. Wout = (
  180. (inp.shape[3] - 1) * stride[1]
  181. - 2 * padding[1]
  182. + dilation[1] * (weight.shape[3] - 1)
  183. + output_pad_w
  184. + 1
  185. )
  186. output_shape = [inp.shape[0], weight.shape[1], Hout, Wout]
  187. output_shape = Tensor(output_shape)
  188. (output,) = apply(op, weight, inp, output_shape)
  189. else:
  190. (output,) = apply(op, weight, inp)
  191. return output