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.

convolution1d_arm.cpp 8.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. // Tencent is pleased to support the open source community by making ncnn available.
  2. //
  3. // Copyright (C) 2021 THL A29 Limited, a Tencent company. All rights reserved.
  4. //
  5. // Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
  6. // in compliance with the License. You may obtain a copy of the License at
  7. //
  8. // https://opensource.org/licenses/BSD-3-Clause
  9. //
  10. // Unless required by applicable law or agreed to in writing, software distributed
  11. // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  12. // CONDITIONS OF ANY KIND, either express or implied. See the License for the
  13. // specific language governing permissions and limitations under the License.
  14. #include "convolution1d_arm.h"
  15. #if __ARM_NEON
  16. #include <arm_neon.h>
  17. #endif // __ARM_NEON
  18. #include "arm_usability.h"
  19. #include "arm_activation.h"
  20. #include "cpu.h"
  21. #include "layer_type.h"
  22. namespace ncnn {
  23. #include "convolution1d_packed.h"
  24. #if NCNN_BF16
  25. #include "convolution1d_packed_bf16s.h"
  26. #endif // NCNN_BF16
  27. Convolution1D_arm::Convolution1D_arm()
  28. {
  29. #if __ARM_NEON
  30. support_packing = true;
  31. #if NCNN_ARM82
  32. support_fp16_storage = cpu_support_arm_asimdhp();
  33. #endif
  34. #endif // __ARM_NEON
  35. #if NCNN_BF16
  36. support_bf16_storage = true;
  37. #endif
  38. }
  39. int Convolution1D_arm::create_pipeline(const Option& opt)
  40. {
  41. if (dynamic_weight)
  42. return 0;
  43. #if NCNN_ARM82
  44. if (support_fp16_storage && opt.use_fp16_storage)
  45. {
  46. return create_pipeline_fp16s(opt);
  47. }
  48. #endif
  49. #if NCNN_BF16
  50. if (opt.use_bf16_storage)
  51. {
  52. return create_pipeline_bf16s(opt);
  53. }
  54. #endif
  55. const int num_input = weight_data_size / kernel_w / num_output;
  56. convolution1d_transform_kernel_packed(weight_data, weight_data_tm, num_input, num_output, kernel_w);
  57. if (opt.lightmode)
  58. weight_data.release();
  59. return 0;
  60. }
  61. int Convolution1D_arm::destroy_pipeline(const Option& /*opt*/)
  62. {
  63. return 0;
  64. }
  65. int Convolution1D_arm::forward(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const
  66. {
  67. int elembits = bottom_blob.elembits();
  68. #if NCNN_ARM82
  69. if (support_fp16_storage && opt.use_fp16_storage && elembits == 16)
  70. {
  71. if (opt.use_fp16_arithmetic)
  72. return forward_fp16sa(bottom_blob, top_blob, opt);
  73. else
  74. return forward_fp16s(bottom_blob, top_blob, opt);
  75. }
  76. #endif
  77. #if NCNN_BF16
  78. if (opt.use_bf16_storage && elembits == 16)
  79. return forward_bf16s(bottom_blob, top_blob, opt);
  80. #endif
  81. int w = bottom_blob.w;
  82. size_t elemsize = bottom_blob.elemsize;
  83. int elempack = bottom_blob.elempack;
  84. const int kernel_extent_w = dilation_w * (kernel_w - 1) + 1;
  85. Mat bottom_blob_bordered;
  86. make_padding(bottom_blob, bottom_blob_bordered, opt);
  87. if (bottom_blob_bordered.empty())
  88. return -100;
  89. w = bottom_blob_bordered.w;
  90. int out_elempack = 1;
  91. #if __ARM_NEON
  92. if (opt.use_packing_layout)
  93. {
  94. out_elempack = num_output % 4 == 0 ? 4 : 1;
  95. }
  96. #endif
  97. size_t out_elemsize = elemsize / elempack * out_elempack;
  98. const int outw = (w - kernel_extent_w) / stride_w + 1;
  99. const int outh = num_output / out_elempack;
  100. top_blob.create(outw, outh, out_elemsize, out_elempack, opt.blob_allocator);
  101. if (top_blob.empty())
  102. return -100;
  103. convolution1d_packed(bottom_blob_bordered, top_blob, weight_data_tm, bias_data, kernel_w, dilation_w, stride_w, activation_type, activation_params, opt);
  104. return 0;
  105. }
  106. int Convolution1D_arm::forward(const std::vector<Mat>& bottom_blobs, std::vector<Mat>& top_blobs, const Option& opt) const
  107. {
  108. const Mat& bottom_blob = bottom_blobs[0];
  109. const Mat& _weight_data = bottom_blobs[1];
  110. Mat& top_blob = top_blobs[0];
  111. const int _kernel_w = _weight_data.w;
  112. const int _num_output = _weight_data.c * _weight_data.elempack;
  113. Mat weight_data_flattened;
  114. flatten(_weight_data, weight_data_flattened, opt);
  115. if (weight_data_flattened.empty())
  116. return -100;
  117. #if NCNN_ARM82
  118. if (opt.use_fp16_storage && cpu_support_arm_asimdhp() && weight_data_flattened.elembits() == 16)
  119. {
  120. Mat weight_data_flattened_fp32;
  121. cast_float16_to_float32(weight_data_flattened, weight_data_flattened_fp32, opt);
  122. weight_data_flattened = weight_data_flattened_fp32;
  123. }
  124. #endif // NCNN_ARM82
  125. #if NCNN_BF16
  126. if (opt.use_bf16_storage && weight_data_flattened.elembits() == 16)
  127. {
  128. Mat weight_data_flattened_fp32;
  129. cast_bfloat16_to_float32(weight_data_flattened, weight_data_flattened_fp32, opt);
  130. weight_data_flattened = weight_data_flattened_fp32;
  131. }
  132. #endif // NCNN_BF16
  133. // weight_data_flattened as pack1
  134. weight_data_flattened.w *= weight_data_flattened.elempack;
  135. weight_data_flattened.elemsize /= weight_data_flattened.elempack;
  136. weight_data_flattened.elempack = 1;
  137. Mat bias_data_flattened;
  138. if (bias_term)
  139. {
  140. const Mat& _bias_data = bottom_blobs[2];
  141. flatten(_bias_data, bias_data_flattened, opt);
  142. if (bias_data_flattened.empty())
  143. return -100;
  144. #if NCNN_ARM82
  145. if (opt.use_fp16_storage && cpu_support_arm_asimdhp() && bias_data_flattened.elembits() == 16)
  146. {
  147. Mat bias_data_flattened_fp32;
  148. cast_float16_to_float32(bias_data_flattened, bias_data_flattened_fp32, opt);
  149. bias_data_flattened = bias_data_flattened_fp32;
  150. }
  151. #endif // NCNN_ARM82
  152. #if NCNN_BF16
  153. if (opt.use_bf16_storage && bias_data_flattened.elembits() == 16)
  154. {
  155. Mat bias_data_flattened_fp32;
  156. cast_bfloat16_to_float32(bias_data_flattened, bias_data_flattened_fp32, opt);
  157. bias_data_flattened = bias_data_flattened_fp32;
  158. }
  159. #endif // NCNN_BF16
  160. // bias_data_flattened as pack1
  161. bias_data_flattened.w *= bias_data_flattened.elempack;
  162. bias_data_flattened.elemsize /= bias_data_flattened.elempack;
  163. bias_data_flattened.elempack = 1;
  164. }
  165. ncnn::Layer* op = ncnn::create_layer_cpu(ncnn::LayerType::Convolution1D);
  166. ncnn::ParamDict pd;
  167. pd.set(0, _num_output);
  168. pd.set(1, _kernel_w);
  169. pd.set(2, dilation_w);
  170. pd.set(3, stride_w);
  171. pd.set(4, pad_left);
  172. pd.set(15, pad_right);
  173. pd.set(18, pad_value);
  174. pd.set(5, bias_term);
  175. pd.set(6, weight_data_flattened.w);
  176. pd.set(9, activation_type);
  177. pd.set(10, activation_params);
  178. op->load_param(pd);
  179. ncnn::Mat weights[2];
  180. weights[0] = weight_data_flattened;
  181. weights[1] = bias_data_flattened;
  182. op->load_model(ncnn::ModelBinFromMatArray(weights));
  183. op->create_pipeline(opt);
  184. op->forward(bottom_blob, top_blob, opt);
  185. op->destroy_pipeline(opt);
  186. delete op;
  187. return 0;
  188. }
  189. #if NCNN_BF16
  190. int Convolution1D_arm::create_pipeline_bf16s(const Option& opt)
  191. {
  192. const int num_input = weight_data_size / kernel_w / num_output;
  193. convolution1d_transform_kernel_packed_bf16s(weight_data, weight_data_tm, num_input, num_output, kernel_w);
  194. if (opt.lightmode)
  195. weight_data.release();
  196. return 0;
  197. }
  198. int Convolution1D_arm::forward_bf16s(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const
  199. {
  200. int w = bottom_blob.w;
  201. size_t elemsize = bottom_blob.elemsize;
  202. int elempack = bottom_blob.elempack;
  203. const int kernel_extent_w = dilation_w * (kernel_w - 1) + 1;
  204. Mat bottom_blob_bordered;
  205. make_padding(bottom_blob, bottom_blob_bordered, opt);
  206. if (bottom_blob_bordered.empty())
  207. return -100;
  208. w = bottom_blob_bordered.w;
  209. int out_elempack = 1;
  210. #if __ARM_NEON
  211. if (opt.use_packing_layout)
  212. {
  213. out_elempack = num_output % 4 == 0 ? 4 : 1;
  214. }
  215. #endif
  216. size_t out_elemsize = elemsize / elempack * out_elempack;
  217. const int outw = (w - kernel_extent_w) / stride_w + 1;
  218. const int outh = num_output / out_elempack;
  219. top_blob.create(outw, outh, out_elemsize, out_elempack, opt.blob_allocator);
  220. if (top_blob.empty())
  221. return -100;
  222. convolution1d_packed_bf16s(bottom_blob_bordered, top_blob, weight_data_tm, bias_data, kernel_w, dilation_w, stride_w, activation_type, activation_params, opt);
  223. return 0;
  224. }
  225. #endif // NCNN_BF16
  226. } // namespace ncnn