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.

padding_arm.cpp 27 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778
  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 "padding_arm.h"
  15. #if __ARM_NEON
  16. #include <arm_neon.h>
  17. #endif // __ARM_NEON
  18. #include "cpu.h"
  19. namespace ncnn {
  20. #if __ARM_NEON
  21. #include "padding_pack4.h"
  22. #include "padding_pack4_bf16s_fp16s.h"
  23. #include "padding_pack8_int8.h"
  24. #if NCNN_ARM82
  25. #include "padding_pack8_fp16s.h"
  26. #endif
  27. #endif // __ARM_NEON
  28. Padding_arm::Padding_arm()
  29. {
  30. #if __ARM_NEON
  31. support_packing = true;
  32. #if NCNN_ARM82
  33. support_fp16_storage = cpu_support_arm_asimdhp();
  34. #endif
  35. #endif // __ARM_NEON
  36. #if NCNN_BF16
  37. support_bf16_storage = true;
  38. #endif
  39. }
  40. int Padding_arm::create_pipeline(const Option& opt)
  41. {
  42. #if NCNN_ARM82
  43. if (support_fp16_storage && opt.use_fp16_storage)
  44. {
  45. value_fp16 = float32_to_float16(value);
  46. ncnn::cast_float32_to_float16(per_channel_pad_data, per_channel_pad_data_fp16, opt);
  47. }
  48. #endif
  49. #if NCNN_BF16
  50. if (opt.use_bf16_storage)
  51. {
  52. value_bf16 = float32_to_bfloat16(value);
  53. ncnn::cast_float32_to_bfloat16(per_channel_pad_data, per_channel_pad_data_bf16, opt);
  54. }
  55. #endif
  56. return 0;
  57. }
  58. int Padding_arm::destroy_pipeline(const Option& /*opt*/)
  59. {
  60. return 0;
  61. }
  62. int Padding_arm::forward(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const
  63. {
  64. if (top == 0 && bottom == 0 && left == 0 && right == 0 && front == 0 && behind == 0)
  65. {
  66. top_blob = bottom_blob;
  67. return 0;
  68. }
  69. int elembits = bottom_blob.elembits();
  70. if (elembits == 8)
  71. return forward_int8(bottom_blob, top_blob, opt);
  72. #if NCNN_ARM82
  73. if (support_fp16_storage && opt.use_fp16_storage && elembits == 16)
  74. return forward_bf16s_fp16s(bottom_blob, top_blob, opt);
  75. #endif
  76. #if NCNN_BF16
  77. if (opt.use_bf16_storage && elembits == 16)
  78. return forward_bf16s_fp16s(bottom_blob, top_blob, opt);
  79. #endif
  80. int w = bottom_blob.w;
  81. int h = bottom_blob.h;
  82. int d = bottom_blob.d;
  83. int channels = bottom_blob.c;
  84. int dims = bottom_blob.dims;
  85. size_t elemsize = bottom_blob.elemsize;
  86. int elempack = bottom_blob.elempack;
  87. #if __ARM_NEON
  88. if (elempack == 4)
  89. {
  90. if (dims == 1)
  91. {
  92. int outw = w * elempack + left + right;
  93. int out_elempack = outw % 4 == 0 ? 4 : 1;
  94. size_t out_elemsize = elemsize / elempack * out_elempack;
  95. if (left % 4 == 0 && out_elempack == 4 && type == 0)
  96. {
  97. top_blob.create(outw / out_elempack, out_elemsize, out_elempack, opt.blob_allocator);
  98. if (top_blob.empty())
  99. return -100;
  100. float32x4_t pad_value = vdupq_n_f32(value);
  101. padding_constant_pack4_neon(bottom_blob, top_blob, 0, 0, left / 4, right / 4, pad_value);
  102. return 0;
  103. }
  104. }
  105. if (dims == 2)
  106. {
  107. int outw = w + left + right;
  108. int outh = h * elempack + top + bottom;
  109. int out_elempack = outh % 4 == 0 ? 4 : 1;
  110. size_t out_elemsize = elemsize / elempack * out_elempack;
  111. if (top % 4 == 0 && out_elempack == 4 && type == 0)
  112. {
  113. top_blob.create(outw, outh / out_elempack, out_elemsize, out_elempack, opt.blob_allocator);
  114. if (top_blob.empty())
  115. return -100;
  116. float32x4_t pad_value = vdupq_n_f32(value);
  117. padding_constant_pack4_neon(bottom_blob, top_blob, top / 4, bottom / 4, left, right, pad_value);
  118. return 0;
  119. }
  120. }
  121. if (dims == 3)
  122. {
  123. int outw = w + left + right;
  124. int outh = h + top + bottom;
  125. int outc = channels * elempack + front + behind;
  126. int out_elempack = outc % 4 == 0 ? 4 : 1;
  127. size_t out_elemsize = elemsize / elempack * out_elempack;
  128. if (front % 4 == 0 && out_elempack == 4 && !(outc != channels * elempack && type != 0))
  129. {
  130. top_blob.create(outw, outh, outc / out_elempack, out_elemsize, out_elempack, opt.blob_allocator);
  131. if (top_blob.empty())
  132. return -100;
  133. int front_ = front / elempack;
  134. #pragma omp parallel for num_threads(opt.num_threads)
  135. for (int q = 0; q < outc / out_elempack; q++)
  136. {
  137. Mat borderm = top_blob.channel(q);
  138. float32x4_t pad_value = per_channel_pad_data_size ? vld1q_f32((const float*)per_channel_pad_data + q * 4) : vdupq_n_f32(value);
  139. //Channel padding
  140. if ((q - front_) < 0 || (q - front_) >= channels)
  141. {
  142. borderm.fill(pad_value);
  143. }
  144. else
  145. {
  146. const Mat m = bottom_blob.channel(q - front_);
  147. if (type == 0)
  148. padding_constant_pack4_neon(m, borderm, top, bottom, left, right, pad_value);
  149. if (type == 1)
  150. padding_replicate_pack4_neon(m, borderm, top, bottom, left, right);
  151. if (type == 2)
  152. padding_reflect_pack4_neon(m, borderm, top, bottom, left, right);
  153. }
  154. }
  155. return 0;
  156. }
  157. }
  158. if (dims == 4)
  159. {
  160. int outw = w + left + right;
  161. int outh = h + top + bottom;
  162. int outd = d + front + behind;
  163. if (type == 0)
  164. {
  165. top_blob.create(outw, outh, outd, channels, elemsize, elempack, opt.blob_allocator);
  166. if (top_blob.empty())
  167. return -100;
  168. #pragma omp parallel for num_threads(opt.num_threads)
  169. for (int q = 0; q < channels; q++)
  170. {
  171. float32x4_t pad_value = per_channel_pad_data_size ? vld1q_f32((const float*)per_channel_pad_data + q * 4) : vdupq_n_f32(value);
  172. for (int z = 0; z < outd; z++)
  173. {
  174. Mat borderm = top_blob.channel(q).depth(z);
  175. // depth padding
  176. if ((z - front) < 0 || (z - front) >= d)
  177. {
  178. borderm.fill(pad_value);
  179. }
  180. else
  181. {
  182. const Mat m = bottom_blob.channel(q).depth(z - front);
  183. padding_constant_pack4_neon(m, borderm, top, bottom, left, right, pad_value);
  184. }
  185. }
  186. }
  187. return 0;
  188. }
  189. }
  190. }
  191. #endif // __ARM_NEON
  192. Mat bottom_blob_unpacked = bottom_blob;
  193. if (elempack != 1)
  194. {
  195. Option opt_pack1 = opt;
  196. opt_pack1.blob_allocator = opt.workspace_allocator;
  197. convert_packing(bottom_blob, bottom_blob_unpacked, 1, opt_pack1);
  198. }
  199. return Padding::forward(bottom_blob_unpacked, top_blob, opt);
  200. }
  201. int Padding_arm::forward_bf16s_fp16s(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const
  202. {
  203. int w = bottom_blob.w;
  204. int h = bottom_blob.h;
  205. int d = bottom_blob.d;
  206. int channels = bottom_blob.c;
  207. int dims = bottom_blob.dims;
  208. size_t elemsize = bottom_blob.elemsize;
  209. int elempack = bottom_blob.elempack;
  210. #if __ARM_NEON
  211. #if NCNN_ARM82
  212. if (elempack == 8)
  213. {
  214. if (dims == 1)
  215. {
  216. int outw = w * elempack + left + right;
  217. int out_elempack = outw % 8 == 0 ? 8 : outw % 4 == 0 ? 4 : 1;
  218. size_t out_elemsize = elemsize / elempack * out_elempack;
  219. if (left % 8 == 0 && out_elempack == 8 && type == 0)
  220. {
  221. top_blob.create(outw / out_elempack, out_elemsize, out_elempack, opt.blob_allocator);
  222. if (top_blob.empty())
  223. return -100;
  224. uint16x8_t pad_value = vdupq_n_u16(value_fp16);
  225. padding_constant_pack8_fp16s_neon(bottom_blob, top_blob, 0, 0, left / 8, right / 8, pad_value);
  226. return 0;
  227. }
  228. }
  229. if (dims == 2)
  230. {
  231. int outw = w + left + right;
  232. int outh = h * elempack + top + bottom;
  233. int out_elempack = outh % 8 == 0 ? 8 : outh % 4 == 0 ? 4 : 1;
  234. size_t out_elemsize = elemsize / elempack * out_elempack;
  235. if (top % 8 == 0 && out_elempack == 8 && type == 0)
  236. {
  237. top_blob.create(outw, outh / out_elempack, out_elemsize, out_elempack, opt.blob_allocator);
  238. if (top_blob.empty())
  239. return -100;
  240. uint16x8_t pad_value = vdupq_n_u16(value_fp16);
  241. padding_constant_pack8_fp16s_neon(bottom_blob, top_blob, top / 8, bottom / 8, left, right, pad_value);
  242. return 0;
  243. }
  244. }
  245. if (dims == 3)
  246. {
  247. int outw = w + left + right;
  248. int outh = h + top + bottom;
  249. int outc = channels * elempack + front + behind;
  250. int out_elempack = outc % 8 == 0 ? 8 : outc % 4 == 0 ? 4 : 1;
  251. size_t out_elemsize = elemsize / elempack * out_elempack;
  252. if (front % 8 == 0 && out_elempack == 8 && !(outc != channels * elempack && type != 0))
  253. {
  254. top_blob.create(outw, outh, outc / out_elempack, out_elemsize, out_elempack, opt.blob_allocator);
  255. if (top_blob.empty())
  256. return -100;
  257. int front_ = front / elempack;
  258. #pragma omp parallel for num_threads(opt.num_threads)
  259. for (int q = 0; q < outc / out_elempack; q++)
  260. {
  261. Mat borderm = top_blob.channel(q);
  262. uint16x8_t pad_value = per_channel_pad_data_size ? vld1q_u16((const unsigned short*)per_channel_pad_data_fp16 + q * 8) : vdupq_n_u16(value_fp16);
  263. //Channel padding
  264. if ((q - front_) < 0 || (q - front_) >= channels)
  265. {
  266. borderm.fill(pad_value);
  267. }
  268. else
  269. {
  270. const Mat m = bottom_blob.channel(q - front_);
  271. if (type == 0)
  272. padding_constant_pack8_fp16s_neon(m, borderm, top, bottom, left, right, pad_value);
  273. if (type == 1)
  274. padding_replicate_pack8_fp16s_neon(m, borderm, top, bottom, left, right);
  275. if (type == 2)
  276. padding_reflect_pack8_fp16s_neon(m, borderm, top, bottom, left, right);
  277. }
  278. }
  279. return 0;
  280. }
  281. }
  282. if (dims == 4)
  283. {
  284. int outw = w + left + right;
  285. int outh = h + top + bottom;
  286. int outd = d + front + behind;
  287. if (type == 0)
  288. {
  289. top_blob.create(outw, outh, outd, channels, elemsize, elempack, opt.blob_allocator);
  290. if (top_blob.empty())
  291. return -100;
  292. #pragma omp parallel for num_threads(opt.num_threads)
  293. for (int q = 0; q < channels; q++)
  294. {
  295. uint16x8_t pad_value = per_channel_pad_data_size ? vld1q_u16((const unsigned short*)per_channel_pad_data_fp16 + q * 8) : vdupq_n_u16(value_fp16);
  296. for (int z = 0; z < outd; z++)
  297. {
  298. Mat borderm = top_blob.channel(q).depth(z);
  299. // depth padding
  300. if ((z - front) < 0 || (z - front) >= d)
  301. {
  302. borderm.fill(pad_value);
  303. }
  304. else
  305. {
  306. const Mat m = bottom_blob.channel(q).depth(z - front);
  307. padding_constant_pack8_fp16s_neon(m, borderm, top, bottom, left, right, pad_value);
  308. }
  309. }
  310. }
  311. return 0;
  312. }
  313. }
  314. }
  315. #endif
  316. if (elempack == 4)
  317. {
  318. if (dims == 1)
  319. {
  320. int outw = w * elempack + left + right;
  321. #if NCNN_ARM82
  322. int out_elempack = support_fp16_storage && opt.use_fp16_arithmetic && outw % 8 == 0 ? 8 : outw % 4 == 0 ? 4 : 1;
  323. #else
  324. int out_elempack = outw % 4 == 0 ? 4 : 1;
  325. #endif
  326. size_t out_elemsize = elemsize / elempack * out_elempack;
  327. if (left % 4 == 0 && out_elempack == 4 && type == 0)
  328. {
  329. top_blob.create(outw / out_elempack, out_elemsize, out_elempack, opt.blob_allocator);
  330. if (top_blob.empty())
  331. return -100;
  332. // clang-format off
  333. // *INDENT-OFF*
  334. uint16x4_t pad_value;
  335. #if NCNN_ARM82
  336. if (support_fp16_storage && opt.use_fp16_storage)
  337. {
  338. pad_value = vdup_n_u16(value_fp16);
  339. }
  340. else
  341. #endif
  342. #if NCNN_BF16
  343. if (opt.use_bf16_storage)
  344. {
  345. pad_value = vdup_n_u16(value_bf16);
  346. }
  347. else
  348. #endif
  349. {
  350. // shall never reach here
  351. pad_value = vdup_n_u16(0);
  352. }
  353. // *INDENT-ON*
  354. // clang-format on
  355. padding_constant_pack4_bf16_fp16s_neon(bottom_blob, top_blob, 0, 0, left / 4, right / 4, vcombine_u16(pad_value, pad_value));
  356. return 0;
  357. }
  358. }
  359. if (dims == 2)
  360. {
  361. int outw = w + left + right;
  362. int outh = h * elempack + top + bottom;
  363. #if NCNN_ARM82
  364. int out_elempack = support_fp16_storage && opt.use_fp16_arithmetic && outh % 8 == 0 ? 8 : outh % 4 == 0 ? 4 : 1;
  365. #else
  366. int out_elempack = outh % 4 == 0 ? 4 : 1;
  367. #endif
  368. size_t out_elemsize = elemsize / elempack * out_elempack;
  369. if (top % 4 == 0 && out_elempack == 4 && type == 0)
  370. {
  371. top_blob.create(outw, outh / out_elempack, out_elemsize, out_elempack, opt.blob_allocator);
  372. if (top_blob.empty())
  373. return -100;
  374. // clang-format off
  375. // *INDENT-OFF*
  376. uint16x4_t pad_value;
  377. #if NCNN_ARM82
  378. if (support_fp16_storage && opt.use_fp16_storage)
  379. {
  380. pad_value = vdup_n_u16(value_fp16);
  381. }
  382. else
  383. #endif
  384. #if NCNN_BF16
  385. if (opt.use_bf16_storage)
  386. {
  387. pad_value = vdup_n_u16(value_bf16);
  388. }
  389. else
  390. #endif
  391. {
  392. // shall never reach here
  393. pad_value = vdup_n_u16(0);
  394. }
  395. // *INDENT-ON*
  396. // clang-format on
  397. padding_constant_pack4_bf16_fp16s_neon(bottom_blob, top_blob, top / 4, bottom / 4, left, right, vcombine_u16(pad_value, pad_value));
  398. return 0;
  399. }
  400. }
  401. if (dims == 3)
  402. {
  403. int outw = w + left + right;
  404. int outh = h + top + bottom;
  405. int outc = channels * elempack + front + behind;
  406. #if NCNN_ARM82
  407. int out_elempack = support_fp16_storage && opt.use_fp16_arithmetic && outc % 8 == 0 ? 8 : outc % 4 == 0 ? 4 : 1;
  408. #else
  409. int out_elempack = outc % 4 == 0 ? 4 : 1;
  410. #endif
  411. size_t out_elemsize = elemsize / elempack * out_elempack;
  412. if (front % 4 == 0 && out_elempack == 4 && !(outc != channels * elempack && type != 0))
  413. {
  414. top_blob.create(outw, outh, outc / out_elempack, out_elemsize, out_elempack, opt.blob_allocator);
  415. if (top_blob.empty())
  416. return -100;
  417. int front_ = front / elempack;
  418. #pragma omp parallel for num_threads(opt.num_threads)
  419. for (int q = 0; q < outc / out_elempack; q++)
  420. {
  421. Mat borderm = top_blob.channel(q);
  422. // clang-format off
  423. // *INDENT-OFF*
  424. uint16x4_t pad_value;
  425. #if NCNN_ARM82
  426. if (support_fp16_storage && opt.use_fp16_storage)
  427. {
  428. pad_value = per_channel_pad_data_size ? vld1_u16((const unsigned short*)per_channel_pad_data_fp16 + q * 4) : vdup_n_u16(value_fp16);
  429. }
  430. else
  431. #endif
  432. #if NCNN_BF16
  433. if (opt.use_bf16_storage)
  434. {
  435. pad_value = per_channel_pad_data_size ? vld1_u16((const unsigned short*)per_channel_pad_data_bf16 + q * 4) : vdup_n_u16(value_bf16);
  436. }
  437. else
  438. #endif
  439. {
  440. // shall never reach here
  441. pad_value = vdup_n_u16(0);
  442. }
  443. // *INDENT-ON*
  444. // clang-format on
  445. //Channel padding
  446. if ((q - front_) < 0 || (q - front_) >= channels)
  447. {
  448. borderm.fill(pad_value);
  449. }
  450. else
  451. {
  452. const Mat m = bottom_blob.channel(q - front_);
  453. if (type == 0)
  454. padding_constant_pack4_bf16_fp16s_neon(m, borderm, top, bottom, left, right, vcombine_u16(pad_value, pad_value));
  455. if (type == 1)
  456. padding_replicate_pack4_bf16_fp16s_neon(m, borderm, top, bottom, left, right);
  457. if (type == 2)
  458. padding_reflect_pack4_bf16_fp16s_neon(m, borderm, top, bottom, left, right);
  459. }
  460. }
  461. return 0;
  462. }
  463. }
  464. if (dims == 4)
  465. {
  466. int outw = w + left + right;
  467. int outh = h + top + bottom;
  468. int outd = d + front + behind;
  469. if (type == 0)
  470. {
  471. top_blob.create(outw, outh, outd, channels, elemsize, elempack, opt.blob_allocator);
  472. if (top_blob.empty())
  473. return -100;
  474. #pragma omp parallel for num_threads(opt.num_threads)
  475. for (int q = 0; q < channels; q++)
  476. {
  477. // clang-format off
  478. // *INDENT-OFF*
  479. uint16x4_t pad_value;
  480. #if NCNN_ARM82
  481. if (support_fp16_storage && opt.use_fp16_storage)
  482. {
  483. pad_value = per_channel_pad_data_size ? vld1_u16((const unsigned short*)per_channel_pad_data_fp16 + q * 4) : vdup_n_u16(value_fp16);
  484. }
  485. else
  486. #endif
  487. #if NCNN_BF16
  488. if (opt.use_bf16_storage)
  489. {
  490. pad_value = per_channel_pad_data_size ? vld1_u16((const unsigned short*)per_channel_pad_data_bf16 + q * 4) : vdup_n_u16(value_bf16);
  491. }
  492. else
  493. #endif
  494. {
  495. // shall never reach here
  496. pad_value = vdup_n_u16(0);
  497. }
  498. // *INDENT-ON*
  499. // clang-format on
  500. for (int z = 0; z < outd; z++)
  501. {
  502. Mat borderm = top_blob.channel(q).depth(z);
  503. // depth padding
  504. if ((z - front) < 0 || (z - front) >= d)
  505. {
  506. borderm.fill(pad_value);
  507. }
  508. else
  509. {
  510. const Mat m = bottom_blob.channel(q).depth(z - front);
  511. padding_constant_pack4_bf16_fp16s_neon(m, borderm, top, bottom, left, right, vcombine_u16(pad_value, pad_value));
  512. }
  513. }
  514. }
  515. return 0;
  516. }
  517. }
  518. }
  519. #endif // __ARM_NEON
  520. Mat bottom_blob_unpacked = bottom_blob;
  521. if (elempack != 1)
  522. {
  523. Option opt_pack1 = opt;
  524. opt_pack1.blob_allocator = opt.workspace_allocator;
  525. convert_packing(bottom_blob, bottom_blob_unpacked, 1, opt_pack1);
  526. }
  527. return Padding::forward(bottom_blob_unpacked, top_blob, opt);
  528. }
  529. int Padding_arm::forward_int8(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const
  530. {
  531. int w = bottom_blob.w;
  532. int h = bottom_blob.h;
  533. int d = bottom_blob.d;
  534. int channels = bottom_blob.c;
  535. int dims = bottom_blob.dims;
  536. size_t elemsize = bottom_blob.elemsize;
  537. int elempack = bottom_blob.elempack;
  538. #if __ARM_NEON
  539. if (elempack == 8)
  540. {
  541. if (dims == 1)
  542. {
  543. int outw = w * elempack + left + right;
  544. int out_elempack = outw % 8 == 0 ? 8 : 1;
  545. size_t out_elemsize = elemsize / elempack * out_elempack;
  546. if (left % 8 == 0 && out_elempack == 8 && type == 0)
  547. {
  548. top_blob.create(outw / out_elempack, out_elemsize, out_elempack, opt.blob_allocator);
  549. if (top_blob.empty())
  550. return -100;
  551. int8x8_t pad_value = vdup_n_s8((signed char)value);
  552. padding_constant_pack8_int8_neon(bottom_blob, top_blob, 0, 0, left / 8, right / 8, pad_value);
  553. return 0;
  554. }
  555. }
  556. if (dims == 2)
  557. {
  558. int outw = w + left + right;
  559. int outh = h * elempack + top + bottom;
  560. int out_elempack = outh % 8 == 0 ? 8 : 1;
  561. size_t out_elemsize = elemsize / elempack * out_elempack;
  562. if (top % 8 == 0 && out_elempack == 8 && type == 0)
  563. {
  564. top_blob.create(outw, outh / out_elempack, out_elemsize, out_elempack, opt.blob_allocator);
  565. if (top_blob.empty())
  566. return -100;
  567. int8x8_t pad_value = vdup_n_s8((signed char)value);
  568. padding_constant_pack8_int8_neon(bottom_blob, top_blob, top / 8, bottom / 8, left, right, pad_value);
  569. return 0;
  570. }
  571. }
  572. if (dims == 3)
  573. {
  574. int outw = w + left + right;
  575. int outh = h + top + bottom;
  576. int outc = channels * elempack + front + behind;
  577. int out_elempack = outc % 8 == 0 ? 8 : 1;
  578. size_t out_elemsize = elemsize / elempack * out_elempack;
  579. top_blob.create(outw, outh, outc / out_elempack, out_elemsize, out_elempack, opt.blob_allocator);
  580. if (top_blob.empty())
  581. return -100;
  582. if (front % 8 == 0 && out_elempack == 8 && !(outc != channels * elempack && type != 0))
  583. {
  584. int front_ = front / elempack;
  585. #pragma omp parallel for num_threads(opt.num_threads)
  586. for (int q = 0; q < outc / out_elempack; q++)
  587. {
  588. Mat borderm = top_blob.channel(q);
  589. // TODO perchannel
  590. // int8x8_t pad_value = per_channel_pad_data_size ? vld1_s8(per_channel_pad_data + q * 8) : vdup_n_s8((signed char)value);
  591. int8x8_t pad_value = vdup_n_s8((signed char)value);
  592. //Channel padding
  593. if ((q - front_) < 0 || (q - front_) >= channels)
  594. {
  595. borderm.fill<int8x8_t>(pad_value);
  596. }
  597. else
  598. {
  599. const Mat m = bottom_blob.channel(q - front_);
  600. if (type == 0)
  601. padding_constant_pack8_int8_neon(m, borderm, top, bottom, left, right, pad_value);
  602. if (type == 1)
  603. padding_replicate_pack8_int8_neon(m, borderm, top, bottom, left, right);
  604. if (type == 2)
  605. padding_reflect_pack8_int8_neon(m, borderm, top, bottom, left, right);
  606. }
  607. }
  608. return 0;
  609. }
  610. }
  611. if (dims == 4)
  612. {
  613. int outw = w + left + right;
  614. int outh = h + top + bottom;
  615. int outd = d + front + behind;
  616. top_blob.create(outw, outh, outd, channels, elemsize, elempack, opt.blob_allocator);
  617. if (top_blob.empty())
  618. return -100;
  619. if (type == 0)
  620. {
  621. #pragma omp parallel for num_threads(opt.num_threads)
  622. for (int q = 0; q < channels; q++)
  623. {
  624. // TODO perchannel
  625. // int8x8_t pad_value = per_channel_pad_data_size ? vld1_s8(per_channel_pad_data + q * 8) : vdup_n_s8((signed char)value);
  626. int8x8_t pad_value = vdup_n_s8((signed char)value);
  627. for (int z = 0; z < outd; z++)
  628. {
  629. Mat borderm = top_blob.channel(q).depth(z);
  630. // depth padding
  631. if ((z - front) < 0 || (z - front) >= d)
  632. {
  633. borderm.fill<int8x8_t>(pad_value);
  634. }
  635. else
  636. {
  637. const Mat m = bottom_blob.channel(q).depth(z - front);
  638. padding_constant_pack8_int8_neon(m, borderm, top, bottom, left, right, pad_value);
  639. }
  640. }
  641. }
  642. return 0;
  643. }
  644. }
  645. }
  646. #endif // __ARM_NEON
  647. Mat bottom_blob_unpacked = bottom_blob;
  648. if (elempack != 1)
  649. {
  650. Option opt_pack1 = opt;
  651. opt_pack1.blob_allocator = opt.workspace_allocator;
  652. convert_packing(bottom_blob, bottom_blob_unpacked, 1, opt_pack1);
  653. }
  654. return Padding::forward(bottom_blob_unpacked, top_blob, opt);
  655. }
  656. } // namespace ncnn