| 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_reshape(const ncnn::Mat& a, int outw, int outh, int outd, int outc) |
| 18 | { |
| 19 | ncnn::ParamDict pd; |
| 20 | pd.set(0, outw); // w |
| 21 | pd.set(1, outh); // h |
| 22 | pd.set(11, outd); // d |
| 23 | pd.set(2, outc); // c |
| 24 | |
| 25 | std::vector<ncnn::Mat> weights(0); |
| 26 | |
| 27 | int ret = test_layer("Reshape", pd, weights, a); |
| 28 | if (ret != 0) |
| 29 | { |
| 30 | fprintf(stderr, "test_reshape failed a.dims=%d a=(%d %d %d %d) outw=%d outh=%d outd=%d outc=%d\n", a.dims, a.w, a.h, a.d, a.c, outw, outh, outd, outc); |
| 31 | } |
| 32 | |
| 33 | return ret; |
| 34 | } |
| 35 | |
| 36 | static int test_reshape_0() |
| 37 | { |
| 38 | ncnn::Mat a = RandomMat(3, 8, 5, 32); |
| 39 | |
| 40 | return 0 |
| 41 | || test_reshape(a, 5, 8, 3, 32) |
| 42 | || test_reshape(a, 3, 8, 32, 5) |
| 43 | || test_reshape(a, 32, 5, 8, 3) |
| 44 | || test_reshape(a, 4, 3, 16, -1) |
| 45 | || test_reshape(a, 6, 16, -1, 2) |
| 46 | || test_reshape(a, 4, -1, 8, 8) |
| 47 | || test_reshape(a, -1, 16, 6, 2) |
| 48 | || test_reshape(a, 16, 6, -233, -1) |
| 49 | || test_reshape(a, 8, -1, -233, 8) |
| 50 | || test_reshape(a, -1, 6, -233, 16) |
| 51 | || test_reshape(a, 8, -1, -233, -233) |
| 52 | || test_reshape(a, -1, 6, -233, -233) |
| 53 | || test_reshape(a, -1, -233, -233, -233); |
| 54 | } |
| 55 | |
| 56 | static int test_reshape_1() |
| 57 | { |
| 58 | ncnn::Mat a = RandomMat(4, 5, 12, 26); |
| 59 | |
| 60 | return 0 |
| 61 | || test_reshape(a, 12, 5, 4, 26) |
| 62 | || test_reshape(a, 4, 26, 12, 5) |
| 63 | || test_reshape(a, 13, 5, 12, 8) |
| 64 | || test_reshape(a, 2, 10, 8, -1) |
| 65 | || test_reshape(a, 13, 4, -1, 10) |
| 66 | || test_reshape(a, 13, -1, 12, 4) |
| 67 | || test_reshape(a, -1, 26, 3, 16) |
| 68 | || test_reshape(a, 13, 4, -233, -1) |
| 69 | || test_reshape(a, 26, -1, -233, 12) |
| 70 | || test_reshape(a, -1, 13, -233, 16) |
| 71 | || test_reshape(a, 12, -1, -233, -233) |
| 72 | || test_reshape(a, -1, 24, -233, -233) |
| 73 | || test_reshape(a, -1, -233, -233, -233); |
| 74 | } |
| 75 | |
| 76 | static int test_reshape_2() |
| 77 | { |
| 78 | ncnn::Mat a = RandomMat(6, 7, 32); |
| 79 | |
| 80 | return 0 |
| 81 | || test_reshape(a, 6, 8, 4, 7) |
| 82 | || test_reshape(a, 2, 6, 7, 16) |
| 83 | || test_reshape(a, 7, 6, -233, 32) |
| 84 | || test_reshape(a, 6, 32, -233, 7) |
| 85 | || test_reshape(a, 32, 7, -233, 6) |
| 86 | || test_reshape(a, 2, 6, -233, -1) |
| 87 | || test_reshape(a, -1, 8, -233, 2) |
| 88 | || test_reshape(a, -1, 4, -233, -233) |
| 89 | || test_reshape(a, 8, -1, -233, -233) |
| 90 | || test_reshape(a, 32, 42, -233, -233) |
| 91 | || test_reshape(a, -1, -233, -233, -233); |
| 92 | } |
| 93 | |
| 94 | static int test_reshape_3() |
| 95 | { |
| 96 | ncnn::Mat a = RandomMat(4, 14, 26); |
| 97 | |
| 98 | return 0 |
| 99 | || test_reshape(a, 13, 4, 2, 14) |
| 100 | || test_reshape(a, 1, 13, 7, 16) |
| 101 | || test_reshape(a, 14, 8, -233, 13) |
| 102 | || test_reshape(a, 4, 13, -233, 28) |
| 103 | || test_reshape(a, 13, 14, -233, 8) |
| 104 | || test_reshape(a, 2, 7, -233, -1) |
| 105 | || test_reshape(a, -1, 13, -233, 2) |
| 106 | || test_reshape(a, -1, 4, -233, -233) |
| 107 | || test_reshape(a, 8, -1, -233, -233) |
| 108 | || test_reshape(a, 16, 91, -233, -233) |
| 109 | || test_reshape(a, -1, -233, -233, -233); |
| 110 | } |
| 111 | |
| 112 | static int test_reshape_4() |
| 113 | { |
| 114 | ncnn::Mat a = RandomMat(14, 32); |
| 115 | |
| 116 | return 0 |
| 117 | || test_reshape(a, 2, 7, 2, 16) |
| 118 | || test_reshape(a, 16, 1, 7, 4) |
| 119 | || test_reshape(a, 7, 2, -233, 32) |
| 120 | || test_reshape(a, 4, 16, -233, 7) |
| 121 | || test_reshape(a, 16, 14, -233, 2) |
| 122 | || test_reshape(a, 2, 4, -233, -1) |
| 123 | || test_reshape(a, -1, 8, -233, 2) |
| 124 | || test_reshape(a, 28, 16, -233, -233) |
| 125 | || test_reshape(a, -1, 14, -233, -233) |
| 126 | || test_reshape(a, 16, -1, -233, -233) |
| 127 | || test_reshape(a, -1, -233, -233, -233); |
| 128 | } |
| 129 | |
| 130 | static int test_reshape_5() |
| 131 | { |
| 132 | ncnn::Mat a = RandomMat(12, 28); |
| 133 | |
| 134 | return 0 |
| 135 | || test_reshape(a, 4, 3, 2, 14) |
| 136 | || test_reshape(a, 1, 3, 14, 8) |
| 137 | || test_reshape(a, 7, 2, -233, 24) |
| 138 | || test_reshape(a, 2, 24, -233, 7) |
| 139 | || test_reshape(a, 12, 14, -233, 2) |
| 140 | || test_reshape(a, 2, 4, -233, -1) |
| 141 | || test_reshape(a, -1, 4, -233, 2) |
| 142 | || test_reshape(a, 21, 16, -233, -233) |
| 143 | || test_reshape(a, -1, 14, -233, -233) |
| 144 | || test_reshape(a, 3, -1, -233, -233) |
| 145 | || test_reshape(a, -1, -233, -233, -233); |
| 146 | } |
| 147 | |
| 148 | static int test_reshape_6() |
| 149 | { |
| 150 | ncnn::Mat a = RandomMat(240); |
| 151 | |
| 152 | return 0 |
| 153 | || test_reshape(a, 1, 1, 1, 240) |
| 154 | || test_reshape(a, 10, 1, 1, 24) |
| 155 | || test_reshape(a, 3, 5, -233, 16) |
| 156 | || test_reshape(a, 3, 8, -233, 10) |
| 157 | || test_reshape(a, 8, 5, -233, 6) |
| 158 | || test_reshape(a, 2, 5, -233, -1) |
| 159 | || test_reshape(a, -1, 5, -233, 4) |
| 160 | || test_reshape(a, 8, 30, -233, -233) |
| 161 | || test_reshape(a, -1, 2, -233, -233) |
| 162 | || test_reshape(a, 24, -1, -233, -233) |
| 163 | || test_reshape(a, -1, -233, -233, -233); |
| 164 | } |
| 165 | |
| 166 | static int test_reshape_7() |
| 167 | { |
| 168 | ncnn::Mat a = RandomMat(210); |
| 169 | |
| 170 | return 0 |
| 171 | || test_reshape(a, 1, 1, 210, 1) |
| 172 | || test_reshape(a, 5, 2, 7, 3) |
| 173 | || test_reshape(a, 3, 5, -233, 14) |
| 174 | || test_reshape(a, 3, 14, -233, 5) |
| 175 | || test_reshape(a, 14, 5, -233, 3) |
| 176 | || test_reshape(a, 2, 5, -233, -1) |
| 177 | || test_reshape(a, -1, 5, -233, 2) |
| 178 | || test_reshape(a, 6, 35, -233, -233) |
| 179 | || test_reshape(a, -1, 7, -233, -233) |
| 180 | || test_reshape(a, 21, -1, -233, -233) |
| 181 | || test_reshape(a, -1, -233, -233, -233); |
| 182 | } |
| 183 | |
| 184 | static int test_reshape_8() |
| 185 | { |
| 186 | ncnn::Mat a = RandomMat(1, 19, 15, 18); |
| 187 | |
| 188 | return test_reshape(a, 19, 15, -233, 18); |
| 189 | } |
| 190 | |
| 191 | int main() |
| 192 | { |
| 193 | SRAND(7767517); |
| 194 | |
| 195 | return 0 |
| 196 | || test_reshape_0() |
| 197 | || test_reshape_1() |
| 198 | || test_reshape_2() |
| 199 | || test_reshape_3() |
| 200 | || test_reshape_4() |
| 201 | || test_reshape_5() |
| 202 | || test_reshape_6() |
| 203 | || test_reshape_7() |
| 204 | || test_reshape_8(); |
| 205 | } |