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

7 years ago
Fix warnings on Visual Studio (#1456) * Fix warning C4244 in src/layer/convolution.cpp C4244: '=': conversion from 'double' to 'float', possible loss of data * Fix warning C4244 in src/layer/convolution_sgemm_int8.h C4244: 'initializing': conversion from 'double' to 'int', possible loss of data * Fix warning C4244 in src/layer/deconvolution.cpp C4244: '=': conversion from 'double' to 'float', possible loss of data * Fix warning C4244 in src/layer/elu.cpp C4244: '=': conversion from 'double' to 'float', possible loss of data * Fix warning C4267 in src/layer/embed.cpp C4267: 'initializing': conversion from 'size_t' to 'int', possible loss of data * Fix warning C4244 in src/layer/exp.cpp C4244: '=': conversion from 'double' to 'float', possible loss of data * Fix warning C4244 in src/layer/innerproduct.cpp C4244: '=': conversion from 'double' to 'float', possible loss of data * Fix warning C4244 in src/layer/log.cpp C4244: '=': conversion from 'double' to 'float', possible loss of data C4244: 'initializing': conversion from 'double' to 'float', possible loss of data * Fix warning C4244 in src/layer/lrn.cpp C4244: '=': conversion from 'double' to 'float', possible loss of data * Fix warning C4244 in src/layer/mvn.cp C4244: 'initializing': conversion from 'double' to 'float', possible loss of data * Fix warning C4244 in src/layer/power.cpp C4244: '=': conversion from 'double' to 'float', possible loss of data * Fix warnings C4244 and C4267 in src/layer/proposal.cpp C4244: 'initializing': conversion from 'double' to 'float', possible loss of data C4244: 'initializing': conversion from 'double' to 'int', possible loss of data C4267: 'argument': conversion from 'size_t' to 'int', possible loss of data C4267: 'initializing': conversion from 'size_t' to 'int', possible loss of data * Fix warning C4244 in src/layer/reduction.cpp C4244: 'return': conversion from 'double' to 'T', possible loss of data * Fix warning C4244 in src/layer/tanh.cpp C4244: '=': conversion from 'double' to 'float', possible loss of data * Fix warning C4244 in src/layer/binaryop.cpp C4244: '=': conversion from 'double' to 'float', possible loss of data * Fix warnings C4244 and C4267 in src/layer/unaryop.cpp C4244: 'return': conversion from 'double' to 'T', possible loss of data C4267: 'initializing': conversion from 'size_t' to 'int', possible loss of data * Fix warning C4244 in src/layer/x86/convolutiondepthwise_3x3_int8.h C4244: 'initializing': conversion from 'double' to 'int', possible loss of data
6 years ago
7 years ago
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 "innerproduct.h"
  15. #include "layer_type.h"
  16. #include <algorithm>
  17. namespace ncnn {
  18. InnerProduct::InnerProduct()
  19. {
  20. one_blob_only = true;
  21. support_inplace = false;
  22. }
  23. int InnerProduct::load_param(const ParamDict& pd)
  24. {
  25. num_output = pd.get(0, 0);
  26. bias_term = pd.get(1, 0);
  27. weight_data_size = pd.get(2, 0);
  28. int8_scale_term = pd.get(8, 0);
  29. activation_type = pd.get(9, 0);
  30. activation_params = pd.get(10, Mat());
  31. if (int8_scale_term)
  32. {
  33. use_int8_inference = true;
  34. }
  35. return 0;
  36. }
  37. int InnerProduct::load_model(const ModelBin& mb)
  38. {
  39. weight_data = mb.load(weight_data_size, 0);
  40. if (weight_data.empty())
  41. return -100;
  42. if (bias_term)
  43. {
  44. bias_data = mb.load(num_output, 1);
  45. if (bias_data.empty())
  46. return -100;
  47. }
  48. if (int8_scale_term)
  49. {
  50. weight_data_int8_scales = mb.load(num_output, 1);
  51. bottom_blob_int8_scale = mb.load(1, 1)[0];
  52. }
  53. return 0;
  54. }
  55. int InnerProduct::create_pipeline(const Option& opt)
  56. {
  57. // runtime quantize the weight data
  58. if (opt.use_int8_inference && weight_data.elemsize == (size_t)4u && int8_scale_term)
  59. {
  60. Mat int8_weight_data(weight_data_size, (size_t)1u);
  61. if (int8_weight_data.empty())
  62. return -100;
  63. const int weight_data_size_output = weight_data_size / num_output;
  64. for (int p = 0; p < num_output; p++)
  65. {
  66. Option opt_q = opt;
  67. opt_q.blob_allocator = int8_weight_data.allocator;
  68. const Mat weight_data_n = weight_data.range(weight_data_size_output * p, weight_data_size_output);
  69. Mat int8_weight_data_n = int8_weight_data.range(weight_data_size_output * p, weight_data_size_output);
  70. quantize_float32_to_int8(weight_data_n, int8_weight_data_n, weight_data_int8_scales[p], opt_q);
  71. }
  72. weight_data = int8_weight_data;
  73. }
  74. return 0;
  75. }
  76. int InnerProduct::forward(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const
  77. {
  78. if (opt.use_int8_inference && weight_data.elemsize == (size_t)1u)
  79. {
  80. return forward_int8(bottom_blob, top_blob, opt);
  81. }
  82. int w = bottom_blob.w;
  83. int h = bottom_blob.h;
  84. int channels = bottom_blob.c;
  85. size_t elemsize = bottom_blob.elemsize;
  86. int size = w * h;
  87. top_blob.create(num_output, elemsize, opt.blob_allocator);
  88. if (top_blob.empty())
  89. return -100;
  90. // num_output
  91. #pragma omp parallel for num_threads(opt.num_threads)
  92. for (int p = 0; p < num_output; p++)
  93. {
  94. float sum = 0.f;
  95. if (bias_term)
  96. sum = bias_data[p];
  97. // channels
  98. for (int q = 0; q < channels; q++)
  99. {
  100. const float* w = (const float*)weight_data + size * channels * p + size * q;
  101. const float* m = bottom_blob.channel(q);
  102. for (int i = 0; i < size; i++)
  103. {
  104. sum += m[i] * w[i];
  105. }
  106. }
  107. if (activation_type == 1)
  108. {
  109. sum = std::max(sum, 0.f);
  110. }
  111. else if (activation_type == 2)
  112. {
  113. float slope = activation_params[0];
  114. sum = sum > 0.f ? sum : sum * slope;
  115. }
  116. else if (activation_type == 3)
  117. {
  118. float min = activation_params[0];
  119. float max = activation_params[1];
  120. if (sum < min)
  121. sum = min;
  122. if (sum > max)
  123. sum = max;
  124. }
  125. else if (activation_type == 4)
  126. {
  127. sum = static_cast<float>(1.f / (1.f + exp(-sum)));
  128. }
  129. else if (activation_type == 5)
  130. {
  131. sum = static_cast<float>(sum * tanh(log(exp(sum) + 1.f)));
  132. }
  133. top_blob[p] = sum;
  134. }
  135. return 0;
  136. }
  137. int InnerProduct::forward_int8(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const
  138. {
  139. int w = bottom_blob.w;
  140. int h = bottom_blob.h;
  141. int channels = bottom_blob.c;
  142. size_t elemsize = bottom_blob.elemsize;
  143. int size = w * h;
  144. Mat bottom_blob_tm = bottom_blob;
  145. if (elemsize != 1)
  146. {
  147. Option opt_g = opt;
  148. opt_g.blob_allocator = opt.workspace_allocator;
  149. quantize_float32_to_int8(bottom_blob, bottom_blob_tm, bottom_blob_int8_scale, opt_g);
  150. }
  151. top_blob.create(num_output, 4u, opt.blob_allocator);
  152. if (top_blob.empty())
  153. return -100;
  154. // num_output
  155. #pragma omp parallel for num_threads(opt.num_threads)
  156. for (int p = 0; p < num_output; p++)
  157. {
  158. float* outptr = top_blob;
  159. int sum = 0;
  160. // channels
  161. for (int q = 0; q < channels; q++)
  162. {
  163. const signed char* w = (const signed char*)weight_data + size * channels * p + size * q;
  164. const signed char* m = bottom_blob_tm.channel(q);
  165. for (int i = 0; i < size; i++)
  166. {
  167. sum += m[i] * w[i];
  168. }
  169. }
  170. // dequantize and relu
  171. float scale_in;
  172. if (weight_data_int8_scales[p] == 0)
  173. scale_in = 0;
  174. else
  175. scale_in = 1.f / (bottom_blob_int8_scale * weight_data_int8_scales[p]);
  176. float sumfp32 = sum * scale_in;
  177. if (bias_term)
  178. sumfp32 += bias_data[p];
  179. if (activation_type == 1)
  180. {
  181. sumfp32 = std::max(sumfp32, 0.f);
  182. }
  183. outptr[p] = sumfp32;
  184. }
  185. return 0;
  186. }
  187. } // namespace ncnn