| 1 | // Tencent is pleased to support the open source community by making ncnn available. |
| 2 | // |
| 3 | // Copyright (C) 2021 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_dequantize(const ncnn::Mat& a, int scale_data_size, int bias_data_size) |
| 18 | { |
| 19 | ncnn::ParamDict pd; |
| 20 | pd.set(0, scale_data_size); |
| 21 | pd.set(1, bias_data_size); |
| 22 | |
| 23 | std::vector<ncnn::Mat> weights(bias_data_size ? 2 : 1); |
| 24 | weights[0] = RandomMat(scale_data_size); |
| 25 | if (bias_data_size) |
| 26 | weights[1] = RandomMat(bias_data_size); |
| 27 | |
| 28 | int flag = TEST_LAYER_DISABLE_AUTO_INPUT_CASTING; |
| 29 | int ret = test_layer("Dequantize", pd, weights, a, 0.001, 0, flag); |
| 30 | if (ret != 0) |
| 31 | { |
| 32 | fprintf(stderr, "test_dequantize failed a.dims=%d a=(%d %d %d) scale_data_size=%d bias_data_size=%d\n", a.dims, a.w, a.h, a.c, scale_data_size, bias_data_size); |
| 33 | } |
| 34 | |
| 35 | return ret; |
| 36 | } |
| 37 | |
| 38 | static int test_dequantize_pack8(const ncnn::Mat& a, int scale_data_size, int bias_data_size) |
| 39 | { |
| 40 | ncnn::ParamDict pd; |
| 41 | pd.set(0, scale_data_size); |
| 42 | pd.set(1, bias_data_size); |
| 43 | |
| 44 | std::vector<ncnn::Mat> weights(bias_data_size ? 2 : 1); |
| 45 | weights[0] = RandomMat(scale_data_size); |
| 46 | if (bias_data_size) |
| 47 | weights[1] = RandomMat(bias_data_size); |
| 48 | |
| 49 | int flag = TEST_LAYER_DISABLE_AUTO_INPUT_CASTING | TEST_LAYER_ENABLE_FORCE_INPUT_PACK8; |
| 50 | int ret = test_layer("Dequantize", pd, weights, a, 0.001, 0, flag); |
| 51 | if (ret != 0) |
| 52 | { |
| 53 | fprintf(stderr, "test_dequantize_pack8 failed a.dims=%d a=(%d %d %d) scale_data_size=%d bias_data_size=%d\n", a.dims, a.w, a.h, a.c, scale_data_size, bias_data_size); |
| 54 | } |
| 55 | |
| 56 | return ret; |
| 57 | } |
| 58 | |
| 59 | static int test_dequantize_0() |
| 60 | { |
| 61 | return 0 |
| 62 | || test_dequantize(RandomIntMat(5, 7, 24), 1, 24) |
| 63 | || test_dequantize(RandomIntMat(5, 7, 24), 1, 1) |
| 64 | || test_dequantize(RandomIntMat(5, 7, 24), 1, 0) |
| 65 | || test_dequantize(RandomIntMat(5, 7, 24), 24, 24) |
| 66 | || test_dequantize(RandomIntMat(5, 7, 24), 24, 1) |
| 67 | || test_dequantize(RandomIntMat(5, 7, 24), 24, 0) |
| 68 | || test_dequantize(RandomIntMat(7, 9, 12), 1, 12) |
| 69 | || test_dequantize(RandomIntMat(7, 9, 12), 1, 1) |
| 70 | || test_dequantize(RandomIntMat(7, 9, 12), 1, 0) |
| 71 | || test_dequantize(RandomIntMat(7, 9, 12), 12, 12) |
| 72 | || test_dequantize(RandomIntMat(7, 9, 12), 12, 1) |
| 73 | || test_dequantize(RandomIntMat(7, 9, 12), 12, 0) |
| 74 | || test_dequantize(RandomIntMat(3, 5, 13), 1, 13) |
| 75 | || test_dequantize(RandomIntMat(3, 5, 13), 1, 1) |
| 76 | || test_dequantize(RandomIntMat(3, 5, 13), 1, 0) |
| 77 | || test_dequantize(RandomIntMat(3, 5, 13), 13, 13) |
| 78 | || test_dequantize(RandomIntMat(3, 5, 13), 13, 1) |
| 79 | || test_dequantize(RandomIntMat(3, 5, 13), 13, 0); |
| 80 | } |
| 81 | |
| 82 | static int test_dequantize_1() |
| 83 | { |
| 84 | return 0 |
| 85 | || test_dequantize(RandomIntMat(15, 24), 1, 24) |
| 86 | || test_dequantize(RandomIntMat(15, 24), 1, 1) |
| 87 | || test_dequantize(RandomIntMat(15, 24), 1, 0) |
| 88 | || test_dequantize(RandomIntMat(15, 24), 24, 24) |
| 89 | || test_dequantize(RandomIntMat(15, 24), 24, 1) |
| 90 | || test_dequantize(RandomIntMat(15, 24), 24, 0) |
| 91 | || test_dequantize(RandomIntMat(17, 12), 1, 12) |
| 92 | || test_dequantize(RandomIntMat(17, 12), 1, 1) |
| 93 | || test_dequantize(RandomIntMat(17, 12), 1, 0) |
| 94 | || test_dequantize(RandomIntMat(17, 12), 12, 12) |
| 95 | || test_dequantize(RandomIntMat(17, 12), 12, 1) |
| 96 | || test_dequantize(RandomIntMat(17, 12), 12, 0) |
| 97 | || test_dequantize(RandomIntMat(19, 15), 1, 15) |
| 98 | || test_dequantize(RandomIntMat(19, 15), 1, 1) |
| 99 | || test_dequantize(RandomIntMat(19, 15), 1, 0) |
| 100 | || test_dequantize(RandomIntMat(19, 15), 15, 15) |
| 101 | || test_dequantize(RandomIntMat(19, 15), 15, 1) |
| 102 | || test_dequantize(RandomIntMat(19, 15), 15, 0); |
| 103 | } |
| 104 | |
| 105 | static int test_dequantize_2() |
| 106 | { |
| 107 | return 0 |
| 108 | || test_dequantize(RandomIntMat(128), 1, 128) |
| 109 | || test_dequantize(RandomIntMat(128), 1, 1) |
| 110 | || test_dequantize(RandomIntMat(128), 1, 0) |
| 111 | || test_dequantize(RandomIntMat(128), 128, 128) |
| 112 | || test_dequantize(RandomIntMat(128), 128, 1) |
| 113 | || test_dequantize(RandomIntMat(128), 128, 0) |
| 114 | || test_dequantize(RandomIntMat(124), 1, 124) |
| 115 | || test_dequantize(RandomIntMat(124), 1, 1) |
| 116 | || test_dequantize(RandomIntMat(124), 1, 0) |
| 117 | || test_dequantize(RandomIntMat(124), 124, 124) |
| 118 | || test_dequantize(RandomIntMat(124), 124, 1) |
| 119 | || test_dequantize(RandomIntMat(124), 124, 0) |
| 120 | || test_dequantize(RandomIntMat(127), 1, 127) |
| 121 | || test_dequantize(RandomIntMat(127), 1, 1) |
| 122 | || test_dequantize(RandomIntMat(127), 1, 0) |
| 123 | || test_dequantize(RandomIntMat(127), 127, 127) |
| 124 | || test_dequantize(RandomIntMat(127), 127, 1) |
| 125 | || test_dequantize(RandomIntMat(127), 127, 0); |
| 126 | } |
| 127 | |
| 128 | static int test_dequantize_3() |
| 129 | { |
| 130 | return 0 |
| 131 | || test_dequantize_pack8(RandomIntMat(5, 7, 24), 1, 24) |
| 132 | || test_dequantize_pack8(RandomIntMat(5, 7, 24), 1, 1) |
| 133 | || test_dequantize_pack8(RandomIntMat(5, 7, 24), 1, 0) |
| 134 | || test_dequantize_pack8(RandomIntMat(5, 7, 24), 24, 24) |
| 135 | || test_dequantize_pack8(RandomIntMat(5, 7, 24), 24, 1) |
| 136 | || test_dequantize_pack8(RandomIntMat(5, 7, 24), 24, 0) |
| 137 | || test_dequantize_pack8(RandomIntMat(15, 24), 1, 24) |
| 138 | || test_dequantize_pack8(RandomIntMat(15, 24), 1, 1) |
| 139 | || test_dequantize_pack8(RandomIntMat(15, 24), 1, 0) |
| 140 | || test_dequantize_pack8(RandomIntMat(15, 24), 24, 24) |
| 141 | || test_dequantize_pack8(RandomIntMat(15, 24), 24, 1) |
| 142 | || test_dequantize_pack8(RandomIntMat(15, 24), 24, 0) |
| 143 | || test_dequantize_pack8(RandomIntMat(128), 1, 128) |
| 144 | || test_dequantize_pack8(RandomIntMat(128), 1, 1) |
| 145 | || test_dequantize_pack8(RandomIntMat(128), 1, 0) |
| 146 | || test_dequantize_pack8(RandomIntMat(128), 128, 128) |
| 147 | || test_dequantize_pack8(RandomIntMat(128), 128, 1) |
| 148 | || test_dequantize_pack8(RandomIntMat(128), 128, 0); |
| 149 | } |
| 150 | |
| 151 | int main() |
| 152 | { |
| 153 | SRAND(7767517); |
| 154 | |
| 155 | return 0 |
| 156 | || test_dequantize_0() |
| 157 | || test_dequantize_1() |
| 158 | || test_dequantize_2() |
| 159 | || test_dequantize_3(); |
| 160 | } |