| 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 int test_permute(const ncnn::Mat& a, int order_type) |
| 18 | { |
| 19 | ncnn::ParamDict pd; |
| 20 | pd.set(0, order_type); |
| 21 | |
| 22 | std::vector<ncnn::Mat> weights(0); |
| 23 | |
| 24 | int ret = test_layer("Permute", pd, weights, a); |
| 25 | if (ret != 0) |
| 26 | { |
| 27 | fprintf(stderr, "test_permute failed a.dims=%d a=(%d %d %d %d) order_type=%d\n", a.dims, a.w, a.h, a.d, a.c, order_type); |
| 28 | } |
| 29 | |
| 30 | return ret; |
| 31 | } |
| 32 | |
| 33 | static int test_permute_0() |
| 34 | { |
| 35 | ncnn::Mat a = RandomMat(128); |
| 36 | ncnn::Mat b = RandomMat(127); |
| 37 | |
| 38 | return 0 |
| 39 | || test_permute(a, 0) |
| 40 | || test_permute(b, 0); |
| 41 | } |
| 42 | |
| 43 | static int test_permute_1() |
| 44 | { |
| 45 | ncnn::Mat a = RandomMat(12, 32); |
| 46 | ncnn::Mat b = RandomMat(8, 15); |
| 47 | ncnn::Mat c = RandomMat(11, 16); |
| 48 | ncnn::Mat d = RandomMat(7, 9); |
| 49 | |
| 50 | for (int order_type = 0; order_type < 2; order_type++) |
| 51 | { |
| 52 | int ret = 0 |
| 53 | || test_permute(a, order_type) |
| 54 | || test_permute(b, order_type) |
| 55 | || test_permute(c, order_type) |
| 56 | || test_permute(d, order_type); |
| 57 | |
| 58 | if (ret != 0) |
| 59 | return -1; |
| 60 | } |
| 61 | |
| 62 | return 0; |
| 63 | } |
| 64 | |
| 65 | static int test_permute_2() |
| 66 | { |
| 67 | ncnn::Mat a = RandomMat(8, 16, 32); |
| 68 | ncnn::Mat b = RandomMat(12, 8, 16); |
| 69 | ncnn::Mat c = RandomMat(7, 14, 12); |
| 70 | ncnn::Mat d = RandomMat(4, 4, 13); |
| 71 | ncnn::Mat e = RandomMat(1, 2, 7); |
| 72 | ncnn::Mat f = RandomMat(8, 5, 6); |
| 73 | |
| 74 | for (int order_type = 0; order_type < 6; order_type++) |
| 75 | { |
| 76 | int ret = 0 |
| 77 | || test_permute(a, order_type) |
| 78 | || test_permute(b, order_type) |
| 79 | || test_permute(c, order_type) |
| 80 | || test_permute(d, order_type) |
| 81 | || test_permute(e, order_type) |
| 82 | || test_permute(f, order_type); |
| 83 | |
| 84 | if (ret != 0) |
| 85 | return -1; |
| 86 | } |
| 87 | |
| 88 | return 0; |
| 89 | } |
| 90 | |
| 91 | static int test_permute_3() |
| 92 | { |
| 93 | ncnn::Mat a = RandomMat(8, 12, 16, 32); |
| 94 | ncnn::Mat b = RandomMat(12, 4, 8, 16); |
| 95 | ncnn::Mat c = RandomMat(7, 8, 14, 12); |
| 96 | ncnn::Mat d = RandomMat(4, 4, 4, 13); |
| 97 | ncnn::Mat e = RandomMat(1, 2, 3, 7); |
| 98 | ncnn::Mat f = RandomMat(8, 6, 5, 6); |
| 99 | |
| 100 | for (int order_type = 0; order_type < 24; order_type++) |
| 101 | { |
| 102 | int ret = 0 |
| 103 | || test_permute(a, order_type) |
| 104 | || test_permute(b, order_type) |
| 105 | || test_permute(c, order_type) |
| 106 | || test_permute(d, order_type) |
| 107 | || test_permute(e, order_type) |
| 108 | || test_permute(f, order_type); |
| 109 | |
| 110 | if (ret != 0) |
| 111 | return -1; |
| 112 | } |
| 113 | |
| 114 | return 0; |
| 115 | } |
| 116 | |
| 117 | int main() |
| 118 | { |
| 119 | SRAND(7767517); |
| 120 | |
| 121 | return 0 |
| 122 | || test_permute_0() |
| 123 | || test_permute_1() |
| 124 | || test_permute_2() |
| 125 | || test_permute_3(); |
| 126 | } |