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 16 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
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
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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663
  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_int8_inference = false;
  26. opt.use_packing_layout = false;
  27. ncnn::Layer* op = ncnn::create_layer("Cast");
  28. op->load_param(pd);
  29. ncnn::ModelBinFromMatArray mb(weights.data());
  30. op->load_model(mb);
  31. op->create_pipeline(opt);
  32. ncnn::Mat a_fp16;
  33. if (type_from == 2)
  34. {
  35. ncnn::cast_float32_to_float16(a, a_fp16, opt);
  36. }
  37. else
  38. {
  39. a_fp16 = a;
  40. }
  41. ncnn::Mat b;
  42. ((ncnn::Cast*)op)->ncnn::Cast::forward(a_fp16, b, opt);
  43. ncnn::Mat c;
  44. op->forward(a_fp16, c, opt);
  45. op->destroy_pipeline(opt);
  46. delete op;
  47. if (CompareMat(b, c, 0.001) != 0)
  48. {
  49. 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);
  50. return -1;
  51. }
  52. return 0;
  53. }
  54. static int test_cast_cpu_packed(const ncnn::Mat& a, int type_from, int type_to)
  55. {
  56. ncnn::ParamDict pd;
  57. pd.set(0, type_from);
  58. pd.set(1, type_to);
  59. std::vector<ncnn::Mat> weights(0);
  60. ncnn::Option opt;
  61. opt.num_threads = 1;
  62. opt.use_vulkan_compute = false;
  63. opt.use_packing_layout = false;
  64. ncnn::Layer* op = ncnn::create_layer("Cast");
  65. op->load_param(pd);
  66. ncnn::ModelBinFromMatArray mb(weights.data());
  67. op->load_model(mb);
  68. op->create_pipeline(opt);
  69. ncnn::Mat a_fp16;
  70. if (type_from == 2)
  71. {
  72. ncnn::cast_float32_to_float16(a, a_fp16, opt);
  73. }
  74. else
  75. {
  76. a_fp16 = a;
  77. }
  78. ncnn::Mat b;
  79. ((ncnn::Cast*)op)->ncnn::Cast::forward(a_fp16, b, opt);
  80. ncnn::Mat a4;
  81. ncnn::convert_packing(a, a4, 4, opt);
  82. ncnn::Mat a4_fp16;
  83. if (type_from == 2)
  84. {
  85. ncnn::cast_float32_to_float16(a4, a4_fp16, opt);
  86. }
  87. else
  88. {
  89. a4_fp16 = a4;
  90. }
  91. ncnn::Mat c;
  92. op->forward(a4_fp16, c, opt);
  93. op->destroy_pipeline(opt);
  94. delete op;
  95. if (CompareMat(b, c, 0.001) != 0)
  96. {
  97. 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);
  98. return -1;
  99. }
  100. return 0;
  101. }
  102. #if NCNN_VULKAN
  103. static int test_cast_gpu_fp16p(const ncnn::Mat& a, int type_from, int type_to)
  104. {
  105. ncnn::ParamDict pd;
  106. pd.set(0, type_from);
  107. pd.set(1, type_to);
  108. std::vector<ncnn::Mat> weights(0);
  109. ncnn::Option opt;
  110. opt.num_threads = 1;
  111. opt.use_vulkan_compute = true;
  112. opt.use_int8_inference = false;
  113. opt.use_fp16_packed = true;
  114. opt.use_fp16_storage = false;
  115. opt.use_fp16_arithmetic = false;
  116. opt.use_int8_storage = false;
  117. opt.use_int8_arithmetic = false;
  118. opt.use_packing_layout = true;
  119. opt.use_image_storage = false;
  120. ncnn::VulkanDevice* vkdev = ncnn::get_gpu_device();
  121. ncnn::VkAllocator* blob_vkallocator = vkdev->acquire_blob_allocator();
  122. ncnn::VkAllocator* staging_vkallocator = vkdev->acquire_staging_allocator();
  123. opt.blob_vkallocator = blob_vkallocator;
  124. opt.workspace_vkallocator = blob_vkallocator;
  125. opt.staging_vkallocator = staging_vkallocator;
  126. if (!vkdev->info.support_fp16_packed) opt.use_fp16_packed = false;
  127. if (!vkdev->info.support_fp16_storage) opt.use_fp16_storage = false;
  128. ncnn::Layer* op = ncnn::create_layer("Cast");
  129. op->vkdev = vkdev;
  130. op->load_param(pd);
  131. ncnn::ModelBinFromMatArray mb(weights.data());
  132. op->load_model(mb);
  133. op->create_pipeline(opt);
  134. ncnn::Mat a_fp16;
  135. if (type_from == 2)
  136. {
  137. ncnn::cast_float32_to_float16(a, a_fp16, opt);
  138. }
  139. else
  140. {
  141. a_fp16 = a;
  142. }
  143. ncnn::Mat b;
  144. ((ncnn::Cast*)op)->ncnn::Cast::forward(a_fp16, b, opt);
  145. ncnn::Mat d;
  146. // pack
  147. ncnn::Mat a4;
  148. ncnn::convert_packing(a, a4, 4, opt);
  149. ncnn::Mat a4_fp16;
  150. if (type_from == 2 && a4.elempack == 4)
  151. {
  152. ncnn::cast_float32_to_float16(a4, a4_fp16, opt);
  153. }
  154. else
  155. {
  156. a4_fp16 = a4;
  157. }
  158. // forward
  159. ncnn::VkCompute cmd(vkdev);
  160. // upload
  161. ncnn::VkMat a4_gpu;
  162. cmd.record_clone(a4_fp16, a4_gpu, opt);
  163. ncnn::VkMat d4_gpu;
  164. if (op->support_inplace)
  165. {
  166. op->forward_inplace(a4_gpu, cmd, opt);
  167. d4_gpu = a4_gpu;
  168. }
  169. else
  170. {
  171. op->forward(a4_gpu, d4_gpu, cmd, opt);
  172. }
  173. // download
  174. cmd.record_clone(d4_gpu, d, opt);
  175. cmd.submit_and_wait();
  176. op->destroy_pipeline(opt);
  177. delete op;
  178. vkdev->reclaim_blob_allocator(blob_vkallocator);
  179. vkdev->reclaim_staging_allocator(staging_vkallocator);
  180. if (CompareMat(b, d, 0.001) != 0)
  181. {
  182. 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);
  183. return -1;
  184. }
  185. return 0;
  186. }
  187. static int test_cast_gpu_fp16p_pack8(const ncnn::Mat& a, int type_from, int type_to)
  188. {
  189. ncnn::ParamDict pd;
  190. pd.set(0, type_from);
  191. pd.set(1, type_to);
  192. std::vector<ncnn::Mat> weights(0);
  193. ncnn::Option opt;
  194. opt.num_threads = 1;
  195. opt.use_vulkan_compute = true;
  196. opt.use_int8_inference = false;
  197. opt.use_fp16_packed = true;
  198. opt.use_fp16_storage = false;
  199. opt.use_fp16_arithmetic = false;
  200. opt.use_int8_storage = false;
  201. opt.use_int8_arithmetic = false;
  202. opt.use_packing_layout = true;
  203. opt.use_shader_pack8 = true;
  204. opt.use_image_storage = false;
  205. ncnn::VulkanDevice* vkdev = ncnn::get_gpu_device();
  206. ncnn::VkAllocator* blob_vkallocator = vkdev->acquire_blob_allocator();
  207. ncnn::VkAllocator* staging_vkallocator = vkdev->acquire_staging_allocator();
  208. opt.blob_vkallocator = blob_vkallocator;
  209. opt.workspace_vkallocator = blob_vkallocator;
  210. opt.staging_vkallocator = staging_vkallocator;
  211. if (!vkdev->info.support_fp16_packed) opt.use_fp16_packed = false;
  212. if (!vkdev->info.support_fp16_storage) opt.use_fp16_storage = false;
  213. ncnn::Layer* op = ncnn::create_layer("Cast");
  214. op->vkdev = vkdev;
  215. op->load_param(pd);
  216. ncnn::ModelBinFromMatArray mb(weights.data());
  217. op->load_model(mb);
  218. op->create_pipeline(opt);
  219. ncnn::Mat a_fp16;
  220. if (type_from == 2)
  221. {
  222. ncnn::cast_float32_to_float16(a, a_fp16, opt);
  223. }
  224. else
  225. {
  226. a_fp16 = a;
  227. }
  228. ncnn::Mat b;
  229. ((ncnn::Cast*)op)->ncnn::Cast::forward(a_fp16, b, opt);
  230. ncnn::Mat d;
  231. // pack
  232. ncnn::Mat a4;
  233. ncnn::convert_packing(a, a4, 8, opt);
  234. if (a4.elempack != 8)
  235. ncnn::convert_packing(a, a4, 4, opt);
  236. ncnn::Mat a4_fp16;
  237. if (type_from == 2 && (a4.elempack == 4 || a4.elempack == 8))
  238. {
  239. ncnn::cast_float32_to_float16(a4, a4_fp16, opt);
  240. }
  241. else
  242. {
  243. a4_fp16 = a4;
  244. }
  245. // forward
  246. ncnn::VkCompute cmd(vkdev);
  247. // upload
  248. ncnn::VkMat a4_gpu;
  249. cmd.record_clone(a4_fp16, a4_gpu, opt);
  250. ncnn::VkMat d4_gpu;
  251. if (op->support_inplace)
  252. {
  253. op->forward_inplace(a4_gpu, cmd, opt);
  254. d4_gpu = a4_gpu;
  255. }
  256. else
  257. {
  258. op->forward(a4_gpu, d4_gpu, cmd, opt);
  259. }
  260. // download
  261. cmd.record_clone(d4_gpu, d, opt);
  262. cmd.submit_and_wait();
  263. op->destroy_pipeline(opt);
  264. delete op;
  265. vkdev->reclaim_blob_allocator(blob_vkallocator);
  266. vkdev->reclaim_staging_allocator(staging_vkallocator);
  267. if (CompareMat(b, d, 0.001) != 0)
  268. {
  269. 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);
  270. return -1;
  271. }
  272. return 0;
  273. }
  274. static int test_cast_gpu_image_fp16p(const ncnn::Mat& a, int type_from, int type_to)
  275. {
  276. ncnn::ParamDict pd;
  277. pd.set(0, type_from);
  278. pd.set(1, type_to);
  279. std::vector<ncnn::Mat> weights(0);
  280. ncnn::Option opt;
  281. opt.num_threads = 1;
  282. opt.use_vulkan_compute = true;
  283. opt.use_int8_inference = false;
  284. opt.use_fp16_packed = true;
  285. opt.use_fp16_storage = false;
  286. opt.use_fp16_arithmetic = false;
  287. opt.use_int8_storage = false;
  288. opt.use_int8_arithmetic = false;
  289. opt.use_packing_layout = true;
  290. opt.use_image_storage = true;
  291. opt.use_image_fp16_packed = true;
  292. opt.use_image_fp16_storage = false;
  293. opt.use_image_fp16_arithmetic = false;
  294. ncnn::VulkanDevice* vkdev = ncnn::get_gpu_device();
  295. ncnn::VkAllocator* blob_vkallocator = vkdev->acquire_blob_allocator();
  296. ncnn::VkAllocator* staging_vkallocator = vkdev->acquire_staging_allocator();
  297. opt.blob_vkallocator = blob_vkallocator;
  298. opt.workspace_vkallocator = blob_vkallocator;
  299. opt.staging_vkallocator = staging_vkallocator;
  300. if (!vkdev->info.support_fp16_packed) opt.use_fp16_packed = false;
  301. if (!vkdev->info.support_fp16_storage) opt.use_fp16_storage = false;
  302. if (!vkdev->info.support_image_storage) opt.use_image_storage = false;
  303. if (!vkdev->info.support_image_fp16_packed) opt.use_image_fp16_packed = false;
  304. if (!vkdev->info.support_image_fp16_storage) opt.use_image_fp16_storage = false;
  305. ncnn::Layer* op = ncnn::create_layer("Cast");
  306. op->vkdev = vkdev;
  307. op->load_param(pd);
  308. ncnn::ModelBinFromMatArray mb(weights.data());
  309. op->load_model(mb);
  310. op->create_pipeline(opt);
  311. ncnn::Mat a_fp16;
  312. if (type_from == 2)
  313. {
  314. ncnn::cast_float32_to_float16(a, a_fp16, opt);
  315. }
  316. else
  317. {
  318. a_fp16 = a;
  319. }
  320. ncnn::Mat b;
  321. ((ncnn::Cast*)op)->ncnn::Cast::forward(a_fp16, b, opt);
  322. ncnn::Mat d;
  323. // pack
  324. ncnn::Mat a4;
  325. ncnn::convert_packing(a, a4, 4, opt);
  326. ncnn::Mat a4_fp16;
  327. if (type_from == 2 && a4.elempack == 4)
  328. {
  329. ncnn::cast_float32_to_float16(a4, a4_fp16, opt);
  330. }
  331. else
  332. {
  333. a4_fp16 = a4;
  334. }
  335. // forward
  336. ncnn::VkCompute cmd(vkdev);
  337. // upload
  338. ncnn::VkImageMat a4_gpu;
  339. cmd.record_clone(a4_fp16, a4_gpu, opt);
  340. ncnn::VkImageMat d4_gpu;
  341. if (op->support_inplace)
  342. {
  343. op->forward_inplace(a4_gpu, cmd, opt);
  344. d4_gpu = a4_gpu;
  345. }
  346. else
  347. {
  348. op->forward(a4_gpu, d4_gpu, cmd, opt);
  349. }
  350. // download
  351. cmd.record_clone(d4_gpu, d, opt);
  352. cmd.submit_and_wait();
  353. op->destroy_pipeline(opt);
  354. delete op;
  355. vkdev->reclaim_blob_allocator(blob_vkallocator);
  356. vkdev->reclaim_staging_allocator(staging_vkallocator);
  357. if (CompareMat(b, d, 0.001) != 0)
  358. {
  359. fprintf(stderr, "test_cast_gpu_image_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);
  360. return -1;
  361. }
  362. return 0;
  363. }
  364. static int test_cast_gpu_image_fp16p_pack8(const ncnn::Mat& a, int type_from, int type_to)
  365. {
  366. ncnn::ParamDict pd;
  367. pd.set(0, type_from);
  368. pd.set(1, type_to);
  369. std::vector<ncnn::Mat> weights(0);
  370. ncnn::Option opt;
  371. opt.num_threads = 1;
  372. opt.use_vulkan_compute = true;
  373. opt.use_int8_inference = false;
  374. opt.use_fp16_packed = true;
  375. opt.use_fp16_storage = false;
  376. opt.use_fp16_arithmetic = false;
  377. opt.use_int8_storage = false;
  378. opt.use_int8_arithmetic = false;
  379. opt.use_packing_layout = true;
  380. opt.use_shader_pack8 = true;
  381. opt.use_image_storage = true;
  382. opt.use_image_fp16_packed = true;
  383. opt.use_image_fp16_storage = false;
  384. opt.use_image_fp16_arithmetic = false;
  385. ncnn::VulkanDevice* vkdev = ncnn::get_gpu_device();
  386. ncnn::VkAllocator* blob_vkallocator = vkdev->acquire_blob_allocator();
  387. ncnn::VkAllocator* staging_vkallocator = vkdev->acquire_staging_allocator();
  388. opt.blob_vkallocator = blob_vkallocator;
  389. opt.workspace_vkallocator = blob_vkallocator;
  390. opt.staging_vkallocator = staging_vkallocator;
  391. if (!vkdev->info.support_fp16_packed) opt.use_fp16_packed = false;
  392. if (!vkdev->info.support_fp16_storage) opt.use_fp16_storage = false;
  393. if (!vkdev->info.support_image_storage) opt.use_image_storage = false;
  394. if (!vkdev->info.support_image_fp16_packed) opt.use_image_fp16_packed = false;
  395. if (!vkdev->info.support_image_fp16_storage) opt.use_image_fp16_storage = false;
  396. ncnn::Layer* op = ncnn::create_layer("Cast");
  397. op->vkdev = vkdev;
  398. op->load_param(pd);
  399. ncnn::ModelBinFromMatArray mb(weights.data());
  400. op->load_model(mb);
  401. op->create_pipeline(opt);
  402. ncnn::Mat a_fp16;
  403. if (type_from == 2)
  404. {
  405. ncnn::cast_float32_to_float16(a, a_fp16, opt);
  406. }
  407. else
  408. {
  409. a_fp16 = a;
  410. }
  411. ncnn::Mat b;
  412. ((ncnn::Cast*)op)->ncnn::Cast::forward(a_fp16, b, opt);
  413. ncnn::Mat d;
  414. // pack
  415. ncnn::Mat a4;
  416. ncnn::convert_packing(a, a4, 8, opt);
  417. if (a4.elempack != 8)
  418. ncnn::convert_packing(a, a4, 4, opt);
  419. ncnn::Mat a4_fp16;
  420. if (type_from == 2 && (a4.elempack == 4 || a4.elempack == 8))
  421. {
  422. ncnn::cast_float32_to_float16(a4, a4_fp16, opt);
  423. }
  424. else
  425. {
  426. a4_fp16 = a4;
  427. }
  428. // forward
  429. ncnn::VkCompute cmd(vkdev);
  430. // upload
  431. ncnn::VkImageMat a4_gpu;
  432. cmd.record_clone(a4_fp16, a4_gpu, opt);
  433. ncnn::VkImageMat d4_gpu;
  434. if (op->support_inplace)
  435. {
  436. op->forward_inplace(a4_gpu, cmd, opt);
  437. d4_gpu = a4_gpu;
  438. }
  439. else
  440. {
  441. op->forward(a4_gpu, d4_gpu, cmd, opt);
  442. }
  443. // download
  444. cmd.record_clone(d4_gpu, d, opt);
  445. cmd.submit_and_wait();
  446. op->destroy_pipeline(opt);
  447. delete op;
  448. vkdev->reclaim_blob_allocator(blob_vkallocator);
  449. vkdev->reclaim_staging_allocator(staging_vkallocator);
  450. if (CompareMat(b, d, 0.001) != 0)
  451. {
  452. fprintf(stderr, "test_cast_gpu_image_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);
  453. return -1;
  454. }
  455. return 0;
  456. }
  457. #endif // NCNN_VULKAN
  458. static int test_cast(const ncnn::Mat& a, int type_from, int type_to)
  459. {
  460. return 0
  461. || test_cast_cpu(a, type_from, type_to)
  462. || test_cast_cpu_packed(a, type_from, type_to)
  463. #if NCNN_VULKAN
  464. || test_cast_gpu_fp16p(a, type_from, type_to)
  465. || test_cast_gpu_fp16p_pack8(a, type_from, type_to)
  466. || test_cast_gpu_image_fp16p(a, type_from, type_to)
  467. || test_cast_gpu_image_fp16p_pack8(a, type_from, type_to)
  468. #endif // NCNN_VULKAN
  469. ;
  470. }
  471. static int test_cast_0()
  472. {
  473. return 0
  474. || test_cast(RandomMat(6, 7, 16), 1, 2)
  475. || test_cast(RandomMat(3, 5, 13), 1, 2)
  476. || test_cast(RandomMat(6, 7, 16), 2, 1)
  477. || test_cast(RandomMat(3, 5, 13), 2, 1)
  478. ;
  479. }
  480. static int test_cast_1()
  481. {
  482. return 0
  483. || test_cast(RandomMat(6, 16), 1, 2)
  484. || test_cast(RandomMat(7, 15), 1, 2)
  485. || test_cast(RandomMat(6, 16), 2, 1)
  486. || test_cast(RandomMat(7, 15), 2, 1)
  487. ;
  488. }
  489. static int test_cast_2()
  490. {
  491. return 0
  492. || test_cast(RandomMat(128), 1, 2)
  493. || test_cast(RandomMat(127), 1, 2)
  494. || test_cast(RandomMat(128), 2, 1)
  495. || test_cast(RandomMat(127), 2, 1)
  496. ;
  497. }
  498. int main()
  499. {
  500. SRAND(7767517);
  501. return 0
  502. || test_cast_0()
  503. || test_cast_1()
  504. || test_cast_2()
  505. ;
  506. }