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