| 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_gelu(const ncnn::Mat& a, bool fast_gelu) |
| 18 | { |
| 19 | ncnn::ParamDict pd; |
| 20 | pd.set(0, fast_gelu ? 1 : 0); |
| 21 | |
| 22 | std::vector<ncnn::Mat> weights(0); |
| 23 | |
| 24 | int ret = test_layer("GELU", pd, weights, a); |
| 25 | if (ret != 0) |
| 26 | { |
| 27 | fprintf(stderr, "test_gelu failed a.dims=%d a=(%d %d %d %d) fast_gelu=%s\n", a.dims, a.w, a.h, a.d, a.c, fast_gelu ? "true" : "false"); |
| 28 | } |
| 29 | |
| 30 | return ret; |
| 31 | } |
| 32 | |
| 33 | static int test_gelu_0() |
| 34 | { |
| 35 | return 0 |
| 36 | || test_gelu(RandomMat(6, 7, 9, 32), false) |
| 37 | || test_gelu(RandomMat(6, 7, 9, 32), true) |
| 38 | || test_gelu(RandomMat(5, 6, 7, 24), false) |
| 39 | || test_gelu(RandomMat(5, 6, 7, 24), true) |
| 40 | || test_gelu(RandomMat(7, 8, 9, 12), false) |
| 41 | || test_gelu(RandomMat(7, 8, 9, 12), true) |
| 42 | || test_gelu(RandomMat(3, 4, 5, 13), false) |
| 43 | || test_gelu(RandomMat(3, 4, 5, 13), true); |
| 44 | } |
| 45 | |
| 46 | static int test_gelu_1() |
| 47 | { |
| 48 | return 0 |
| 49 | || test_gelu(RandomMat(9, 7, 32), false) |
| 50 | || test_gelu(RandomMat(9, 7, 32), true) |
| 51 | || test_gelu(RandomMat(5, 7, 24), false) |
| 52 | || test_gelu(RandomMat(5, 7, 24), true) |
| 53 | || test_gelu(RandomMat(7, 9, 12), false) |
| 54 | || test_gelu(RandomMat(7, 9, 12), true) |
| 55 | || test_gelu(RandomMat(3, 5, 13), false) |
| 56 | || test_gelu(RandomMat(3, 5, 13), true); |
| 57 | } |
| 58 | |
| 59 | static int test_gelu_2() |
| 60 | { |
| 61 | return 0 |
| 62 | || test_gelu(RandomMat(13, 32), false) |
| 63 | || test_gelu(RandomMat(13, 32), true) |
| 64 | || test_gelu(RandomMat(15, 24), false) |
| 65 | || test_gelu(RandomMat(15, 24), true) |
| 66 | || test_gelu(RandomMat(17, 12), false) |
| 67 | || test_gelu(RandomMat(17, 12), true) |
| 68 | || test_gelu(RandomMat(19, 15), false) |
| 69 | || test_gelu(RandomMat(19, 15), true); |
| 70 | } |
| 71 | |
| 72 | static int test_gelu_3() |
| 73 | { |
| 74 | return 0 |
| 75 | || test_gelu(RandomMat(128), false) |
| 76 | || test_gelu(RandomMat(128), true) |
| 77 | || test_gelu(RandomMat(124), false) |
| 78 | || test_gelu(RandomMat(124), true) |
| 79 | || test_gelu(RandomMat(127), false) |
| 80 | || test_gelu(RandomMat(127), true) |
| 81 | || test_gelu(RandomMat(120), false) |
| 82 | || test_gelu(RandomMat(120), true); |
| 83 | } |
| 84 | |
| 85 | int main() |
| 86 | { |
| 87 | SRAND(7767517); |
| 88 | |
| 89 | return 0 |
| 90 | || test_gelu_0() |
| 91 | || test_gelu_1() |
| 92 | || test_gelu_2() |
| 93 | || test_gelu_3(); |
| 94 | } |