You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_cast.cpp 16 kB

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