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.

convolutiondepthwise1d.cpp 8.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. // Tencent is pleased to support the open source community by making ncnn available.
  2. //
  3. // Copyright (C) 2017 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 "convolutiondepthwise1d.h"
  15. #include "layer_type.h"
  16. #include "fused_activation.h"
  17. namespace ncnn {
  18. ConvolutionDepthWise1D::ConvolutionDepthWise1D()
  19. {
  20. one_blob_only = true;
  21. support_inplace = false;
  22. }
  23. int ConvolutionDepthWise1D::load_param(const ParamDict& pd)
  24. {
  25. num_output = pd.get(0, 0);
  26. kernel_w = pd.get(1, 0);
  27. dilation_w = pd.get(2, 1);
  28. stride_w = pd.get(3, 1);
  29. pad_left = pd.get(4, 0);
  30. pad_right = pd.get(15, pad_left);
  31. pad_value = pd.get(18, 0.f);
  32. bias_term = pd.get(5, 0);
  33. weight_data_size = pd.get(6, 0);
  34. group = pd.get(7, 1);
  35. activation_type = pd.get(9, 0);
  36. activation_params = pd.get(10, Mat());
  37. dynamic_weight = pd.get(19, 0);
  38. if (dynamic_weight)
  39. {
  40. one_blob_only = false;
  41. }
  42. if (num_output % group != 0)
  43. {
  44. // reject invalid group
  45. return -100;
  46. }
  47. return 0;
  48. }
  49. int ConvolutionDepthWise1D::load_model(const ModelBin& mb)
  50. {
  51. if (dynamic_weight)
  52. return 0;
  53. weight_data = mb.load(weight_data_size, 0);
  54. if (weight_data.empty())
  55. return -100;
  56. if (bias_term)
  57. {
  58. bias_data = mb.load(num_output, 1);
  59. if (bias_data.empty())
  60. return -100;
  61. }
  62. return 0;
  63. }
  64. static int convolutiondepthwise1d(const Mat& bottom_blob, Mat& top_blob, const Mat& weight_data, const Mat& bias_data, int kernel_w, int stride_w, int dilation_w, int group, int activation_type, const Mat& activation_params, const Option& opt)
  65. {
  66. const int h = bottom_blob.h;
  67. const int outw = top_blob.w;
  68. const int outh = top_blob.h;
  69. const int bias_term = bias_data.empty() ? 0 : 1;
  70. // depth-wise
  71. if (h == group && group == outh)
  72. {
  73. #pragma omp parallel for num_threads(opt.num_threads)
  74. for (int g = 0; g < group; g++)
  75. {
  76. float* outptr = top_blob.row(g);
  77. const float* kptr = (const float*)weight_data + kernel_w * g;
  78. for (int j = 0; j < outw; j++)
  79. {
  80. float sum = 0.f;
  81. if (bias_term)
  82. sum = bias_data[g];
  83. const float* sptr = bottom_blob.row(g) + j * stride_w;
  84. for (int k = 0; k < kernel_w; k++)
  85. {
  86. float val = *sptr;
  87. float w = kptr[k];
  88. sum += val * w;
  89. sptr += dilation_w;
  90. }
  91. outptr[j] = activation_ss(sum, activation_type, activation_params);
  92. }
  93. }
  94. }
  95. else
  96. {
  97. // group convolution
  98. const int h_g = h / group;
  99. const int outh_g = outh / group;
  100. #ifdef _WIN32
  101. #pragma omp parallel for num_threads(opt.num_threads)
  102. #else
  103. #pragma omp parallel for collapse(2) num_threads(opt.num_threads)
  104. #endif
  105. for (int g = 0; g < group; g++)
  106. {
  107. for (int p = 0; p < outh_g; p++)
  108. {
  109. float* outptr = top_blob.row(g * outh_g + p);
  110. const float* weight_data_ptr = (const float*)weight_data + kernel_w * h_g * outh_g * g;
  111. for (int j = 0; j < outw; j++)
  112. {
  113. float sum = 0.f;
  114. if (bias_term)
  115. sum = bias_data[outh_g * g + p];
  116. const float* kptr = weight_data_ptr + kernel_w * h_g * p;
  117. for (int q = 0; q < h_g; q++)
  118. {
  119. const float* sptr = bottom_blob.row(h_g * g + q) + j * stride_w;
  120. for (int k = 0; k < kernel_w; k++)
  121. {
  122. float val = *sptr;
  123. float w = kptr[k];
  124. sum += val * w;
  125. sptr += dilation_w;
  126. }
  127. kptr += kernel_w;
  128. }
  129. outptr[j] = activation_ss(sum, activation_type, activation_params);
  130. }
  131. }
  132. }
  133. }
  134. return 0;
  135. }
  136. int ConvolutionDepthWise1D::forward(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const
  137. {
  138. Mat bottom_blob_bordered;
  139. make_padding(bottom_blob, bottom_blob_bordered, opt);
  140. if (bottom_blob_bordered.empty())
  141. return -100;
  142. const int w = bottom_blob_bordered.w;
  143. const size_t elemsize = bottom_blob.elemsize;
  144. const int kernel_extent_w = dilation_w * (kernel_w - 1) + 1;
  145. const int outw = (w - kernel_extent_w) / stride_w + 1;
  146. top_blob.create(outw, num_output, elemsize, opt.blob_allocator);
  147. if (top_blob.empty())
  148. return -100;
  149. int ret = convolutiondepthwise1d(bottom_blob_bordered, top_blob, weight_data, bias_data, kernel_w, stride_w, dilation_w, group, activation_type, activation_params, opt);
  150. if (ret != 0)
  151. return ret;
  152. return 0;
  153. }
  154. int ConvolutionDepthWise1D::forward(const std::vector<Mat>& bottom_blobs, std::vector<Mat>& top_blobs, const Option& opt) const
  155. {
  156. const Mat& bottom_blob = bottom_blobs[0];
  157. const Mat& _weight_data = bottom_blobs[1];
  158. Mat& top_blob = top_blobs[0];
  159. const int _kernel_w = _weight_data.w;
  160. const int _num_output = _weight_data.c;
  161. Mat weight_data_flattened;
  162. flatten(_weight_data, weight_data_flattened, opt);
  163. if (weight_data_flattened.empty())
  164. return -100;
  165. Mat bias_data_flattened;
  166. if (bias_term)
  167. {
  168. const Mat& _bias_data = bottom_blobs[2];
  169. flatten(_bias_data, bias_data_flattened, opt);
  170. if (bias_data_flattened.empty())
  171. return -100;
  172. }
  173. Mat bottom_blob_bordered;
  174. make_padding(bottom_blob, bottom_blob_bordered, _kernel_w, opt);
  175. if (bottom_blob_bordered.empty())
  176. return -100;
  177. const int w = bottom_blob_bordered.w;
  178. const size_t elemsize = bottom_blob_bordered.elemsize;
  179. const int kernel_extent_w = dilation_w * (_kernel_w - 1) + 1;
  180. const int outw = (w - kernel_extent_w) / stride_w + 1;
  181. top_blob.create(outw, _num_output, elemsize, opt.blob_allocator);
  182. if (top_blob.empty())
  183. return -100;
  184. int ret = convolutiondepthwise1d(bottom_blob_bordered, top_blob, weight_data_flattened, bias_data_flattened, _kernel_w, stride_w, dilation_w, group, activation_type, activation_params, opt);
  185. if (ret != 0)
  186. return ret;
  187. return 0;
  188. }
  189. void ConvolutionDepthWise1D::make_padding(const Mat& bottom_blob, Mat& bottom_blob_bordered, const Option& opt) const
  190. {
  191. make_padding(bottom_blob, bottom_blob_bordered, kernel_w, opt);
  192. }
  193. void ConvolutionDepthWise1D::make_padding(const Mat& bottom_blob, Mat& bottom_blob_bordered, int _kernel_w, const Option& opt) const
  194. {
  195. int w = bottom_blob.w;
  196. const int kernel_extent_w = dilation_w * (_kernel_w - 1) + 1;
  197. bottom_blob_bordered = bottom_blob;
  198. if (pad_left > 0 || pad_right > 0)
  199. {
  200. Option opt_b = opt;
  201. opt_b.blob_allocator = opt.workspace_allocator;
  202. copy_make_border(bottom_blob, bottom_blob_bordered, 0, 0, pad_left, pad_right, BORDER_CONSTANT, pad_value, opt_b);
  203. }
  204. else if (pad_left == -233 && pad_right == -233)
  205. {
  206. // tensorflow padding=SAME or onnx padding=SAME_UPPER
  207. int wpad = kernel_extent_w + (w - 1) / stride_w * stride_w - w;
  208. if (wpad > 0)
  209. {
  210. Option opt_b = opt;
  211. opt_b.blob_allocator = opt.workspace_allocator;
  212. copy_make_border(bottom_blob, bottom_blob_bordered, 0, 0, wpad / 2, wpad - wpad / 2, BORDER_CONSTANT, pad_value, opt_b);
  213. }
  214. }
  215. else if (pad_left == -234 && pad_right == -234)
  216. {
  217. // onnx padding=SAME_LOWER
  218. int wpad = kernel_extent_w + (w - 1) / stride_w * stride_w - w;
  219. if (wpad > 0)
  220. {
  221. Option opt_b = opt;
  222. opt_b.blob_allocator = opt.workspace_allocator;
  223. copy_make_border(bottom_blob, bottom_blob_bordered, 0, 0, wpad - wpad / 2, wpad / 2, BORDER_CONSTANT, pad_value, opt_b);
  224. }
  225. }
  226. }
  227. } // namespace ncnn