forked from openvinotoolkit/openvino
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
117 additions
and
1 deletion.
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
41 changes: 41 additions & 0 deletions
41
...ugins/intel_cpu/src/transformations/cpu_opset/common/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,41 @@ | ||
// 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->is_dynamic() || 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; | ||
}; | ||
} | ||
|
||
ov::intel_cpu::ConvertArithmeticReduction::ConvertArithmeticReduction() { | ||
auto m = std::make_shared<ov::pass::pattern::Matcher>( | ||
ov::pass::pattern::wrap_type<ov::op::util::ArithmeticReductionKeepDims>({ov::pass::pattern::any_input(), | ||
ov::pass::pattern::wrap_type<ov::opset8::Constant>()}), "ConvertArithmeticReduction"); | ||
register_matcher(m, convert_reduce<ov::op::util::ArithmeticReductionKeepDims>()); | ||
} | ||
|
||
ov::intel_cpu::ConvertLogicalReduction::ConvertLogicalReduction() { | ||
auto m = std::make_shared<ov::pass::pattern::Matcher>( | ||
ov::pass::pattern::wrap_type<ov::op::util::LogicalReductionKeepDims>({ov::pass::pattern::any_input(), | ||
ov::pass::pattern::wrap_type<ov::opset8::Constant>()}), "ConvertLogicalReduction"); | ||
register_matcher(m, convert_reduce<ov::op::util::LogicalReductionKeepDims>()); | ||
} |
73 changes: 73 additions & 0 deletions
73
...ugins/intel_cpu/src/transformations/cpu_opset/common/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,73 @@ | ||
// 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" | ||
|
||
/* | ||
* 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(); | ||
}; | ||
|
||
class ConvertArithmeticReduction: public ConvertReduceNoKeepDimsBase { | ||
public: | ||
OPENVINO_RTTI("ConvertArithmeticReduction", "0"); | ||
ConvertArithmeticReduction(); | ||
}; | ||
|
||
class ConvertLogicalReduction: public ConvertReduceNoKeepDimsBase { | ||
public: | ||
OPENVINO_RTTI("ConvertLogicalReduction", "0"); | ||
ConvertLogicalReduction(); | ||
}; | ||
|
||
class ConvertReduceNoKeepDims: public ov::pass::GraphRewrite { | ||
public: | ||
OPENVINO_RTTI("ConvertReduceNoKeepDims", "0"); | ||
ConvertReduceNoKeepDims() { | ||
add_matcher<ConvertArithmeticReduction>(); | ||
add_matcher<ConvertLogicalReduction>(); | ||
} | ||
}; | ||
|
||
} // 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