| 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_scale(const ncnn::Mat& a, int bias) |
| 18 | { |
| 19 | int scale_data_size; |
| 20 | if (a.dims == 1) scale_data_size = a.w; |
| 21 | if (a.dims == 2) scale_data_size = a.h; |
| 22 | if (a.dims == 3) scale_data_size = a.c; |
| 23 | |
| 24 | ncnn::ParamDict pd; |
| 25 | pd.set(0, scale_data_size); |
| 26 | pd.set(1, bias); |
| 27 | |
| 28 | std::vector<ncnn::Mat> weights(bias ? 2 : 1); |
| 29 | weights[0] = RandomMat(scale_data_size); |
| 30 | if (bias) |
| 31 | weights[1] = RandomMat(scale_data_size); |
| 32 | |
| 33 | int ret = test_layer("Scale", pd, weights, a); |
| 34 | if (ret != 0) |
| 35 | { |
| 36 | fprintf(stderr, "test_scale failed a.dims=%d a=(%d %d %d) bias=%d\n", a.dims, a.w, a.h, a.c, bias); |
| 37 | } |
| 38 | |
| 39 | return ret; |
| 40 | } |
| 41 | |
| 42 | static int test_scale_attention(const ncnn::Mat& a) |
| 43 | { |
| 44 | int scale_data_size; |
| 45 | if (a.dims == 1) scale_data_size = a.w; |
| 46 | if (a.dims == 2) scale_data_size = a.h; |
| 47 | if (a.dims == 3) scale_data_size = a.c; |
| 48 | |
| 49 | ncnn::ParamDict pd; |
| 50 | pd.set(0, -233); |
| 51 | |
| 52 | std::vector<ncnn::Mat> weights(0); |
| 53 | |
| 54 | std::vector<ncnn::Mat> ab(2); |
| 55 | ab[0] = a; |
| 56 | ab[1] = RandomMat(scale_data_size); |
| 57 | |
| 58 | int ret = test_layer("Scale", pd, weights, ab, 2); |
| 59 | if (ret != 0) |
| 60 | { |
| 61 | fprintf(stderr, "test_scale_attention failed a.dims=%d a=(%d %d %d)\n", a.dims, a.w, a.h, a.c); |
| 62 | } |
| 63 | |
| 64 | return ret; |
| 65 | } |
| 66 | |
| 67 | static int test_scale_0() |
| 68 | { |
| 69 | return 0 |
| 70 | || test_scale(RandomMat(5, 3, 48), 0) |
| 71 | || test_scale(RandomMat(5, 3, 48), 1) |
| 72 | || test_scale(RandomMat(5, 7, 24), 0) |
| 73 | || test_scale(RandomMat(5, 7, 24), 1) |
| 74 | || test_scale(RandomMat(7, 9, 12), 0) |
| 75 | || test_scale(RandomMat(7, 9, 12), 1) |
| 76 | || test_scale(RandomMat(3, 5, 13), 0) |
| 77 | || test_scale(RandomMat(3, 5, 13), 1); |
| 78 | } |
| 79 | |
| 80 | static int test_scale_1() |
| 81 | { |
| 82 | return 0 |
| 83 | || test_scale(RandomMat(13, 48), 0) |
| 84 | || test_scale(RandomMat(13, 48), 1) |
| 85 | || test_scale(RandomMat(15, 24), 0) |
| 86 | || test_scale(RandomMat(15, 24), 1) |
| 87 | || test_scale(RandomMat(17, 12), 0) |
| 88 | || test_scale(RandomMat(17, 12), 1) |
| 89 | || test_scale(RandomMat(19, 15), 0) |
| 90 | || test_scale(RandomMat(19, 15), 1); |
| 91 | } |
| 92 | |
| 93 | static int test_scale_2() |
| 94 | { |
| 95 | return 0 |
| 96 | || test_scale(RandomMat(128), 0) |
| 97 | || test_scale(RandomMat(128), 1) |
| 98 | || test_scale(RandomMat(124), 0) |
| 99 | || test_scale(RandomMat(124), 1) |
| 100 | || test_scale(RandomMat(127), 0) |
| 101 | || test_scale(RandomMat(127), 1); |
| 102 | } |
| 103 | |
| 104 | static int test_scale_3() |
| 105 | { |
| 106 | return 0 |
| 107 | || test_scale_attention(RandomMat(5, 6, 48)) |
| 108 | || test_scale_attention(RandomMat(5, 7, 24)) |
| 109 | || test_scale_attention(RandomMat(7, 9, 12)) |
| 110 | || test_scale_attention(RandomMat(3, 5, 13)); |
| 111 | } |
| 112 | |
| 113 | static int test_scale_4() |
| 114 | { |
| 115 | return 0 |
| 116 | || test_scale_attention(RandomMat(25, 48)) |
| 117 | || test_scale_attention(RandomMat(15, 24)) |
| 118 | || test_scale_attention(RandomMat(17, 12)) |
| 119 | || test_scale_attention(RandomMat(19, 15)); |
| 120 | } |
| 121 | |
| 122 | static int test_scale_5() |
| 123 | { |
| 124 | return 0 |
| 125 | || test_scale_attention(RandomMat(128)) |
| 126 | || test_scale_attention(RandomMat(124)) |
| 127 | || test_scale_attention(RandomMat(127)); |
| 128 | } |
| 129 | |
| 130 | int main() |
| 131 | { |
| 132 | SRAND(7767517); |
| 133 | |
| 134 | return 0 |
| 135 | || test_scale_0() |
| 136 | || test_scale_1() |
| 137 | || test_scale_2() |
| 138 | || test_scale_3() |
| 139 | || test_scale_4() |
| 140 | || test_scale_5(); |
| 141 | } |