| 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 | |
| 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(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("Slice", pd, weights, a0, slices.w); |
| 79 | if (ret != 0) |
| 80 | { |
| 81 | fprintf(stderr, "test_slice 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_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("Slice", pd, weights, a0, indices.w); |
| 102 | if (ret != 0) |
| 103 | { |
| 104 | fprintf(stderr, "test_slice_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[] = { |
| 116 | RandomMat(30, 32, 36, 48), |
| 117 | RandomMat(36, 30, 32, 51), |
| 118 | RandomMat(30, 32, 36, 60) |
| 119 | }; |
| 120 | |
| 121 | for (int i = 0; i < sizeof(a) / sizeof(a[0]); i++) |
| 122 | { |
| 123 | int ret = 0 |
| 124 | || test_slice(a[i], IntArrayMat(-233, -233, -233), 0) |
| 125 | || test_slice(a[i], IntArrayMat(-233, -233, -233), 1) |
| 126 | || test_slice(a[i], IntArrayMat(-233, -233, -233), -2) |
| 127 | || test_slice(a[i], IntArrayMat(-233, -233, -233), 3) |
| 128 | || test_slice(a[i], IntArrayMat(3, 12, 16, -233), 0) |
| 129 | || test_slice(a[i], IntArrayMat(12, 16, -233), 0) |
| 130 | || test_slice(a[i], IntArrayMat(32, 8, -233), 0) |
| 131 | || test_slice(a[i], IntArrayMat(2, 12, 16, -233), 1) |
| 132 | || test_slice(a[i], IntArrayMat(16, 4, 5, -233), -2) |
| 133 | || test_slice(a[i], IntArrayMat(8, 2, 16, -233), 3) |
| 134 | || test_slice_indices(a[i], IntArrayMat(2, -24, -8), 0) |
| 135 | || test_slice_indices(a[i], IntArrayMat(4, 20, 4), 1) |
| 136 | || test_slice_indices(a[i], IntArrayMat(16, -16), -2) |
| 137 | || test_slice_indices(a[i], IntArrayMat(1, -12), 3); |
| 138 | |
| 139 | if (ret != 0) |
| 140 | return ret; |
| 141 | } |
| 142 | |
| 143 | return 0; |
| 144 | } |
| 145 | |
| 146 | static int test_slice_1() |
| 147 | { |
| 148 | ncnn::Mat a[] = { |
| 149 | RandomMat(51, 36, 48), |
| 150 | RandomMat(48, 48, 51), |
| 151 | RandomMat(51, 36, 60) |
| 152 | }; |
| 153 | |
| 154 | for (int i = 0; i < sizeof(a) / sizeof(a[0]); i++) |
| 155 | { |
| 156 | int ret = 0 |
| 157 | || test_slice(a[i], IntArrayMat(-233, -233, -233), 0) |
| 158 | || test_slice(a[i], IntArrayMat(-233, -233, -233), 1) |
| 159 | || test_slice(a[i], IntArrayMat(-233, -233, -233), -1) |
| 160 | || test_slice(a[i], IntArrayMat(3, 12, 16, -233), 0) |
| 161 | || test_slice(a[i], IntArrayMat(12, 16, -233), 0) |
| 162 | || test_slice(a[i], IntArrayMat(32, 8, -233), 0) |
| 163 | || test_slice(a[i], IntArrayMat(2, 12, 16, -233), 1) |
| 164 | || test_slice(a[i], IntArrayMat(16, 4, 5, -233), -1) |
| 165 | || test_slice_indices(a[i], IntArrayMat(2, -24, -8), 0) |
| 166 | || test_slice_indices(a[i], IntArrayMat(4, 20, 4), 1) |
| 167 | || test_slice_indices(a[i], IntArrayMat(1, -12), 2); |
| 168 | |
| 169 | if (ret != 0) |
| 170 | return ret; |
| 171 | } |
| 172 | |
| 173 | return 0; |
| 174 | } |
| 175 | |
| 176 | static int test_slice_2() |
| 177 | { |
| 178 | ncnn::Mat a[] = { |
| 179 | RandomMat(36, 48), |
| 180 | RandomMat(48, 51), |
| 181 | RandomMat(36, 60) |
| 182 | }; |
| 183 | |
| 184 | for (int i = 0; i < sizeof(a) / sizeof(a[0]); i++) |
| 185 | { |
| 186 | int ret = 0 |
| 187 | || test_slice(a[i], IntArrayMat(-233, -233, -233), 0) |
| 188 | || test_slice(a[i], IntArrayMat(-233, -233, -233), -1) |
| 189 | || test_slice(a[i], IntArrayMat(3, 12, 16, -233), 0) |
| 190 | || test_slice(a[i], IntArrayMat(12, 16, -233), 0) |
| 191 | || test_slice(a[i], IntArrayMat(32, 8, -233), -2) |
| 192 | || test_slice(a[i], IntArrayMat(2, 12, 16, -233), -1) |
| 193 | || test_slice_indices(a[i], IntArrayMat(2, -24, -8), 0) |
| 194 | || test_slice_indices(a[i], IntArrayMat(1, -12), 1); |
| 195 | |
| 196 | if (ret != 0) |
| 197 | return ret; |
| 198 | } |
| 199 | |
| 200 | return 0; |
| 201 | } |
| 202 | |
| 203 | static int test_slice_3() |
| 204 | { |
| 205 | ncnn::Mat a[] = { |
| 206 | RandomMat(48), |
| 207 | RandomMat(51), |
| 208 | RandomMat(60) |
| 209 | }; |
| 210 | |
| 211 | for (int i = 0; i < sizeof(a) / sizeof(a[0]); i++) |
| 212 | { |
| 213 | int ret = 0 |
| 214 | || test_slice(a[i], IntArrayMat(-233, -233, -233), 0) |
| 215 | || test_slice(a[i], IntArrayMat(3, 12, 16, -233), 0) |
| 216 | || test_slice(a[i], IntArrayMat(12, 16, -233), 0) |
| 217 | || test_slice(a[i], IntArrayMat(32, 8, -233), -1) |
| 218 | || test_slice_indices(a[i], IntArrayMat(2, -24, -8), 0); |
| 219 | |
| 220 | if (ret != 0) |
| 221 | return ret; |
| 222 | } |
| 223 | |
| 224 | return 0; |
| 225 | } |
| 226 | |
| 227 | int main() |
| 228 | { |
| 229 | SRAND(7767517); |
| 230 | |
| 231 | return 0 |
| 232 | || test_slice_0() |
| 233 | || test_slice_1() |
| 234 | || test_slice_2() |
| 235 | || test_slice_3(); |
| 236 | } |