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

adreno image shader + fp16 + fp16a (#1714) * wip * wip * fix * image and imageview can not be destroyed until command execution ends * fast copy path for tightly packed data * wip * texture load works * 1d 3d image * record clone image, multiple commands share one image reference * upload download image * layer forward accept vkimagemat * vkimagemat graph works * staging vkimagemat for passing dynamic parameters, macro for fp32+image shader, padding image shader * vkimagemat elemsize * convolution test pass * conv1x1s1 image shader * fast staging image allocator from host memory, pooling image shader * convolutiondepthwise image shader * innerproduct image shader * packing image shader * crop deconvolution image shader * resolve spirv binding types * image fp16 and fp16a, cast image shader * eltwise image shader * wip * absval image shader * deconvolutiondepthwise image shader * concat image shader, squeezenet works * noop split image shader * uniform precision hint * layer support_image_storage * wip * vulkan device utility operator * command is storage and packing option aware * fallback to cpu on image allocation failed, mobilenetssd works * flatten image shader, enable more test * ci test * check imgfp32 imgfp16 imgfp16a features * fix ci test * fix ci test * upgrade swiftshader * wip * opt aggressive * imgfp16p * opt none * convolution winograd image shader * fix flush range, fast copy path for continous buffer * minor fix * fix innerproduct * wip ... * wip * cast fix * packing test * wip * image fp16p is fp16p * wip * silence * more line info * code clean * softmax image shader
6 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
  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. Mat Mat::from_float16(const unsigned short* data, int size)
  91. {
  92. Mat m(size);
  93. if (m.empty())
  94. return m;
  95. float* ptr = m;//.data;
  96. #if __ARM_NEON && (__ARM_FP & 2)
  97. int nn = cpu_support_arm_vfpv4() ? size >> 2 : 0;
  98. int remain = size - (nn << 2);
  99. #else
  100. int remain = size;
  101. #endif // __ARM_NEON
  102. #if __ARM_NEON && (__ARM_FP & 2)
  103. #if __aarch64__
  104. if (nn > 0)
  105. {
  106. asm volatile(
  107. "0: \n"
  108. "ld1 {v0.4h}, [%1], #8 \n"
  109. "fcvtl v1.4s, v0.4h \n"
  110. "subs %w0, %w0, #1 \n"
  111. "st1 {v1.4s}, [%2], #16 \n"
  112. "bne 0b \n"
  113. : "=r"(nn), // %0
  114. "=r"(data), // %1
  115. "=r"(ptr) // %2
  116. : "0"(nn),
  117. "1"(data),
  118. "2"(ptr)
  119. : "cc", "memory", "v0", "v1"
  120. );
  121. }
  122. #else
  123. if (nn > 0)
  124. {
  125. asm volatile(
  126. "0: \n"
  127. "pld [%1, #64] \n"
  128. "vld1.s16 {d0}, [%1 :64]! \n"
  129. "vcvt.f32.f16 q1, d0 \n"
  130. "subs %0, #1 \n"
  131. "vst1.f32 {d2-d3}, [%2 :128]! \n"
  132. "bne 0b \n"
  133. : "=r"(nn), // %0
  134. "=r"(data), // %1
  135. "=r"(ptr) // %2
  136. : "0"(nn),
  137. "1"(data),
  138. "2"(ptr)
  139. : "cc", "memory", "q0", "q1"
  140. );
  141. }
  142. #endif // __aarch64__
  143. #endif // __ARM_NEON
  144. for (; remain>0; remain--)
  145. {
  146. *ptr = float16_to_float32(*data);
  147. data++;
  148. ptr++;
  149. }
  150. return m;
  151. }
  152. #if NCNN_VULKAN
  153. #if __ANDROID_API__ >= 26
  154. VkImageMat VkImageMat::from_android_hardware_buffer(VkAndroidHardwareBufferImageAllocator* allocator)
  155. {
  156. int width = allocator->width();
  157. int height = allocator->height();
  158. return VkImageMat(width, height, allocator);
  159. }
  160. #endif // __ANDROID_API__ >= 26
  161. #endif // NCNN_VULKAN
  162. unsigned short float32_to_float16(float value)
  163. {
  164. // 1 : 8 : 23
  165. union
  166. {
  167. unsigned int u;
  168. float f;
  169. } tmp;
  170. tmp.f = value;
  171. // 1 : 8 : 23
  172. unsigned short sign = (tmp.u & 0x80000000) >> 31;
  173. unsigned short exponent = (tmp.u & 0x7F800000) >> 23;
  174. unsigned int significand = tmp.u & 0x7FFFFF;
  175. // NCNN_LOGE("%d %d %d", sign, exponent, significand);
  176. // 1 : 5 : 10
  177. unsigned short fp16;
  178. if (exponent == 0)
  179. {
  180. // zero or denormal, always underflow
  181. fp16 = (sign << 15) | (0x00 << 10) | 0x00;
  182. }
  183. else if (exponent == 0xFF)
  184. {
  185. // infinity or NaN
  186. fp16 = (sign << 15) | (0x1F << 10) | (significand ? 0x200 : 0x00);
  187. }
  188. else
  189. {
  190. // normalized
  191. short newexp = exponent + (- 127 + 15);
  192. if (newexp >= 31)
  193. {
  194. // overflow, return infinity
  195. fp16 = (sign << 15) | (0x1F << 10) | 0x00;
  196. }
  197. else if (newexp <= 0)
  198. {
  199. // underflow
  200. if (newexp >= -10)
  201. {
  202. // denormal half-precision
  203. unsigned short sig = (significand | 0x800000) >> (14 - newexp);
  204. fp16 = (sign << 15) | (0x00 << 10) | sig;
  205. }
  206. else
  207. {
  208. // underflow
  209. fp16 = (sign << 15) | (0x00 << 10) | 0x00;
  210. }
  211. }
  212. else
  213. {
  214. fp16 = (sign << 15) | (newexp << 10) | (significand >> 13);
  215. }
  216. }
  217. return fp16;
  218. }
  219. float float16_to_float32(unsigned short value)
  220. {
  221. // 1 : 5 : 10
  222. unsigned short sign = (value & 0x8000) >> 15;
  223. unsigned short exponent = (value & 0x7c00) >> 10;
  224. unsigned short significand = value & 0x03FF;
  225. // NCNN_LOGE("%d %d %d", sign, exponent, significand);
  226. // 1 : 8 : 23
  227. union
  228. {
  229. unsigned int u;
  230. float f;
  231. } tmp;
  232. if (exponent == 0)
  233. {
  234. if (significand == 0)
  235. {
  236. // zero
  237. tmp.u = (sign << 31);
  238. }
  239. else
  240. {
  241. // denormal
  242. exponent = 0;
  243. // find non-zero bit
  244. while ((significand & 0x200) == 0)
  245. {
  246. significand <<= 1;
  247. exponent++;
  248. }
  249. significand <<= 1;
  250. significand &= 0x3FF;
  251. tmp.u = (sign << 31) | ((-exponent + (-15 + 127)) << 23) | (significand << 13);
  252. }
  253. }
  254. else if (exponent == 0x1F)
  255. {
  256. // infinity or NaN
  257. tmp.u = (sign << 31) | (0xFF << 23) | (significand << 13);
  258. }
  259. else
  260. {
  261. // normalized
  262. tmp.u = (sign << 31) | ((exponent + (-15 + 127)) << 23) | (significand << 13);
  263. }
  264. return tmp.f;
  265. }
  266. void copy_make_border(const Mat& src, Mat& dst, int top, int bottom, int left, int right, int type, float v, const Option& opt)
  267. {
  268. Layer* padding = create_layer(LayerType::Padding);
  269. ParamDict pd;
  270. pd.set(0, top);
  271. pd.set(1, bottom);
  272. pd.set(2, left);
  273. pd.set(3, right);
  274. pd.set(4, type);
  275. pd.set(5, v);
  276. padding->load_param(pd);
  277. padding->create_pipeline(opt);
  278. padding->forward(src, dst, opt);
  279. padding->destroy_pipeline(opt);
  280. delete padding;
  281. }
  282. void copy_cut_border(const Mat& src, Mat& dst, int top, int bottom, int left, int right, const Option& opt)
  283. {
  284. if (left + right > src.w || top + bottom > src.h)
  285. {
  286. NCNN_LOGE("copy_cut_border parameter error, top: %d, bottom: %d, left: %d, right: %d, src.w: %d, src.h: %d", top, bottom, left, right, src.w, src.h);
  287. return;
  288. }
  289. Layer* crop = create_layer(LayerType::Crop);
  290. ParamDict pd;
  291. pd.set(0, left);
  292. pd.set(1, top);
  293. pd.set(2, 0);
  294. pd.set(3, src.w - left - right);
  295. pd.set(4, src.h - top - bottom);
  296. pd.set(5, -233);
  297. crop->load_param(pd);
  298. crop->create_pipeline(opt);
  299. crop->forward(src, dst, opt);
  300. crop->destroy_pipeline(opt);
  301. delete crop;
  302. }
  303. void resize_bilinear(const Mat& src, Mat& dst, int w, int h, const Option& opt)
  304. {
  305. Layer* interp = create_layer(LayerType::Interp);
  306. ParamDict pd;
  307. pd.set(0, 2);
  308. pd.set(3, h);
  309. pd.set(4, w);
  310. interp->load_param(pd);
  311. interp->create_pipeline(opt);
  312. interp->forward(src, dst, opt);
  313. interp->destroy_pipeline(opt);
  314. delete interp;
  315. }
  316. void resize_bicubic(const Mat& src, Mat& dst, int w, int h, const Option& opt)
  317. {
  318. Layer* interp = create_layer(LayerType::Interp);
  319. ParamDict pd;
  320. pd.set(0, 3);
  321. pd.set(3, h);
  322. pd.set(4, w);
  323. interp->load_param(pd);
  324. interp->create_pipeline(opt);
  325. interp->forward(src, dst, opt);
  326. interp->destroy_pipeline(opt);
  327. delete interp;
  328. }
  329. void convert_packing(const Mat& src, Mat& dst, int _elempack, const Option& opt)
  330. {
  331. Layer* packing = create_layer(LayerType::Packing);
  332. ParamDict pd;
  333. pd.set(0, _elempack);
  334. packing->load_param(pd);
  335. packing->create_pipeline(opt);
  336. packing->forward(src, dst, opt);
  337. packing->destroy_pipeline(opt);
  338. delete packing;
  339. }
  340. void cast_float32_to_float16(const Mat& src, Mat& dst, const Option& opt)
  341. {
  342. Layer* cast = create_layer(LayerType::Cast);
  343. ParamDict pd;
  344. pd.set(0, 1);
  345. pd.set(1, 2);
  346. cast->load_param(pd);
  347. cast->create_pipeline(opt);
  348. cast->forward(src, dst, opt);
  349. cast->destroy_pipeline(opt);
  350. delete cast;
  351. }
  352. void cast_float16_to_float32(const Mat& src, Mat& dst, const Option& opt)
  353. {
  354. Layer* cast = create_layer(LayerType::Cast);
  355. ParamDict pd;
  356. pd.set(0, 2);
  357. pd.set(1, 1);
  358. cast->load_param(pd);
  359. cast->create_pipeline(opt);
  360. cast->forward(src, dst, opt);
  361. cast->destroy_pipeline(opt);
  362. delete cast;
  363. }
  364. void cast_int8_to_float32(const Mat& src, Mat& dst, const Option& opt)
  365. {
  366. Layer* cast = create_layer(LayerType::Cast);
  367. ParamDict pd;
  368. pd.set(0, 3);
  369. pd.set(1, 1);
  370. cast->load_param(pd);
  371. cast->create_pipeline(opt);
  372. cast->forward(src, dst, opt);
  373. cast->destroy_pipeline(opt);
  374. delete cast;
  375. }
  376. void cast_float32_to_bfloat16(const Mat& src, Mat& dst, const Option& opt)
  377. {
  378. Layer* cast = create_layer(LayerType::Cast);
  379. ParamDict pd;
  380. pd.set(0, 1);
  381. pd.set(1, 4);
  382. cast->load_param(pd);
  383. cast->create_pipeline(opt);
  384. cast->forward(src, dst, opt);
  385. cast->destroy_pipeline(opt);
  386. delete cast;
  387. }
  388. void cast_bfloat16_to_float32(const Mat& src, Mat& dst, const Option& opt)
  389. {
  390. Layer* cast = create_layer(LayerType::Cast);
  391. ParamDict pd;
  392. pd.set(0, 4);
  393. pd.set(1, 1);
  394. cast->load_param(pd);
  395. cast->create_pipeline(opt);
  396. cast->forward(src, dst, opt);
  397. cast->destroy_pipeline(opt);
  398. delete cast;
  399. }
  400. void quantize_float32_to_int8(const Mat& src, Mat& dst, float scale, const Option& opt)
  401. {
  402. Layer* quantize = create_layer(LayerType::Quantize);
  403. ParamDict pd;
  404. pd.set(0, scale);
  405. quantize->load_param(pd);
  406. quantize->create_pipeline(opt);
  407. quantize->forward(src, dst, opt);
  408. quantize->destroy_pipeline(opt);
  409. delete quantize;
  410. }
  411. void dequantize_int32_to_float32(Mat& m, float scale, const float* bias, int bias_data_size, const Option& opt)
  412. {
  413. Layer* dequantize = create_layer(LayerType::Dequantize);
  414. ParamDict pd;
  415. pd.set(0, scale);
  416. pd.set(1, bias ? 1 : 0);
  417. pd.set(2, bias_data_size);
  418. dequantize->load_param(pd);
  419. Mat weights[1];
  420. weights[0] = Mat(bias_data_size, (void*)bias);
  421. dequantize->load_model(ModelBinFromMatArray(weights));
  422. dequantize->create_pipeline(opt);
  423. dequantize->forward_inplace(m, opt);
  424. dequantize->destroy_pipeline(opt);
  425. delete dequantize;
  426. }
  427. 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)
  428. {
  429. Layer* requantize = create_layer(LayerType::Requantize);
  430. ParamDict pd;
  431. pd.set(0, scale_in);
  432. pd.set(1, scale_out);
  433. pd.set(2, bias ? 1 : 0);
  434. pd.set(3, bias_data_size);
  435. pd.set(4, fusion_relu);
  436. requantize->load_param(pd);
  437. Mat weights[1];
  438. weights[0] = Mat(bias_data_size, (void*)bias);
  439. requantize->load_model(ModelBinFromMatArray(weights));
  440. requantize->create_pipeline(opt);
  441. requantize->forward(src, dst, opt);
  442. requantize->destroy_pipeline(opt);
  443. delete requantize;
  444. }
  445. } // namespace ncnn