-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add mish fusion transformation (#1399)
* Add mish fusion transformation * Add mish op to python api
- Loading branch information
Showing
10 changed files
with
160 additions
and
2 deletions.
There are no files selected for viewing
32 changes: 32 additions & 0 deletions
32
inference-engine/src/transformations/include/transformations/mish_fusion.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// Copyright (C) 2020 Intel Corporation | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
#pragma once | ||
|
||
#include <vector> | ||
#include <memory> | ||
|
||
#include <transformations_visibility.hpp> | ||
|
||
#include <ngraph/ngraph.hpp> | ||
#include <ngraph/pass/graph_rewrite.hpp> | ||
#include "ngraph/pattern/matcher.hpp" | ||
|
||
namespace ngraph { | ||
namespace pass { | ||
|
||
class TRANSFORMATIONS_API MishFusion; | ||
|
||
} // namespace pass | ||
} // namespace ngraph | ||
|
||
/** | ||
* @ingroup ie_transformation_common_api | ||
* @brief MishFusion transformation replaces group of | ||
* operations: x * tanh(log(exp(x) + 1)) to Mish op. | ||
*/ | ||
class ngraph::pass::MishFusion: public ngraph::pass::MatcherPass { | ||
public: | ||
MishFusion(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
inference-engine/src/transformations/src/transformations/mish_fusion.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// Copyright (C) 2018-2020 Intel Corporation | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
#include "transformations/mish_fusion.hpp" | ||
|
||
#include <memory> | ||
#include <vector> | ||
|
||
#include <ngraph/opsets/opset4.hpp> | ||
#include <ngraph/rt_info.hpp> | ||
#include <ngraph/pattern/op/wrap_type.hpp> | ||
|
||
ngraph::pass::MishFusion::MishFusion() { | ||
auto input = ngraph::pattern::any_input(); | ||
auto exp = std::make_shared<ngraph::opset4::Exp>(input); | ||
auto add = std::make_shared<ngraph::opset4::Add>(exp, ngraph::pattern::wrap_type<ngraph::opset4::Constant>()); | ||
auto log = std::make_shared<ngraph::opset4::Log>(add); | ||
auto tanh = std::make_shared<ngraph::opset4::Tanh>(log); | ||
auto mul = std::make_shared<ngraph::opset4::Multiply>(input, tanh); | ||
|
||
ngraph::graph_rewrite_callback matcher_pass_callback = [=](ngraph::pattern::Matcher& m) { | ||
auto & pattern_to_output = m.get_pattern_value_map(); | ||
auto exp_input = pattern_to_output.at(input); | ||
|
||
auto mish = std::make_shared<ngraph::opset4::Mish>(exp_input); | ||
|
||
mish->set_friendly_name(m.get_match_root()->get_friendly_name()); | ||
ngraph::copy_runtime_info({pattern_to_output.at(mul).get_node_shared_ptr(), | ||
pattern_to_output.at(tanh).get_node_shared_ptr(), | ||
pattern_to_output.at(log).get_node_shared_ptr(), | ||
pattern_to_output.at(add).get_node_shared_ptr(), | ||
pattern_to_output.at(exp).get_node_shared_ptr()}, mish); | ||
ngraph::replace_node(m.get_match_root(), mish); | ||
return true; | ||
}; | ||
|
||
auto m = std::make_shared<ngraph::pattern::Matcher>(mul, "MishFusion"); | ||
register_matcher(m, matcher_pass_callback); | ||
} |
52 changes: 52 additions & 0 deletions
52
inference-engine/tests/functional/inference_engine/transformations/mish_fusion_test.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// Copyright (C) 2020 Intel Corporation | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
#include <gtest/gtest.h> | ||
|
||
#include <string> | ||
#include <memory> | ||
#include <queue> | ||
|
||
#include <ngraph/function.hpp> | ||
#include <ngraph/opsets/opset4.hpp> | ||
#include <ngraph/pass/manager.hpp> | ||
#include <ngraph/pass/visualize_tree.hpp> | ||
#include <transformations/mish_fusion.hpp> | ||
#include <transformations/init_node_info.hpp> | ||
#include <transformations/utils/utils.hpp> | ||
|
||
#include "common_test_utils/ngraph_test_utils.hpp" | ||
|
||
using namespace testing; | ||
|
||
TEST(TransformationTests, MishFusing) { | ||
std::shared_ptr<ngraph::Function> f(nullptr), f_ref(nullptr); | ||
{ | ||
auto input0 = std::make_shared<ngraph::opset4::Parameter>(ngraph::element::f64, ngraph::Shape{3, 1, 2}); | ||
auto exp = std::make_shared<ngraph::opset4::Exp>(input0); | ||
auto input_const = ngraph::opset4::Constant::create(ngraph::element::f64, ngraph::Shape{1}, {-1}); | ||
auto add = std::make_shared<ngraph::opset4::Add>(exp, input_const); | ||
auto log = std::make_shared<ngraph::opset4::Log>(add); | ||
auto tanh = std::make_shared<ngraph::opset4::Tanh>(log); | ||
auto mul = std::make_shared<ngraph::opset4::Multiply>(input0, tanh); | ||
|
||
f = std::make_shared<ngraph::Function>(ngraph::NodeVector{mul}, ngraph::ParameterVector{input0}); | ||
|
||
ngraph::pass::Manager manager; | ||
manager.register_pass<ngraph::pass::InitNodeInfo>(); | ||
manager.register_pass<ngraph::pass::MishFusion>(); | ||
manager.run_passes(f); | ||
ASSERT_NO_THROW(check_rt_info(f)); | ||
} | ||
|
||
{ | ||
auto data = std::make_shared<ngraph::opset4::Parameter>(ngraph::element::f32, ngraph::Shape{3, 1, 2}); | ||
auto mish = std::make_shared<ngraph::opset4::Mish>(data); | ||
|
||
f_ref = std::make_shared<ngraph::Function>(ngraph::NodeVector{mish}, ngraph::ParameterVector{data}); | ||
} | ||
|
||
auto res = compare_functions(f, f_ref); | ||
ASSERT_TRUE(res.first) << res.second; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters