Skip to content

Commit 697d7c3

Browse files
committed
Add Swish NGraph backend and visitor API tests.
Signed-off-by: Luwei Zhou <luwei.zhou@intel.com>
1 parent 25d0a66 commit 697d7c3

File tree

3 files changed

+103
-0
lines changed

3 files changed

+103
-0
lines changed

ngraph/test/CMakeLists.txt

+2
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,7 @@ set(SRC
295295
visitors/op/squeeze.cpp
296296
visitors/op/sqrt.cpp
297297
visitors/op/strided_slice.cpp
298+
visitors/op/swish.cpp
298299
visitors/op/tanh.cpp
299300
visitors/op/topk.cpp
300301
visitors/op/transpose.cpp
@@ -471,6 +472,7 @@ set(MULTI_TEST_SRC
471472
backend/squared_difference.in.cpp
472473
backend/squeeze.in.cpp
473474
backend/subtract.in.cpp
475+
backend/swish.in.cpp
474476
backend/tan.in.cpp
475477
backend/tanh.in.cpp
476478
backend/tile.in.cpp

ngraph/test/backend/swish.in.cpp

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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+
}

ngraph/test/visitors/op/swish.cpp

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright (C) 2018-2021 Intel Corporation
2+
// SPDX-License-Identifier: Apache-2.0
3+
//
4+
5+
#include "gtest/gtest.h"
6+
7+
#include "ngraph/ngraph.hpp"
8+
#include "ngraph/op/util/attr_types.hpp"
9+
#include "ngraph/opsets/opset4.hpp"
10+
#include "util/visitor.hpp"
11+
12+
using namespace std;
13+
using namespace ngraph;
14+
using ngraph::test::NodeBuilder;
15+
16+
TEST(attributes, swish_op)
17+
{
18+
NodeBuilder::get_ops().register_factory<opset4::Swish>();
19+
const auto A = make_shared<op::Parameter>(element::f32, Shape{5, 2});
20+
21+
const auto swish = make_shared<opset4::Swish>(A);
22+
NodeBuilder builder(swish);
23+
24+
const auto expected_attr_count = 0;
25+
EXPECT_EQ(builder.get_value_map_size(), expected_attr_count);
26+
}

0 commit comments

Comments
 (0)