| 1 | // Tencent is pleased to support the open source community by making ncnn available. |
| 2 | // |
| 3 | // Copyright (C) 2022 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_deconvolutiondepthwise1d(int w, int h, int outh, int kernel, int dilation, int stride, int pad, int bias, int group, int output_pad_right, int output_w) |
| 18 | { |
| 19 | ncnn::Mat a = RandomMat(w, h); |
| 20 | |
| 21 | if (output_w > 0 && pad != -233 && pad != -234) |
| 22 | { |
| 23 | pad = -233; |
| 24 | } |
| 25 | |
| 26 | ncnn::ParamDict pd; |
| 27 | pd.set(0, outh); |
| 28 | pd.set(1, kernel); |
| 29 | pd.set(2, dilation); |
| 30 | pd.set(3, stride); |
| 31 | pd.set(4, pad); |
| 32 | pd.set(5, bias); |
| 33 | pd.set(6, outh / group * h / group * kernel * group); |
| 34 | pd.set(7, group); |
| 35 | |
| 36 | int activation_type = RAND() % 5; // 0 1 2 3 4 |
| 37 | ncnn::Mat activation_params(2); |
| 38 | activation_params[0] = RandomFloat(-1, 0); // alpha |
| 39 | activation_params[1] = RandomFloat(0, 1); // beta |
| 40 | pd.set(9, activation_type); |
| 41 | pd.set(10, activation_params); |
| 42 | |
| 43 | pd.set(18, output_pad_right); |
| 44 | pd.set(20, output_w); |
| 45 | |
| 46 | std::vector<ncnn::Mat> weights(2); |
| 47 | weights[0] = RandomMat(outh / group * h / group * kernel * group); |
| 48 | weights[1] = RandomMat(outh); |
| 49 | |
| 50 | int ret = test_layer("DeconvolutionDepthWise1D", pd, weights, a); |
| 51 | if (ret != 0) |
| 52 | { |
| 53 | fprintf(stderr, "test_deconvolutiondepthwise1d failed w=%d h=%d outh=%d kernel=%d dilation=%d stride=%d pad=%d bias=%d group=%d act=%d actparams=[%f,%f] output_pad_right=%d output_w=%d\n", w, h, outh, kernel, dilation, stride, pad, bias, group, activation_type, activation_params[0], activation_params[1], output_pad_right, output_w); |
| 54 | } |
| 55 | |
| 56 | return ret; |
| 57 | } |
| 58 | |
| 59 | static int test_deconvolutiondepthwise1d_0() |
| 60 | { |
| 61 | static const int kdsp[16][4] = { |
| 62 | {1, 1, 1, 0}, |
| 63 | {1, 1, 2, 0}, |
| 64 | {2, 1, 1, 1}, |
| 65 | {2, 1, 2, -233}, |
| 66 | {3, 1, 1, 1}, |
| 67 | {3, 1, 2, 1}, |
| 68 | {3, 2, 1, 1}, |
| 69 | {4, 1, 1, -233}, |
| 70 | {4, 1, 2, -234}, |
| 71 | {4, 2, 1, -234}, |
| 72 | {5, 1, 1, 2}, |
| 73 | {5, 1, 2, 2}, |
| 74 | {5, 2, 2, 2}, |
| 75 | {7, 1, 1, 3}, |
| 76 | {7, 1, 2, 3}, |
| 77 | {7, 2, 1, -233}, |
| 78 | }; |
| 79 | |
| 80 | for (int i = 0; i < 16; i++) |
| 81 | { |
| 82 | const int k = kdsp[i][0]; |
| 83 | const int d = kdsp[i][1]; |
| 84 | const int s = kdsp[i][2]; |
| 85 | const int p = kdsp[i][3]; |
| 86 | |
| 87 | int ret = 0 |
| 88 | || test_deconvolutiondepthwise1d(15, 1, 1, k, d, s, p, 1, 1, 0, 0) |
| 89 | || test_deconvolutiondepthwise1d(15, 2, 2, k, d, s, p, 0, 1, 1, 7) |
| 90 | || test_deconvolutiondepthwise1d(15, 2, 2, k, d, s, p, 1, 2, 1, 0) |
| 91 | || test_deconvolutiondepthwise1d(15, 3, 3, k, d, s, p, 0, 3, 0, 0) |
| 92 | || test_deconvolutiondepthwise1d(15, 4, 2, k, d, s, p, 1, 2, 0, 7) |
| 93 | || test_deconvolutiondepthwise1d(15, 4, 4, k, d, s, p, 0, 4, 2, 0) |
| 94 | || test_deconvolutiondepthwise1d(15, 7, 7, k, d, s, p, 1, 7, 2, 0) |
| 95 | || test_deconvolutiondepthwise1d(15, 8, 8, k, d, s, p, 0, 2, 0, 7) |
| 96 | || test_deconvolutiondepthwise1d(15, 8, 8, k, d, s, p, 1, 8, 0, 0) |
| 97 | || test_deconvolutiondepthwise1d(15, 12, 12, k, d, s, p, 0, 4, 3, 0) |
| 98 | || test_deconvolutiondepthwise1d(15, 15, 15, k, d, s, p, 1, 15, 3, 7) |
| 99 | || test_deconvolutiondepthwise1d(15, 16, 8, k, d, s, p, 0, 2, 0, 0) |
| 100 | || test_deconvolutiondepthwise1d(15, 16, 16, k, d, s, p, 1, 16, 0, 0); |
| 101 | |
| 102 | if (ret != 0) |
| 103 | return -1; |
| 104 | } |
| 105 | |
| 106 | return 0; |
| 107 | } |
| 108 | |
| 109 | static int test_deconvolutiondepthwise1d_dynamic(int w, int h, int outh, int kernel, int dilation, int stride, int pad, int bias, int group, int output_pad_right, int output_w) |
| 110 | { |
| 111 | ncnn::Mat a = RandomMat(w, h); |
| 112 | |
| 113 | if (output_w > 0 && pad != -233 && pad != -234) |
| 114 | { |
| 115 | pad = -233; |
| 116 | } |
| 117 | |
| 118 | ncnn::ParamDict pd; |
| 119 | pd.set(0, 0); |
| 120 | pd.set(1, 0); |
| 121 | pd.set(2, dilation); |
| 122 | pd.set(3, stride); |
| 123 | pd.set(4, pad); |
| 124 | pd.set(5, bias); |
| 125 | pd.set(6, 0); |
| 126 | pd.set(7, group); |
| 127 | pd.set(28, 1); // dynamic weight |
| 128 | |
| 129 | int activation_type = RAND() % 5; // 0 1 2 3 4 |
| 130 | ncnn::Mat activation_params(2); |
| 131 | activation_params[0] = RandomFloat(-1, 0); // alpha |
| 132 | activation_params[1] = RandomFloat(0, 1); // beta |
| 133 | pd.set(9, activation_type); |
| 134 | pd.set(10, activation_params); |
| 135 | |
| 136 | pd.set(18, output_pad_right); |
| 137 | pd.set(20, output_w); |
| 138 | |
| 139 | std::vector<ncnn::Mat> as(bias ? 3 : 2); |
| 140 | as[0] = a; |
| 141 | as[1] = RandomMat(kernel, outh / group, h); |
| 142 | if (bias) |
| 143 | as[2] = RandomMat(outh); |
| 144 | |
| 145 | std::vector<ncnn::Mat> weights(0); |
| 146 | |
| 147 | int ret = test_layer("DeconvolutionDepthWise1D", pd, weights, as); |
| 148 | if (ret != 0) |
| 149 | { |
| 150 | fprintf(stderr, "test_deconvolutiondepthwise1d_dynamic failed w=%d h=%d outh=%d kernel=%d dilation=%d stride=%d pad=%d bias=%d group=%d act=%d actparams=[%f,%f] output_pad_right=%d output_w=%d\n", w, h, outh, kernel, dilation, stride, pad, bias, group, activation_type, activation_params[0], activation_params[1], output_pad_right, output_w); |
| 151 | } |
| 152 | |
| 153 | return ret; |
| 154 | } |
| 155 | |
| 156 | static int test_deconvolutiondepthwise1d_1() |
| 157 | { |
| 158 | static const int kdsp[16][4] = { |
| 159 | {1, 1, 1, 0}, |
| 160 | {1, 1, 2, 0}, |
| 161 | {2, 1, 1, 1}, |
| 162 | {2, 1, 2, -233}, |
| 163 | {3, 1, 1, 1}, |
| 164 | {3, 1, 2, 1}, |
| 165 | {3, 2, 1, 1}, |
| 166 | {4, 1, 1, -233}, |
| 167 | {4, 1, 2, -234}, |
| 168 | {4, 2, 1, -234}, |
| 169 | {5, 1, 1, 2}, |
| 170 | {5, 1, 2, 2}, |
| 171 | {5, 2, 2, 2}, |
| 172 | {7, 1, 1, 3}, |
| 173 | {7, 1, 2, 3}, |
| 174 | {7, 2, 1, -233}, |
| 175 | }; |
| 176 | |
| 177 | for (int i = 0; i < 16; i++) |
| 178 | { |
| 179 | const int k = kdsp[i][0]; |
| 180 | const int d = kdsp[i][1]; |
| 181 | const int s = kdsp[i][2]; |
| 182 | const int p = kdsp[i][3]; |
| 183 | |
| 184 | int ret = 0 |
| 185 | || test_deconvolutiondepthwise1d_dynamic(15, 1, 1, k, d, s, p, 1, 1, 0, 0) |
| 186 | || test_deconvolutiondepthwise1d_dynamic(15, 2, 2, k, d, s, p, 0, 1, 1, 7) |
| 187 | || test_deconvolutiondepthwise1d_dynamic(15, 2, 2, k, d, s, p, 1, 2, 1, 0) |
| 188 | || test_deconvolutiondepthwise1d_dynamic(15, 3, 3, k, d, s, p, 0, 3, 0, 0) |
| 189 | || test_deconvolutiondepthwise1d_dynamic(15, 4, 2, k, d, s, p, 1, 2, 0, 7) |
| 190 | || test_deconvolutiondepthwise1d_dynamic(15, 4, 4, k, d, s, p, 0, 4, 2, 0) |
| 191 | || test_deconvolutiondepthwise1d_dynamic(15, 7, 7, k, d, s, p, 1, 7, 2, 0) |
| 192 | || test_deconvolutiondepthwise1d_dynamic(15, 8, 8, k, d, s, p, 0, 2, 0, 7) |
| 193 | || test_deconvolutiondepthwise1d_dynamic(15, 8, 8, k, d, s, p, 1, 8, 0, 0) |
| 194 | || test_deconvolutiondepthwise1d_dynamic(15, 12, 12, k, d, s, p, 0, 4, 3, 0) |
| 195 | || test_deconvolutiondepthwise1d_dynamic(15, 15, 15, k, d, s, p, 1, 15, 3, 7) |
| 196 | || test_deconvolutiondepthwise1d_dynamic(15, 16, 8, k, d, s, p, 0, 2, 0, 0) |
| 197 | || test_deconvolutiondepthwise1d_dynamic(15, 16, 16, k, d, s, p, 1, 16, 0, 0); |
| 198 | |
| 199 | if (ret != 0) |
| 200 | return -1; |
| 201 | } |
| 202 | |
| 203 | return 0; |
| 204 | } |
| 205 | |
| 206 | int main() |
| 207 | { |
| 208 | SRAND(7767517); |
| 209 | |
| 210 | return test_deconvolutiondepthwise1d_0() || test_deconvolutiondepthwise1d_1(); |
| 211 | } |