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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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_activation.h"
  19. #include "arm_usability.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. return 0;
  58. }
  59. int Convolution1D_arm::destroy_pipeline(const Option& /*opt*/)
  60. {
  61. return 0;
  62. }
  63. int Convolution1D_arm::forward(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const
  64. {
  65. int elembits = bottom_blob.elembits();
  66. #if NCNN_ARM82
  67. if (support_fp16_storage && opt.use_fp16_storage && elembits == 16)
  68. {
  69. if (opt.use_fp16_arithmetic)
  70. return forward_fp16sa(bottom_blob, top_blob, opt);
  71. else
  72. return forward_fp16s(bottom_blob, top_blob, opt);
  73. }
  74. #endif
  75. #if NCNN_BF16
  76. if (opt.use_bf16_storage && elembits == 16)
  77. return forward_bf16s(bottom_blob, top_blob, opt);
  78. #endif
  79. int w = bottom_blob.w;
  80. size_t elemsize = bottom_blob.elemsize;
  81. int elempack = bottom_blob.elempack;
  82. const int kernel_extent_w = dilation_w * (kernel_w - 1) + 1;
  83. Mat bottom_blob_bordered;
  84. make_padding(bottom_blob, bottom_blob_bordered, opt);
  85. if (bottom_blob_bordered.empty())
  86. return -100;
  87. w = bottom_blob_bordered.w;
  88. int out_elempack = 1;
  89. #if __ARM_NEON
  90. if (opt.use_packing_layout)
  91. {
  92. out_elempack = num_output % 4 == 0 ? 4 : 1;
  93. }
  94. #endif
  95. size_t out_elemsize = elemsize / elempack * out_elempack;
  96. const int outw = (w - kernel_extent_w) / stride_w + 1;
  97. const int outh = num_output / out_elempack;
  98. top_blob.create(outw, outh, out_elemsize, out_elempack, opt.blob_allocator);
  99. if (top_blob.empty())
  100. return -100;
  101. convolution1d_packed(bottom_blob_bordered, top_blob, weight_data_tm, bias_data, kernel_w, dilation_w, stride_w, activation_type, activation_params, opt);
  102. return 0;
  103. }
  104. int Convolution1D_arm::forward(const std::vector<Mat>& bottom_blobs, std::vector<Mat>& top_blobs, const Option& opt) const
  105. {
  106. const Mat& bottom_blob = bottom_blobs[0];
  107. const Mat& _weight_data = bottom_blobs[1];
  108. Mat& top_blob = top_blobs[0];
  109. const int _kernel_w = _weight_data.w;
  110. const int _num_output = _weight_data.c * _weight_data.elempack;
  111. Mat weight_data_flattened;
  112. flatten(_weight_data, weight_data_flattened, opt);
  113. if (weight_data_flattened.empty())
  114. return -100;
  115. #if NCNN_ARM82
  116. if (opt.use_fp16_storage && cpu_support_arm_asimdhp() && weight_data_flattened.elembits() == 16)
  117. {
  118. Mat weight_data_flattened_fp32;
  119. cast_float16_to_float32(weight_data_flattened, weight_data_flattened_fp32, opt);
  120. weight_data_flattened = weight_data_flattened_fp32;
  121. }
  122. #endif // NCNN_ARM82
  123. #if NCNN_BF16
  124. if (opt.use_bf16_storage && weight_data_flattened.elembits() == 16)
  125. {
  126. Mat weight_data_flattened_fp32;
  127. cast_bfloat16_to_float32(weight_data_flattened, weight_data_flattened_fp32, opt);
  128. weight_data_flattened = weight_data_flattened_fp32;
  129. }
  130. #endif // NCNN_BF16
  131. // weight_data_flattened as pack1
  132. weight_data_flattened.w *= weight_data_flattened.elempack;
  133. weight_data_flattened.elemsize /= weight_data_flattened.elempack;
  134. weight_data_flattened.elempack = 1;
  135. Mat bias_data_flattened;
  136. if (bias_term)
  137. {
  138. const Mat& _bias_data = bottom_blobs[2];
  139. flatten(_bias_data, bias_data_flattened, opt);
  140. if (bias_data_flattened.empty())
  141. return -100;
  142. #if NCNN_ARM82
  143. if (opt.use_fp16_storage && cpu_support_arm_asimdhp() && bias_data_flattened.elembits() == 16)
  144. {
  145. Mat bias_data_flattened_fp32;
  146. cast_float16_to_float32(bias_data_flattened, bias_data_flattened_fp32, opt);
  147. bias_data_flattened = bias_data_flattened_fp32;
  148. }
  149. #endif // NCNN_ARM82
  150. #if NCNN_BF16
  151. if (opt.use_bf16_storage && bias_data_flattened.elembits() == 16)
  152. {
  153. Mat bias_data_flattened_fp32;
  154. cast_bfloat16_to_float32(bias_data_flattened, bias_data_flattened_fp32, opt);
  155. bias_data_flattened = bias_data_flattened_fp32;
  156. }
  157. #endif // NCNN_BF16
  158. // bias_data_flattened as pack1
  159. bias_data_flattened.w *= bias_data_flattened.elempack;
  160. bias_data_flattened.elemsize /= bias_data_flattened.elempack;
  161. bias_data_flattened.elempack = 1;
  162. }
  163. ncnn::Layer* op = ncnn::create_layer(ncnn::LayerType::Convolution1D);
  164. ncnn::ParamDict pd;
  165. pd.set(0, _num_output);
  166. pd.set(1, _kernel_w);
  167. pd.set(2, dilation_w);
  168. pd.set(3, stride_w);
  169. pd.set(4, pad_left);
  170. pd.set(15, pad_right);
  171. pd.set(18, pad_value);
  172. pd.set(5, bias_term);
  173. pd.set(6, weight_data_flattened.w);
  174. pd.set(9, activation_type);
  175. pd.set(10, activation_params);
  176. op->load_param(pd);
  177. ncnn::Mat weights[2];
  178. weights[0] = weight_data_flattened;
  179. weights[1] = bias_data_flattened;
  180. op->load_model(ncnn::ModelBinFromMatArray(weights));
  181. op->create_pipeline(opt);
  182. op->forward(bottom_blob, top_blob, opt);
  183. op->destroy_pipeline(opt);
  184. delete op;
  185. return 0;
  186. }
  187. #if NCNN_BF16
  188. int Convolution1D_arm::create_pipeline_bf16s(const Option& /*opt*/)
  189. {
  190. const int num_input = weight_data_size / kernel_w / num_output;
  191. convolution1d_transform_kernel_packed_bf16s(weight_data, weight_data_tm, num_input, num_output, kernel_w);
  192. return 0;
  193. }
  194. int Convolution1D_arm::forward_bf16s(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const
  195. {
  196. int w = bottom_blob.w;
  197. size_t elemsize = bottom_blob.elemsize;
  198. int elempack = bottom_blob.elempack;
  199. const int kernel_extent_w = dilation_w * (kernel_w - 1) + 1;
  200. Mat bottom_blob_bordered;
  201. make_padding(bottom_blob, bottom_blob_bordered, opt);
  202. if (bottom_blob_bordered.empty())
  203. return -100;
  204. w = bottom_blob_bordered.w;
  205. int out_elempack = 1;
  206. #if __ARM_NEON
  207. if (opt.use_packing_layout)
  208. {
  209. out_elempack = num_output % 4 == 0 ? 4 : 1;
  210. }
  211. #endif
  212. size_t out_elemsize = elemsize / elempack * out_elempack;
  213. const int outw = (w - kernel_extent_w) / stride_w + 1;
  214. const int outh = num_output / out_elempack;
  215. top_blob.create(outw, outh, out_elemsize, out_elempack, opt.blob_allocator);
  216. if (top_blob.empty())
  217. return -100;
  218. convolution1d_packed_bf16s(bottom_blob_bordered, top_blob, weight_data_tm, bias_data, kernel_w, dilation_w, stride_w, activation_type, activation_params, opt);
  219. return 0;
  220. }
  221. #endif // NCNN_BF16
  222. } // namespace ncnn