| 1 | // Tencent is pleased to support the open source community by making ncnn available. |
| 2 | // |
| 3 | // Copyright (C) 2024 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 | #if NCNN_INT8 |
| 18 | static int test_gemm_int8(int M, int N, int K, float alpha, int transA, int transB, int output_elemtype, int output_transpose, int constantA, int constantB, int output_N1M) |
| 19 | { |
| 20 | ncnn::ParamDict pd; |
| 21 | pd.set(0, alpha); |
| 22 | pd.set(1, 1.f); // beta |
| 23 | pd.set(2, transA); |
| 24 | pd.set(3, transB); |
| 25 | pd.set(4, constantA); |
| 26 | pd.set(5, constantB); |
| 27 | pd.set(6, 1); |
| 28 | pd.set(7, M); |
| 29 | pd.set(8, N); |
| 30 | pd.set(9, K); |
| 31 | pd.set(10, -1); |
| 32 | pd.set(11, output_N1M); |
| 33 | pd.set(13, output_elemtype); |
| 34 | pd.set(14, output_transpose); |
| 35 | pd.set(18, 2); // int8_scale_term |
| 36 | |
| 37 | std::vector<ncnn::Mat> weights; |
| 38 | if (constantA) weights.push_back(transA ? (output_N1M ? RandomS8Mat(M, 1, K) : RandomS8Mat(M, K)) : (output_N1M ? RandomS8Mat(K, 1, M) : RandomS8Mat(K, M))); |
| 39 | if (constantB) weights.push_back(transB ? (output_N1M ? RandomS8Mat(K, 1, N) : RandomS8Mat(K, N)) : (output_N1M ? RandomS8Mat(N, 1, K) : RandomS8Mat(N, K))); |
| 40 | if (constantA) weights.push_back(RandomMat(M, 10.f, 20.f)); |
| 41 | if (constantB) weights.push_back(RandomMat(1, 10.f, 20.f)); |
| 42 | |
| 43 | std::vector<ncnn::Mat> a; |
| 44 | if (!constantA) a.push_back(transA ? (output_N1M ? ncnn::Mat(M, 1, K) : ncnn::Mat(M, K)) : (output_N1M ? ncnn::Mat(K, 1, M) : ncnn::Mat(K, M))); |
| 45 | if (!constantB) a.push_back(transB ? (output_N1M ? ncnn::Mat(K, 1, N) : ncnn::Mat(K, N)) : (output_N1M ? ncnn::Mat(N, 1, K) : ncnn::Mat(N, K))); |
| 46 | |
| 47 | for (size_t i = 0; i < a.size(); i++) |
| 48 | { |
| 49 | Randomize(a[i], -10.f, 10.f); |
| 50 | } |
| 51 | |
| 52 | int ret = test_layer("Gemm", pd, weights, a); |
| 53 | if (ret != 0) |
| 54 | { |
| 55 | fprintf(stderr, "test_gemm_int8 failed M=%d N=%d K=%d alpha=%f transA=%d transB=%d output_elemtype=%d output_transpose=%d constantA=%d constantB=%d output_N1M=%d\n", M, N, K, alpha, transA, transB, output_elemtype, output_transpose, constantA, constantB, output_N1M); |
| 56 | } |
| 57 | |
| 58 | return ret; |
| 59 | } |
| 60 | |
| 61 | static int test_gemm_int8_bias(int M, int N, int K, const ncnn::Mat& C, float alpha, float beta, int transA, int transB, int output_elemtype, int output_transpose, int constantA, int constantB, int constantC) |
| 62 | { |
| 63 | int broadcast_type_C = 0; |
| 64 | if (C.dims == 1 && C.w == 1) |
| 65 | { |
| 66 | // scalar |
| 67 | broadcast_type_C = 0; |
| 68 | } |
| 69 | if (C.dims == 1 && C.w == M) |
| 70 | { |
| 71 | // M |
| 72 | // auto broadcast from h to w is the ncnn-style convention |
| 73 | broadcast_type_C = 1; |
| 74 | } |
| 75 | if (C.dims == 1 && C.w == N) |
| 76 | { |
| 77 | // N |
| 78 | broadcast_type_C = 4; |
| 79 | } |
| 80 | if (C.dims == 2 && C.w == 1 && C.h == M) |
| 81 | { |
| 82 | // Mx1 |
| 83 | broadcast_type_C = 2; |
| 84 | } |
| 85 | if (C.dims == 2 && C.w == N && C.h == M) |
| 86 | { |
| 87 | // MxN |
| 88 | broadcast_type_C = 3; |
| 89 | } |
| 90 | if (C.dims == 2 && C.w == N && C.h == 1) |
| 91 | { |
| 92 | // 1xN |
| 93 | broadcast_type_C = 4; |
| 94 | } |
| 95 | |
| 96 | ncnn::ParamDict pd; |
| 97 | pd.set(0, alpha); |
| 98 | pd.set(1, beta); |
| 99 | pd.set(2, transA); |
| 100 | pd.set(3, transB); |
| 101 | pd.set(4, constantA); |
| 102 | pd.set(5, constantB); |
| 103 | pd.set(6, constantC); |
| 104 | pd.set(7, M); |
| 105 | pd.set(8, N); |
| 106 | pd.set(9, K); |
| 107 | pd.set(10, broadcast_type_C); |
| 108 | // pd.set(12, 1); // output_elempack |
| 109 | pd.set(13, output_elemtype); |
| 110 | pd.set(14, output_transpose); |
| 111 | pd.set(18, 2); // int8_scale_term |
| 112 | |
| 113 | std::vector<ncnn::Mat> weights; |
| 114 | if (constantA) weights.push_back(transA ? RandomS8Mat(M, K) : RandomS8Mat(K, M)); |
| 115 | if (constantB) weights.push_back(transB ? RandomS8Mat(K, N) : RandomS8Mat(N, K)); |
| 116 | if (constantC) weights.push_back(C); |
| 117 | if (constantA) weights.push_back(RandomMat(M, 10.f, 20.f)); |
| 118 | if (constantB) weights.push_back(RandomMat(1, 10.f, 20.f)); |
| 119 | |
| 120 | std::vector<ncnn::Mat> a; |
| 121 | if (!constantA) a.push_back(transA ? ncnn::Mat(M, K) : ncnn::Mat(K, M)); |
| 122 | if (!constantB) a.push_back(transB ? ncnn::Mat(K, N) : ncnn::Mat(N, K)); |
| 123 | if (!constantC) a.push_back(C); |
| 124 | |
| 125 | for (size_t i = 0; i < a.size(); i++) |
| 126 | { |
| 127 | Randomize(a[i], -10.f, 10.f); |
| 128 | } |
| 129 | |
| 130 | int ret = test_layer("Gemm", pd, weights, a); |
| 131 | if (ret != 0) |
| 132 | { |
| 133 | fprintf(stderr, "test_gemm_int8_bias failed M=%d N=%d K=%d C.dims=%d C=(%d %d %d) alpha=%f beta=%f transA=%d transB=%d output_elemtype=%d output_transpose=%d constantA=%d constantB=%d constantC=%d\n", M, N, K, C.dims, C.w, C.h, C.c, alpha, beta, transA, transB, output_elemtype, output_transpose, constantA, constantB, constantC); |
| 134 | } |
| 135 | |
| 136 | return ret; |
| 137 | } |
| 138 | |
| 139 | static int test_gemm_int8_fp16s(int M, int N, int K, float alpha, int transA, int transB, int output_elemtype, int output_transpose, int constantA, int constantB, int output_N1M) |
| 140 | { |
| 141 | ncnn::ParamDict pd; |
| 142 | pd.set(0, alpha); |
| 143 | pd.set(1, 1.f); // beta |
| 144 | pd.set(2, transA); |
| 145 | pd.set(3, transB); |
| 146 | pd.set(4, constantA); |
| 147 | pd.set(5, constantB); |
| 148 | pd.set(6, 1); |
| 149 | pd.set(7, M); |
| 150 | pd.set(8, N); |
| 151 | pd.set(9, K); |
| 152 | pd.set(10, -1); |
| 153 | pd.set(11, output_N1M); |
| 154 | pd.set(13, output_elemtype); |
| 155 | pd.set(14, output_transpose); |
| 156 | pd.set(18, 2); // int8_scale_term |
| 157 | |
| 158 | std::vector<ncnn::Mat> weights; |
| 159 | if (constantA) weights.push_back(transA ? (output_N1M ? RandomS8Mat(M, 1, K) : RandomS8Mat(M, K)) : (output_N1M ? RandomS8Mat(K, 1, M) : RandomS8Mat(K, M))); |
| 160 | if (constantB) weights.push_back(transB ? (output_N1M ? RandomS8Mat(K, 1, N) : RandomS8Mat(K, N)) : (output_N1M ? RandomS8Mat(N, 1, K) : RandomS8Mat(N, K))); |
| 161 | if (constantA) weights.push_back(RandomMat(M, 10.f, 20.f)); |
| 162 | if (constantB) weights.push_back(RandomMat(1, 10.f, 20.f)); |
| 163 | |
| 164 | std::vector<ncnn::Mat> a; |
| 165 | if (!constantA) a.push_back(transA ? (output_N1M ? ncnn::Mat(M, 1, K) : ncnn::Mat(M, K)) : (output_N1M ? ncnn::Mat(K, 1, M) : ncnn::Mat(K, M))); |
| 166 | if (!constantB) a.push_back(transB ? (output_N1M ? ncnn::Mat(K, 1, N) : ncnn::Mat(K, N)) : (output_N1M ? ncnn::Mat(N, 1, K) : ncnn::Mat(N, K))); |
| 167 | |
| 168 | for (size_t i = 0; i < a.size(); i++) |
| 169 | { |
| 170 | Randomize(a[i], -10.f, 10.f); |
| 171 | } |
| 172 | |
| 173 | ncnn::Option opt; |
| 174 | opt.num_threads = 1; |
| 175 | opt.use_packing_layout = true; |
| 176 | opt.use_fp16_packed = false; |
| 177 | opt.use_fp16_storage = true; |
| 178 | opt.use_fp16_arithmetic = false; |
| 179 | opt.use_bf16_storage = false; |
| 180 | |
| 181 | float epsilon = 0.001; |
| 182 | |
| 183 | int ret = test_layer_opt("Gemm", pd, weights, opt, a, 1, epsilon); |
| 184 | if (ret != 0) |
| 185 | { |
| 186 | fprintf(stderr, "test_gemm_int8_fp16s failed M=%d N=%d K=%d alpha=%f transA=%d transB=%d output_elemtype=%d output_transpose=%d constantA=%d constantB=%d output_N1M=%d\n", M, N, K, alpha, transA, transB, output_elemtype, output_transpose, constantA, constantB, output_N1M); |
| 187 | return ret; |
| 188 | } |
| 189 | |
| 190 | return 0; |
| 191 | } |
| 192 | |
| 193 | static int test_gemm_0(int M, int N, int K) |
| 194 | { |
| 195 | return 0 |
| 196 | || test_gemm_int8(M, N, K, 2.1f, 0, 1, 0, 0, 0, 0, 0) |
| 197 | || test_gemm_int8(M, N, K, 3.1f, 1, 1, 0, 0, 0, 0, 0) |
| 198 | || test_gemm_int8(M, N, K, 4.1f, 0, 0, 0, 0, 0, 0, 1) |
| 199 | || test_gemm_int8(M, N, K, 5.1f, 1, 0, 0, 0, 0, 0, 1) |
| 200 | |
| 201 | || test_gemm_int8(M, N, K, 0.2f, 0, 1, 0, 0, 1, 0, 1) |
| 202 | || test_gemm_int8(M, N, K, 0.3f, 1, 1, 0, 0, 1, 0, 1) |
| 203 | || test_gemm_int8(M, N, K, 0.4f, 0, 0, 0, 0, 0, 1, 0) |
| 204 | || test_gemm_int8(M, N, K, 0.5f, 0, 1, 0, 0, 0, 1, 0) |
| 205 | |
| 206 | || test_gemm_int8(M, N, K, 1.2f, 0, 1, 0, 0, 1, 1, 0) |
| 207 | || test_gemm_int8(M, N, K, 1.3f, 1, 1, 0, 0, 1, 1, 1) |
| 208 | || test_gemm_int8(M, N, K, 1.4f, 0, 0, 0, 0, 1, 1, 0) |
| 209 | || test_gemm_int8(M, N, K, 1.5f, 1, 0, 0, 0, 1, 1, 1) |
| 210 | |
| 211 | || test_gemm_int8(M, N, K, -1.2f, 0, 1, 0, 1, 0, 0, 0) |
| 212 | || test_gemm_int8(M, N, K, -1.3f, 1, 1, 0, 1, 0, 0, 0) |
| 213 | || test_gemm_int8(M, N, K, -1.4f, 0, 0, 0, 1, 0, 0, 1) |
| 214 | || test_gemm_int8(M, N, K, -1.5f, 1, 0, 0, 1, 0, 0, 1) |
| 215 | |
| 216 | || test_gemm_int8(M, N, K, -2.0f, 0, 1, 0, 1, 1, 0, 1) |
| 217 | || test_gemm_int8(M, N, K, -3.0f, 1, 1, 0, 1, 1, 0, 1) |
| 218 | || test_gemm_int8(M, N, K, -4.0f, 0, 0, 0, 1, 0, 1, 0) |
| 219 | || test_gemm_int8(M, N, K, -5.0f, 0, 1, 0, 1, 0, 1, 0) |
| 220 | |
| 221 | || test_gemm_int8(M, N, K, -2.1f, 0, 1, 0, 1, 1, 1, 0) |
| 222 | || test_gemm_int8(M, N, K, -3.1f, 1, 1, 0, 1, 1, 1, 1) |
| 223 | || test_gemm_int8(M, N, K, -4.1f, 0, 0, 0, 1, 1, 1, 0) |
| 224 | || test_gemm_int8(M, N, K, -5.1f, 1, 0, 0, 1, 1, 1, 1) |
| 225 | |
| 226 | || test_gemm_int8_fp16s(M, N, K, 1.f, 0, 1, 0, 0, 0, 0, 0) |
| 227 | || test_gemm_int8_fp16s(M, N, K, 1.f, 1, 0, 0, 1, 0, 0, 0); |
| 228 | } |
| 229 | |
| 230 | static int test_gemm_1(int M, int N, int K) |
| 231 | { |
| 232 | return 0 |
| 233 | || test_gemm_int8_bias(M, N, K, RandomMat(1), 2.1f, 0.5f, 0, 0, 0, 0, 0, 0, 0) |
| 234 | || test_gemm_int8_bias(M, N, K, RandomMat(1), 2.1f, 0.5f, 0, 0, 1, 1, 0, 0, 0) |
| 235 | || test_gemm_int8_bias(M, N, K, RandomMat(M), 3.1f, 0.6f, 0, 1, 2, 0, 0, 0, 0) |
| 236 | || test_gemm_int8_bias(M, N, K, RandomMat(M), 3.1f, 0.6f, 0, 1, 3, 1, 0, 0, 0) |
| 237 | || test_gemm_int8_bias(M, N, K, RandomMat(1, M), 4.1f, 0.7f, 1, 0, 0, 0, 0, 0, 0) |
| 238 | || test_gemm_int8_bias(M, N, K, RandomMat(1, M), 4.1f, 0.7f, 1, 0, 1, 1, 0, 0, 0) |
| 239 | || test_gemm_int8_bias(M, N, K, RandomMat(N, M), 5.1f, -0.8f, 1, 1, 2, 0, 0, 0, 0) |
| 240 | || test_gemm_int8_bias(M, N, K, RandomMat(N, M), 5.1f, -0.8f, 1, 1, 3, 1, 0, 0, 0) |
| 241 | || test_gemm_int8_bias(M, N, K, RandomMat(N, M), 1.f, 1.f, 1, 1, 0, 0, 0, 0, 0) |
| 242 | || test_gemm_int8_bias(M, N, K, RandomMat(N, M), 1.f, 1.f, 1, 1, 1, 1, 0, 0, 0) |
| 243 | || test_gemm_int8_bias(M, N, K, RandomMat(N, 1), 2.1f, -0.5f, 0, 0, 2, 0, 0, 0, 0) |
| 244 | || test_gemm_int8_bias(M, N, K, RandomMat(N, 1), 2.1f, -0.5f, 0, 0, 3, 1, 0, 0, 0) |
| 245 | || test_gemm_int8_bias(M, N, K, RandomMat(N, 1), 0.8f, 1.f, 0, 0, 0, 0, 0, 0, 0) |
| 246 | || test_gemm_int8_bias(M, N, K, RandomMat(N), 0.8f, 1.f, 0, 0, 1, 1, 0, 0, 0) |
| 247 | || test_gemm_int8_bias(M, N, K, RandomMat(N), 3.1f, -0.6f, 0, 1, 2, 0, 0, 0, 0) |
| 248 | || test_gemm_int8_bias(M, N, K, RandomMat(N), 3.1f, -0.6f, 0, 1, 3, 1, 0, 0, 0) |
| 249 | |
| 250 | || test_gemm_int8_bias(M, N, K, RandomMat(1), -2.1f, 0.5f, 0, 0, 0, 0, 1, 1, 1) |
| 251 | || test_gemm_int8_bias(M, N, K, RandomMat(1), -2.1f, 0.5f, 0, 0, 1, 1, 1, 1, 1) |
| 252 | || test_gemm_int8_bias(M, N, K, RandomMat(M), -3.1f, 0.6f, 0, 1, 2, 0, 1, 1, 1) |
| 253 | || test_gemm_int8_bias(M, N, K, RandomMat(M), -3.1f, 0.6f, 0, 1, 3, 1, 1, 1, 1) |
| 254 | || test_gemm_int8_bias(M, N, K, RandomMat(1, M), -4.1f, 0.7f, 1, 0, 0, 0, 1, 1, 1) |
| 255 | || test_gemm_int8_bias(M, N, K, RandomMat(1, M), -4.1f, 0.7f, 1, 0, 1, 1, 1, 1, 1) |
| 256 | || test_gemm_int8_bias(M, N, K, RandomMat(N, M), -5.1f, -0.8f, 1, 1, 2, 0, 1, 1, 1) |
| 257 | || test_gemm_int8_bias(M, N, K, RandomMat(N, M), -5.1f, -0.8f, 1, 1, 3, 1, 1, 1, 1) |
| 258 | || test_gemm_int8_bias(M, N, K, RandomMat(N, M), 1.f, 1.f, 1, 1, 0, 0, 1, 1, 1) |
| 259 | || test_gemm_int8_bias(M, N, K, RandomMat(N, M), 1.f, 1.f, 1, 1, 1, 1, 1, 1, 1) |
| 260 | || test_gemm_int8_bias(M, N, K, RandomMat(N, 1), -2.1f, -0.5f, 0, 0, 2, 0, 1, 1, 1) |
| 261 | || test_gemm_int8_bias(M, N, K, RandomMat(N, 1), -2.1f, -0.5f, 0, 0, 3, 1, 1, 1, 1) |
| 262 | || test_gemm_int8_bias(M, N, K, RandomMat(N, 1), 0.8f, 1.f, 0, 0, 0, 0, 1, 1, 1) |
| 263 | || test_gemm_int8_bias(M, N, K, RandomMat(N), 0.8f, 1.f, 0, 0, 1, 1, 1, 1, 1) |
| 264 | || test_gemm_int8_bias(M, N, K, RandomMat(N), -3.1f, -0.6f, 0, 1, 2, 0, 1, 1, 1) |
| 265 | || test_gemm_int8_bias(M, N, K, RandomMat(N), -3.1f, -0.6f, 0, 1, 3, 1, 1, 1, 1); |
| 266 | } |
| 267 | #endif // NCNN_INT8 |
| 268 | |
| 269 | int main() |
| 270 | { |
| 271 | SRAND(7767517); |
| 272 | |
| 273 | #if NCNN_INT8 |
| 274 | int mnk[][3] = { |
| 275 | {1, 1, 1}, |
| 276 | {1, 1, 23}, |
| 277 | {1, 1, 47}, |
| 278 | {1, 23, 1}, |
| 279 | {1, 23, 23}, |
| 280 | {1, 31, 1}, |
| 281 | {1, 35, 1}, |
| 282 | {1, 35, 47}, |
| 283 | {1, 47, 1}, |
| 284 | {2, 2, 2}, |
| 285 | {3, 3, 3}, |
| 286 | {4, 4, 4}, |
| 287 | {5, 5, 5}, |
| 288 | {6, 6, 6}, |
| 289 | {7, 7, 7}, |
| 290 | {7, 31, 3}, |
| 291 | {8, 8, 8}, |
| 292 | {12, 12, 23}, |
| 293 | {12, 23, 12}, |
| 294 | {12, 31, 12}, |
| 295 | {15, 15, 15}, |
| 296 | {16, 16, 16}, |
| 297 | {19, 44, 7}, |
| 298 | {20, 28, 7}, |
| 299 | {23, 31, 1}, |
| 300 | {23, 31, 23}, |
| 301 | {24, 24, 47}, |
| 302 | {24, 35, 24}, |
| 303 | {24, 47, 24}, |
| 304 | {31, 31, 31}, |
| 305 | {32, 32, 9}, |
| 306 | {35, 47, 48}, |
| 307 | {35, 48, 47}, |
| 308 | {40, 40, 40}, |
| 309 | {47, 48, 47} |
| 310 | }; |
| 311 | |
| 312 | int mnk_count = sizeof(mnk) / sizeof(int) / 3; |
| 313 | |
| 314 | for (int i = 0; i < mnk_count; i++) |
| 315 | { |
| 316 | int M = mnk[i][0]; |
| 317 | int N = mnk[i][1]; |
| 318 | int K = mnk[i][2]; |
| 319 | |
| 320 | int ret = test_gemm_0(M, N, K) || test_gemm_1(M, N, K); |
| 321 | if (ret != 0) |
| 322 | return ret; |
| 323 | |
| 324 | if (M != N) |
| 325 | { |
| 326 | int ret = test_gemm_0(N, M, K) || test_gemm_1(N, M, K); |
| 327 | if (ret != 0) |
| 328 | return ret; |
| 329 | } |
| 330 | } |
| 331 | #else |
| 332 | // test nothing for non-int8 build |
| 333 | #endif |
| 334 | |
| 335 | return 0; |
| 336 | } |