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.

packing.cpp 7.9 kB

7 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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 "packing.h"
  15. namespace ncnn {
  16. DEFINE_LAYER_CREATOR(Packing)
  17. Packing::Packing()
  18. {
  19. one_blob_only = true;
  20. support_inplace = false;
  21. support_vulkan = false;
  22. #if NCNN_VULKAN
  23. pipeline_packing_1to4 = 0;
  24. pipeline_packing_4to1 = 0;
  25. #endif // NCNN_VULKAN
  26. }
  27. int Packing::load_param(const ParamDict& pd)
  28. {
  29. out_packing = pd.get(0, 1);
  30. use_padding = pd.get(1, 0);
  31. return 0;
  32. }
  33. int Packing::forward(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const
  34. {
  35. int packing = bottom_blob.packing;
  36. if (packing == out_packing)
  37. {
  38. top_blob = bottom_blob;
  39. return 0;
  40. }
  41. int w = bottom_blob.w;
  42. int h = bottom_blob.h;
  43. int channels = bottom_blob.c;
  44. int dims = bottom_blob.dims;
  45. size_t elemsize = bottom_blob.elemsize;
  46. if (!use_padding)
  47. {
  48. // identity if use_padding not allowed
  49. if (dims == 1 && w * packing % out_packing != 0)
  50. {
  51. top_blob = bottom_blob;
  52. return 0;
  53. }
  54. if (dims == 2 && h * packing % out_packing != 0)
  55. {
  56. top_blob = bottom_blob;
  57. return 0;
  58. }
  59. if (dims == 3 && channels * packing % out_packing != 0)
  60. {
  61. top_blob = bottom_blob;
  62. return 0;
  63. }
  64. }
  65. if (dims == 1)
  66. {
  67. if (out_packing == 1)
  68. {
  69. top_blob = bottom_blob;
  70. top_blob.w = w * packing;
  71. top_blob.elemsize = elemsize / packing;
  72. return 0;
  73. }
  74. int outw = (w * packing + out_packing - 1) / out_packing;
  75. size_t out_elemsize = elemsize / packing * out_packing;
  76. top_blob.create(outw, out_elemsize, out_packing, opt.blob_allocator);
  77. if (top_blob.empty())
  78. return -100;
  79. memcpy(top_blob.data, bottom_blob.data, w * elemsize);
  80. return 0;
  81. }
  82. if (dims == 2)
  83. {
  84. int outh = (h * packing + out_packing - 1) / out_packing;
  85. size_t out_elemsize = elemsize / packing * out_packing;
  86. size_t lane_size = out_elemsize / out_packing;
  87. top_blob.create(w, outh, out_elemsize, out_packing, opt.blob_allocator);
  88. if (top_blob.empty())
  89. return -100;
  90. #pragma omp parallel for
  91. for (int i = 0; i < outh; i++)
  92. {
  93. unsigned char* outptr = (unsigned char*)top_blob + i * w * out_elemsize;
  94. for (int j = 0; j < w; j++)
  95. {
  96. unsigned char* out_elem_ptr = outptr + j * out_elemsize;
  97. for (int k = 0; k < out_packing; k++)
  98. {
  99. int srcy = (i * out_packing + k) / packing;
  100. if (srcy >= h)
  101. break;
  102. int srck = (i * out_packing + k) % packing;
  103. const unsigned char* ptr = (const unsigned char*)bottom_blob + srcy * w * elemsize;
  104. const unsigned char* elem_ptr = ptr + j * elemsize;
  105. memcpy(out_elem_ptr + k * lane_size, elem_ptr + srck * lane_size, lane_size);
  106. }
  107. }
  108. }
  109. return 0;
  110. }
  111. if (dims == 3)
  112. {
  113. int outc = (channels * packing + out_packing - 1) / out_packing;
  114. size_t out_elemsize = elemsize / packing * out_packing;
  115. size_t lane_size = out_elemsize / out_packing;
  116. top_blob.create(w, h, outc, out_elemsize, out_packing, opt.blob_allocator);
  117. if (top_blob.empty())
  118. return -100;
  119. #pragma omp parallel for
  120. for (int q = 0; q < outc; q++)
  121. {
  122. Mat out = top_blob.channel(q);
  123. for (int i = 0; i < h; i++)
  124. {
  125. unsigned char* outptr = (unsigned char*)out + i * w * out_elemsize;
  126. for (int j = 0; j < w; j++)
  127. {
  128. unsigned char* out_elem_ptr = outptr + j * out_elemsize;
  129. for (int k = 0; k < out_packing; k++)
  130. {
  131. int srcq = (q * out_packing + k) / packing;
  132. if (srcq >= channels)
  133. break;
  134. int srck = (q * out_packing + k) % packing;
  135. const Mat m = bottom_blob.channel(srcq);
  136. const unsigned char* ptr = (const unsigned char*)m + i * w * elemsize;
  137. const unsigned char* elem_ptr = ptr + j * elemsize;
  138. memcpy(out_elem_ptr + k * lane_size, elem_ptr + srck * lane_size, lane_size);
  139. }
  140. }
  141. }
  142. }
  143. return 0;
  144. }
  145. return 0;
  146. }
  147. #if NCNN_VULKAN
  148. int Packing::create_pipeline()
  149. {
  150. std::vector<vk_specialization_type> specializations;
  151. pipeline_packing_1to4 = new Pipeline(vkdev);
  152. pipeline_packing_1to4->set_optimal_local_size_xyz();
  153. pipeline_packing_1to4->create("packing_1to4", specializations, 2, 10);
  154. pipeline_packing_4to1 = new Pipeline(vkdev);
  155. pipeline_packing_4to1->set_optimal_local_size_xyz();
  156. pipeline_packing_4to1->create("packing_4to1", specializations, 2, 10);
  157. return 0;
  158. }
  159. int Packing::destroy_pipeline()
  160. {
  161. delete pipeline_packing_1to4;
  162. pipeline_packing_1to4 = 0;
  163. delete pipeline_packing_4to1;
  164. pipeline_packing_4to1 = 0;
  165. return 0;
  166. }
  167. int Packing::forward(const VkMat& bottom_blob, VkMat& top_blob, VkCompute& cmd, const Option& opt) const
  168. {
  169. int packing = bottom_blob.packing;
  170. if (packing == out_packing)
  171. {
  172. top_blob = bottom_blob;
  173. return 0;
  174. }
  175. int w = bottom_blob.w;
  176. int h = bottom_blob.h;
  177. int channels = bottom_blob.c;
  178. int dims = bottom_blob.dims;
  179. size_t elemsize = bottom_blob.elemsize;
  180. if (dims == 1)
  181. {
  182. // TODO
  183. return -1;
  184. }
  185. if (dims == 2)
  186. {
  187. // TODO
  188. return -1;
  189. }
  190. if (dims == 3)
  191. {
  192. int outc = (channels * packing + out_packing - 1) / out_packing;
  193. size_t out_elemsize = elemsize / packing * out_packing;
  194. top_blob.create(w, h, outc, out_elemsize, out_packing, opt.blob_vkallocator, opt.staging_vkallocator);
  195. if (top_blob.empty())
  196. return -100;
  197. }
  198. // fprintf(stderr, "Packing::forward %p %p\n", bottom_blob.buffer(), top_blob.buffer());
  199. std::vector<VkMat> bindings(2);
  200. bindings[0] = bottom_blob;
  201. bindings[1] = top_blob;
  202. std::vector<vk_constant_type> constants(10);
  203. constants[0].i = bottom_blob.dims;
  204. constants[1].i = bottom_blob.w;
  205. constants[2].i = bottom_blob.h;
  206. constants[3].i = bottom_blob.c;
  207. constants[4].i = bottom_blob.cstep;
  208. constants[5].i = top_blob.dims;
  209. constants[6].i = top_blob.w;
  210. constants[7].i = top_blob.h;
  211. constants[8].i = top_blob.c;
  212. constants[9].i = top_blob.cstep;
  213. // record
  214. cmd.record_prepare_compute_barrier(bottom_blob);
  215. cmd.record_prepare_compute_barrier(top_blob);
  216. if (packing == 1 && out_packing == 4)
  217. {
  218. cmd.record_pipeline(pipeline_packing_1to4, bindings, constants, top_blob);
  219. }
  220. if (packing == 4 && out_packing == 1)
  221. {
  222. cmd.record_pipeline(pipeline_packing_4to1, bindings, constants, bottom_blob);
  223. }
  224. return 0;
  225. }
  226. #endif // NCNN_VULKAN
  227. } // namespace ncnn