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.

mat.cpp 10 kB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  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 "mat.h"
  15. #if __ARM_NEON
  16. #include <arm_neon.h>
  17. #endif // __ARM_NEON
  18. #include <math.h>
  19. #include "cpu.h"
  20. #include "layer_type.h"
  21. #include "layer.h"
  22. #if NCNN_VULKAN
  23. #if __ANDROID_API__ >= 26
  24. #include <android/hardware_buffer.h>
  25. #endif // __ANDROID_API__ >= 26
  26. #endif // NCNN_VULKAN
  27. namespace ncnn {
  28. void Mat::substract_mean_normalize(const float* mean_vals, const float* norm_vals)
  29. {
  30. Layer* op;
  31. if (mean_vals && !norm_vals)
  32. {
  33. // substract mean only
  34. op = create_layer(LayerType::Bias);
  35. ParamDict pd;
  36. pd.set(0, c);
  37. op->load_param(pd);
  38. Mat weights[1];
  39. weights[0] = Mat(c);
  40. for (int q=0; q<c; q++)
  41. {
  42. weights[0][q] = -mean_vals[q];
  43. }
  44. op->load_model(ModelBinFromMatArray(weights));
  45. }
  46. else if (!mean_vals && norm_vals)
  47. {
  48. // normalize only
  49. op = create_layer(LayerType::Scale);
  50. ParamDict pd;
  51. pd.set(0, c);
  52. op->load_param(pd);
  53. Mat weights[1];
  54. weights[0] = Mat(c);
  55. for (int q=0; q<c; q++)
  56. {
  57. weights[0][q] = norm_vals[q];
  58. }
  59. op->load_model(ModelBinFromMatArray(weights));
  60. }
  61. else if (mean_vals && norm_vals)
  62. {
  63. // substract mean and normalize
  64. op = create_layer(LayerType::Scale);
  65. ParamDict pd;
  66. pd.set(0, c);
  67. pd.set(1, 1);
  68. op->load_param(pd);
  69. Mat weights[2];
  70. weights[0] = Mat(c);
  71. weights[1] = Mat(c);
  72. for (int q=0; q<c; q++)
  73. {
  74. weights[0][q] = norm_vals[q];
  75. weights[1][q] = - mean_vals[q] * norm_vals[q];
  76. }
  77. op->load_model(ModelBinFromMatArray(weights));
  78. }
  79. else // if (!mean_vals && !norm_vals)
  80. {
  81. return;
  82. }
  83. Option opt;
  84. opt.num_threads = 1;// TODO
  85. op->create_pipeline(opt);
  86. op->forward_inplace(*this, opt);
  87. op->destroy_pipeline(opt);
  88. delete op;
  89. }
  90. // convert half precision floating point to float
  91. static float half2float(unsigned short value)
  92. {
  93. // 1 : 5 : 10
  94. unsigned short sign = (value & 0x8000) >> 15;
  95. unsigned short exponent = (value & 0x7c00) >> 10;
  96. unsigned short significand = value & 0x03FF;
  97. // fprintf(stderr, "%d %d %d\n", sign, exponent, significand);
  98. // 1 : 8 : 23
  99. union
  100. {
  101. unsigned int u;
  102. float f;
  103. } tmp;
  104. if (exponent == 0)
  105. {
  106. if (significand == 0)
  107. {
  108. // zero
  109. tmp.u = (sign << 31);
  110. }
  111. else
  112. {
  113. // denormal
  114. exponent = 0;
  115. // find non-zero bit
  116. while ((significand & 0x200) == 0)
  117. {
  118. significand <<= 1;
  119. exponent++;
  120. }
  121. significand <<= 1;
  122. significand &= 0x3FF;
  123. tmp.u = (sign << 31) | ((-exponent + (-15 + 127)) << 23) | (significand << 13);
  124. }
  125. }
  126. else if (exponent == 0x1F)
  127. {
  128. // infinity or NaN
  129. tmp.u = (sign << 31) | (0xFF << 23) | (significand << 13);
  130. }
  131. else
  132. {
  133. // normalized
  134. tmp.u = (sign << 31) | ((exponent + (-15 + 127)) << 23) | (significand << 13);
  135. }
  136. return tmp.f;
  137. }
  138. Mat Mat::from_float16(const unsigned short* data, int size)
  139. {
  140. Mat m(size);
  141. if (m.empty())
  142. return m;
  143. float* ptr = m;//.data;
  144. #if __ARM_NEON && (__ARM_FP & 2)
  145. int nn = cpu_support_arm_vfpv4() ? size >> 2 : 0;
  146. int remain = size - (nn << 2);
  147. #else
  148. int remain = size;
  149. #endif // __ARM_NEON
  150. #if __ARM_NEON && (__ARM_FP & 2)
  151. #if __aarch64__
  152. if (nn > 0)
  153. {
  154. asm volatile(
  155. "0: \n"
  156. "ld1 {v0.4h}, [%1], #8 \n"
  157. "fcvtl v1.4s, v0.4h \n"
  158. "subs %w0, %w0, #1 \n"
  159. "st1 {v1.4s}, [%2], #16 \n"
  160. "bne 0b \n"
  161. : "=r"(nn), // %0
  162. "=r"(data), // %1
  163. "=r"(ptr) // %2
  164. : "0"(nn),
  165. "1"(data),
  166. "2"(ptr)
  167. : "cc", "memory", "v0", "v1"
  168. );
  169. }
  170. #else
  171. if (nn > 0)
  172. {
  173. asm volatile(
  174. "0: \n"
  175. "pld [%1, #64] \n"
  176. "vld1.s16 {d0}, [%1 :64]! \n"
  177. "vcvt.f32.f16 q1, d0 \n"
  178. "subs %0, #1 \n"
  179. "vst1.f32 {d2-d3}, [%2 :128]! \n"
  180. "bne 0b \n"
  181. : "=r"(nn), // %0
  182. "=r"(data), // %1
  183. "=r"(ptr) // %2
  184. : "0"(nn),
  185. "1"(data),
  186. "2"(ptr)
  187. : "cc", "memory", "q0", "q1"
  188. );
  189. }
  190. #endif // __aarch64__
  191. #endif // __ARM_NEON
  192. for (; remain>0; remain--)
  193. {
  194. *ptr = half2float(*data);
  195. data++;
  196. ptr++;
  197. }
  198. return m;
  199. }
  200. #if NCNN_VULKAN
  201. #if __ANDROID_API__ >= 26
  202. VkImageMat VkImageMat::from_android_hardware_buffer(AHardwareBuffer* hb, VkAndroidHardwareBufferImageAllocator* allocator)
  203. {
  204. AHardwareBuffer_Desc bufferDesc;
  205. AHardwareBuffer_describe(hb, &bufferDesc);
  206. VkImageMat m;
  207. m.allocator = allocator;
  208. m.width = bufferDesc.width;
  209. m.height = bufferDesc.height;
  210. m.format = VK_FORMAT_UNDEFINED;
  211. m.data = allocator->fastMalloc(hb);
  212. m.refcount = (int*)((unsigned char*)m.data + offsetof(VkImageMemory, refcount));
  213. *m.refcount = 1;
  214. return m;
  215. }
  216. #endif // __ANDROID_API__ >= 26
  217. #endif // NCNN_VULKAN
  218. void copy_make_border(const Mat& src, Mat& dst, int top, int bottom, int left, int right, int type, float v, const Option& opt)
  219. {
  220. Layer* padding = create_layer(LayerType::Padding);
  221. ParamDict pd;
  222. pd.set(0, top);
  223. pd.set(1, bottom);
  224. pd.set(2, left);
  225. pd.set(3, right);
  226. pd.set(4, type);
  227. pd.set(5, v);
  228. padding->load_param(pd);
  229. padding->create_pipeline(opt);
  230. padding->forward(src, dst, opt);
  231. padding->destroy_pipeline(opt);
  232. delete padding;
  233. }
  234. void copy_cut_border(const Mat& src, Mat& dst, int top, int bottom, int left, int right, const Option& opt)
  235. {
  236. Layer* crop = create_layer(LayerType::Crop);
  237. ParamDict pd;
  238. pd.set(0, left);
  239. pd.set(1, top);
  240. pd.set(2, 0);
  241. pd.set(3, src.w - left - right);
  242. pd.set(4, src.h - top - bottom);
  243. pd.set(5, -233);
  244. crop->load_param(pd);
  245. crop->create_pipeline(opt);
  246. crop->forward(src, dst, opt);
  247. crop->destroy_pipeline(opt);
  248. delete crop;
  249. }
  250. void resize_bilinear(const Mat& src, Mat& dst, int w, int h, const Option& opt)
  251. {
  252. Layer* interp = create_layer(LayerType::Interp);
  253. ParamDict pd;
  254. pd.set(0, 2);
  255. pd.set(3, h);
  256. pd.set(4, w);
  257. interp->load_param(pd);
  258. interp->create_pipeline(opt);
  259. interp->forward(src, dst, opt);
  260. interp->destroy_pipeline(opt);
  261. delete interp;
  262. }
  263. void resize_bicubic(const Mat& src, Mat& dst, int w, int h, const Option& opt)
  264. {
  265. Layer* interp = create_layer(LayerType::Interp);
  266. ParamDict pd;
  267. pd.set(0, 3);
  268. pd.set(3, h);
  269. pd.set(4, w);
  270. interp->load_param(pd);
  271. interp->create_pipeline(opt);
  272. interp->forward(src, dst, opt);
  273. interp->destroy_pipeline(opt);
  274. delete interp;
  275. }
  276. void convert_packing(const Mat& src, Mat& dst, int _elempack, const Option& opt)
  277. {
  278. Layer* packing = create_layer(LayerType::Packing);
  279. ParamDict pd;
  280. pd.set(0, _elempack);
  281. packing->load_param(pd);
  282. packing->create_pipeline(opt);
  283. packing->forward(src, dst, opt);
  284. packing->destroy_pipeline(opt);
  285. delete packing;
  286. }
  287. void cast_float32_to_float16(const Mat& src, Mat& dst, const Option& opt)
  288. {
  289. Layer* cast = create_layer(LayerType::Cast);
  290. ParamDict pd;
  291. pd.set(0, 1);
  292. pd.set(1, 2);
  293. cast->load_param(pd);
  294. cast->create_pipeline(opt);
  295. cast->forward(src, dst, opt);
  296. cast->destroy_pipeline(opt);
  297. delete cast;
  298. }
  299. void cast_float16_to_float32(const Mat& src, Mat& dst, const Option& opt)
  300. {
  301. Layer* cast = create_layer(LayerType::Cast);
  302. ParamDict pd;
  303. pd.set(0, 2);
  304. pd.set(1, 1);
  305. cast->load_param(pd);
  306. cast->create_pipeline(opt);
  307. cast->forward(src, dst, opt);
  308. cast->destroy_pipeline(opt);
  309. delete cast;
  310. }
  311. void quantize_float32_to_int8(const Mat& src, Mat& dst, float scale, const Option& opt)
  312. {
  313. Layer* quantize = create_layer(LayerType::Quantize);
  314. ParamDict pd;
  315. pd.set(0, scale);
  316. quantize->load_param(pd);
  317. quantize->create_pipeline(opt);
  318. quantize->forward(src, dst, opt);
  319. quantize->destroy_pipeline(opt);
  320. delete quantize;
  321. }
  322. void dequantize_int32_to_float32(Mat& m, float scale, const float* bias, int bias_data_size, const Option& opt)
  323. {
  324. Layer* dequantize = create_layer(LayerType::Dequantize);
  325. ParamDict pd;
  326. pd.set(0, scale);
  327. pd.set(1, bias ? 1 : 0);
  328. pd.set(2, bias_data_size);
  329. dequantize->load_param(pd);
  330. Mat weights[1];
  331. weights[0] = Mat(bias_data_size, (void*)bias);
  332. dequantize->load_model(ModelBinFromMatArray(weights));
  333. dequantize->create_pipeline(opt);
  334. dequantize->forward_inplace(m, opt);
  335. dequantize->destroy_pipeline(opt);
  336. delete dequantize;
  337. }
  338. void requantize_int8_to_int8(const Mat& src, Mat& dst, float scale_in, float scale_out, const float* bias, int bias_data_size, int fusion_relu, const Option& opt)
  339. {
  340. Layer* requantize = create_layer(LayerType::Requantize);
  341. ParamDict pd;
  342. pd.set(0, scale_in);
  343. pd.set(1, scale_out);
  344. pd.set(2, bias ? 1 : 0);
  345. pd.set(3, bias_data_size);
  346. pd.set(4, fusion_relu);
  347. requantize->load_param(pd);
  348. Mat weights[1];
  349. weights[0] = Mat(bias_data_size, (void*)bias);
  350. requantize->load_model(ModelBinFromMatArray(weights));
  351. requantize->create_pipeline(opt);
  352. requantize->forward(src, dst, opt);
  353. requantize->destroy_pipeline(opt);
  354. delete requantize;
  355. }
  356. } // namespace ncnn