|
| 1 | +// Copyright (C) 2018-2021 Intel Corporation |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | +// |
| 4 | +#include "gtest/gtest.h" |
| 5 | +#include "ngraph/ngraph.hpp" |
| 6 | +#include "util/engine/test_engines.hpp" |
| 7 | +#include "util/test_case.hpp" |
| 8 | +#include "util/test_control.hpp" |
| 9 | + |
| 10 | +using namespace std; |
| 11 | +using namespace ngraph; |
| 12 | + |
| 13 | +static string s_manifest = "${MANIFEST}"; |
| 14 | +using TestEngine = test::ENGINE_CLASS_NAME(${BACKEND_NAME}); |
| 15 | + |
| 16 | +NGRAPH_TEST(${BACKEND_NAME}, swish_2D_with_beta0_6) |
| 17 | +{ |
| 18 | + Shape in_shape{2, 4}; |
| 19 | + element::Type et = element::f32; |
| 20 | + auto beta = 0.6f; |
| 21 | + |
| 22 | + auto args0 = make_shared<op::Parameter>(et, in_shape); |
| 23 | + auto args1 = make_shared<op::Parameter>(et, Shape{}); |
| 24 | + auto swish = make_shared<op::v4::Swish>(args0, args1); |
| 25 | + auto f = make_shared<Function>(swish, ParameterVector{args0, args1}); |
| 26 | + |
| 27 | + vector<vector<float>> in_vec{vector<float>{0.4, -5.7, -6, 3, -0.9, 23, 5, 3.3} , vector<float>{beta}}; |
| 28 | + vector<float> out_vec{in_vec[0]}; |
| 29 | + std::transform(out_vec.begin(), out_vec.end(), out_vec.begin(), [&beta](float x) -> float { return (x / (1.0f + std::exp(x * beta * -1.0f)));}); |
| 30 | + |
| 31 | + auto test_case = test::TestCase<TestEngine>(f); |
| 32 | + test_case.add_multiple_inputs<float>(in_vec); |
| 33 | + test_case.add_expected_output<float>(in_shape, out_vec); |
| 34 | + test_case.run(); |
| 35 | +} |
| 36 | + |
| 37 | +NGRAPH_TEST(${BACKEND_NAME}, swish_2D_without_beta) |
| 38 | +{ |
| 39 | + Shape in_shape{2, 3}; |
| 40 | + element::Type et = element::f32; |
| 41 | + |
| 42 | + auto args0 = make_shared<op::Parameter>(et, in_shape); |
| 43 | + auto swish = make_shared<op::v4::Swish>(args0); |
| 44 | + auto f = make_shared<Function>(swish, ParameterVector{args0}); |
| 45 | + |
| 46 | + vector<float> in_vec{1, 8, -8, 17, -0.5, -1}; |
| 47 | + vector<float> out_vec{in_vec}; |
| 48 | + std::transform(out_vec.begin(), out_vec.end(), out_vec.begin(), [](float x) -> float { return (x / (1.0f + std::exp(x * -1.0f)));}); |
| 49 | + |
| 50 | + auto test_case = test::TestCase<TestEngine>(f); |
| 51 | + test_case.add_input<float>(in_vec); |
| 52 | + test_case.add_expected_output<float>(in_shape, out_vec); |
| 53 | + test_case.run(); |
| 54 | +} |
| 55 | + |
| 56 | +NGRAPH_TEST(${BACKEND_NAME}, swish_4D_with_beta0_33) |
| 57 | +{ |
| 58 | + Shape in_shape{2, 2, 1, 2}; |
| 59 | + element::Type et = element::f32; |
| 60 | + auto beta = 0.33f; |
| 61 | + |
| 62 | + auto args0 = make_shared<op::Parameter>(et, in_shape); |
| 63 | + auto args1 = make_shared<op::Parameter>(et, Shape{}); |
| 64 | + auto swish = make_shared<op::v4::Swish>(args0, args1); |
| 65 | + auto f = make_shared<Function>(swish, ParameterVector{args0, args1}); |
| 66 | + |
| 67 | + vector<vector<float>> in_vec{vector<float>{0.1, 0.6, 20, -7, -5.3, 3.5, -9, 11} , vector<float>{beta}}; |
| 68 | + vector<float> out_vec{in_vec[0]}; |
| 69 | + std::transform(out_vec.begin(), out_vec.end(), out_vec.begin(), [&beta](float x) -> float { return (x / (1.0f + std::exp(x * beta * -1.0f)));}); |
| 70 | + |
| 71 | + auto test_case = test::TestCase<TestEngine>(f); |
| 72 | + test_case.add_multiple_inputs<float>(in_vec); |
| 73 | + test_case.add_expected_output<float>(in_shape, out_vec); |
| 74 | + test_case.run(); |
| 75 | +} |
0 commit comments