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.

lrn_vulkan.cpp 7.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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 "lrn_vulkan.h"
  15. #include <math.h>
  16. namespace ncnn {
  17. DEFINE_LAYER_CREATOR(LRN_vulkan)
  18. LRN_vulkan::LRN_vulkan()
  19. {
  20. support_vulkan = true;
  21. pipeline_lrn_square_pad = 0;
  22. pipeline_lrn_norm = 0;
  23. pipeline_lrn_square_pad_across_channel_pack4 = 0;
  24. pipeline_lrn_norm_across_channel_pack4 = 0;
  25. pipeline_lrn_square_pad_within_channel_pack4 = 0;
  26. pipeline_lrn_norm_within_channel_pack4 = 0;
  27. }
  28. int LRN_vulkan::create_pipeline(const Option& opt)
  29. {
  30. {
  31. pipeline_lrn_square_pad = new Pipeline(vkdev);
  32. pipeline_lrn_square_pad->set_optimal_local_size_xyz();
  33. std::vector<vk_specialization_type> specializations(3);
  34. specializations[0].i = region_type;
  35. int pad = local_size / 2;
  36. if (pad == 0)
  37. {
  38. specializations[1].i = 0;
  39. specializations[2].i = 0;
  40. }
  41. else
  42. {
  43. specializations[1].i = pad;
  44. specializations[2].i = local_size - pad - 1;
  45. }
  46. pipeline_lrn_square_pad->create("lrn_square_pad", opt, specializations, 2, 10);
  47. // pack4
  48. if (region_type == 0)
  49. {
  50. pipeline_lrn_square_pad_across_channel_pack4 = new Pipeline(vkdev);
  51. pipeline_lrn_square_pad_across_channel_pack4->set_optimal_local_size_xyz();
  52. pipeline_lrn_square_pad_across_channel_pack4->create("lrn_square_pad_across_channel_pack4", opt, specializations, 2, 10);
  53. }
  54. if (region_type == 1)
  55. {
  56. pipeline_lrn_square_pad_within_channel_pack4 = new Pipeline(vkdev);
  57. pipeline_lrn_square_pad_within_channel_pack4->set_optimal_local_size_xyz();
  58. pipeline_lrn_square_pad_within_channel_pack4->create("lrn_square_pad_within_channel_pack4", opt, specializations, 2, 10);
  59. }
  60. }
  61. {
  62. pipeline_lrn_norm = new Pipeline(vkdev);
  63. pipeline_lrn_norm->set_optimal_local_size_xyz();
  64. std::vector<vk_specialization_type> specializations(5);
  65. specializations[0].i = region_type;
  66. specializations[1].i = local_size;
  67. specializations[2].f = alpha;
  68. specializations[3].f = beta;
  69. specializations[4].f = bias;
  70. pipeline_lrn_norm->create("lrn_norm", opt, specializations, 2, 10);
  71. // pack4
  72. if (region_type == 0)
  73. {
  74. pipeline_lrn_norm_across_channel_pack4 = new Pipeline(vkdev);
  75. pipeline_lrn_norm_across_channel_pack4->set_optimal_local_size_xyz();
  76. pipeline_lrn_norm_across_channel_pack4->create("lrn_norm_across_channel_pack4", opt, specializations, 2, 10);
  77. }
  78. if (region_type == 1)
  79. {
  80. pipeline_lrn_norm_within_channel_pack4 = new Pipeline(vkdev);
  81. pipeline_lrn_norm_within_channel_pack4->set_optimal_local_size_xyz();
  82. pipeline_lrn_norm_within_channel_pack4->create("lrn_norm_within_channel_pack4", opt, specializations, 2, 10);
  83. }
  84. }
  85. return 0;
  86. }
  87. int LRN_vulkan::destroy_pipeline(const Option& opt)
  88. {
  89. delete pipeline_lrn_square_pad;
  90. pipeline_lrn_square_pad = 0;
  91. delete pipeline_lrn_norm;
  92. pipeline_lrn_norm = 0;
  93. delete pipeline_lrn_square_pad_across_channel_pack4;
  94. pipeline_lrn_square_pad_across_channel_pack4 = 0;
  95. delete pipeline_lrn_norm_across_channel_pack4;
  96. pipeline_lrn_norm_across_channel_pack4 = 0;
  97. delete pipeline_lrn_square_pad_within_channel_pack4;
  98. pipeline_lrn_square_pad_within_channel_pack4 = 0;
  99. delete pipeline_lrn_norm_within_channel_pack4;
  100. pipeline_lrn_norm_within_channel_pack4 = 0;
  101. return 0;
  102. }
  103. int LRN_vulkan::forward_inplace(VkMat& bottom_top_blob, VkCompute& cmd, const Option& opt) const
  104. {
  105. int w = bottom_top_blob.w;
  106. int h = bottom_top_blob.h;
  107. int channels = bottom_top_blob.c;
  108. size_t elemsize = bottom_top_blob.elemsize;
  109. int packing = bottom_top_blob.packing;
  110. VkMat square_workspace;
  111. int pad = local_size / 2;
  112. if (pad == 0)
  113. {
  114. square_workspace.create(w, h, channels, elemsize, packing, opt.workspace_vkallocator, opt.staging_vkallocator);
  115. }
  116. else if (region_type == NormRegion_ACROSS_CHANNELS)
  117. {
  118. // always create scalar square workspace blob for norm across channel
  119. square_workspace.create(w, h, channels * packing + local_size - 1, 4u, 1, opt.workspace_vkallocator, opt.staging_vkallocator);
  120. }
  121. else if (region_type == NormRegion_WITHIN_CHANNEL)
  122. {
  123. square_workspace.create(w + local_size - 1, h + local_size - 1, channels, elemsize, packing, opt.workspace_vkallocator, opt.staging_vkallocator);
  124. }
  125. // square pad
  126. {
  127. std::vector<VkMat> bindings(2);
  128. bindings[0] = bottom_top_blob;
  129. bindings[1] = square_workspace;
  130. std::vector<vk_constant_type> constants(10);
  131. constants[0].i = bottom_top_blob.dims;
  132. constants[1].i = bottom_top_blob.w;
  133. constants[2].i = bottom_top_blob.h;
  134. constants[3].i = bottom_top_blob.c;
  135. constants[4].i = bottom_top_blob.cstep;
  136. constants[5].i = square_workspace.dims;
  137. constants[6].i = square_workspace.w;
  138. constants[7].i = square_workspace.h;
  139. constants[8].i = square_workspace.c;
  140. constants[9].i = square_workspace.cstep;
  141. const Pipeline* pipeline = 0;
  142. if (packing == 4)
  143. {
  144. if (region_type == 0) pipeline = pipeline_lrn_square_pad_across_channel_pack4;
  145. if (region_type == 1) pipeline = pipeline_lrn_square_pad_within_channel_pack4;
  146. }
  147. else
  148. {
  149. pipeline = pipeline_lrn_square_pad;
  150. }
  151. cmd.record_pipeline(pipeline, bindings, constants, square_workspace);
  152. }
  153. // norm
  154. {
  155. std::vector<VkMat> bindings(2);
  156. bindings[0] = square_workspace;
  157. bindings[1] = bottom_top_blob;
  158. std::vector<vk_constant_type> constants(10);
  159. constants[0].i = square_workspace.dims;
  160. constants[1].i = square_workspace.w;
  161. constants[2].i = square_workspace.h;
  162. constants[3].i = square_workspace.c;
  163. constants[4].i = square_workspace.cstep;
  164. constants[5].i = bottom_top_blob.dims;
  165. constants[6].i = bottom_top_blob.w;
  166. constants[7].i = bottom_top_blob.h;
  167. constants[8].i = bottom_top_blob.c;
  168. constants[9].i = bottom_top_blob.cstep;
  169. const Pipeline* pipeline = 0;
  170. if (packing == 4)
  171. {
  172. if (region_type == 0) pipeline = pipeline_lrn_norm_across_channel_pack4;
  173. if (region_type == 1) pipeline = pipeline_lrn_norm_within_channel_pack4;
  174. }
  175. else
  176. {
  177. pipeline = pipeline_lrn_norm;
  178. }
  179. cmd.record_pipeline(pipeline, bindings, constants, bottom_top_blob);
  180. }
  181. return 0;
  182. }
  183. } // namespace ncnn