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.cpp 7.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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.h"
  15. #include "fused_activation.h"
  16. namespace ncnn {
  17. Convolution1D::Convolution1D()
  18. {
  19. one_blob_only = true;
  20. support_inplace = false;
  21. }
  22. int Convolution1D::load_param(const ParamDict& pd)
  23. {
  24. num_output = pd.get(0, 0);
  25. kernel_w = pd.get(1, 0);
  26. dilation_w = pd.get(2, 1);
  27. stride_w = pd.get(3, 1);
  28. pad_left = pd.get(4, 0);
  29. pad_right = pd.get(15, pad_left);
  30. pad_value = pd.get(18, 0.f);
  31. bias_term = pd.get(5, 0);
  32. weight_data_size = pd.get(6, 0);
  33. activation_type = pd.get(9, 0);
  34. activation_params = pd.get(10, Mat());
  35. dynamic_weight = pd.get(19, 0);
  36. if (dynamic_weight)
  37. {
  38. one_blob_only = false;
  39. }
  40. return 0;
  41. }
  42. int Convolution1D::load_model(const ModelBin& mb)
  43. {
  44. if (dynamic_weight)
  45. return 0;
  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 Convolution1D::create_pipeline(const Option&)
  58. {
  59. if (dynamic_weight)
  60. return 0;
  61. return 0;
  62. }
  63. static int convolution1d(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 activation_type, const Mat& activation_params, const Option& opt)
  64. {
  65. const int h = bottom_blob.h;
  66. const int outw = top_blob.w;
  67. const int outh = top_blob.h;
  68. const int bias_term = bias_data.empty() ? 0 : 1;
  69. #pragma omp parallel for num_threads(opt.num_threads)
  70. for (int p = 0; p < outh; p++)
  71. {
  72. float* outptr = top_blob.row(p);
  73. for (int j = 0; j < outw; j++)
  74. {
  75. float sum = 0.f;
  76. if (bias_term)
  77. sum = bias_data[p];
  78. const float* kptr = (const float*)weight_data + kernel_w * h * p;
  79. for (int q = 0; q < h; q++)
  80. {
  81. const float* sptr = bottom_blob.row(q) + j * stride_w;
  82. for (int k = 0; k < kernel_w; k++)
  83. {
  84. float val = *sptr;
  85. float wt = kptr[k];
  86. sum += val * wt;
  87. sptr += dilation_w;
  88. }
  89. kptr += kernel_w;
  90. }
  91. sum = activation_ss(sum, activation_type, activation_params);
  92. outptr[j] = sum;
  93. }
  94. }
  95. return 0;
  96. }
  97. int Convolution1D::forward(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const
  98. {
  99. Mat bottom_blob_bordered;
  100. make_padding(bottom_blob, bottom_blob_bordered, opt);
  101. if (bottom_blob_bordered.empty())
  102. return -100;
  103. const int w = bottom_blob_bordered.w;
  104. const size_t elemsize = bottom_blob_bordered.elemsize;
  105. const int kernel_extent_w = dilation_w * (kernel_w - 1) + 1;
  106. const int outw = (w - kernel_extent_w) / stride_w + 1;
  107. top_blob.create(outw, num_output, elemsize, opt.blob_allocator);
  108. if (top_blob.empty())
  109. return -100;
  110. int ret = convolution1d(bottom_blob_bordered, top_blob, weight_data, bias_data, kernel_w, stride_w, dilation_w, activation_type, activation_params, opt);
  111. if (ret != 0)
  112. return ret;
  113. return 0;
  114. }
  115. int Convolution1D::forward(const std::vector<Mat>& bottom_blobs, std::vector<Mat>& top_blobs, const Option& opt) const
  116. {
  117. const Mat& bottom_blob = bottom_blobs[0];
  118. const Mat& _weight_data = bottom_blobs[1];
  119. Mat& top_blob = top_blobs[0];
  120. const int _kernel_w = _weight_data.w;
  121. const int _num_output = _weight_data.c;
  122. Mat weight_data_flattened;
  123. flatten(_weight_data, weight_data_flattened, opt);
  124. if (weight_data_flattened.empty())
  125. return -100;
  126. Mat bias_data_flattened;
  127. if (bias_term)
  128. {
  129. const Mat& _bias_data = bottom_blobs[2];
  130. flatten(_bias_data, bias_data_flattened, opt);
  131. if (bias_data_flattened.empty())
  132. return -100;
  133. }
  134. Mat bottom_blob_bordered;
  135. make_padding(bottom_blob, bottom_blob_bordered, _kernel_w, opt);
  136. if (bottom_blob_bordered.empty())
  137. return -100;
  138. const int w = bottom_blob_bordered.w;
  139. const size_t elemsize = bottom_blob_bordered.elemsize;
  140. const int kernel_extent_w = dilation_w * (_kernel_w - 1) + 1;
  141. const int outw = (w - kernel_extent_w) / stride_w + 1;
  142. top_blob.create(outw, _num_output, elemsize, opt.blob_allocator);
  143. if (top_blob.empty())
  144. return -100;
  145. int ret = convolution1d(bottom_blob_bordered, top_blob, weight_data_flattened, bias_data_flattened, _kernel_w, stride_w, dilation_w, activation_type, activation_params, opt);
  146. if (ret != 0)
  147. return ret;
  148. return 0;
  149. }
  150. void Convolution1D::make_padding(const Mat& bottom_blob, Mat& bottom_blob_bordered, const Option& opt) const
  151. {
  152. make_padding(bottom_blob, bottom_blob_bordered, kernel_w, opt);
  153. }
  154. void Convolution1D::make_padding(const Mat& bottom_blob, Mat& bottom_blob_bordered, int _kernel_w, const Option& opt) const
  155. {
  156. int w = bottom_blob.w;
  157. const int kernel_extent_w = dilation_w * (_kernel_w - 1) + 1;
  158. bottom_blob_bordered = bottom_blob;
  159. if (pad_left > 0 || pad_right > 0)
  160. {
  161. Option opt_b = opt;
  162. opt_b.blob_allocator = opt.workspace_allocator;
  163. copy_make_border(bottom_blob, bottom_blob_bordered, 0, 0, pad_left, pad_right, BORDER_CONSTANT, pad_value, opt_b);
  164. }
  165. else if (pad_left == -233 && pad_right == -233)
  166. {
  167. // tensorflow padding=SAME or onnx padding=SAME_UPPER
  168. int wpad = kernel_extent_w + (w - 1) / stride_w * stride_w - w;
  169. if (wpad > 0)
  170. {
  171. Option opt_b = opt;
  172. opt_b.blob_allocator = opt.workspace_allocator;
  173. copy_make_border(bottom_blob, bottom_blob_bordered, 0, 0, wpad / 2, wpad - wpad / 2, BORDER_CONSTANT, pad_value, opt_b);
  174. }
  175. }
  176. else if (pad_left == -234 && pad_right == -234)
  177. {
  178. // onnx padding=SAME_LOWER
  179. int wpad = kernel_extent_w + (w - 1) / stride_w * stride_w - w;
  180. if (wpad > 0)
  181. {
  182. Option opt_b = opt;
  183. opt_b.blob_allocator = opt.workspace_allocator;
  184. copy_make_border(bottom_blob, bottom_blob_bordered, 0, 0, wpad - wpad / 2, wpad / 2, BORDER_CONSTANT, pad_value, opt_b);
  185. }
  186. }
  187. }
  188. } // namespace ncnn