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.

test_slice.cpp 3.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. // Tencent is pleased to support the open source community by making ncnn available.
  2. //
  3. // Copyright (C) 2020 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 "layer/slice.h"
  15. #include "testutil.h"
  16. static ncnn::Mat IntArrayMat(int a0)
  17. {
  18. ncnn::Mat m(1);
  19. int* p = m;
  20. p[0] = a0;
  21. return m;
  22. }
  23. static ncnn::Mat IntArrayMat(int a0, int a1)
  24. {
  25. ncnn::Mat m(2);
  26. int* p = m;
  27. p[0] = a0;
  28. p[1] = a1;
  29. return m;
  30. }
  31. static ncnn::Mat IntArrayMat(int a0, int a1, int a2)
  32. {
  33. ncnn::Mat m(3);
  34. int* p = m;
  35. p[0] = a0;
  36. p[1] = a1;
  37. p[2] = a2;
  38. return m;
  39. }
  40. static void print_int_array(const ncnn::Mat& a)
  41. {
  42. const int* pa = a;
  43. fprintf(stderr, "[");
  44. for (int i = 0; i < a.w; i++)
  45. {
  46. fprintf(stderr, " %d", pa[i]);
  47. }
  48. fprintf(stderr, " ]");
  49. }
  50. static int test_slice(const ncnn::Mat& a, const ncnn::Mat& slices, int axis)
  51. {
  52. ncnn::ParamDict pd;
  53. pd.set(0, slices);
  54. pd.set(1, axis);
  55. std::vector<ncnn::Mat> weights(0);
  56. ncnn::Option opt;
  57. opt.num_threads = 1;
  58. opt.use_vulkan_compute = true;
  59. opt.use_int8_inference = false;
  60. std::vector<ncnn::Mat> a0(1);
  61. a0[0] = a;
  62. int ret = test_layer<ncnn::Slice>("Slice", pd, weights, opt, a0, slices.w);
  63. if (ret != 0)
  64. {
  65. fprintf(stderr, "test_slice failed a.dims=%d a=(%d %d %d)", a.dims, a.w, a.h, a.c);
  66. fprintf(stderr, " slices=");
  67. print_int_array(slices);
  68. fprintf(stderr, " axis=%d\n", axis);
  69. }
  70. return ret;
  71. }
  72. static int test_slice_0()
  73. {
  74. ncnn::Mat a = RandomMat(48, 36, 24);
  75. return 0
  76. || test_slice(a, IntArrayMat(-233, -233, -233), 0)
  77. || test_slice(a, IntArrayMat(-233, -233, -233), 1)
  78. || test_slice(a, IntArrayMat(-233, -233, -233), 2);
  79. }
  80. static int test_slice_1()
  81. {
  82. ncnn::Mat a = RandomMat(7, 3, 16);
  83. return test_slice(a, IntArrayMat(3, 8, -233), 0);
  84. }
  85. static int test_slice_2()
  86. {
  87. ncnn::Mat a = RandomMat(7, 16, 2);
  88. ncnn::Mat b = RandomMat(7, 16, 24);
  89. return 0
  90. || test_slice(a, IntArrayMat(3, 8, -233), 1)
  91. || test_slice(b, IntArrayMat(3, 8, 5), 1);
  92. }
  93. static int test_slice_3()
  94. {
  95. ncnn::Mat a = RandomMat(16, 7, 2);
  96. ncnn::Mat b = RandomMat(16, 7, 8);
  97. return 0
  98. || test_slice(a, IntArrayMat(5, 4, 7), 2)
  99. || test_slice(b, IntArrayMat(5, 4, 7), 2);
  100. }
  101. static int test_slice_4()
  102. {
  103. ncnn::Mat a = RandomMat(7, 16);
  104. ncnn::Mat b = RandomMat(16, 2);
  105. ncnn::Mat c = RandomMat(16, 8);
  106. return 0
  107. || test_slice(a, IntArrayMat(3, 8, 5), 0)
  108. || test_slice(b, IntArrayMat(3, -233, -233), 1)
  109. || test_slice(c, IntArrayMat(3, 8, 5), 1);
  110. }
  111. static int test_slice_5()
  112. {
  113. ncnn::Mat a = RandomMat(16);
  114. ncnn::Mat b = RandomMat(24);
  115. return 0
  116. || test_slice(a, IntArrayMat(3, 8, 5), 0)
  117. || test_slice(b, IntArrayMat(4, 8, -233), 0);
  118. }
  119. int main()
  120. {
  121. SRAND(7767517);
  122. return 0
  123. || test_slice_0()
  124. || test_slice_1()
  125. || test_slice_2()
  126. || test_slice_3()
  127. || test_slice_4()
  128. || test_slice_5();
  129. }