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