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.

innerproduct.cpp 8.4 kB

7 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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 "innerproduct.h"
  15. #include <algorithm>
  16. #include "layer_type.h"
  17. namespace ncnn {
  18. DEFINE_LAYER_CREATOR(InnerProduct)
  19. InnerProduct::InnerProduct()
  20. {
  21. one_blob_only = true;
  22. support_inplace = false;
  23. quantize = 0;
  24. }
  25. int InnerProduct::load_param(const ParamDict& pd)
  26. {
  27. num_output = pd.get(0, 0);
  28. bias_term = pd.get(1, 0);
  29. weight_data_size = pd.get(2, 0);
  30. int8_scale_term = pd.get(8, 0);
  31. activation_type = pd.get(9, 0);
  32. activation_params = pd.get(10, Mat());
  33. return 0;
  34. }
  35. int InnerProduct::load_model(const ModelBin& mb)
  36. {
  37. weight_data = mb.load(weight_data_size, 0);
  38. if (weight_data.empty())
  39. return -100;
  40. if (bias_term)
  41. {
  42. bias_data = mb.load(num_output, 1);
  43. if (bias_data.empty())
  44. return -100;
  45. }
  46. if (int8_scale_term)
  47. {
  48. weight_data_int8_scales = mb.load(num_output, 1);
  49. bottom_blob_int8_scale = mb.load(1, 1)[0];
  50. }
  51. return 0;
  52. }
  53. int InnerProduct::create_pipeline(const Option& opt)
  54. {
  55. bool weight_data_is_int8 = (weight_data.elemsize == (size_t)1u);
  56. bool weight_data_is_float32 = (weight_data.elemsize == (size_t)4u);
  57. if (weight_data_is_int8 && !opt.use_int8_inference)
  58. {
  59. fprintf(stderr, "quantized int8 weight loaded but use_int8_inference disabled\n");
  60. return -1;
  61. }
  62. use_int8_inference = opt.use_int8_inference && (weight_data_is_int8 || (weight_data_is_float32 && int8_scale_term));
  63. // initial the quantize,dequantize op layer
  64. if (use_int8_inference)
  65. {
  66. quantize = ncnn::create_layer(ncnn::LayerType::Quantize);
  67. {
  68. ncnn::ParamDict pd;
  69. pd.set(0, bottom_blob_int8_scale);// scale
  70. quantize->load_param(pd);
  71. quantize->create_pipeline(opt);
  72. }
  73. dequantize_ops.resize(num_output);
  74. for (int n=0; n<num_output; n++)
  75. {
  76. dequantize_ops[n] = ncnn::create_layer(ncnn::LayerType::Dequantize);
  77. float top_rescale = 1.f;
  78. if (weight_data_int8_scales[n] == 0)
  79. top_rescale = 0;
  80. else
  81. top_rescale = 1.f / (bottom_blob_int8_scale * weight_data_int8_scales[n]);
  82. ncnn::ParamDict pd;
  83. pd.set(0, top_rescale);// scale
  84. pd.set(1, bias_term); // bias_term
  85. pd.set(2, 1); // bias_data_size
  86. dequantize_ops[n]->load_param(pd);
  87. ncnn::Mat weights[1];
  88. weights[0] = bias_data.range(n, 1);
  89. dequantize_ops[n]->load_model(ModelBinFromMatArray(weights));
  90. dequantize_ops[n]->create_pipeline(opt);
  91. }
  92. }
  93. // runtime quantize the weight data
  94. if (weight_data_is_float32 && use_int8_inference)
  95. {
  96. // quantize weight to int8
  97. Mat int8_weight_data(weight_data_size, (size_t)1u);
  98. if (int8_weight_data.empty())
  99. return -100;
  100. const int weight_data_size_output = weight_data_size / num_output;
  101. for (int n=0; n<num_output; n++)
  102. {
  103. Layer* op = ncnn::create_layer(ncnn::LayerType::Quantize);
  104. ncnn::ParamDict pd;
  105. pd.set(0, weight_data_int8_scales[n]);// scale
  106. op->load_param(pd);
  107. op->create_pipeline(opt);
  108. ncnn::Option opt;
  109. opt.blob_allocator = int8_weight_data.allocator;
  110. const Mat weight_data_n = weight_data.range(weight_data_size_output * n, weight_data_size_output);
  111. Mat int8_weight_data_n = int8_weight_data.range(weight_data_size_output * n, weight_data_size_output);
  112. op->forward(weight_data_n, int8_weight_data_n, opt);
  113. delete op;
  114. }
  115. weight_data = int8_weight_data;
  116. }
  117. return 0;
  118. }
  119. int InnerProduct::destroy_pipeline(const Option& opt)
  120. {
  121. if (quantize)
  122. {
  123. quantize->destroy_pipeline(opt);
  124. delete quantize;
  125. quantize = 0;
  126. }
  127. for (int i=0; i<(int)dequantize_ops.size(); i++)
  128. {
  129. dequantize_ops[i]->destroy_pipeline(opt);
  130. delete dequantize_ops[i];
  131. }
  132. dequantize_ops.clear();
  133. return 0;
  134. }
  135. int InnerProduct::forward(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const
  136. {
  137. int w = bottom_blob.w;
  138. int h = bottom_blob.h;
  139. int channels = bottom_blob.c;
  140. size_t elemsize = bottom_blob.elemsize;
  141. int size = w * h;
  142. top_blob.create(num_output, elemsize, opt.blob_allocator);
  143. if (top_blob.empty())
  144. return -100;
  145. if (use_int8_inference)
  146. {
  147. Mat bottom_blob_tm = bottom_blob;
  148. if (elemsize != 1)
  149. {
  150. Mat bottom_blob_int8;
  151. bottom_blob_int8.create(w, h, channels, (size_t)1u, opt.workspace_allocator);
  152. if (bottom_blob_int8.empty())
  153. return -100;
  154. // quantize, scale and round to nearest
  155. {
  156. ncnn::Option opt_g = opt;
  157. opt_g.blob_allocator = bottom_blob_int8.allocator;
  158. quantize->forward(bottom_blob, bottom_blob_int8, opt_g);
  159. }
  160. bottom_blob_tm = bottom_blob_int8;
  161. }
  162. // num_output
  163. #pragma omp parallel for num_threads(opt.num_threads)
  164. for (int p=0; p<num_output; p++)
  165. {
  166. int sum = 0;
  167. int* out = top_blob;
  168. // channels
  169. for (int q=0; q<channels; q++)
  170. {
  171. const signed char* w = (const signed char*)weight_data + size * channels * p + size * q;
  172. const signed char* m = bottom_blob_tm.channel(q);
  173. for (int i = 0; i < size; i++)
  174. {
  175. sum += m[i] * w[i];
  176. }
  177. }
  178. out[p] = sum;
  179. }
  180. #pragma omp parallel for num_threads(opt.num_threads)
  181. for (int p=0; p<num_output; p++)
  182. {
  183. int* out_s32 = top_blob;
  184. float* out_f32 = top_blob;
  185. float top_rescale = 1.f;
  186. if (weight_data_int8_scales[p] == 0)
  187. top_rescale = 0;
  188. else
  189. top_rescale = 1.f / (bottom_blob_int8_scale * weight_data_int8_scales[p]);
  190. if (bias_term)
  191. out_f32[p] = out_s32[p] * top_rescale + bias_data[p];
  192. else
  193. out_f32[p] = out_s32[p] * top_rescale;
  194. if (activation_type == 1)
  195. {
  196. out_f32[p] = std::max(out_f32[p], 0.f);
  197. }
  198. }
  199. return 0;
  200. }
  201. // num_output
  202. #pragma omp parallel for num_threads(opt.num_threads)
  203. for (int p=0; p<num_output; p++)
  204. {
  205. float sum = 0.f;
  206. if (bias_term)
  207. sum = bias_data[p];
  208. // channels
  209. for (int q=0; q<channels; q++)
  210. {
  211. const float* w = (const float*)weight_data + size * channels * p + size * q;
  212. const float* m = bottom_blob.channel(q);
  213. for (int i = 0; i < size; i++)
  214. {
  215. sum += m[i] * w[i];
  216. }
  217. }
  218. if (activation_type == 1)
  219. {
  220. sum = std::max(sum, 0.f);
  221. }
  222. else if (activation_type == 2)
  223. {
  224. float slope = activation_params[0];
  225. sum = sum > 0.f ? sum : sum * slope;
  226. }
  227. else if (activation_type == 3)
  228. {
  229. float min = activation_params[0];
  230. float max = activation_params[1];
  231. if (sum < min)
  232. sum = min;
  233. if (sum > max)
  234. sum = max;
  235. }
  236. else if (activation_type == 4)
  237. {
  238. sum = 1.f / (1.f + exp(-sum));
  239. }
  240. top_blob[p] = sum;
  241. }
  242. return 0;
  243. }
  244. } // namespace ncnn