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_oom.cpp 4.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. // Tencent is pleased to support the open source community by making ncnn available.
  2. //
  3. // Copyright (C) 2024 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. static std::vector<int> IntArray(int a0)
  16. {
  17. std::vector<int> m(1);
  18. m[0] = a0;
  19. return m;
  20. }
  21. static std::vector<int> IntArray(int a0, int a1)
  22. {
  23. std::vector<int> m(2);
  24. m[0] = a0;
  25. m[1] = a1;
  26. return m;
  27. }
  28. static std::vector<int> IntArray(int a0, int a1, int a2)
  29. {
  30. std::vector<int> m(3);
  31. m[0] = a0;
  32. m[1] = a1;
  33. m[2] = a2;
  34. return m;
  35. }
  36. static std::vector<int> IntArray(int a0, int a1, int a2, int a3)
  37. {
  38. std::vector<int> m(4);
  39. m[0] = a0;
  40. m[1] = a1;
  41. m[2] = a2;
  42. m[3] = a3;
  43. return m;
  44. }
  45. static void print_int_array(const std::vector<int>& a)
  46. {
  47. fprintf(stderr, "[");
  48. for (size_t i = 0; i < a.size(); i++)
  49. {
  50. fprintf(stderr, " %d", a[i]);
  51. }
  52. fprintf(stderr, " ]");
  53. }
  54. static int test_slice_oom(const ncnn::Mat& a, const std::vector<int>& slices_array, int axis)
  55. {
  56. ncnn::Mat slices(slices_array.size());
  57. {
  58. int* p = slices;
  59. for (size_t i = 0; i < slices_array.size(); i++)
  60. {
  61. p[i] = slices_array[i];
  62. }
  63. }
  64. ncnn::ParamDict pd;
  65. pd.set(0, slices);
  66. pd.set(1, axis);
  67. std::vector<ncnn::Mat> weights(0);
  68. std::vector<ncnn::Mat> a0(1);
  69. a0[0] = a;
  70. int ret = test_layer_oom("Slice", pd, weights, a0, slices.w);
  71. if (ret != 0)
  72. {
  73. fprintf(stderr, "test_slice_oom failed a.dims=%d a=(%d %d %d %d)", a.dims, a.w, a.h, a.d, a.c);
  74. fprintf(stderr, " slices=");
  75. print_int_array(slices_array);
  76. fprintf(stderr, " axis=%d\n", axis);
  77. }
  78. return ret;
  79. }
  80. static int test_slice_oom_indices(const ncnn::Mat& a, const std::vector<int>& indices_array, int axis)
  81. {
  82. ncnn::Mat indices(indices_array.size());
  83. {
  84. int* p = indices;
  85. for (size_t i = 0; i < indices_array.size(); i++)
  86. {
  87. p[i] = indices_array[i];
  88. }
  89. }
  90. ncnn::ParamDict pd;
  91. pd.set(1, axis);
  92. pd.set(2, indices);
  93. std::vector<ncnn::Mat> weights(0);
  94. std::vector<ncnn::Mat> a0(1);
  95. a0[0] = a;
  96. int ret = test_layer_oom("Slice", pd, weights, a0, indices.w);
  97. if (ret != 0)
  98. {
  99. fprintf(stderr, "test_slice_oom_indices failed a.dims=%d a=(%d %d %d %d)", a.dims, a.w, a.h, a.d, a.c);
  100. fprintf(stderr, " indices=");
  101. print_int_array(indices_array);
  102. fprintf(stderr, " axis=%d\n", axis);
  103. }
  104. return ret;
  105. }
  106. static int test_slice_0()
  107. {
  108. ncnn::Mat a = RandomMat(48, 48, 48, 48);
  109. return 0
  110. || test_slice_oom(a, IntArray(3, 12, 16, -233), 0)
  111. || test_slice_oom(a, IntArray(3, 12, 16, -233), 1)
  112. || test_slice_oom(a, IntArray(3, 12, 16, -233), 2)
  113. || test_slice_oom(a, IntArray(3, 12, 16, -233), 3)
  114. || test_slice_oom_indices(a, IntArray(2, -24, -8), 0);
  115. }
  116. static int test_slice_1()
  117. {
  118. ncnn::Mat a = RandomMat(48, 48, 48);
  119. return 0
  120. || test_slice_oom(a, IntArray(3, 12, 16, -233), 0)
  121. || test_slice_oom(a, IntArray(3, 12, 16, -233), 1)
  122. || test_slice_oom(a, IntArray(3, 12, 16, -233), 2)
  123. || test_slice_oom_indices(a, IntArray(2, -24, -8), 0);
  124. }
  125. static int test_slice_2()
  126. {
  127. ncnn::Mat a = RandomMat(48, 48);
  128. return 0
  129. || test_slice_oom(a, IntArray(3, 12, 16, -233), 0)
  130. || test_slice_oom(a, IntArray(3, 12, 16, -233), 1)
  131. || test_slice_oom_indices(a, IntArray(2, -24, -8), 0);
  132. }
  133. static int test_slice_3()
  134. {
  135. ncnn::Mat a = RandomMat(48);
  136. return 0
  137. || test_slice_oom(a, IntArray(3, 12, 16, -233), 0)
  138. || test_slice_oom_indices(a, IntArray(2, -24, -8), 0);
  139. }
  140. int main()
  141. {
  142. SRAND(7767517);
  143. return 0
  144. || test_slice_0()
  145. || test_slice_1()
  146. || test_slice_2()
  147. || test_slice_3();
  148. }