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

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