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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 "testutil.h"
  15. #include "layer/slice.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="); print_int_array(slices);
  67. fprintf(stderr, " axis=%d\n", axis);
  68. }
  69. return ret;
  70. }
  71. static int test_slice_0()
  72. {
  73. ncnn::Mat a = RandomMat(48, 36, 24);
  74. return 0
  75. || test_slice(a, IntArrayMat(-233, -233, -233), 0)
  76. || test_slice(a, IntArrayMat(-233, -233, -233), 1)
  77. || test_slice(a, IntArrayMat(-233, -233, -233), 2)
  78. ;
  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. }
  94. static int test_slice_3()
  95. {
  96. ncnn::Mat a = RandomMat(16, 7, 2);
  97. ncnn::Mat b = RandomMat(16, 7, 8);
  98. return 0
  99. || test_slice(a, IntArrayMat(5, 4, 7), 2)
  100. || test_slice(b, IntArrayMat(5, 4, 7), 2)
  101. ;
  102. }
  103. static int test_slice_4()
  104. {
  105. ncnn::Mat a = RandomMat(7, 16);
  106. ncnn::Mat b = RandomMat(16, 2);
  107. ncnn::Mat c = RandomMat(16, 8);
  108. return 0
  109. || test_slice(a, IntArrayMat(3, 8, 5), 0)
  110. || test_slice(b, IntArrayMat(3, -233, -233), 1)
  111. || test_slice(c, IntArrayMat(3, 8, 5), 1)
  112. ;
  113. }
  114. static int test_slice_5()
  115. {
  116. ncnn::Mat a = RandomMat(16);
  117. ncnn::Mat b = RandomMat(24);
  118. return 0
  119. || test_slice(a, IntArrayMat(3, 8, 5), 0)
  120. || test_slice(b, IntArrayMat(4, 8, -233), 0)
  121. ;
  122. }
  123. int main()
  124. {
  125. SRAND(7767517);
  126. return 0
  127. || test_slice_0()
  128. || test_slice_1()
  129. || test_slice_2()
  130. || test_slice_3()
  131. || test_slice_4()
  132. || test_slice_5()
  133. ;
  134. }