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.

normalize_vulkan.cpp 17 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  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 "normalize_vulkan.h"
  15. #include <algorithm>
  16. #include "layer_shader_type.h"
  17. namespace ncnn {
  18. DEFINE_LAYER_CREATOR(Normalize_vulkan)
  19. Normalize_vulkan::Normalize_vulkan()
  20. {
  21. support_vulkan = true;
  22. pipeline_normalize_reduce_sum4_fp16_to_fp32 = 0;
  23. pipeline_normalize_reduce_sum4_fp32[0] = 0;
  24. pipeline_normalize_reduce_sum4_fp32[1] = 0;
  25. pipeline_normalize_coeffs = 0;
  26. pipeline_normalize_norm = 0;
  27. pipeline_normalize_reduce_sum4_fp16_to_fp32_pack4 = 0;
  28. pipeline_normalize_reduce_sum4_fp32_pack4[0] = 0;
  29. pipeline_normalize_reduce_sum4_fp32_pack4[1] = 0;
  30. pipeline_normalize_coeffs_pack4 = 0;
  31. pipeline_normalize_norm_pack4 = 0;
  32. pipeline_normalize_reduce_sum4_fp16_to_fp32_pack8 = 0;
  33. pipeline_normalize_reduce_sum4_fp32_pack8[0] = 0;
  34. pipeline_normalize_reduce_sum4_fp32_pack8[1] = 0;
  35. pipeline_normalize_coeffs_pack8 = 0;
  36. pipeline_normalize_norm_pack8 = 0;
  37. }
  38. int Normalize_vulkan::create_pipeline(const Option& opt)
  39. {
  40. const Mat& shape = top_shapes.empty() ? Mat() : top_shapes[0];
  41. int elempack = 1;
  42. if (shape.dims == 1) elempack = opt.use_shader_pack8 && shape.w % 8 == 0 ? 8 : shape.w % 4 == 0 ? 4 : 1;
  43. if (shape.dims == 2) elempack = opt.use_shader_pack8 && shape.h % 8 == 0 ? 8 : shape.h % 4 == 0 ? 4 : 1;
  44. if (shape.dims == 3) elempack = opt.use_shader_pack8 && shape.c % 8 == 0 ? 8 : shape.c % 4 == 0 ? 4 : 1;
  45. size_t elemsize;
  46. if (opt.use_fp16_storage)
  47. {
  48. elemsize = elempack * 2u;
  49. }
  50. else if (opt.use_fp16_packed)
  51. {
  52. elemsize = elempack == 1 ? 4u : elempack * 2u;
  53. }
  54. else
  55. {
  56. elemsize = elempack * 4u;
  57. }
  58. Mat shape_packed;
  59. if (shape.dims == 1) shape_packed = Mat(shape.w / elempack, (void*)0, elemsize, elempack);
  60. if (shape.dims == 2) shape_packed = Mat(shape.w, shape.h / elempack, (void*)0, elemsize, elempack);
  61. if (shape.dims == 3) shape_packed = Mat(shape.w, shape.h, shape.c / elempack, (void*)0, elemsize, elempack);
  62. {
  63. std::vector<vk_specialization_type> specializations(2);
  64. specializations[0].i = across_spatial;
  65. specializations[1].i = across_channel;
  66. Mat local_size_xyz;// TODO select by across_channel / across_spatial
  67. // pack1
  68. if (shape.dims == 0 || elempack == 1)
  69. {
  70. pipeline_normalize_reduce_sum4_fp16_to_fp32 = new Pipeline(vkdev);
  71. pipeline_normalize_reduce_sum4_fp16_to_fp32->set_optimal_local_size_xyz(local_size_xyz);
  72. pipeline_normalize_reduce_sum4_fp16_to_fp32->create(LayerShaderType::normalize_reduce_sum4_fp16_to_fp32, opt, specializations);
  73. pipeline_normalize_reduce_sum4_fp32[0] = new Pipeline(vkdev);
  74. pipeline_normalize_reduce_sum4_fp32[0]->set_optimal_local_size_xyz(local_size_xyz);
  75. pipeline_normalize_reduce_sum4_fp32[0]->create(LayerShaderType::normalize_reduce_sum4_fp32, opt, specializations);
  76. pipeline_normalize_reduce_sum4_fp32[1] = new Pipeline(vkdev);
  77. pipeline_normalize_reduce_sum4_fp32[1]->set_optimal_local_size_xyz(local_size_xyz);
  78. pipeline_normalize_reduce_sum4_fp32[1]->create(LayerShaderType::normalize_reduce_sum4_fp32, opt, specializations);
  79. }
  80. // pack4
  81. if (shape.dims == 0 || elempack == 4)
  82. {
  83. pipeline_normalize_reduce_sum4_fp16_to_fp32_pack4 = new Pipeline(vkdev);
  84. pipeline_normalize_reduce_sum4_fp16_to_fp32_pack4->set_optimal_local_size_xyz(local_size_xyz);
  85. pipeline_normalize_reduce_sum4_fp16_to_fp32_pack4->create(LayerShaderType::normalize_reduce_sum4_fp16_to_fp32_pack4, opt, specializations);
  86. pipeline_normalize_reduce_sum4_fp32_pack4[0] = new Pipeline(vkdev);
  87. pipeline_normalize_reduce_sum4_fp32_pack4[0]->set_optimal_local_size_xyz(local_size_xyz);
  88. pipeline_normalize_reduce_sum4_fp32_pack4[0]->create(LayerShaderType::normalize_reduce_sum4_fp32_pack4, opt, specializations);
  89. pipeline_normalize_reduce_sum4_fp32_pack4[1] = new Pipeline(vkdev);
  90. pipeline_normalize_reduce_sum4_fp32_pack4[1]->set_optimal_local_size_xyz(local_size_xyz);
  91. pipeline_normalize_reduce_sum4_fp32_pack4[1]->create(LayerShaderType::normalize_reduce_sum4_fp32_pack4, opt, specializations);
  92. }
  93. // pack8
  94. if ((opt.use_shader_pack8 && shape.dims == 0) || elempack == 8)
  95. {
  96. pipeline_normalize_reduce_sum4_fp16_to_fp32_pack8 = new Pipeline(vkdev);
  97. pipeline_normalize_reduce_sum4_fp16_to_fp32_pack8->set_optimal_local_size_xyz(local_size_xyz);
  98. pipeline_normalize_reduce_sum4_fp16_to_fp32_pack8->create(LayerShaderType::normalize_reduce_sum4_fp16_to_fp32_pack8, opt, specializations);
  99. pipeline_normalize_reduce_sum4_fp32_pack8[0] = new Pipeline(vkdev);
  100. pipeline_normalize_reduce_sum4_fp32_pack8[0]->set_optimal_local_size_xyz(local_size_xyz);
  101. pipeline_normalize_reduce_sum4_fp32_pack8[0]->create(LayerShaderType::normalize_reduce_sum4_fp32_pack8, opt, specializations);
  102. pipeline_normalize_reduce_sum4_fp32_pack8[1] = new Pipeline(vkdev);
  103. pipeline_normalize_reduce_sum4_fp32_pack8[1]->set_optimal_local_size_xyz(local_size_xyz);
  104. pipeline_normalize_reduce_sum4_fp32_pack8[1]->create(LayerShaderType::normalize_reduce_sum4_fp32_pack8, opt, specializations);
  105. }
  106. }
  107. {
  108. std::vector<vk_specialization_type> specializations(4);
  109. specializations[0].i = across_spatial;
  110. specializations[1].i = across_channel;
  111. specializations[2].f = eps;
  112. specializations[3].i = eps_mode;
  113. Mat local_size_xyz;// TODO resolve sqsum_workspace shape
  114. if (shape.dims == 0 || elempack == 1)
  115. {
  116. pipeline_normalize_coeffs = new Pipeline(vkdev);
  117. pipeline_normalize_coeffs->set_optimal_local_size_xyz(local_size_xyz);
  118. pipeline_normalize_coeffs->create(LayerShaderType::normalize_coeffs, opt, specializations);
  119. }
  120. if (shape.dims == 0 || elempack == 4)
  121. {
  122. pipeline_normalize_coeffs_pack4 = new Pipeline(vkdev);
  123. pipeline_normalize_coeffs_pack4->set_optimal_local_size_xyz(local_size_xyz);
  124. pipeline_normalize_coeffs_pack4->create(LayerShaderType::normalize_coeffs_pack4, opt, specializations);
  125. }
  126. if ((opt.use_shader_pack8 && shape.dims == 0) || elempack == 8)
  127. {
  128. pipeline_normalize_coeffs_pack8 = new Pipeline(vkdev);
  129. pipeline_normalize_coeffs_pack8->set_optimal_local_size_xyz(local_size_xyz);
  130. pipeline_normalize_coeffs_pack8->create(LayerShaderType::normalize_coeffs_pack8, opt, specializations);
  131. }
  132. }
  133. {
  134. std::vector<vk_specialization_type> specializations(5 + 5);
  135. specializations[0].i = across_spatial;
  136. specializations[1].i = across_channel;
  137. specializations[2].i = channel_shared;
  138. specializations[3].i = (scale_data_size == 1 && scale_data[0] == 1.f) ? 0 : 1;
  139. specializations[4].f = channel_shared ? scale_data[0] : 1.f;
  140. specializations[5 + 0].i = shape_packed.dims;
  141. specializations[5 + 1].i = shape_packed.w;
  142. specializations[5 + 2].i = shape_packed.h;
  143. specializations[5 + 3].i = shape_packed.c;
  144. specializations[5 + 4].i = shape_packed.cstep;
  145. Mat local_size_xyz;
  146. if (shape_packed.dims != 0)
  147. {
  148. local_size_xyz.w = std::min(4, shape_packed.w);
  149. local_size_xyz.h = std::min(4, shape_packed.h);
  150. local_size_xyz.c = std::min(4, shape_packed.c);
  151. }
  152. if (shape.dims == 0 || elempack == 1)
  153. {
  154. pipeline_normalize_norm = new Pipeline(vkdev);
  155. pipeline_normalize_norm->set_optimal_local_size_xyz(local_size_xyz);
  156. pipeline_normalize_norm->create(LayerShaderType::normalize_norm, opt, specializations);
  157. }
  158. if (shape.dims == 0 || elempack == 4)
  159. {
  160. pipeline_normalize_norm_pack4 = new Pipeline(vkdev);
  161. pipeline_normalize_norm_pack4->set_optimal_local_size_xyz(local_size_xyz);
  162. pipeline_normalize_norm_pack4->create(LayerShaderType::normalize_norm_pack4, opt, specializations);
  163. }
  164. if ((opt.use_shader_pack8 && shape.dims == 0) || elempack == 8)
  165. {
  166. pipeline_normalize_norm_pack8 = new Pipeline(vkdev);
  167. pipeline_normalize_norm_pack8->set_optimal_local_size_xyz(local_size_xyz);
  168. pipeline_normalize_norm_pack8->create(LayerShaderType::normalize_norm_pack8, opt, specializations);
  169. }
  170. }
  171. return 0;
  172. }
  173. int Normalize_vulkan::destroy_pipeline(const Option& /*opt*/)
  174. {
  175. delete pipeline_normalize_reduce_sum4_fp16_to_fp32;
  176. pipeline_normalize_reduce_sum4_fp16_to_fp32 = 0;
  177. delete pipeline_normalize_reduce_sum4_fp32[0];
  178. delete pipeline_normalize_reduce_sum4_fp32[1];
  179. pipeline_normalize_reduce_sum4_fp32[0] = 0;
  180. pipeline_normalize_reduce_sum4_fp32[1] = 0;
  181. delete pipeline_normalize_reduce_sum4_fp16_to_fp32_pack4;
  182. pipeline_normalize_reduce_sum4_fp16_to_fp32_pack4 = 0;
  183. delete pipeline_normalize_reduce_sum4_fp32_pack4[0];
  184. delete pipeline_normalize_reduce_sum4_fp32_pack4[1];
  185. pipeline_normalize_reduce_sum4_fp32_pack4[0] = 0;
  186. pipeline_normalize_reduce_sum4_fp32_pack4[1] = 0;
  187. delete pipeline_normalize_reduce_sum4_fp16_to_fp32_pack8;
  188. pipeline_normalize_reduce_sum4_fp16_to_fp32_pack8 = 0;
  189. delete pipeline_normalize_reduce_sum4_fp32_pack8[0];
  190. delete pipeline_normalize_reduce_sum4_fp32_pack8[1];
  191. pipeline_normalize_reduce_sum4_fp32_pack8[0] = 0;
  192. pipeline_normalize_reduce_sum4_fp32_pack8[1] = 0;
  193. delete pipeline_normalize_coeffs;
  194. pipeline_normalize_coeffs = 0;
  195. delete pipeline_normalize_coeffs_pack4;
  196. pipeline_normalize_coeffs_pack4 = 0;
  197. delete pipeline_normalize_coeffs_pack8;
  198. pipeline_normalize_coeffs_pack8 = 0;
  199. delete pipeline_normalize_norm;
  200. pipeline_normalize_norm = 0;
  201. delete pipeline_normalize_norm_pack4;
  202. pipeline_normalize_norm_pack4 = 0;
  203. delete pipeline_normalize_norm_pack8;
  204. pipeline_normalize_norm_pack8 = 0;
  205. return 0;
  206. }
  207. int Normalize_vulkan::upload_model(VkTransfer& cmd, const Option& opt)
  208. {
  209. if (!channel_shared && !(scale_data_size == 1 && scale_data[0] == 1.f))
  210. {
  211. int elempack = opt.use_shader_pack8 && scale_data_size % 8 == 0 ? 8 : scale_data_size % 4 == 0 ? 4 : 1;
  212. Mat scale_data_packed;
  213. convert_packing(scale_data, scale_data_packed, elempack);
  214. cmd.record_upload(scale_data_packed, scale_data_gpu, opt);
  215. }
  216. return 0;
  217. }
  218. int Normalize_vulkan::forward_inplace(VkMat& bottom_top_blob, VkCompute& cmd, const Option& opt) const
  219. {
  220. // int w = bottom_top_blob.w;
  221. // int h = bottom_top_blob.h;
  222. // int size = w * h;
  223. size_t elemsize = bottom_top_blob.elemsize;
  224. int elempack = bottom_top_blob.elempack;
  225. // reduce square sum
  226. VkMat sqsum_workspace;
  227. {
  228. {
  229. int reduced_w;
  230. int reduced_h;
  231. int reduced_c;
  232. if (across_spatial && across_channel)
  233. {
  234. reduced_w = (bottom_top_blob.w * bottom_top_blob.h + 1) / 2;
  235. reduced_h = 1;
  236. reduced_c = (bottom_top_blob.c + 1) / 2;
  237. }
  238. else if (across_spatial && !across_channel)
  239. {
  240. reduced_w = (bottom_top_blob.w * bottom_top_blob.h + 3) / 4;
  241. reduced_h = 1;
  242. reduced_c = bottom_top_blob.c;
  243. }
  244. else // if (!across_spatial && across_channel)
  245. {
  246. reduced_w = bottom_top_blob.w * bottom_top_blob.h;
  247. reduced_h = 1;
  248. reduced_c = (bottom_top_blob.c + 3) / 4;
  249. }
  250. sqsum_workspace.create(reduced_w, reduced_h, reduced_c, 4u*elempack, elempack, opt.workspace_vkallocator);
  251. {
  252. std::vector<VkMat> bindings(2);
  253. bindings[0] = bottom_top_blob;
  254. bindings[1] = sqsum_workspace;
  255. std::vector<vk_constant_type> constants(6);
  256. constants[0].i = bottom_top_blob.w * bottom_top_blob.h;
  257. constants[1].i = bottom_top_blob.c;
  258. constants[2].i = bottom_top_blob.cstep;
  259. constants[3].i = sqsum_workspace.w;
  260. constants[4].i = sqsum_workspace.c;
  261. constants[5].i = sqsum_workspace.cstep;
  262. const Pipeline* pipeline = elempack == 8 ? pipeline_normalize_reduce_sum4_fp16_to_fp32_pack8
  263. : elempack == 4 ? pipeline_normalize_reduce_sum4_fp16_to_fp32_pack4
  264. : pipeline_normalize_reduce_sum4_fp16_to_fp32;
  265. cmd.record_pipeline(pipeline, bindings, constants, sqsum_workspace);
  266. }
  267. }
  268. int pb = 0;
  269. while ((across_spatial && sqsum_workspace.w > 1) || (across_channel && sqsum_workspace.c > 1))
  270. {
  271. int reduced_w;
  272. int reduced_h;
  273. int reduced_c;
  274. if (across_spatial && across_channel)
  275. {
  276. reduced_w = (sqsum_workspace.w + 1) / 2;
  277. reduced_h = 1;
  278. reduced_c = (sqsum_workspace.c + 1) / 2;
  279. }
  280. else if (across_spatial && !across_channel)
  281. {
  282. reduced_w = (sqsum_workspace.w + 3) / 4;
  283. reduced_h = 1;
  284. reduced_c = sqsum_workspace.c;
  285. }
  286. else // if (!across_spatial && across_channel)
  287. {
  288. reduced_w = sqsum_workspace.w;
  289. reduced_h = 1;
  290. reduced_c = (sqsum_workspace.c + 3) / 4;
  291. }
  292. VkMat sqsum_workspace_reduced;
  293. sqsum_workspace_reduced.create(reduced_w, reduced_h, reduced_c, 4u*elempack, elempack, opt.workspace_vkallocator);
  294. {
  295. std::vector<VkMat> bindings(2);
  296. bindings[0] = sqsum_workspace;
  297. bindings[1] = sqsum_workspace_reduced;
  298. std::vector<vk_constant_type> constants(6);
  299. constants[0].i = sqsum_workspace.w;
  300. constants[1].i = sqsum_workspace.c;
  301. constants[2].i = sqsum_workspace.cstep;
  302. constants[3].i = sqsum_workspace_reduced.w;
  303. constants[4].i = sqsum_workspace_reduced.c;
  304. constants[5].i = sqsum_workspace_reduced.cstep;
  305. const Pipeline* pipeline = elempack == 8 ? pipeline_normalize_reduce_sum4_fp32_pack8[pb%2]
  306. : elempack == 4 ? pipeline_normalize_reduce_sum4_fp32_pack4[pb%2]
  307. : pipeline_normalize_reduce_sum4_fp32[pb%2];
  308. cmd.record_pipeline(pipeline, bindings, constants, sqsum_workspace_reduced);
  309. pb++;
  310. }
  311. sqsum_workspace = sqsum_workspace_reduced;
  312. }
  313. }
  314. // coeffs
  315. VkMat coeffs_workspace;
  316. coeffs_workspace.create(sqsum_workspace.w * sqsum_workspace.h * sqsum_workspace.c, elemsize, elempack, opt.workspace_vkallocator);
  317. {
  318. std::vector<VkMat> bindings(2);
  319. bindings[0] = sqsum_workspace;
  320. bindings[1] = coeffs_workspace;
  321. std::vector<vk_constant_type> constants(3);
  322. constants[0].i = sqsum_workspace.w;
  323. constants[1].i = sqsum_workspace.c;
  324. constants[2].i = sqsum_workspace.cstep;
  325. const Pipeline* pipeline = elempack == 8 ? pipeline_normalize_coeffs_pack8
  326. : elempack == 4 ? pipeline_normalize_coeffs_pack4
  327. : pipeline_normalize_coeffs;
  328. cmd.record_pipeline(pipeline, bindings, constants, sqsum_workspace);
  329. }
  330. // norm
  331. {
  332. std::vector<VkMat> bindings(3);
  333. bindings[0] = bottom_top_blob;
  334. bindings[1] = coeffs_workspace;
  335. bindings[2] = channel_shared || (scale_data_size == 1 && scale_data[0] == 1.f) ? coeffs_workspace : scale_data_gpu;
  336. std::vector<vk_constant_type> constants(5);
  337. constants[0].i = bottom_top_blob.dims;
  338. constants[1].i = bottom_top_blob.w;
  339. constants[2].i = bottom_top_blob.h;
  340. constants[3].i = bottom_top_blob.c;
  341. constants[4].i = bottom_top_blob.cstep;
  342. const Pipeline* pipeline = elempack == 8 ? pipeline_normalize_norm_pack8
  343. : elempack == 4 ? pipeline_normalize_norm_pack4
  344. : pipeline_normalize_norm;
  345. cmd.record_pipeline(pipeline, bindings, constants, bottom_top_blob);
  346. }
  347. return 0;
  348. }
  349. } // namespace ncnn