QuantizedOps.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. #pragma once
  2. #include <ATen/core/Tensor.h>
  3. #include <ATen/core/IListRef.h>
  4. #include <ATen/Dispatch.h>
  5. #include <ATen/TensorIterator.h>
  6. #include <ATen/native/Activation.h>
  7. #include <ATen/native/DispatchStub.h>
  8. namespace at {
  9. namespace native {
  10. using qrelu_fn = void (*)(const at::Tensor& /*qx*/, at::Tensor& /*qy*/);
  11. using qrelu_leaky_fn = void (*)(Tensor& /*out*/, const Tensor& /*qx*/,
  12. const Scalar& /*negval_*/);
  13. using qgelu_fn = void (*)(const at::Tensor& /*qx*/, at::Tensor& /*qy*/, GeluType /* approximate */);
  14. using qsigmoid_fn = void (*)(const at::Tensor& /*qx*/, at::Tensor& /*qy*/, double output_scale, int64_t output_zero_point);
  15. using qhardsigmoid_fn = void (*)(const at::Tensor& /*qx*/, at::Tensor& /*qy*/);
  16. using qclamp_fn = void (*)(
  17. const at::Tensor& /*qx*/,
  18. const Scalar& min,
  19. const Scalar& max,
  20. at::Tensor& /*qy*/);
  21. using qclamp_minmax_fn = void (*)(
  22. const at::Tensor& /*qx*/,
  23. const Scalar& /*min or max*/,
  24. at::Tensor& /*qy*/);
  25. using qthreshold_fn = void (*)(
  26. const at::Tensor& /*qx*/,
  27. const Scalar& threshold,
  28. const Scalar& value,
  29. at::Tensor& /*qy*/);
  30. using qtanh_fn = void (*)(const at::Tensor& /*qx*/, at::Tensor& /*qy*/);
  31. using qelu_fn = void(*)(
  32. const at::Tensor& /*qx*/,
  33. const Scalar& /*alpha*/,
  34. const Scalar& /*scale*/,
  35. const Scalar& /*input_scale*/,
  36. at::Tensor& /*qy*/);
  37. using qbinary_fn =
  38. void (*)(Tensor& /*out*/, const Tensor& /*self*/, const Tensor& /*other*/);
  39. using qadd_scalar_fn =
  40. void (*)(Tensor& /*out*/, const Tensor& /*self*/, const Scalar& other /*other*/);
  41. using qhardswish_fn = void (*)(const at::Tensor& /*qx*/, at::Tensor& /*qy*/);
  42. using qdropout_fn = void(*)(
  43. const at::Tensor& /*qx*/,
  44. const Scalar& /*p*/,
  45. bool training /*training*/,
  46. at::Tensor& /*qy*/);
  47. using qmaxpool_2d_fn = void (*)(
  48. const Tensor& qx,
  49. int64_t iC, // input/output channels
  50. int64_t iH,
  51. int64_t iW, // input sizes
  52. int64_t oH,
  53. int64_t oW, // output sizes
  54. int64_t kH,
  55. int64_t kW, // kernel size
  56. int64_t sH,
  57. int64_t sW, // strides
  58. int64_t pH,
  59. int64_t pW, // padding
  60. int64_t dH,
  61. int64_t dW, // dilation
  62. Tensor& qy);
  63. using qadaptive_avg_pool2d_fn = void (*)(
  64. const Tensor& qx,
  65. Tensor& qy,
  66. int64_t sizeB,
  67. int64_t sizeC,
  68. int64_t isizeH,
  69. int64_t isizeW,
  70. int64_t osizeH,
  71. int64_t osizeW,
  72. int64_t istrideB,
  73. int64_t istrideC,
  74. int64_t istrideH,
  75. int64_t istrideW);
  76. using qadaptive_avg_pool3d_fn = void (*)(
  77. const Tensor& qx,
  78. Tensor& qy,
  79. int64_t sizeB,
  80. int64_t sizeC,
  81. int64_t isizeD,
  82. int64_t isizeH,
  83. int64_t isizeW,
  84. int64_t osizeD,
  85. int64_t osizeH,
  86. int64_t osizeW,
  87. int64_t istrideB,
  88. int64_t istrideC,
  89. int64_t istrideD,
  90. int64_t istrideH,
  91. int64_t istrideW);
  92. using qavg_pool2d_fn = void (*)(
  93. const Tensor& qx,
  94. Tensor& qy,
  95. int64_t nBatch,
  96. int64_t nInputPlane,
  97. int64_t inputWidth,
  98. int64_t inputHeight,
  99. int64_t outputWidth,
  100. int64_t outputHeight,
  101. int kW,
  102. int kH,
  103. int dW,
  104. int dH,
  105. int padW,
  106. int padH,
  107. bool count_include_pad,
  108. c10::optional<int64_t> divisor_override);
  109. using qavg_pool3d_fn = void (*)(
  110. const Tensor& qx,
  111. Tensor& qy,
  112. int64_t nBatch,
  113. int64_t nInputPlane,
  114. int64_t inputWidth,
  115. int64_t inputHeight,
  116. int64_t inputDepth,
  117. int64_t outputWidth,
  118. int64_t outputHeight,
  119. int64_t outputDepth,
  120. int kW,
  121. int kH,
  122. int kD,
  123. int dW,
  124. int dH,
  125. int dD,
  126. int padW,
  127. int padH,
  128. int padD,
  129. bool count_include_pad,
  130. c10::optional<int64_t> divisor_override);
  131. using qupsample_bilinear2d_fn = void (*)(
  132. Tensor& output,
  133. const Tensor& input,
  134. int64_t input_height,
  135. int64_t input_width,
  136. int64_t output_height,
  137. int64_t output_width,
  138. int64_t nbatch,
  139. int64_t channels,
  140. bool align_corners,
  141. c10::optional<double> scales_h,
  142. c10::optional<double> scales_w);
  143. using qcat_nhwc_fn = Tensor (*)(
  144. const MaterializedITensorListRef& qxs,
  145. int64_t dim,
  146. double scale,
  147. int64_t zero_point);
  148. using qtopk_fn = void(*)(Tensor&, Tensor&, const Tensor&, int64_t, int64_t, bool, bool);
  149. using qbatch_norm_fn = void(*)(int64_t, int64_t, int64_t, int64_t, int64_t, const Tensor&, const Tensor&, const Tensor&, Tensor&);
  150. using qnormalize_fn = void (*)(
  151. const Tensor& /* X */,
  152. const Tensor& /* gamma */,
  153. const Tensor& /* beta */,
  154. bool /* affine_per_channel */,
  155. int /* num_channels */,
  156. int /* num_groups */,
  157. int64_t /* M */,
  158. int64_t /* N */,
  159. double /* eps */,
  160. Tensor* /* Y */);
  161. using qmean_inner_dim_fn = void (*)(
  162. const Tensor& /* X */,
  163. OptionalIntArrayRef /* opt_dim */,
  164. bool /* keepdim */,
  165. c10::optional<ScalarType> /* opt_dtype */,
  166. Tensor& /* Y */);
  167. using qstd_inner_dim_fn = void (*)(
  168. const Tensor& /* X */,
  169. OptionalIntArrayRef /* dim */,
  170. optional<int64_t> /* unbiased */,
  171. bool /* keepdim */,
  172. Tensor& /* Y */);
  173. using qnormalize_nhwc_fn = void (*)(
  174. const Tensor& /* X */,
  175. const Tensor& /* gamma */,
  176. const Tensor& /* beta */,
  177. bool /* affine_per_channel */,
  178. int /* num_channels */,
  179. int /* num_groups */,
  180. int64_t /* M */,
  181. int64_t /* N */,
  182. double /* eps */,
  183. Tensor* /* Y */);
  184. using qprelu_fn = void (*)(Tensor& /*out*/, const Tensor& /*qx*/,
  185. const Tensor& /*qw*/);
  186. DECLARE_DISPATCH(qadaptive_avg_pool2d_fn, qadaptive_avg_pool2d_nhwc_stub);
  187. DECLARE_DISPATCH(qadaptive_avg_pool3d_fn, qadaptive_avg_pool3d_ndhwc_stub);
  188. DECLARE_DISPATCH(qadd_scalar_fn, qadd_scalar_relu_stub);
  189. DECLARE_DISPATCH(qadd_scalar_fn, qadd_scalar_stub);
  190. DECLARE_DISPATCH(qavg_pool2d_fn, qavg_pool2d_nhwc_stub);
  191. DECLARE_DISPATCH(qavg_pool3d_fn, qavg_pool3d_nhwc_stub);
  192. DECLARE_DISPATCH(qbatch_norm_fn, qbatch_norm_relu_stub);
  193. DECLARE_DISPATCH(qbatch_norm_fn, qbatch_norm_stub);
  194. DECLARE_DISPATCH(qbinary_fn, qadd_relu_stub);
  195. DECLARE_DISPATCH(qbinary_fn, qadd_stub);
  196. DECLARE_DISPATCH(qbinary_fn, qmul_relu_stub);
  197. DECLARE_DISPATCH(qbinary_fn, qmul_stub);
  198. DECLARE_DISPATCH(qcat_nhwc_fn, qcat_nhwc_stub);
  199. DECLARE_DISPATCH(qcat_nhwc_fn, qcat_relu_nhwc_stub);
  200. DECLARE_DISPATCH(qclamp_fn, qclamp_stub);
  201. DECLARE_DISPATCH(qclamp_minmax_fn, qclamp_min_stub);
  202. DECLARE_DISPATCH(qclamp_minmax_fn, qclamp_max_stub);
  203. DECLARE_DISPATCH(qelu_fn, qelu_stub);
  204. DECLARE_DISPATCH(qhardsigmoid_fn, qhardsigmoid_stub);
  205. DECLARE_DISPATCH(qhardswish_fn, qhardswish_stub);
  206. DECLARE_DISPATCH(qdropout_fn, qdropout_stub);
  207. DECLARE_DISPATCH(qmaxpool_2d_fn, qmaxpool_2d_nhwc_stub);
  208. DECLARE_DISPATCH(qnormalize_fn, quantized_normalize_stub);
  209. DECLARE_DISPATCH(qnormalize_nhwc_fn, quantized_groupnorm_nhwc_stub);
  210. DECLARE_DISPATCH(qrelu_fn, qrelu_stub);
  211. DECLARE_DISPATCH(qrelu_leaky_fn, qrelu_leaky_stub);
  212. DECLARE_DISPATCH(qgelu_fn, qgelu_stub);
  213. DECLARE_DISPATCH(qsigmoid_fn, qsigmoid_stub);
  214. DECLARE_DISPATCH(qtanh_fn, qtanh_stub);
  215. DECLARE_DISPATCH(qthreshold_fn, qthreshold_stub);
  216. DECLARE_DISPATCH(qtopk_fn, qtopk_stub);
  217. DECLARE_DISPATCH(qupsample_bilinear2d_fn, qupsample_bilinear2d_nhwc_stub);
  218. DECLARE_DISPATCH(qmean_inner_dim_fn, qmean_inner_dim_stub);
  219. DECLARE_DISPATCH(qstd_inner_dim_fn, qstd_inner_dim_stub);
  220. DECLARE_DISPATCH(qprelu_fn, qprelu_stub);
  221. } // namespace native
  222. } // namespace at