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