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.

prelu_vulkan.cpp 6.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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 "prelu_vulkan.h"
  15. #include "layer_shader_type.h"
  16. namespace ncnn {
  17. PReLU_vulkan::PReLU_vulkan()
  18. {
  19. support_vulkan = true;
  20. support_image_storage = true;
  21. pipeline_prelu = 0;
  22. pipeline_prelu_pack4 = 0;
  23. pipeline_prelu_pack8 = 0;
  24. }
  25. int PReLU_vulkan::create_pipeline(const Option& opt)
  26. {
  27. const Mat& shape = top_shapes.empty() ? Mat() : top_shapes[0];
  28. int elempack = 1;
  29. if (shape.dims == 0) elempack = opt.use_shader_pack8 && num_slope % 8 == 0 ? 8 : num_slope % 4 == 0 ? 4 : 1;
  30. if (shape.dims == 1) elempack = opt.use_shader_pack8 && shape.w % 8 == 0 ? 8 : shape.w % 4 == 0 ? 4 : 1;
  31. if (shape.dims == 2) elempack = opt.use_shader_pack8 && shape.h % 8 == 0 ? 8 : shape.h % 4 == 0 ? 4 : 1;
  32. if (shape.dims == 3) elempack = opt.use_shader_pack8 && shape.c % 8 == 0 ? 8 : shape.c % 4 == 0 ? 4 : 1;
  33. size_t elemsize;
  34. if (opt.use_fp16_storage)
  35. {
  36. elemsize = elempack * 2u;
  37. }
  38. else if (opt.use_fp16_packed)
  39. {
  40. elemsize = elempack == 1 ? 4u : elempack * 2u;
  41. }
  42. else
  43. {
  44. elemsize = elempack * 4u;
  45. }
  46. Mat shape_packed;
  47. if (shape.dims == 1) shape_packed = Mat(shape.w / elempack, (void*)0, elemsize, elempack);
  48. if (shape.dims == 2) shape_packed = Mat(shape.w, shape.h / elempack, (void*)0, elemsize, elempack);
  49. if (shape.dims == 3) shape_packed = Mat(shape.w, shape.h, shape.c / elempack, (void*)0, elemsize, elempack);
  50. std::vector<vk_specialization_type> specializations(2 + 5);
  51. specializations[0].i = num_slope;
  52. specializations[1].f = num_slope == 1 ? slope_data[0] : 1.f;
  53. specializations[2 + 0].i = shape_packed.dims;
  54. specializations[2 + 1].i = shape_packed.w;
  55. specializations[2 + 2].i = shape_packed.h;
  56. specializations[2 + 3].i = shape_packed.c;
  57. specializations[2 + 4].i = shape_packed.cstep;
  58. Mat local_size_xyz(4, 4, std::min(4, num_slope / elempack), (void*)0);
  59. if (shape_packed.dims == 1)
  60. {
  61. local_size_xyz.w = std::min(64, shape_packed.w);
  62. local_size_xyz.h = 1;
  63. local_size_xyz.c = 1;
  64. }
  65. if (shape_packed.dims == 2)
  66. {
  67. local_size_xyz.w = std::min(8, shape_packed.w);
  68. local_size_xyz.h = std::min(8, shape_packed.h);
  69. local_size_xyz.c = 1;
  70. }
  71. if (shape_packed.dims == 3)
  72. {
  73. local_size_xyz.w = std::min(4, shape_packed.w);
  74. local_size_xyz.h = std::min(4, shape_packed.h);
  75. local_size_xyz.c = std::min(4, shape_packed.c);
  76. }
  77. // pack1
  78. if (num_slope == 1 || elempack == 1)
  79. {
  80. pipeline_prelu = new Pipeline(vkdev);
  81. pipeline_prelu->set_optimal_local_size_xyz(local_size_xyz);
  82. pipeline_prelu->create(LayerShaderType::prelu, opt, specializations);
  83. }
  84. // pack4
  85. if (num_slope == 1 || elempack == 4)
  86. {
  87. pipeline_prelu_pack4 = new Pipeline(vkdev);
  88. pipeline_prelu_pack4->set_optimal_local_size_xyz(local_size_xyz);
  89. pipeline_prelu_pack4->create(LayerShaderType::prelu_pack4, opt, specializations);
  90. }
  91. // pack8
  92. if ((opt.use_shader_pack8 && num_slope == 1) || elempack == 8)
  93. {
  94. pipeline_prelu_pack8 = new Pipeline(vkdev);
  95. pipeline_prelu_pack8->set_optimal_local_size_xyz(local_size_xyz);
  96. pipeline_prelu_pack8->create(LayerShaderType::prelu_pack8, opt, specializations);
  97. }
  98. return 0;
  99. }
  100. int PReLU_vulkan::destroy_pipeline(const Option& /*opt*/)
  101. {
  102. delete pipeline_prelu;
  103. pipeline_prelu = 0;
  104. delete pipeline_prelu_pack4;
  105. pipeline_prelu_pack4 = 0;
  106. delete pipeline_prelu_pack8;
  107. pipeline_prelu_pack8 = 0;
  108. return 0;
  109. }
  110. int PReLU_vulkan::upload_model(VkTransfer& cmd, const Option& opt)
  111. {
  112. if (num_slope > 1)
  113. {
  114. int elempack = opt.use_shader_pack8 && num_slope % 8 == 0 ? 8 : num_slope % 4 == 0 ? 4 : 1;
  115. Mat slope_data_packed;
  116. convert_packing(slope_data, slope_data_packed, elempack, opt);
  117. if (opt.use_image_storage)
  118. {
  119. cmd.record_upload(slope_data_packed, slope_data_gpu_image, opt);
  120. }
  121. else
  122. {
  123. cmd.record_upload(slope_data_packed, slope_data_gpu, opt);
  124. }
  125. if (opt.lightmode)
  126. {
  127. slope_data.release();
  128. }
  129. }
  130. return 0;
  131. }
  132. int PReLU_vulkan::forward_inplace(VkMat& bottom_top_blob, VkCompute& cmd, const Option& /*opt*/) const
  133. {
  134. int elempack = bottom_top_blob.elempack;
  135. std::vector<VkMat> bindings(2);
  136. bindings[0] = bottom_top_blob;
  137. bindings[1] = slope_data_gpu;
  138. std::vector<vk_constant_type> constants(5);
  139. constants[0].i = bottom_top_blob.dims;
  140. constants[1].i = bottom_top_blob.w;
  141. constants[2].i = bottom_top_blob.h;
  142. constants[3].i = bottom_top_blob.c;
  143. constants[4].i = bottom_top_blob.cstep;
  144. const Pipeline* pipeline = elempack == 8 ? pipeline_prelu_pack8
  145. : elempack == 4 ? pipeline_prelu_pack4
  146. : pipeline_prelu;
  147. cmd.record_pipeline(pipeline, bindings, constants, bottom_top_blob);
  148. return 0;
  149. }
  150. int PReLU_vulkan::forward_inplace(VkImageMat& bottom_top_blob, VkCompute& cmd, const Option& /*opt*/) const
  151. {
  152. int elempack = bottom_top_blob.elempack;
  153. std::vector<VkImageMat> bindings(3);
  154. bindings[0] = bottom_top_blob;
  155. bindings[1] = bottom_top_blob;
  156. bindings[2] = slope_data_gpu_image;
  157. std::vector<vk_constant_type> constants(5);
  158. constants[0].i = bottom_top_blob.dims;
  159. constants[1].i = bottom_top_blob.w;
  160. constants[2].i = bottom_top_blob.h;
  161. constants[3].i = bottom_top_blob.c;
  162. constants[4].i = 0; //bottom_top_blob.cstep;
  163. const Pipeline* pipeline = elempack == 8 ? pipeline_prelu_pack8
  164. : elempack == 4 ? pipeline_prelu_pack4
  165. : pipeline_prelu;
  166. cmd.record_pipeline(pipeline, bindings, constants, bottom_top_blob);
  167. return 0;
  168. }
  169. } // namespace ncnn