-
Notifications
You must be signed in to change notification settings - Fork 2.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add mish op to ngraph #1187
Merged
GlebKazantaev
merged 19 commits into
openvinotoolkit:master
from
iimironov:imironov/CVS-32956-add-mish-op-to-ngraph
Jul 20, 2020
Merged
Add mish op to ngraph #1187
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
9bbc1ca
Add mish op to ngraph
96b613c
Update mish op
080fbe1
Set v4 namespase for tests
5102744
Add mish to cmake
daf299e
Add comments for mish op.
08d65e4
Refactoring code style
cd429b4
Update version to v1 for Mish op
25feeae
Add value propogation test for Mish op
86bc573
Refactoring mish op according to review
024cff4
Fix mish version
b5840e6
Update cmake file
8c204a9
Fix mish value propogation unit test
44d61cd
Merge branch 'master' of https://github.com/iimironov/openvino into i…
e3b9ff0
Add unit test for mish op
4ddf7d9
Merge branch 'master' of https://github.com/iimironov/openvino into i…
814a1d7
Merge branch 'master' of https://github.com/iimironov/openvino into i…
9ee487f
Merge branch 'master' of https://github.com/iimironov/openvino into i…
84cb3af
Merge branch 'master' of https://github.com/iimironov/openvino into i…
2915430
Merge branch 'master' of https://github.com/iimironov/openvino into i…
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
//***************************************************************************** | ||
// Copyright 2017-2020 Intel Corporation | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
//***************************************************************************** | ||
|
||
#include "ngraph/op/mish.hpp" | ||
#include "ngraph/attribute_visitor.hpp" | ||
|
||
#include "ngraph/runtime/host_tensor.hpp" | ||
#include "ngraph/runtime/reference/mish.hpp" | ||
|
||
using namespace std; | ||
using namespace ngraph; | ||
|
||
constexpr NodeTypeInfo op::v4::Mish::type_info; | ||
|
||
op::v4::Mish::Mish(const Output<Node>& arg) | ||
: Op({arg}) | ||
{ | ||
constructor_validate_and_infer_types(); | ||
} | ||
|
||
bool op::v4::Mish::visit_attributes(AttributeVisitor& visitor) | ||
{ | ||
return true; | ||
} | ||
|
||
void op::v4::Mish::validate_and_infer_types() | ||
{ | ||
set_output_size(1); | ||
set_output_type(0, get_input_element_type(0), get_input_partial_shape(0)); | ||
} | ||
|
||
shared_ptr<Node> op::v4::Mish::clone_with_new_inputs(const OutputVector& new_args) const | ||
{ | ||
check_new_args_count(this, new_args); | ||
return make_shared<Mish>(new_args.at(0)); | ||
} | ||
|
||
namespace | ||
{ | ||
template <element::Type_t ET> | ||
inline bool evaluate(const HostTensorPtr& arg0, const HostTensorPtr& out, const size_t count) | ||
{ | ||
using T = typename element_type_traits<ET>::value_type; | ||
runtime::reference::mish<T>(arg0->get_data_ptr<ET>(), out->get_data_ptr<ET>(), count); | ||
return true; | ||
} | ||
|
||
bool evaluate_mish(const HostTensorPtr& arg0, const HostTensorPtr& out, const size_t count) | ||
{ | ||
bool rc = true; | ||
out->set_unary(arg0); | ||
|
||
switch (arg0->get_element_type()) | ||
{ | ||
TYPE_CASE(f16)(arg0, out, count); | ||
break; | ||
TYPE_CASE(f32)(arg0, out, count); | ||
break; | ||
default: rc = false; break; | ||
} | ||
return rc; | ||
} | ||
} | ||
|
||
bool op::v4::Mish::evaluate(const HostTensorVector& outputs, const HostTensorVector& inputs) | ||
{ | ||
return evaluate_mish(inputs[0], outputs[0], shape_size(get_output_shape(0))); | ||
} |
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 2017-2020 Intel Corporation | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
//***************************************************************************** | ||
|
||
#pragma once | ||
|
||
#include "ngraph/node.hpp" | ||
#include "ngraph/op/op.hpp" | ||
|
||
namespace ngraph | ||
{ | ||
namespace op | ||
{ | ||
namespace v4 | ||
{ | ||
/// \brief A Self Regularized Non-Monotonic Neural Activation Function | ||
/// f(x) = x * tanh(log(exp(x) + 1.)) | ||
/// | ||
class NGRAPH_API Mish : public ngraph::op::Op | ||
{ | ||
public: | ||
static constexpr NodeTypeInfo type_info{"Mish", 4}; | ||
const NodeTypeInfo& get_type_info() const override { return type_info; } | ||
Mish() = default; | ||
/// \brief Constructs an Mish operation. | ||
/// | ||
/// \param data Input tensor | ||
Mish(const Output<Node>& arg); | ||
bool visit_attributes(AttributeVisitor& visitor) override; | ||
void validate_and_infer_types() override; | ||
|
||
virtual std::shared_ptr<Node> | ||
clone_with_new_inputs(const OutputVector& new_args) const override; | ||
|
||
bool evaluate(const HostTensorVector& outputs, | ||
const HostTensorVector& inputs) override; | ||
}; | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
//***************************************************************************** | ||
// Copyright 2017-2020 Intel Corporation | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
//***************************************************************************** | ||
|
||
#pragma once | ||
|
||
#include <cmath> | ||
#include <cstddef> | ||
|
||
namespace ngraph | ||
{ | ||
namespace runtime | ||
{ | ||
namespace reference | ||
{ | ||
template <typename T> | ||
void mish(const T* arg, T* out, size_t count) | ||
{ | ||
for (size_t i = 0; i < count; i++) | ||
{ | ||
out[i] = arg[i] * std::tanh(std::log((std::exp(arg[i]) + 1.0))); | ||
} | ||
} | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
//***************************************************************************** | ||
// Copyright 2017-2020 Intel Corporation | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
//***************************************************************************** | ||
|
||
#include <string> | ||
#include <vector> | ||
|
||
#include "gtest/gtest.h" | ||
|
||
#include "ngraph/op/mish.hpp" | ||
#include "ngraph/runtime/host_tensor.hpp" | ||
#include "ngraph/validation_util.hpp" | ||
#include "runtime/backend.hpp" | ||
#include "util/test_tools.hpp" | ||
#include "util/type_prop.hpp" | ||
|
||
using namespace std; | ||
using namespace ngraph; | ||
|
||
TEST(op_eval, mish_0D) | ||
{ | ||
auto p = make_shared<op::Parameter>(element::f32, Shape{}); | ||
auto mish = make_shared<op::v4::Mish>(p); | ||
auto fun = make_shared<Function>(OutputVector{mish}, ParameterVector{p}); | ||
|
||
std::vector<std::vector<float>> inputs{{-1.0}, {1.0}, {20.0}}; | ||
std::vector<std::vector<float>> expected_result{{-0.303401}, {0.86509835720062256}, {20.0}}; | ||
|
||
for (size_t i = 0; i < inputs.size(); i++) | ||
{ | ||
auto result = make_shared<HostTensor>(); | ||
ASSERT_TRUE( | ||
fun->evaluate({result}, {make_host_tensor<element::Type_t::f32>(Shape{}, inputs[i])})); | ||
EXPECT_EQ(result->get_element_type(), element::f32); | ||
EXPECT_EQ(result->get_shape(), (Shape{})); | ||
auto result_data = read_vector<float>(result); | ||
EXPECT_NEAR(result_data[0], expected_result[i][0], 0.3); | ||
iimironov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} |
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,54 @@ | ||
//***************************************************************************** | ||
// Copyright 2017-2020 Intel Corporation | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
//***************************************************************************** | ||
|
||
#include "gtest/gtest.h" | ||
#include "ngraph/ngraph.hpp" | ||
#include "util/type_prop.hpp" | ||
|
||
using namespace std; | ||
using namespace ngraph; | ||
|
||
TEST(type_prop, mish) | ||
{ | ||
auto data = make_shared<op::Parameter>(element::f32, Shape{1, 3, 6}); | ||
auto mish_func = make_shared<op::v4::Mish>(data); | ||
EXPECT_EQ(mish_func->get_element_type(), element::f32); | ||
EXPECT_EQ(mish_func->get_shape(), (Shape{1, 3, 6})); | ||
} | ||
|
||
TEST(type_prop, mish_partial) | ||
iimironov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
auto data = make_shared<op::Parameter>(element::f32, PartialShape{1, Dimension::dynamic(), 6}); | ||
auto mish_func = make_shared<op::v4::Mish>(data); | ||
EXPECT_EQ(mish_func->get_element_type(), element::f32); | ||
ASSERT_TRUE(mish_func->get_output_partial_shape(0).same_scheme( | ||
(PartialShape{1, Dimension::dynamic(), 6}))); | ||
|
||
// rank unknown | ||
auto mish_partial = make_shared<op::v4::Mish>( | ||
make_shared<op::Parameter>(element::f32, PartialShape::dynamic())); | ||
ASSERT_TRUE(mish_partial->get_output_partial_shape(0).same_scheme(PartialShape::dynamic())); | ||
} | ||
|
||
TEST(type_prop, mish_partial_static_rank) | ||
{ | ||
auto data = make_shared<op::Parameter>(element::f32, PartialShape{1, Dimension::dynamic(), 6}); | ||
auto mish_func = make_shared<op::v4::Mish>(data); | ||
EXPECT_EQ(mish_func->get_element_type(), element::f32); | ||
ASSERT_TRUE(mish_func->get_output_partial_shape(0).same_scheme( | ||
(PartialShape{1, Dimension::dynamic(), 6}))); | ||
ASSERT_TRUE(mish_func->get_output_partial_shape(0).rank().is_static()); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why did you remove this line?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This line was moved to new place (in alphabet order).