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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. if (num_output % group != 0)
  38. {
  39. // reject invalid group
  40. return -100;
  41. }
  42. return 0;
  43. }
  44. int ConvolutionDepthWise1D::load_model(const ModelBin& mb)
  45. {
  46. weight_data = mb.load(weight_data_size, 0);
  47. if (weight_data.empty())
  48. return -100;
  49. if (bias_term)
  50. {
  51. bias_data = mb.load(num_output, 1);
  52. if (bias_data.empty())
  53. return -100;
  54. }
  55. return 0;
  56. }
  57. int ConvolutionDepthWise1D::create_pipeline(const Option& opt)
  58. {
  59. return 0;
  60. }
  61. int ConvolutionDepthWise1D::forward(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const
  62. {
  63. // convolv with NxN kernel
  64. // value = value + bias
  65. int w = bottom_blob.w;
  66. int h = bottom_blob.h;
  67. size_t elemsize = bottom_blob.elemsize;
  68. if (h % group != 0 || num_output % group != 0)
  69. {
  70. // reject invalid group
  71. return -100;
  72. }
  73. // NCNN_LOGE("ConvolutionDepthWise1D input %d x %d pad = %d ksize=%d stride=%d", w, h, pad_w, kernel_w, stride_w);
  74. const int kernel_extent_w = dilation_w * (kernel_w - 1) + 1;
  75. Mat bottom_blob_bordered;
  76. make_padding(bottom_blob, bottom_blob_bordered, opt);
  77. if (bottom_blob_bordered.empty())
  78. return -100;
  79. w = bottom_blob_bordered.w;
  80. h = bottom_blob_bordered.h;
  81. int outw = (w - kernel_extent_w) / stride_w + 1;
  82. // float32
  83. top_blob.create(outw, num_output, elemsize, opt.blob_allocator);
  84. if (top_blob.empty())
  85. return -100;
  86. // depth-wise
  87. if (h == group && group == num_output)
  88. {
  89. #pragma omp parallel for num_threads(opt.num_threads)
  90. for (int g = 0; g < group; g++)
  91. {
  92. float* outptr = top_blob.row(g);
  93. const float* kptr = (const float*)weight_data + kernel_w * g;
  94. for (int j = 0; j < outw; j++)
  95. {
  96. float sum = 0.f;
  97. if (bias_term)
  98. sum = bias_data[g];
  99. const float* sptr = bottom_blob_bordered.row(g) + j * stride_w;
  100. for (int k = 0; k < kernel_w; k++)
  101. {
  102. float val = *sptr;
  103. float w = kptr[k];
  104. sum += val * w;
  105. sptr += dilation_w;
  106. }
  107. outptr[j] = activation_ss(sum, activation_type, activation_params);
  108. }
  109. }
  110. }
  111. else
  112. {
  113. // group convolution
  114. const int h_g = h / group;
  115. const int num_output_g = num_output / group;
  116. #ifdef _WIN32
  117. #pragma omp parallel for num_threads(opt.num_threads)
  118. #else // _WIN32
  119. #pragma omp parallel for collapse(2) num_threads(opt.num_threads)
  120. #endif // _WIN32
  121. for (int g = 0; g < group; g++)
  122. {
  123. for (int p = 0; p < num_output_g; p++)
  124. {
  125. float* outptr = top_blob.row(g * num_output_g + p);
  126. const float* weight_data_ptr = (const float*)weight_data + kernel_w * h_g * num_output_g * g;
  127. for (int j = 0; j < outw; j++)
  128. {
  129. float sum = 0.f;
  130. if (bias_term)
  131. sum = bias_data[num_output_g * g + p];
  132. const float* kptr = weight_data_ptr + kernel_w * h_g * p;
  133. // h_g
  134. for (int q = 0; q < h_g; q++)
  135. {
  136. const float* sptr = bottom_blob_bordered.row(h_g * g + q) + j * stride_w;
  137. for (int k = 0; k < kernel_w; k++)
  138. {
  139. float val = *sptr;
  140. float w = kptr[k];
  141. sum += val * w;
  142. sptr += dilation_w;
  143. }
  144. kptr += kernel_w;
  145. }
  146. outptr[j] = activation_ss(sum, activation_type, activation_params);
  147. }
  148. }
  149. }
  150. }
  151. return 0;
  152. }
  153. void ConvolutionDepthWise1D::make_padding(const Mat& bottom_blob, Mat& bottom_blob_bordered, const Option& opt) const
  154. {
  155. int w = bottom_blob.w;
  156. const int kernel_extent_w = dilation_w * (kernel_w - 1) + 1;
  157. bottom_blob_bordered = bottom_blob;
  158. if (pad_left > 0 || pad_right > 0)
  159. {
  160. Option opt_b = opt;
  161. opt_b.blob_allocator = opt.workspace_allocator;
  162. copy_make_border(bottom_blob, bottom_blob_bordered, 0, 0, pad_left, pad_right, BORDER_CONSTANT, pad_value, opt_b);
  163. }
  164. else if (pad_left == -233 && pad_right == -233)
  165. {
  166. // tensorflow padding=SAME or onnx padding=SAME_UPPER
  167. int wpad = kernel_extent_w + (w - 1) / stride_w * stride_w - w;
  168. if (wpad > 0)
  169. {
  170. Option opt_b = opt;
  171. opt_b.blob_allocator = opt.workspace_allocator;
  172. copy_make_border(bottom_blob, bottom_blob_bordered, 0, 0, wpad / 2, wpad - wpad / 2, BORDER_CONSTANT, pad_value, opt_b);
  173. }
  174. }
  175. else if (pad_left == -234 && pad_right == -234)
  176. {
  177. // onnx padding=SAME_LOWER
  178. int wpad = kernel_extent_w + (w - 1) / stride_w * stride_w - w;
  179. if (wpad > 0)
  180. {
  181. Option opt_b = opt;
  182. opt_b.blob_allocator = opt.workspace_allocator;
  183. copy_make_border(bottom_blob, bottom_blob_bordered, 0, 0, wpad - wpad / 2, wpad / 2, BORDER_CONSTANT, pad_value, opt_b);
  184. }
  185. }
  186. }
  187. } // namespace ncnn