| 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 | #define OP_TYPE_MAX 20 |
| 18 | |
| 19 | static int op_type = 0; |
| 20 | |
| 21 | static int test_unaryop(const ncnn::Mat& _a) |
| 22 | { |
| 23 | ncnn::Mat a = _a; |
| 24 | if (op_type == 2 || op_type == 3) |
| 25 | { |
| 26 | // large dynamic range for floor ceil |
| 27 | for (int i = 0; i < a.total(); i++) |
| 28 | { |
| 29 | a[i] *= 1000; |
| 30 | } |
| 31 | } |
| 32 | if (op_type == 5 || op_type == 6 || op_type == 8 || op_type == 17) |
| 33 | { |
| 34 | // value must be positive for sqrt rsqrt log |
| 35 | Randomize(a, 0.001f, 2.f); |
| 36 | } |
| 37 | if (op_type == 11 || op_type == 12 || op_type == 13) |
| 38 | { |
| 39 | // smaller range for tan asin acos |
| 40 | Randomize(a, -1.f, 1.f); |
| 41 | } |
| 42 | |
| 43 | ncnn::ParamDict pd; |
| 44 | pd.set(0, op_type); |
| 45 | |
| 46 | std::vector<ncnn::Mat> weights(0); |
| 47 | |
| 48 | int ret = test_layer("UnaryOp", pd, weights, a); |
| 49 | if (ret != 0) |
| 50 | { |
| 51 | fprintf(stderr, "test_unaryop failed a.dims=%d a=(%d %d %d %d) op_type=%d\n", a.dims, a.w, a.h, a.d, a.c, op_type); |
| 52 | } |
| 53 | |
| 54 | return ret; |
| 55 | } |
| 56 | |
| 57 | static int test_unaryop_0() |
| 58 | { |
| 59 | return 0 |
| 60 | || test_unaryop(RandomMat(11, 3, 2, 16)) |
| 61 | || test_unaryop(RandomMat(10, 2, 2, 12)) |
| 62 | || test_unaryop(RandomMat(6, 1, 5, 13)); |
| 63 | } |
| 64 | |
| 65 | static int test_unaryop_1() |
| 66 | { |
| 67 | return 0 |
| 68 | || test_unaryop(RandomMat(11, 7, 16)) |
| 69 | || test_unaryop(RandomMat(10, 4, 12)) |
| 70 | || test_unaryop(RandomMat(6, 5, 13)); |
| 71 | } |
| 72 | |
| 73 | static int test_unaryop_2() |
| 74 | { |
| 75 | return 0 |
| 76 | || test_unaryop(RandomMat(12, 16)) |
| 77 | || test_unaryop(RandomMat(10, 12)) |
| 78 | || test_unaryop(RandomMat(14, 15)); |
| 79 | } |
| 80 | |
| 81 | static int test_unaryop_3() |
| 82 | { |
| 83 | return 0 |
| 84 | || test_unaryop(RandomMat(128)) |
| 85 | || test_unaryop(RandomMat(12)) |
| 86 | || test_unaryop(RandomMat(15)); |
| 87 | } |
| 88 | |
| 89 | int main() |
| 90 | { |
| 91 | SRAND(7767517); |
| 92 | |
| 93 | for (op_type = 0; op_type < OP_TYPE_MAX; op_type++) |
| 94 | { |
| 95 | int ret = 0 |
| 96 | || test_unaryop_0() |
| 97 | || test_unaryop_1() |
| 98 | || test_unaryop_2() |
| 99 | || test_unaryop_3(); |
| 100 | |
| 101 | if (ret != 0) |
| 102 | return ret; |
| 103 | } |
| 104 | |
| 105 | return 0; |
| 106 | } |