| 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, int TILE_M, int TILE_N, int TILE_K, float alpha, int transA, int transB, int output_transpose) |
| 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(14, output_transpose); |
| 26 | pd.set(18, 2); // int8_scale_term |
| 27 | |
| 28 | pd.set(20, TILE_M); |
| 29 | pd.set(21, TILE_N); |
| 30 | pd.set(22, TILE_K); |
| 31 | |
| 32 | std::vector<ncnn::Mat> weights(0); |
| 33 | |
| 34 | std::vector<ncnn::Mat> a(2); |
| 35 | a[0] = transA ? ncnn::Mat(M, K) : ncnn::Mat(K, M); |
| 36 | a[1] = transB ? ncnn::Mat(K, N) : ncnn::Mat(N, K); |
| 37 | |
| 38 | Randomize(a[0], -10.f, 10.f); |
| 39 | Randomize(a[1], -10.f, 10.f); |
| 40 | |
| 41 | int ret = test_layer("Gemm", pd, weights, a); |
| 42 | if (ret != 0) |
| 43 | { |
| 44 | fprintf(stderr, "test_gemm_int8 failed M=%d N=%d K=%d TILE_M=%d TILE_N=%d TILE_K=%d alpha=%f transA=%d transB=%d output_transpose=%d\n", M, N, K, TILE_M, TILE_N, TILE_K, alpha, transA, transB, output_transpose); |
| 45 | } |
| 46 | |
| 47 | return ret; |
| 48 | } |
| 49 | |
| 50 | static int test_gemm_0(int M, int N, int K, int TILE_M, int TILE_N, int TILE_K) |
| 51 | { |
| 52 | return 0 |
| 53 | || test_gemm_int8(M, N, K, TILE_M, TILE_N, TILE_K, 2.1f, 0, 0, 0) |
| 54 | || test_gemm_int8(M, N, K, TILE_M, TILE_N, TILE_K, 3.1f, 0, 1, 0) |
| 55 | || test_gemm_int8(M, N, K, TILE_M, TILE_N, TILE_K, 4.1f, 1, 0, 0) |
| 56 | || test_gemm_int8(M, N, K, TILE_M, TILE_N, TILE_K, 5.1f, 1, 1, 0) |
| 57 | || test_gemm_int8(M, N, K, TILE_M, TILE_N, TILE_K, 2.1f, 0, 0, 1) |
| 58 | || test_gemm_int8(M, N, K, TILE_M, TILE_N, TILE_K, 3.1f, 0, 1, 1) |
| 59 | || test_gemm_int8(M, N, K, TILE_M, TILE_N, TILE_K, 4.1f, 1, 0, 1) |
| 60 | || test_gemm_int8(M, N, K, TILE_M, TILE_N, TILE_K, 5.1f, 1, 1, 1); |
| 61 | } |
| 62 | #endif // NCNN_INT8 |
| 63 | |
| 64 | int main() |
| 65 | { |
| 66 | SRAND(7767517); |
| 67 | |
| 68 | #if NCNN_INT8 |
| 69 | int mnk[][3] = { |
| 70 | {1, 1, 1}, |
| 71 | {2, 2, 2}, |
| 72 | {3, 3, 3}, |
| 73 | {4, 4, 4}, |
| 74 | {5, 5, 5}, |
| 75 | {6, 6, 6}, |
| 76 | {7, 7, 7}, |
| 77 | {8, 8, 8}, |
| 78 | {15, 15, 15}, |
| 79 | {16, 16, 16}, |
| 80 | {24, 24, 24}, |
| 81 | {31, 31, 31}, |
| 82 | {31, 32, 31}, |
| 83 | {32, 31, 32}, |
| 84 | {32, 32, 32}, |
| 85 | {20, 32, 20}, |
| 86 | {40, 40, 40}, |
| 87 | {47, 47, 47}, |
| 88 | {48, 48, 48}, |
| 89 | {52, 52, 52}, |
| 90 | {63, 64, 63}, |
| 91 | {64, 63, 64}, |
| 92 | {64, 64, 64} |
| 93 | }; |
| 94 | |
| 95 | int tile_mnk[][3] = { |
| 96 | {1, 1, 1}, |
| 97 | {2, 2, 2}, |
| 98 | {4, 4, 4}, |
| 99 | {8, 8, 8}, |
| 100 | {12, 12, 12}, |
| 101 | {16, 16, 16}, |
| 102 | {20, 20, 20}, |
| 103 | {24, 24, 24}, |
| 104 | {28, 28, 28} |
| 105 | }; |
| 106 | |
| 107 | int mnk_count = sizeof(mnk) / sizeof(int) / 3; |
| 108 | int tile_mnk_count = sizeof(tile_mnk) / sizeof(int) / 3; |
| 109 | |
| 110 | for (int i = 0; i < mnk_count; i++) |
| 111 | { |
| 112 | int M = mnk[i][0]; |
| 113 | int N = mnk[i][1]; |
| 114 | int K = mnk[i][2]; |
| 115 | |
| 116 | for (int j = 0; j < tile_mnk_count; j++) |
| 117 | { |
| 118 | int TILE_M = tile_mnk[j][0]; |
| 119 | int TILE_N = tile_mnk[j][1]; |
| 120 | int TILE_K = tile_mnk[j][2]; |
| 121 | |
| 122 | if (TILE_M >= M && TILE_N >= N && TILE_K >= K) |
| 123 | continue; |
| 124 | |
| 125 | int ret = test_gemm_0(M, N, K, TILE_M, TILE_N, TILE_K); |
| 126 | if (ret != 0) |
| 127 | return ret; |
| 128 | } |
| 129 | |
| 130 | // test no tiling |
| 131 | int ret = test_gemm_0(M, N, K, 100, 100, 100); |
| 132 | if (ret != 0) |
| 133 | return ret; |
| 134 | } |
| 135 | #else |
| 136 | // test nothing for non-int8 build |
| 137 | #endif |
| 138 | |
| 139 | return 0; |
| 140 | } |