| 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 | static int test_embed(int words, int num_output, int input_dim, int bias) |
| 18 | { |
| 19 | ncnn::ParamDict pd; |
| 20 | pd.set(0, num_output); |
| 21 | pd.set(1, input_dim); |
| 22 | pd.set(2, bias); |
| 23 | pd.set(3, num_output * input_dim); |
| 24 | |
| 25 | std::vector<ncnn::Mat> weights(bias ? 2 : 1); |
| 26 | weights[0] = RandomMat(num_output * input_dim); |
| 27 | if (bias) |
| 28 | weights[1] = RandomMat(num_output); |
| 29 | |
| 30 | ncnn::Mat a(words); |
| 31 | RandomizeInt(a, 0, input_dim); |
| 32 | |
| 33 | int ret = test_layer("Embed", pd, weights, a); |
| 34 | if (ret != 0) |
| 35 | { |
| 36 | fprintf(stderr, "test_embed failed words=%d num_output=%d input_dim=%d bias=%d\n", words, num_output, input_dim, bias); |
| 37 | } |
| 38 | |
| 39 | return ret; |
| 40 | } |
| 41 | |
| 42 | static int test_embed_0() |
| 43 | { |
| 44 | return 0 |
| 45 | || test_embed(128, 128, 128, 0) |
| 46 | || test_embed(128, 128, 128, 1) |
| 47 | || test_embed(127, 127, 127, 0) |
| 48 | || test_embed(127, 127, 127, 1) |
| 49 | || test_embed(124, 124, 124, 0) |
| 50 | || test_embed(124, 124, 124, 1); |
| 51 | } |
| 52 | |
| 53 | #if NCNN_INT8 |
| 54 | static int test_embed_int8(int words, int num_output, int input_dim, int bias) |
| 55 | { |
| 56 | ncnn::ParamDict pd; |
| 57 | pd.set(0, num_output); |
| 58 | pd.set(1, input_dim); |
| 59 | pd.set(2, bias); |
| 60 | pd.set(3, num_output * input_dim); |
| 61 | pd.set(18, 2); |
| 62 | |
| 63 | std::vector<ncnn::Mat> weights(bias ? 3 : 2); |
| 64 | weights[0] = RandomS8Mat(num_output * input_dim); |
| 65 | if (bias) |
| 66 | { |
| 67 | weights[1] = RandomMat(num_output); |
| 68 | weights[2] = RandomMat(1, 100.f, 200.f); |
| 69 | } |
| 70 | else |
| 71 | { |
| 72 | weights[1] = RandomMat(1, 100.f, 200.f); |
| 73 | } |
| 74 | |
| 75 | ncnn::Mat a(words); |
| 76 | RandomizeInt(a, 0, input_dim); |
| 77 | |
| 78 | int ret = test_layer("Embed", pd, weights, a); |
| 79 | if (ret != 0) |
| 80 | { |
| 81 | fprintf(stderr, "test_embed_int8 failed words=%d num_output=%d input_dim=%d bias=%d\n", words, num_output, input_dim, bias); |
| 82 | } |
| 83 | |
| 84 | return ret; |
| 85 | } |
| 86 | |
| 87 | static int test_embed_1() |
| 88 | { |
| 89 | return 0 |
| 90 | || test_embed_int8(128, 128, 128, 0) |
| 91 | || test_embed_int8(128, 128, 128, 1) |
| 92 | || test_embed_int8(127, 127, 127, 0) |
| 93 | || test_embed_int8(127, 127, 127, 1) |
| 94 | || test_embed_int8(124, 124, 124, 0) |
| 95 | || test_embed_int8(124, 124, 124, 1); |
| 96 | } |
| 97 | #endif // NCNN_INT8 |
| 98 | |
| 99 | int main() |
| 100 | { |
| 101 | SRAND(7767517); |
| 102 | |
| 103 | #if NCNN_INT8 |
| 104 | return test_embed_0() || test_embed_1(); |
| 105 | #else |
| 106 | return test_embed_0(); |
| 107 | #endif |
| 108 | } |