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 15 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651
  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. ncnn::VulkanDevice* vkdev = ncnn::get_gpu_device();
  292. ncnn::VkAllocator* blob_vkallocator = vkdev->acquire_blob_allocator();
  293. ncnn::VkAllocator* staging_vkallocator = vkdev->acquire_staging_allocator();
  294. opt.blob_vkallocator = blob_vkallocator;
  295. opt.workspace_vkallocator = blob_vkallocator;
  296. opt.staging_vkallocator = staging_vkallocator;
  297. if (!vkdev->info.support_fp16_packed) opt.use_fp16_packed = false;
  298. if (!vkdev->info.support_fp16_storage) opt.use_fp16_storage = false;
  299. ncnn::Layer* op = ncnn::create_layer("Cast");
  300. op->vkdev = vkdev;
  301. op->load_param(pd);
  302. ncnn::ModelBinFromMatArray mb(weights.data());
  303. op->load_model(mb);
  304. op->create_pipeline(opt);
  305. ncnn::Mat a_fp16;
  306. if (type_from == 2)
  307. {
  308. ncnn::cast_float32_to_float16(a, a_fp16, opt);
  309. }
  310. else
  311. {
  312. a_fp16 = a;
  313. }
  314. ncnn::Mat b;
  315. ((ncnn::Cast*)op)->ncnn::Cast::forward(a_fp16, b, opt);
  316. ncnn::Mat d;
  317. // pack
  318. ncnn::Mat a4;
  319. ncnn::convert_packing(a, a4, 4, opt);
  320. ncnn::Mat a4_fp16;
  321. if (type_from == 2 && a4.elempack == 4)
  322. {
  323. ncnn::cast_float32_to_float16(a4, a4_fp16, opt);
  324. }
  325. else
  326. {
  327. a4_fp16 = a4;
  328. }
  329. // forward
  330. ncnn::VkCompute cmd(vkdev);
  331. // upload
  332. ncnn::VkImageMat a4_gpu;
  333. cmd.record_clone(a4_fp16, a4_gpu, opt);
  334. ncnn::VkImageMat d4_gpu;
  335. if (op->support_inplace)
  336. {
  337. op->forward_inplace(a4_gpu, cmd, opt);
  338. d4_gpu = a4_gpu;
  339. }
  340. else
  341. {
  342. op->forward(a4_gpu, d4_gpu, cmd, opt);
  343. }
  344. // download
  345. cmd.record_clone(d4_gpu, d, opt);
  346. cmd.submit_and_wait();
  347. op->destroy_pipeline(opt);
  348. delete op;
  349. vkdev->reclaim_blob_allocator(blob_vkallocator);
  350. vkdev->reclaim_staging_allocator(staging_vkallocator);
  351. if (CompareMat(b, d, 0.001) != 0)
  352. {
  353. 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);
  354. return -1;
  355. }
  356. return 0;
  357. }
  358. static int test_cast_gpu_image_fp16p_pack8(const ncnn::Mat& a, int type_from, int type_to)
  359. {
  360. ncnn::ParamDict pd;
  361. pd.set(0, type_from);
  362. pd.set(1, type_to);
  363. std::vector<ncnn::Mat> weights(0);
  364. ncnn::Option opt;
  365. opt.num_threads = 1;
  366. opt.use_vulkan_compute = true;
  367. opt.use_int8_inference = false;
  368. opt.use_fp16_packed = true;
  369. opt.use_fp16_storage = false;
  370. opt.use_fp16_arithmetic = false;
  371. opt.use_int8_storage = false;
  372. opt.use_int8_arithmetic = false;
  373. opt.use_packing_layout = true;
  374. opt.use_shader_pack8 = true;
  375. opt.use_image_storage = true;
  376. ncnn::VulkanDevice* vkdev = ncnn::get_gpu_device();
  377. ncnn::VkAllocator* blob_vkallocator = vkdev->acquire_blob_allocator();
  378. ncnn::VkAllocator* staging_vkallocator = vkdev->acquire_staging_allocator();
  379. opt.blob_vkallocator = blob_vkallocator;
  380. opt.workspace_vkallocator = blob_vkallocator;
  381. opt.staging_vkallocator = staging_vkallocator;
  382. if (!vkdev->info.support_fp16_packed) opt.use_fp16_packed = false;
  383. if (!vkdev->info.support_fp16_storage) opt.use_fp16_storage = false;
  384. ncnn::Layer* op = ncnn::create_layer("Cast");
  385. op->vkdev = vkdev;
  386. op->load_param(pd);
  387. ncnn::ModelBinFromMatArray mb(weights.data());
  388. op->load_model(mb);
  389. op->create_pipeline(opt);
  390. ncnn::Mat a_fp16;
  391. if (type_from == 2)
  392. {
  393. ncnn::cast_float32_to_float16(a, a_fp16, opt);
  394. }
  395. else
  396. {
  397. a_fp16 = a;
  398. }
  399. ncnn::Mat b;
  400. ((ncnn::Cast*)op)->ncnn::Cast::forward(a_fp16, b, opt);
  401. ncnn::Mat d;
  402. // pack
  403. ncnn::Mat a4;
  404. ncnn::convert_packing(a, a4, 8, opt);
  405. if (a4.elempack != 8)
  406. ncnn::convert_packing(a, a4, 4, opt);
  407. ncnn::Mat a4_fp16;
  408. if (type_from == 2 && (a4.elempack == 4 || a4.elempack == 8))
  409. {
  410. ncnn::cast_float32_to_float16(a4, a4_fp16, opt);
  411. }
  412. else
  413. {
  414. a4_fp16 = a4;
  415. }
  416. // forward
  417. ncnn::VkCompute cmd(vkdev);
  418. // upload
  419. ncnn::VkImageMat a4_gpu;
  420. cmd.record_clone(a4_fp16, a4_gpu, opt);
  421. ncnn::VkImageMat d4_gpu;
  422. if (op->support_inplace)
  423. {
  424. op->forward_inplace(a4_gpu, cmd, opt);
  425. d4_gpu = a4_gpu;
  426. }
  427. else
  428. {
  429. op->forward(a4_gpu, d4_gpu, cmd, opt);
  430. }
  431. // download
  432. cmd.record_clone(d4_gpu, d, opt);
  433. cmd.submit_and_wait();
  434. op->destroy_pipeline(opt);
  435. delete op;
  436. vkdev->reclaim_blob_allocator(blob_vkallocator);
  437. vkdev->reclaim_staging_allocator(staging_vkallocator);
  438. if (CompareMat(b, d, 0.001) != 0)
  439. {
  440. 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);
  441. return -1;
  442. }
  443. return 0;
  444. }
  445. #endif // NCNN_VULKAN
  446. static int test_cast(const ncnn::Mat& a, int type_from, int type_to)
  447. {
  448. return 0
  449. || test_cast_cpu(a, type_from, type_to)
  450. || test_cast_cpu_packed(a, type_from, type_to)
  451. #if NCNN_VULKAN
  452. || test_cast_gpu_fp16p(a, type_from, type_to)
  453. || test_cast_gpu_fp16p_pack8(a, type_from, type_to)
  454. || test_cast_gpu_image_fp16p(a, type_from, type_to)
  455. || test_cast_gpu_image_fp16p_pack8(a, type_from, type_to)
  456. #endif // NCNN_VULKAN
  457. ;
  458. }
  459. static int test_cast_0()
  460. {
  461. return 0
  462. || test_cast(RandomMat(6, 7, 16), 1, 2)
  463. || test_cast(RandomMat(3, 5, 13), 1, 2)
  464. || test_cast(RandomMat(6, 7, 16), 2, 1)
  465. || test_cast(RandomMat(3, 5, 13), 2, 1)
  466. ;
  467. }
  468. static int test_cast_1()
  469. {
  470. return 0
  471. || test_cast(RandomMat(6, 16), 1, 2)
  472. || test_cast(RandomMat(7, 15), 1, 2)
  473. || test_cast(RandomMat(6, 16), 2, 1)
  474. || test_cast(RandomMat(7, 15), 2, 1)
  475. ;
  476. }
  477. static int test_cast_2()
  478. {
  479. return 0
  480. || test_cast(RandomMat(128), 1, 2)
  481. || test_cast(RandomMat(127), 1, 2)
  482. || test_cast(RandomMat(128), 2, 1)
  483. || test_cast(RandomMat(127), 2, 1)
  484. ;
  485. }
  486. int main()
  487. {
  488. SRAND(7767517);
  489. return 0
  490. || test_cast_0()
  491. || test_cast_1()
  492. || test_cast_2()
  493. ;
  494. }