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.

reshape.cpp 8.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. // Tencent is pleased to support the open source community by making ncnn available.
  2. //
  3. // Copyright (C) 2017 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 "reshape.h"
  15. namespace ncnn {
  16. DEFINE_LAYER_CREATOR(Reshape)
  17. Reshape::Reshape()
  18. {
  19. one_blob_only = true;
  20. support_inplace = false;
  21. support_vulkan = true;
  22. #if NCNN_VULKAN
  23. pipeline_reshape = 0;
  24. pipeline_reshape_pack4 = 0;
  25. pipeline_reshape_pack1to4 = 0;
  26. pipeline_reshape_pack4to1 = 0;
  27. #endif // NCNN_VULKAN
  28. }
  29. int Reshape::load_param(const ParamDict& pd)
  30. {
  31. w = pd.get(0, -233);
  32. h = pd.get(1, -233);
  33. c = pd.get(2, -233);
  34. permute = pd.get(3, 0);
  35. ndim = 3;
  36. if (c == -233)
  37. ndim = 2;
  38. if (h == -233)
  39. ndim = 1;
  40. if (w == -233)
  41. ndim = 0;
  42. return 0;
  43. }
  44. int Reshape::forward(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const
  45. {
  46. size_t elemsize = bottom_blob.elemsize;
  47. int total = bottom_blob.w * bottom_blob.h * bottom_blob.c;
  48. if (ndim == 1)
  49. {
  50. int _w = w;
  51. if (_w == 0)
  52. _w = bottom_blob.w;
  53. if (_w == -1)
  54. _w = total;
  55. if (permute == 1)
  56. {
  57. top_blob.create(_w, elemsize, opt.blob_allocator);
  58. if (top_blob.empty())
  59. return -100;
  60. // c-h-w to h-w-c
  61. float* ptr = top_blob;
  62. for (int i=0; i<bottom_blob.h; i++)
  63. {
  64. for (int j=0; j<bottom_blob.w; j++)
  65. {
  66. for (int p=0; p<bottom_blob.c; p++)
  67. {
  68. const float* bptr = bottom_blob.channel(p);
  69. *ptr++ = bptr[i*bottom_blob.w + j];
  70. }
  71. }
  72. }
  73. }
  74. else
  75. {
  76. top_blob = bottom_blob.reshape(_w, opt.blob_allocator);
  77. }
  78. }
  79. else if (ndim == 2)
  80. {
  81. int _w = w;
  82. int _h = h;
  83. if (_w == 0)
  84. _w = bottom_blob.w;
  85. if (_h == 0)
  86. _h = bottom_blob.h;
  87. if (_w == -1)
  88. _w = total / _h;
  89. if (_h == -1)
  90. _h = total / _w;
  91. top_blob = bottom_blob.reshape(_w, _h, opt.blob_allocator);
  92. }
  93. else if (ndim == 3)
  94. {
  95. int _w = w;
  96. int _h = h;
  97. int _c = c;
  98. if (_w == 0)
  99. _w = bottom_blob.w;
  100. if (_h == 0)
  101. _h = bottom_blob.h;
  102. if (_c == 0)
  103. _c = bottom_blob.c;
  104. if (_w == -1)
  105. _w = total / _c / _h;
  106. if (_h == -1)
  107. _h = total / _c / _w;
  108. if (_c == -1)
  109. _c = total / _h / _w;
  110. top_blob = bottom_blob.reshape(_w, _h, _c, opt.blob_allocator);
  111. }
  112. if (top_blob.empty())
  113. return -100;
  114. return 0;
  115. }
  116. #if NCNN_VULKAN
  117. int Reshape::create_pipeline()
  118. {
  119. std::vector<vk_specialization_type> specializations(1);
  120. specializations[0].i = ndim;
  121. // pack1
  122. {
  123. pipeline_reshape = new Pipeline(vkdev);
  124. pipeline_reshape->set_optimal_local_size_xyz();
  125. pipeline_reshape->create("reshape", specializations, 2, 10);
  126. }
  127. // pack4
  128. {
  129. pipeline_reshape_pack4 = new Pipeline(vkdev);
  130. pipeline_reshape_pack4->set_optimal_local_size_xyz();
  131. pipeline_reshape_pack4->create("reshape_pack4", specializations, 2, 10);
  132. }
  133. // pack1to4
  134. {
  135. pipeline_reshape_pack1to4 = new Pipeline(vkdev);
  136. pipeline_reshape_pack1to4->set_optimal_local_size_xyz();
  137. pipeline_reshape_pack1to4->create("reshape_pack1to4", specializations, 2, 10);
  138. }
  139. // pack4to1
  140. {
  141. pipeline_reshape_pack4to1 = new Pipeline(vkdev);
  142. pipeline_reshape_pack4to1->set_optimal_local_size_xyz();
  143. pipeline_reshape_pack4to1->create("reshape_pack4to1", specializations, 2, 10);
  144. }
  145. return 0;
  146. }
  147. int Reshape::destroy_pipeline()
  148. {
  149. delete pipeline_reshape;
  150. pipeline_reshape = 0;
  151. delete pipeline_reshape_pack4;
  152. pipeline_reshape_pack4 = 0;
  153. delete pipeline_reshape_pack1to4;
  154. pipeline_reshape_pack1to4 = 0;
  155. delete pipeline_reshape_pack4to1;
  156. pipeline_reshape_pack4to1 = 0;
  157. return 0;
  158. }
  159. int Reshape::forward(const VkMat& bottom_blob, VkMat& top_blob, VkCompute& cmd, const Option& opt) const
  160. {
  161. int dims = bottom_blob.dims;
  162. size_t elemsize = bottom_blob.elemsize;
  163. int packing = bottom_blob.packing;
  164. int out_packing;
  165. int total = bottom_blob.w * bottom_blob.h * bottom_blob.c * packing;
  166. if (ndim == 1)
  167. {
  168. int _w = w;
  169. if (_w == 0)
  170. _w = dims == 1 ? bottom_blob.w * packing : bottom_blob.w;
  171. if (_w == -1)
  172. _w = total;
  173. // TODO permute support
  174. out_packing = _w % 4 == 0 ? 4 : 1;
  175. size_t out_elemsize = elemsize / packing * out_packing;
  176. if (dims == 1 && bottom_blob.w == _w && packing == out_packing)
  177. {
  178. top_blob = bottom_blob;
  179. return 0;
  180. }
  181. top_blob.create(_w / out_packing, out_elemsize, out_packing, opt.blob_vkallocator, opt.staging_vkallocator);
  182. }
  183. else if (ndim == 2)
  184. {
  185. int _w = w;
  186. int _h = h;
  187. if (_w == 0)
  188. _w = dims == 1 ? bottom_blob.w * packing : bottom_blob.w;
  189. if (_h == 0)
  190. _h = dims == 2 ? bottom_blob.h * packing : bottom_blob.h;
  191. if (_w == -1)
  192. _w = total / _h;
  193. if (_h == -1)
  194. _h = total / _w;
  195. out_packing = _h % 4 == 0 ? 4 : 1;
  196. size_t out_elemsize = elemsize / packing * out_packing;
  197. if (dims == 2 && bottom_blob.h == _h && packing == out_packing)
  198. {
  199. top_blob = bottom_blob;
  200. return 0;
  201. }
  202. top_blob.create(_w, _h / out_packing, out_elemsize, out_packing, opt.blob_vkallocator, opt.staging_vkallocator);
  203. }
  204. else // if (ndim == 3)
  205. {
  206. int _w = w;
  207. int _h = h;
  208. int _c = c;
  209. if (_w == 0)
  210. _w = dims == 1 ? bottom_blob.w * packing : bottom_blob.w;
  211. if (_h == 0)
  212. _h = dims == 2 ? bottom_blob.h * packing : bottom_blob.h;
  213. if (_c == 0)
  214. _c = dims == 3 ? bottom_blob.c * packing : bottom_blob.c;
  215. if (_w == -1)
  216. _w = total / _c / _h;
  217. if (_h == -1)
  218. _h = total / _c / _w;
  219. if (_c == -1)
  220. _c = total / _h / _w;
  221. out_packing = _c % 4 == 0 ? 4 : 1;
  222. size_t out_elemsize = elemsize / packing * out_packing;
  223. if (dims == 3 && bottom_blob.c == _c && packing == out_packing)
  224. {
  225. top_blob = bottom_blob;
  226. top_blob.w = _w;
  227. top_blob.h = _h;
  228. return 0;
  229. }
  230. top_blob.create(_w, _h, _c / out_packing, out_elemsize, out_packing, opt.blob_vkallocator, opt.staging_vkallocator);
  231. }
  232. if (top_blob.empty())
  233. return -100;
  234. // fprintf(stderr, "Reshape::forward %p %p\n", bottom_blob.buffer(), top_blob.buffer());
  235. std::vector<VkMat> bindings(2);
  236. bindings[0] = bottom_blob;
  237. bindings[1] = top_blob;
  238. std::vector<vk_constant_type> constants(10);
  239. constants[0].i = bottom_blob.dims;
  240. constants[1].i = bottom_blob.w;
  241. constants[2].i = bottom_blob.h;
  242. constants[3].i = bottom_blob.c;
  243. constants[4].i = bottom_blob.cstep;
  244. constants[5].i = top_blob.dims;
  245. constants[6].i = top_blob.w;
  246. constants[7].i = top_blob.h;
  247. constants[8].i = top_blob.c;
  248. constants[9].i = top_blob.cstep;
  249. const Pipeline* pipeline = 0;
  250. if (packing == 1 && out_packing == 1)
  251. {
  252. pipeline = pipeline_reshape;
  253. }
  254. else if (packing == 4 && out_packing == 4)
  255. {
  256. pipeline = pipeline_reshape_pack4;
  257. }
  258. else if (packing == 1 && out_packing == 4)
  259. {
  260. pipeline = pipeline_reshape_pack1to4;
  261. }
  262. else if (packing == 4 && out_packing == 1)
  263. {
  264. pipeline = pipeline_reshape_pack4to1;
  265. }
  266. // record
  267. cmd.record_prepare_compute_barrier(bottom_blob);
  268. cmd.record_prepare_compute_barrier(top_blob);
  269. if (packing == 4 && out_packing == 1)
  270. {
  271. cmd.record_pipeline(pipeline, bindings, constants, bottom_blob);
  272. }
  273. else
  274. {
  275. cmd.record_pipeline(pipeline, bindings, constants, top_blob);
  276. }
  277. return 0;
  278. }
  279. #endif // NCNN_VULKAN
  280. } // namespace ncnn