| 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_innerproduct(const ncnn::Mat& a, int outch, int bias) |
| 18 | { |
| 19 | ncnn::ParamDict pd; |
| 20 | pd.set(0, outch); // num_output |
| 21 | pd.set(1, bias); // bias_term |
| 22 | pd.set(2, outch * a.w * a.h * a.c); |
| 23 | |
| 24 | int activation_type = RAND() % 7; // 0 1 2 3 4 5 6 |
| 25 | ncnn::Mat activation_params(2); |
| 26 | activation_params[0] = (activation_type == 6) ? RandomFloat(0, 1) : RandomFloat(-1, 0); // alpha |
| 27 | activation_params[1] = RandomFloat(0, 1); // beta |
| 28 | pd.set(9, activation_type); |
| 29 | pd.set(10, activation_params); |
| 30 | |
| 31 | std::vector<ncnn::Mat> weights(bias ? 2 : 1); |
| 32 | weights[0] = RandomMat(outch * a.w * a.h * a.c); |
| 33 | if (bias) |
| 34 | weights[1] = RandomMat(outch); |
| 35 | |
| 36 | int ret = test_layer("InnerProduct", pd, weights, a); |
| 37 | if (ret != 0) |
| 38 | { |
| 39 | fprintf(stderr, "test_innerproduct failed a.dims=%d a=(%d %d %d) outch=%d bias=%d act=%d actparams=[%f,%f]\n", a.dims, a.w, a.h, a.c, outch, bias, activation_type, activation_params[0], activation_params[1]); |
| 40 | } |
| 41 | |
| 42 | return ret; |
| 43 | } |
| 44 | |
| 45 | static int test_innerproduct_0() |
| 46 | { |
| 47 | return 0 |
| 48 | || test_innerproduct(RandomMat(1, 3, 1), 1, 1) |
| 49 | || test_innerproduct(RandomMat(3, 2, 2), 2, 0) |
| 50 | || test_innerproduct(RandomMat(9, 3, 8), 7, 1) |
| 51 | || test_innerproduct(RandomMat(2, 2, 8), 8, 0) |
| 52 | || test_innerproduct(RandomMat(4, 3, 15), 8, 1) |
| 53 | || test_innerproduct(RandomMat(6, 2, 16), 16, 0) |
| 54 | || test_innerproduct(RandomMat(6, 2, 16), 7, 1) |
| 55 | || test_innerproduct(RandomMat(6, 2, 5), 16, 1); |
| 56 | } |
| 57 | |
| 58 | static int test_innerproduct_1() |
| 59 | { |
| 60 | return 0 |
| 61 | || test_innerproduct(RandomMat(1, 1), 1, 1) |
| 62 | || test_innerproduct(RandomMat(3, 2), 2, 0) |
| 63 | || test_innerproduct(RandomMat(9, 8), 7, 1) |
| 64 | || test_innerproduct(RandomMat(2, 8), 8, 0) |
| 65 | || test_innerproduct(RandomMat(4, 15), 8, 1) |
| 66 | || test_innerproduct(RandomMat(6, 16), 16, 0) |
| 67 | || test_innerproduct(RandomMat(6, 16), 7, 1) |
| 68 | || test_innerproduct(RandomMat(6, 5), 16, 1); |
| 69 | } |
| 70 | |
| 71 | static int test_innerproduct_2() |
| 72 | { |
| 73 | return 0 |
| 74 | || test_innerproduct(RandomMat(1), 1, 1) |
| 75 | || test_innerproduct(RandomMat(2), 2, 0) |
| 76 | || test_innerproduct(RandomMat(8), 7, 1) |
| 77 | || test_innerproduct(RandomMat(8), 8, 0) |
| 78 | || test_innerproduct(RandomMat(15), 8, 1) |
| 79 | || test_innerproduct(RandomMat(15), 15, 1) |
| 80 | || test_innerproduct(RandomMat(16), 16, 0) |
| 81 | || test_innerproduct(RandomMat(16), 7, 1) |
| 82 | || test_innerproduct(RandomMat(5), 16, 0) |
| 83 | || test_innerproduct(RandomMat(32), 16, 1) |
| 84 | || test_innerproduct(RandomMat(12), 16, 0) |
| 85 | || test_innerproduct(RandomMat(16), 12, 1) |
| 86 | || test_innerproduct(RandomMat(24), 32, 1); |
| 87 | } |
| 88 | |
| 89 | #if NCNN_INT8 |
| 90 | static int test_innerproduct_int8(const ncnn::Mat& a, int outch, int bias) |
| 91 | { |
| 92 | ncnn::ParamDict pd; |
| 93 | pd.set(0, outch); // num_output |
| 94 | pd.set(1, bias); // bias_term |
| 95 | pd.set(2, outch * a.w * a.h * a.c); |
| 96 | pd.set(8, 1); // int8_scale_term |
| 97 | |
| 98 | int activation_type = RAND() % 7; // 0 1 2 3 4 5 6 |
| 99 | ncnn::Mat activation_params(2); |
| 100 | activation_params[0] = (activation_type == 6) ? RandomFloat(0, 1) : RandomFloat(-1, 0); // alpha |
| 101 | activation_params[1] = RandomFloat(0, 1); // beta |
| 102 | pd.set(9, activation_type); |
| 103 | pd.set(10, activation_params); |
| 104 | |
| 105 | std::vector<ncnn::Mat> weights(bias ? 4 : 3); |
| 106 | const int k = a.w * a.h * a.c; |
| 107 | weights[0] = RandomMat(outch * k); |
| 108 | ncnn::Mat weight_scales = scales_mat(weights[0], outch, k, k); |
| 109 | ncnn::Mat input_scales = scales_mat(a, 1, k, k); |
| 110 | |
| 111 | if (bias) |
| 112 | { |
| 113 | weights[1] = RandomMat(outch); |
| 114 | weights[2] = weight_scales; |
| 115 | weights[3] = input_scales; |
| 116 | } |
| 117 | else |
| 118 | { |
| 119 | weights[1] = weight_scales; |
| 120 | weights[2] = input_scales; |
| 121 | } |
| 122 | |
| 123 | int flag = TEST_LAYER_DISABLE_GPU_TESTING; |
| 124 | int ret = test_layer("InnerProduct", pd, weights, a, 0.001f, 0, flag); |
| 125 | if (ret != 0) |
| 126 | { |
| 127 | fprintf(stderr, "test_innerproduct_int8 failed a.dims=%d a=(%d %d %d) outch=%d bias=%d act=%d actparams=[%f,%f]\n", a.dims, a.w, a.h, a.c, outch, bias, activation_type, activation_params[0], activation_params[1]); |
| 128 | } |
| 129 | |
| 130 | return ret; |
| 131 | } |
| 132 | |
| 133 | static int test_innerproduct_3() |
| 134 | { |
| 135 | return 0 |
| 136 | || test_innerproduct_int8(RandomMat(1, 3, 1), 1, 1) |
| 137 | || test_innerproduct_int8(RandomMat(3, 2, 2), 2, 1) |
| 138 | || test_innerproduct_int8(RandomMat(5, 3, 3), 3, 1) |
| 139 | || test_innerproduct_int8(RandomMat(7, 2, 3), 12, 1) |
| 140 | || test_innerproduct_int8(RandomMat(9, 3, 4), 4, 1) |
| 141 | || test_innerproduct_int8(RandomMat(2, 2, 7), 7, 1) |
| 142 | || test_innerproduct_int8(RandomMat(4, 3, 8), 3, 1) |
| 143 | || test_innerproduct_int8(RandomMat(6, 2, 8), 8, 1) |
| 144 | || test_innerproduct_int8(RandomMat(8, 3, 15), 15, 1) |
| 145 | || test_innerproduct_int8(RandomMat(7, 2, 16), 4, 1) |
| 146 | || test_innerproduct_int8(RandomMat(6, 3, 16), 16, 1); |
| 147 | } |
| 148 | #endif // NCNN_INT8 |
| 149 | |
| 150 | static int test_innerproduct_gemm(const ncnn::Mat& a, int outch, int bias) |
| 151 | { |
| 152 | ncnn::ParamDict pd; |
| 153 | pd.set(0, outch); |
| 154 | pd.set(1, bias); |
| 155 | pd.set(2, outch * a.w); |
| 156 | |
| 157 | int activation_type = RAND() % 7; |
| 158 | ncnn::Mat activation_params(2); |
| 159 | activation_params[0] = (activation_type == 6) ? RandomFloat(0, 1) : RandomFloat(-1, 0); // alpha |
| 160 | activation_params[1] = RandomFloat(0, 1); |
| 161 | pd.set(9, activation_type); |
| 162 | pd.set(10, activation_params); |
| 163 | |
| 164 | std::vector<ncnn::Mat> weights(bias ? 2 : 1); |
| 165 | weights[0] = RandomMat(outch * a.w); |
| 166 | if (bias) |
| 167 | weights[1] = RandomMat(outch); |
| 168 | |
| 169 | int ret = test_layer("InnerProduct", pd, weights, a); |
| 170 | if (ret != 0) |
| 171 | { |
| 172 | fprintf(stderr, "test_innerproduct_gemm failed a.dims=%d a=(%d %d %d) outch=%d bias=%d act=%d actparams=[%f,%f]\n", a.dims, a.w, a.h, a.c, outch, bias, activation_type, activation_params[0], activation_params[1]); |
| 173 | } |
| 174 | |
| 175 | return ret; |
| 176 | } |
| 177 | |
| 178 | static int test_innerproduct_4() |
| 179 | { |
| 180 | return 0 |
| 181 | || test_innerproduct_gemm(RandomMat(1, 1), 1, 1) |
| 182 | || test_innerproduct_gemm(RandomMat(48, 1), 11, 1) |
| 183 | || test_innerproduct_gemm(RandomMat(1, 5), 1, 1) |
| 184 | || test_innerproduct_gemm(RandomMat(3, 2), 2, 0) |
| 185 | || test_innerproduct_gemm(RandomMat(9, 8), 7, 1) |
| 186 | || test_innerproduct_gemm(RandomMat(2, 8), 8, 0) |
| 187 | || test_innerproduct_gemm(RandomMat(13, 20), 8, 1) |
| 188 | || test_innerproduct_gemm(RandomMat(16, 20), 16, 0) |
| 189 | || test_innerproduct_gemm(RandomMat(11, 24), 8, 0) |
| 190 | || test_innerproduct_gemm(RandomMat(13, 24), 12, 1) |
| 191 | || test_innerproduct_gemm(RandomMat(15, 20), 20, 1) |
| 192 | || test_innerproduct_gemm(RandomMat(16, 20), 11, 1) |
| 193 | || test_innerproduct_gemm(RandomMat(19, 16), 16, 1) |
| 194 | || test_innerproduct_gemm(RandomMat(15, 15), 15, 1) |
| 195 | || test_innerproduct_gemm(RandomMat(14, 15), 8, 1) |
| 196 | || test_innerproduct_gemm(RandomMat(17, 15), 12, 1) |
| 197 | || test_innerproduct_gemm(RandomMat(12, 16), 7, 1) |
| 198 | || test_innerproduct_gemm(RandomMat(11, 32), 32, 1) |
| 199 | || test_innerproduct_gemm(RandomMat(12, 32), 24, 1) |
| 200 | || test_innerproduct_gemm(RandomMat(13, 32), 12, 1) |
| 201 | || test_innerproduct_gemm(RandomMat(14, 32), 14, 1) |
| 202 | || test_innerproduct_gemm(RandomMat(15, 32), 32, 1) |
| 203 | || test_innerproduct_gemm(RandomMat(16, 24), 32, 1) |
| 204 | || test_innerproduct_gemm(RandomMat(17, 20), 32, 1) |
| 205 | || test_innerproduct_gemm(RandomMat(18, 14), 32, 1); |
| 206 | } |
| 207 | |
| 208 | #if NCNN_INT8 |
| 209 | static int test_innerproduct_gemm_int8(const ncnn::Mat& a, int outch, int bias) |
| 210 | { |
| 211 | ncnn::ParamDict pd; |
| 212 | pd.set(0, outch); |
| 213 | pd.set(1, bias); |
| 214 | pd.set(2, outch * a.w); |
| 215 | pd.set(8, 1); // int8_scale_term |
| 216 | |
| 217 | std::vector<ncnn::Mat> weights(bias ? 4 : 3); |
| 218 | const int k = a.w; |
| 219 | weights[0] = RandomMat(outch * k); |
| 220 | ncnn::Mat weight_scales = scales_mat(weights[0], outch, k, k); |
| 221 | ncnn::Mat input_scales = scales_mat(a, 1, k, k); |
| 222 | |
| 223 | if (bias) |
| 224 | { |
| 225 | weights[1] = RandomMat(outch); |
| 226 | weights[2] = weight_scales; |
| 227 | weights[3] = input_scales; |
| 228 | } |
| 229 | else |
| 230 | { |
| 231 | weights[1] = weight_scales; |
| 232 | weights[2] = input_scales; |
| 233 | } |
| 234 | |
| 235 | int flag = TEST_LAYER_DISABLE_GPU_TESTING; |
| 236 | int ret = test_layer("InnerProduct", pd, weights, a, 0.001f, 0, flag); |
| 237 | if (ret != 0) |
| 238 | { |
| 239 | fprintf(stderr, "test_innerproduct_gemm_int8 failed a.dims=%d a=(%d %d %d) outch=%d bias=%d\n", a.dims, a.w, a.h, a.c, outch, bias); |
| 240 | } |
| 241 | |
| 242 | return ret; |
| 243 | } |
| 244 | |
| 245 | static int test_innerproduct_5() |
| 246 | { |
| 247 | return 0 |
| 248 | || test_innerproduct_gemm_int8(RandomMat(1, 5), 1, 1) |
| 249 | || test_innerproduct_gemm_int8(RandomMat(3, 2), 2, 0) |
| 250 | || test_innerproduct_gemm_int8(RandomMat(9, 8), 7, 1) |
| 251 | || test_innerproduct_gemm_int8(RandomMat(2, 8), 8, 0) |
| 252 | || test_innerproduct_gemm_int8(RandomMat(13, 12), 8, 1) |
| 253 | || test_innerproduct_gemm_int8(RandomMat(16, 12), 16, 0) |
| 254 | || test_innerproduct_gemm_int8(RandomMat(4, 15), 8, 1) |
| 255 | || test_innerproduct_gemm_int8(RandomMat(6, 16), 16, 0) |
| 256 | || test_innerproduct_gemm_int8(RandomMat(12, 16), 7, 1); |
| 257 | } |
| 258 | #endif // NCNN_INT8 |
| 259 | |
| 260 | int main() |
| 261 | { |
| 262 | SRAND(7767517); |
| 263 | |
| 264 | #if NCNN_INT8 |
| 265 | return 0 |
| 266 | || test_innerproduct_0() |
| 267 | || test_innerproduct_1() |
| 268 | || test_innerproduct_2() |
| 269 | || test_innerproduct_3() |
| 270 | || test_innerproduct_4() |
| 271 | || test_innerproduct_5(); |
| 272 | #else |
| 273 | return 0 |
| 274 | || test_innerproduct_0() |
| 275 | || test_innerproduct_1() |
| 276 | || test_innerproduct_2() |
| 277 | || test_innerproduct_4(); |
| 278 | #endif |
| 279 | } |