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.

test_cast.cpp 10 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. // Tencent is pleased to support the open source community by making ncnn available.
  2. //
  3. // Copyright (C) 2020 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 "testutil.h"
  15. #include "layer/cast.h"
  16. static int test_cast_cpu(const ncnn::Mat& a, int type_from, int type_to)
  17. {
  18. ncnn::ParamDict pd;
  19. pd.set(0, type_from);
  20. pd.set(1, type_to);
  21. std::vector<ncnn::Mat> weights(0);
  22. ncnn::Option opt;
  23. opt.num_threads = 1;
  24. opt.use_vulkan_compute = false;
  25. opt.use_packing_layout = false;
  26. ncnn::Layer* op = ncnn::create_layer("Cast");
  27. op->load_param(pd);
  28. ncnn::ModelBinFromMatArray mb(weights.data());
  29. op->load_model(mb);
  30. op->create_pipeline(opt);
  31. ncnn::Mat a_fp16;
  32. if (type_from == 2)
  33. {
  34. ncnn::cast_float32_to_float16(a, a_fp16, opt);
  35. }
  36. else
  37. {
  38. a_fp16 = a;
  39. }
  40. ncnn::Mat b;
  41. ((ncnn::Cast*)op)->ncnn::Cast::forward(a_fp16, b, opt);
  42. ncnn::Mat c;
  43. op->forward(a_fp16, c, opt);
  44. op->destroy_pipeline(opt);
  45. delete op;
  46. if (CompareMat(b, c, 0.001) != 0)
  47. {
  48. fprintf(stderr, "test_cast_cpu failed a.dims=%d a=(%d %d %d) type_from=%d type_to=%d\n", a.dims, a.w, a.h, a.c, type_from, type_to);
  49. return -1;
  50. }
  51. return 0;
  52. }
  53. static int test_cast_cpu_packed(const ncnn::Mat& a, int type_from, int type_to)
  54. {
  55. ncnn::ParamDict pd;
  56. pd.set(0, type_from);
  57. pd.set(1, type_to);
  58. std::vector<ncnn::Mat> weights(0);
  59. ncnn::Option opt;
  60. opt.num_threads = 1;
  61. opt.use_vulkan_compute = false;
  62. opt.use_packing_layout = false;
  63. ncnn::Layer* op = ncnn::create_layer("Cast");
  64. op->load_param(pd);
  65. ncnn::ModelBinFromMatArray mb(weights.data());
  66. op->load_model(mb);
  67. op->create_pipeline(opt);
  68. ncnn::Mat a_fp16;
  69. if (type_from == 2)
  70. {
  71. ncnn::cast_float32_to_float16(a, a_fp16, opt);
  72. }
  73. else
  74. {
  75. a_fp16 = a;
  76. }
  77. ncnn::Mat b;
  78. ((ncnn::Cast*)op)->ncnn::Cast::forward(a_fp16, b, opt);
  79. ncnn::Mat a4;
  80. ncnn::convert_packing(a, a4, 4, opt);
  81. ncnn::Mat a4_fp16;
  82. if (type_from == 2)
  83. {
  84. ncnn::cast_float32_to_float16(a4, a4_fp16, opt);
  85. }
  86. else
  87. {
  88. a4_fp16 = a4;
  89. }
  90. ncnn::Mat c;
  91. op->forward(a4_fp16, c, opt);
  92. op->destroy_pipeline(opt);
  93. delete op;
  94. if (CompareMat(b, c, 0.001) != 0)
  95. {
  96. fprintf(stderr, "test_cast_cpu_packed failed a.dims=%d a=(%d %d %d) type_from=%d type_to=%d\n", a.dims, a.w, a.h, a.c, type_from, type_to);
  97. return -1;
  98. }
  99. return 0;
  100. }
  101. #if NCNN_VULKAN
  102. static int test_cast_gpu_fp16p(const ncnn::Mat& a, int type_from, int type_to)
  103. {
  104. ncnn::ParamDict pd;
  105. pd.set(0, type_from);
  106. pd.set(1, type_to);
  107. std::vector<ncnn::Mat> weights(0);
  108. ncnn::Option opt;
  109. opt.num_threads = 1;
  110. opt.use_vulkan_compute = true;
  111. opt.use_int8_inference = false;
  112. opt.use_fp16_packed = true;
  113. opt.use_fp16_storage = false;
  114. opt.use_fp16_arithmetic = false;
  115. opt.use_int8_storage = false;
  116. opt.use_int8_arithmetic = false;
  117. opt.use_packing_layout = true;
  118. ncnn::VulkanDevice* vkdev = ncnn::get_gpu_device();
  119. ncnn::VkAllocator* blob_vkallocator = vkdev->acquire_blob_allocator();
  120. ncnn::VkAllocator* staging_vkallocator = vkdev->acquire_staging_allocator();
  121. opt.blob_vkallocator = blob_vkallocator;
  122. opt.workspace_vkallocator = blob_vkallocator;
  123. opt.staging_vkallocator = staging_vkallocator;
  124. if (!vkdev->info.support_fp16_storage) opt.use_fp16_storage = false;
  125. if (!vkdev->info.support_fp16_packed) opt.use_fp16_packed = false;
  126. ncnn::Layer* op = ncnn::create_layer("Cast");
  127. op->vkdev = vkdev;
  128. op->load_param(pd);
  129. ncnn::ModelBinFromMatArray mb(weights.data());
  130. op->load_model(mb);
  131. op->create_pipeline(opt);
  132. ncnn::Mat a_fp16;
  133. if (type_from == 2)
  134. {
  135. ncnn::cast_float32_to_float16(a, a_fp16, opt);
  136. }
  137. else
  138. {
  139. a_fp16 = a;
  140. }
  141. ncnn::Mat b;
  142. ((ncnn::Cast*)op)->ncnn::Cast::forward(a_fp16, b, opt);
  143. ncnn::Mat d;
  144. // pack
  145. ncnn::Mat a4;
  146. ncnn::convert_packing(a, a4, 4, opt);
  147. ncnn::Mat a4_fp16;
  148. if (type_from == 2 && a4.elempack == 4)
  149. {
  150. ncnn::cast_float32_to_float16(a4, a4_fp16, opt);
  151. }
  152. else
  153. {
  154. a4_fp16 = a4;
  155. }
  156. // upload
  157. ncnn::VkMat a4_gpu;
  158. a4_gpu.create_like(a4_fp16, opt.blob_vkallocator, opt.staging_vkallocator);
  159. a4_gpu.prepare_staging_buffer();
  160. a4_gpu.upload(a4_fp16);
  161. // forward
  162. ncnn::VkCompute cmd(vkdev);
  163. cmd.record_upload(a4_gpu);
  164. ncnn::VkMat d4_gpu;
  165. if (op->support_inplace)
  166. {
  167. d4_gpu.create_like(a4_gpu, a4_gpu.allocator, a4_gpu.staging_allocator);
  168. cmd.record_clone(a4_gpu, d4_gpu);
  169. op->forward_inplace(d4_gpu, cmd, opt);
  170. }
  171. else
  172. {
  173. op->forward(a4_gpu, d4_gpu, cmd, opt);
  174. }
  175. d4_gpu.prepare_staging_buffer();
  176. cmd.record_download(d4_gpu);
  177. cmd.submit_and_wait();
  178. // download
  179. d.create_like(d4_gpu);
  180. d4_gpu.download(d);
  181. op->destroy_pipeline(opt);
  182. delete op;
  183. vkdev->reclaim_blob_allocator(blob_vkallocator);
  184. vkdev->reclaim_staging_allocator(staging_vkallocator);
  185. if (CompareMat(b, d, 0.001) != 0)
  186. {
  187. fprintf(stderr, "test_cast_gpu_fp16p failed a.dims=%d a=(%d %d %d) type_from=%d type_to=%d\n", a.dims, a.w, a.h, a.c, type_from, type_to);
  188. return -1;
  189. }
  190. return 0;
  191. }
  192. static int test_cast_gpu_fp16p_pack8(const ncnn::Mat& a, int type_from, int type_to)
  193. {
  194. ncnn::ParamDict pd;
  195. pd.set(0, type_from);
  196. pd.set(1, type_to);
  197. std::vector<ncnn::Mat> weights(0);
  198. ncnn::Option opt;
  199. opt.num_threads = 1;
  200. opt.use_vulkan_compute = true;
  201. opt.use_int8_inference = false;
  202. opt.use_fp16_packed = true;
  203. opt.use_fp16_storage = false;
  204. opt.use_fp16_arithmetic = false;
  205. opt.use_int8_storage = false;
  206. opt.use_int8_arithmetic = false;
  207. opt.use_packing_layout = true;
  208. opt.use_shader_pack8 = true;
  209. ncnn::VulkanDevice* vkdev = ncnn::get_gpu_device();
  210. ncnn::VkAllocator* blob_vkallocator = vkdev->acquire_blob_allocator();
  211. ncnn::VkAllocator* staging_vkallocator = vkdev->acquire_staging_allocator();
  212. opt.blob_vkallocator = blob_vkallocator;
  213. opt.workspace_vkallocator = blob_vkallocator;
  214. opt.staging_vkallocator = staging_vkallocator;
  215. if (!vkdev->info.support_fp16_storage) opt.use_fp16_storage = false;
  216. if (!vkdev->info.support_fp16_packed) opt.use_fp16_packed = false;
  217. ncnn::Layer* op = ncnn::create_layer("Cast");
  218. op->vkdev = vkdev;
  219. op->load_param(pd);
  220. ncnn::ModelBinFromMatArray mb(weights.data());
  221. op->load_model(mb);
  222. op->create_pipeline(opt);
  223. ncnn::Mat a_fp16;
  224. if (type_from == 2)
  225. {
  226. ncnn::cast_float32_to_float16(a, a_fp16, opt);
  227. }
  228. else
  229. {
  230. a_fp16 = a;
  231. }
  232. ncnn::Mat b;
  233. ((ncnn::Cast*)op)->ncnn::Cast::forward(a_fp16, b, opt);
  234. ncnn::Mat d;
  235. // pack
  236. ncnn::Mat a4;
  237. ncnn::convert_packing(a, a4, 8, opt);
  238. if (a4.elempack != 8)
  239. ncnn::convert_packing(a, a4, 4, opt);
  240. ncnn::Mat a4_fp16;
  241. if (type_from == 2 && (a4.elempack == 4 || a4.elempack == 8))
  242. {
  243. ncnn::cast_float32_to_float16(a4, a4_fp16, opt);
  244. }
  245. else
  246. {
  247. a4_fp16 = a4;
  248. }
  249. // upload
  250. ncnn::VkMat a4_gpu;
  251. a4_gpu.create_like(a4_fp16, opt.blob_vkallocator, opt.staging_vkallocator);
  252. a4_gpu.prepare_staging_buffer();
  253. a4_gpu.upload(a4_fp16);
  254. // forward
  255. ncnn::VkCompute cmd(vkdev);
  256. cmd.record_upload(a4_gpu);
  257. ncnn::VkMat d4_gpu;
  258. if (op->support_inplace)
  259. {
  260. d4_gpu.create_like(a4_gpu, a4_gpu.allocator, a4_gpu.staging_allocator);
  261. cmd.record_clone(a4_gpu, d4_gpu);
  262. op->forward_inplace(d4_gpu, cmd, opt);
  263. }
  264. else
  265. {
  266. op->forward(a4_gpu, d4_gpu, cmd, opt);
  267. }
  268. d4_gpu.prepare_staging_buffer();
  269. cmd.record_download(d4_gpu);
  270. cmd.submit_and_wait();
  271. // download
  272. d.create_like(d4_gpu);
  273. d4_gpu.download(d);
  274. op->destroy_pipeline(opt);
  275. delete op;
  276. vkdev->reclaim_blob_allocator(blob_vkallocator);
  277. vkdev->reclaim_staging_allocator(staging_vkallocator);
  278. if (CompareMat(b, d, 0.001) != 0)
  279. {
  280. fprintf(stderr, "test_cast_gpu_fp16p_pack8 failed a.dims=%d a=(%d %d %d) type_from=%d type_to=%d\n", a.dims, a.w, a.h, a.c, type_from, type_to);
  281. return -1;
  282. }
  283. return 0;
  284. }
  285. #endif // NCNN_VULKAN
  286. static int test_cast(const ncnn::Mat& a, int type_from, int type_to)
  287. {
  288. return 0
  289. || test_cast_cpu(a, type_from, type_to)
  290. || test_cast_cpu_packed(a, type_from, type_to)
  291. #if NCNN_VULKAN
  292. || test_cast_gpu_fp16p(a, type_from, type_to)
  293. || test_cast_gpu_fp16p_pack8(a, type_from, type_to)
  294. #endif // NCNN_VULKAN
  295. ;
  296. }
  297. static int test_cast_0()
  298. {
  299. return 0
  300. || test_cast(RandomMat(6, 7, 16), 1, 2)
  301. || test_cast(RandomMat(3, 5, 13), 1, 2)
  302. || test_cast(RandomMat(6, 7, 16), 2, 1)
  303. || test_cast(RandomMat(3, 5, 13), 2, 1)
  304. ;
  305. }
  306. static int test_cast_1()
  307. {
  308. return 0
  309. || test_cast(RandomMat(6, 16), 1, 2)
  310. || test_cast(RandomMat(7, 15), 1, 2)
  311. || test_cast(RandomMat(6, 16), 2, 1)
  312. || test_cast(RandomMat(7, 15), 2, 1)
  313. ;
  314. }
  315. static int test_cast_2()
  316. {
  317. return 0
  318. || test_cast(RandomMat(128), 1, 2)
  319. || test_cast(RandomMat(127), 1, 2)
  320. || test_cast(RandomMat(128), 2, 1)
  321. || test_cast(RandomMat(127), 2, 1)
  322. ;
  323. }
  324. int main()
  325. {
  326. SRAND(7767517);
  327. return 0
  328. || test_cast_0()
  329. || test_cast_1()
  330. || test_cast_2()
  331. ;
  332. }