-
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
[CPU][ARM] Enable NHWC in Reduce #23108
Merged
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
5787d17
dirty fix
alvoron 2ca90a1
less dirty fix
alvoron 6e6ab19
keep srcDims and dstDims static
alvoron 20e824b
error return in getAxeNchwLayout
alvoron 6873c49
add invalid axe check
alvoron 5d62f3a
change auto to int
alvoron ca2738e
change order of reduce descriptors
alvoron 4138d73
add transformation
alvoron c547654
[CPU] [LPT] LPT tests extension
eshoguli 09176cf
move transformation from common section to arm
alvoron bd6fd13
Revert "[CPU] [LPT] LPT tests extension"
alvoron d5614d7
Subgraph test added
alvoron 48ea9c8
extend axisCast method
alvoron 3c31b36
make template transformation class
alvoron 93dd3ae
replace subgraph test with unit test
alvoron 4fd4655
transformation accepts dynamic shapes
alvoron c6cdbb5
use static vectors in axisCast method
alvoron 778e6dc
addhasSrcNspcLayout and hasDstNspcLayout vars
alvoron 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
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
38 changes: 38 additions & 0 deletions
38
src/plugins/intel_cpu/src/transformations/cpu_opset/arm/pass/convert_reduce_no_keep_dims.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,38 @@ | ||
// Copyright (C) 2020-2024 Intel Corporation | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
|
||
#include "convert_reduce_no_keep_dims.hpp" | ||
|
||
#include "openvino/core/rt_info.hpp" | ||
#include "openvino/opsets/opset8.hpp" | ||
|
||
template <class T> | ||
ov::matcher_pass_callback ov::intel_cpu::ConvertReduceNoKeepDimsBase::convert_reduce() { | ||
return [&](ov::pass::pattern::Matcher& m) { | ||
auto reduce = std::dynamic_pointer_cast<T>(m.get_match_root()); | ||
if (!reduce || reduce->get_keep_dims()) { | ||
return false; | ||
} | ||
|
||
reduce->set_keep_dims(true); | ||
const auto reduce_new = reduce->clone_with_new_inputs({reduce->input_value(0), reduce->input_value(1)}); | ||
std::shared_ptr<ov::Node> squeeze = std::make_shared<ov::op::v0::Squeeze>(reduce_new, reduce->input_value(1)); | ||
squeeze->set_friendly_name(reduce_new->get_friendly_name()); | ||
ov::copy_runtime_info(reduce, {reduce_new, squeeze}); | ||
ov::replace_node(reduce, squeeze); | ||
|
||
return true; | ||
}; | ||
} | ||
|
||
template <typename ReductionType> | ||
ov::intel_cpu::ConvertReduction<ReductionType>::ConvertReduction() { | ||
auto m = std::make_shared<ov::pass::pattern::Matcher>( | ||
ov::pass::pattern::wrap_type<ReductionType>({ov::pass::pattern::any_input(), | ||
ov::pass::pattern::wrap_type<ov::opset8::Constant>()}), "ConvertReduction"); | ||
register_matcher(m, convert_reduce<ReductionType>()); | ||
} | ||
|
||
template class ov::intel_cpu::ConvertReduction<ov::op::util::LogicalReductionKeepDims>; | ||
template class ov::intel_cpu::ConvertReduction<ov::op::util::ArithmeticReductionKeepDims>; |
71 changes: 71 additions & 0 deletions
71
src/plugins/intel_cpu/src/transformations/cpu_opset/arm/pass/convert_reduce_no_keep_dims.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,71 @@ | ||
// Copyright (C) 2020-2024 Intel Corporation | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
#pragma once | ||
|
||
#include "openvino/pass/pattern/op/wrap_type.hpp" | ||
#include "openvino/pass/graph_rewrite.hpp" | ||
#include "openvino/op/util/arithmetic_reductions_keep_dims.hpp" | ||
#include "openvino/op/util/logical_reduction_keep_dims.hpp" | ||
|
||
/* | ||
* Description: | ||
* ConvertReduceNoKeepDimsBase detects Reduce operations with keepDims = false. | ||
* Such Reduce operation is replaced with Reduce operation with keepDims = true and Squeeze | ||
* which removes undesired dimensions. | ||
* | ||
* Before: | ||
* | ||
* +--------------+ +-----------------+ | ||
* | Data | | Axes tensor | | ||
* +-----------+--+ +-+---------------+ | ||
* | | | ||
* +---------------------------+ | ||
* | Reduce (keepDims = false) | | ||
* +---------------------------+ | ||
* | ||
* After: | ||
* | ||
* +--------------+ +-----------------+ | ||
* | Data | | Axes tensor | | ||
* +-----------+--+ +-+------------+--+ | ||
* | | | | ||
* +---------------------------+ | | ||
* | Reduce (keepDims = true) | | | ||
* +-----------------------+---+ | | ||
* | | | ||
* +--------v------v-+ | ||
* | Squeeze | | ||
* +-----------------+ | ||
* | ||
*/ | ||
|
||
namespace ov { | ||
namespace intel_cpu { | ||
|
||
class ConvertReduceNoKeepDimsBase: public ov::pass::MatcherPass { | ||
public: | ||
OPENVINO_RTTI("ConvertReduceNoKeepDims", "0"); | ||
template <class T> | ||
ov::matcher_pass_callback convert_reduce(); | ||
}; | ||
|
||
template <typename ReductionType> | ||
class ConvertReduction: public ConvertReduceNoKeepDimsBase { | ||
public: | ||
OPENVINO_RTTI("ConvertReduction", "0"); | ||
ConvertReduction(); | ||
}; | ||
|
||
|
||
class ConvertReduceNoKeepDims: public ov::pass::GraphRewrite { | ||
public: | ||
OPENVINO_RTTI("ConvertReduceNoKeepDims", "0"); | ||
ConvertReduceNoKeepDims() { | ||
add_matcher<ConvertReduction<ov::op::util::LogicalReductionKeepDims>>(); | ||
add_matcher<ConvertReduction<ov::op::util::ArithmeticReductionKeepDims>>(); | ||
} | ||
}; | ||
|
||
} // namespace intel_cpu | ||
} // namespace ov |
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
91 changes: 91 additions & 0 deletions
91
src/plugins/intel_cpu/tests/unit/transformations/arm/convert_reduce_no_keep_dims.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,91 @@ | ||
// Copyright (C) 2018-2024 Intel Corporation | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
#include <gtest/gtest.h> | ||
|
||
#include <openvino/opsets/opset1.hpp> | ||
#include <transformations/cpu_opset/arm/pass/convert_reduce_no_keep_dims.hpp> | ||
#include "common_test_utils/ov_test_utils.hpp" | ||
|
||
using namespace ov::intel_cpu; | ||
|
||
template <class T> | ||
class ConvertReduceNoKeepDimsTest : public testing::Test {}; | ||
|
||
template <class T> | ||
static std::shared_ptr<ov::Model> createInitGraph(std::shared_ptr<ov::opset1::Parameter> param) { | ||
auto axes = ov::opset1::Constant::create(ov::element::i64, ov::Shape{2}, {0, 1}); | ||
auto reduce = std::make_shared<T>(param, axes, false); | ||
return std::make_shared<ov::Model>(ov::NodeVector{ reduce }, ov::ParameterVector{ param }); | ||
} | ||
|
||
template <class T> | ||
static std::shared_ptr<ov::Model> createRefGraph(std::shared_ptr<ov::opset1::Parameter> param) { | ||
auto axes = ov::opset1::Constant::create(ov::element::i64, ov::Shape{2}, {0, 1}); | ||
auto reduce = std::make_shared<T>(param, axes, true); | ||
auto squeeze = std::make_shared<ov::opset1::Squeeze>(reduce, axes); | ||
return std::make_shared<ov::Model>(ov::NodeVector{ squeeze }, ov::ParameterVector{ param }); | ||
} | ||
|
||
template <class T> | ||
static bool registerAndRunReducePass(std::shared_ptr<ov::Model> model) { | ||
ov::pass::Manager manager; | ||
if (std::is_base_of<ov::op::util::LogicalReductionKeepDims, T>::value) { | ||
manager.register_pass<ConvertReduction<ov::op::util::LogicalReductionKeepDims>>(); | ||
} else if (std::is_base_of<ov::op::util::ArithmeticReductionKeepDims, T>::value) { | ||
manager.register_pass<ConvertReduction<ov::op::util::ArithmeticReductionKeepDims>>(); | ||
} else { | ||
return false; | ||
} | ||
manager.run_passes(model); | ||
return true; | ||
} | ||
|
||
static ov::Shape static_param_shape = ov::Shape{2, 19, 2, 9}; | ||
static ov::PartialShape dynamic_param_shape = ov::PartialShape{2, -1, 2, 9}; | ||
|
||
TYPED_TEST_SUITE_P(ConvertReduceNoKeepDimsTest); | ||
|
||
TYPED_TEST_P(ConvertReduceNoKeepDimsTest, CheckConvertReduceTransformationIsAppliedForStaticShapes) { | ||
ov::element::Type_t dataType = std::is_base_of<ov::op::util::LogicalReductionKeepDims, TypeParam>::value ? | ||
ov::element::boolean : ov::element::f32; | ||
auto param = std::make_shared<ov::opset1::Parameter>(dataType, static_param_shape); | ||
auto model = createInitGraph<TypeParam>(param); | ||
auto model_ref = createRefGraph<TypeParam>(param); | ||
|
||
if (!registerAndRunReducePass<TypeParam>(model)) { | ||
FAIL() << "Reduce pass is not registered."; | ||
} | ||
|
||
auto res = compare_functions(model, model_ref); | ||
ASSERT_TRUE(res.first) << res.second; | ||
} | ||
|
||
TYPED_TEST_P(ConvertReduceNoKeepDimsTest, CheckConvertReduceTransformationIsAppliedForDynaimcShapes) { | ||
ov::element::Type_t dataType = std::is_base_of<ov::op::util::LogicalReductionKeepDims, TypeParam>::value ? | ||
ov::element::boolean : ov::element::f32; | ||
auto param = std::make_shared<ov::opset1::Parameter>(dataType, dynamic_param_shape); | ||
auto model = createInitGraph<TypeParam>(param); | ||
auto model_ref = createRefGraph<TypeParam>(param); | ||
|
||
if (!registerAndRunReducePass<TypeParam>(model)) { | ||
FAIL() << "Reduce pass is not registered."; | ||
} | ||
|
||
auto res = compare_functions(model, model_ref); | ||
ASSERT_TRUE(res.first) << res.second; | ||
} | ||
|
||
REGISTER_TYPED_TEST_SUITE_P(ConvertReduceNoKeepDimsTest, | ||
CheckConvertReduceTransformationIsAppliedForStaticShapes, | ||
CheckConvertReduceTransformationIsAppliedForDynaimcShapes); | ||
|
||
using reduceTypes = ::testing::Types<ov::opset1::ReduceMin, | ||
ov::opset1::ReduceMax, | ||
ov::opset1::ReduceSum, | ||
ov::opset1::ReduceProd, | ||
ov::opset1::ReduceMean, | ||
ov::opset1::ReduceLogicalAnd, | ||
ov::opset1::ReduceLogicalOr>; | ||
INSTANTIATE_TYPED_TEST_SUITE_P(ConvertReduce, ConvertReduceNoKeepDimsTest, reduceTypes); |
Oops, something went wrong.
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.
@itikhono Could you please share your opinion regarding the appropriate place for this transformation? We do have an inverted one (where we fuse squeeze into reduce instead) in the common transformations scope. Isn't it better to place this one into the location of the common transformations as well?
I am not proposing to do this in scope of the current PR, but we could do it right after.