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

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