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