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