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.

reorg_vulkan.cpp 3.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. // Tencent is pleased to support the open source community by making ncnn available.
  2. //
  3. // Copyright (C) 2019 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 "reorg_vulkan.h"
  15. namespace ncnn {
  16. DEFINE_LAYER_CREATOR(Reorg_vulkan)
  17. Reorg_vulkan::Reorg_vulkan()
  18. {
  19. support_vulkan = true;
  20. pipeline_reorg = 0;
  21. pipeline_reorg_pack4 = 0;
  22. pipeline_reorg_pack1to4 = 0;
  23. }
  24. int Reorg_vulkan::create_pipeline(const Option& opt)
  25. {
  26. std::vector<vk_specialization_type> specializations(1);
  27. specializations[0].i = stride;
  28. // pack1
  29. {
  30. pipeline_reorg = new Pipeline(vkdev);
  31. pipeline_reorg->set_optimal_local_size_xyz();
  32. pipeline_reorg->create("reorg", opt, specializations, 2, 10);
  33. }
  34. // pack4
  35. {
  36. pipeline_reorg_pack4 = new Pipeline(vkdev);
  37. pipeline_reorg_pack4->set_optimal_local_size_xyz();
  38. pipeline_reorg_pack4->create("reorg_pack4", opt, specializations, 2, 10);
  39. }
  40. // pack1to4
  41. {
  42. pipeline_reorg_pack1to4 = new Pipeline(vkdev);
  43. pipeline_reorg_pack1to4->set_optimal_local_size_xyz();
  44. pipeline_reorg_pack1to4->create("reorg_pack1to4", opt, specializations, 2, 10);
  45. }
  46. return 0;
  47. }
  48. int Reorg_vulkan::destroy_pipeline(const Option& opt)
  49. {
  50. delete pipeline_reorg;
  51. pipeline_reorg = 0;
  52. delete pipeline_reorg_pack4;
  53. pipeline_reorg_pack4 = 0;
  54. delete pipeline_reorg_pack1to4;
  55. pipeline_reorg_pack1to4 = 0;
  56. return 0;
  57. }
  58. int Reorg_vulkan::forward(const VkMat& bottom_blob, VkMat& top_blob, VkCompute& cmd, const Option& opt) const
  59. {
  60. int w = bottom_blob.w;
  61. int h = bottom_blob.h;
  62. int channels = bottom_blob.c;
  63. size_t elemsize = bottom_blob.elemsize;
  64. int packing = bottom_blob.packing;
  65. int outw = w / stride;
  66. int outh = h / stride;
  67. int outc = channels * packing * stride * stride;
  68. int out_packing = outc % 4 == 0 ? 4 : 1;
  69. size_t out_elemsize = elemsize / packing * out_packing;
  70. if (opt.use_fp16_packed && !opt.use_fp16_storage)
  71. {
  72. if (out_packing == 4) out_elemsize = 4*2u;
  73. if (out_packing == 1) out_elemsize = 4u;
  74. }
  75. top_blob.create(outw, outh, outc / out_packing, out_elemsize, out_packing, opt.blob_vkallocator, opt.staging_vkallocator);
  76. if (top_blob.empty())
  77. return -100;
  78. std::vector<VkMat> bindings(2);
  79. bindings[0] = bottom_blob;
  80. bindings[1] = top_blob;
  81. std::vector<vk_constant_type> constants(10);
  82. constants[0].i = bottom_blob.dims;
  83. constants[1].i = bottom_blob.w;
  84. constants[2].i = bottom_blob.h;
  85. constants[3].i = bottom_blob.c;
  86. constants[4].i = bottom_blob.cstep;
  87. constants[5].i = top_blob.dims;
  88. constants[6].i = top_blob.w;
  89. constants[7].i = top_blob.h;
  90. constants[8].i = top_blob.c;
  91. constants[9].i = top_blob.cstep;
  92. const Pipeline* pipeline = 0;
  93. if (packing == 1 && out_packing == 1)
  94. {
  95. pipeline = pipeline_reorg;
  96. }
  97. else if (packing == 4) // assert out_packing == 4
  98. {
  99. pipeline = pipeline_reorg_pack4;
  100. }
  101. else if (packing == 1 && out_packing == 4)
  102. {
  103. pipeline = pipeline_reorg_pack1to4;
  104. }
  105. cmd.record_pipeline(pipeline, bindings, constants, top_blob);
  106. return 0;
  107. }
  108. } // namespace ncnn