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.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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_fp16_packed = false;
  60. opt.use_fp16_storage = false;
  61. opt.use_fp16_arithmetic = false;
  62. opt.use_int8_storage = false;
  63. opt.use_int8_arithmetic = false;
  64. std::vector<ncnn::Mat> a0(1);
  65. a0[0] = a;
  66. int ret = test_layer<ncnn::Slice>("Slice", pd, weights, opt, a0, slices.w);
  67. if (ret != 0)
  68. {
  69. fprintf(stderr, "test_slice failed a.dims=%d a=(%d %d %d)", a.dims, a.w, a.h, a.c);
  70. fprintf(stderr, " slices="); print_int_array(slices);
  71. fprintf(stderr, " axis=%d\n", axis);
  72. }
  73. return ret;
  74. }
  75. static int test_slice_0()
  76. {
  77. ncnn::Mat a = RandomMat(48, 36, 24);
  78. return 0
  79. || test_slice(a, IntArrayMat(-233, -233, -233), 0)
  80. || test_slice(a, IntArrayMat(-233, -233, -233), 1)
  81. || test_slice(a, IntArrayMat(-233, -233, -233), 2)
  82. ;
  83. }
  84. static int test_slice_1()
  85. {
  86. ncnn::Mat a = RandomMat(7, 3, 16);
  87. return test_slice(a, IntArrayMat(3, 8, -233), 0);
  88. }
  89. static int test_slice_2()
  90. {
  91. ncnn::Mat a = RandomMat(7, 16, 2);
  92. ncnn::Mat b = RandomMat(7, 16, 24);
  93. return 0
  94. || test_slice(a, IntArrayMat(3, 8, -233), 1)
  95. || test_slice(b, IntArrayMat(3, 8, 5), 1)
  96. ;
  97. }
  98. static int test_slice_3()
  99. {
  100. ncnn::Mat a = RandomMat(16, 7, 2);
  101. ncnn::Mat b = RandomMat(16, 7, 8);
  102. return 0
  103. || test_slice(a, IntArrayMat(5, 4, 7), 2)
  104. || test_slice(b, IntArrayMat(5, 4, 7), 2)
  105. ;
  106. }
  107. static int test_slice_4()
  108. {
  109. ncnn::Mat a = RandomMat(7, 16);
  110. ncnn::Mat b = RandomMat(16, 2);
  111. ncnn::Mat c = RandomMat(16, 8);
  112. return 0
  113. || test_slice(a, IntArrayMat(3, 8, 5), 0)
  114. || test_slice(b, IntArrayMat(3, -233, -233), 1)
  115. || test_slice(c, IntArrayMat(3, 8, 5), 1)
  116. ;
  117. }
  118. static int test_slice_5()
  119. {
  120. ncnn::Mat a = RandomMat(16);
  121. ncnn::Mat b = RandomMat(24);
  122. return 0
  123. || test_slice(a, IntArrayMat(3, 8, 5), 0)
  124. || test_slice(b, IntArrayMat(4, 8, -233), 0)
  125. ;
  126. }
  127. int main()
  128. {
  129. SRAND(7767517);
  130. return 0
  131. || test_slice_0()
  132. || test_slice_1()
  133. || test_slice_2()
  134. || test_slice_3()
  135. || test_slice_4()
  136. || test_slice_5()
  137. ;
  138. }