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.
[Transformations] Add Squeeze-15 downgrade transformation
- Loading branch information
1 parent
dd6ed6c
commit c6140dd
Showing
4 changed files
with
164 additions
and
0 deletions.
There are no files selected for viewing
23 changes: 23 additions & 0 deletions
23
...on/transformations/include/transformations/op_conversions/convert_squeeze15_downgrade.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,23 @@ | ||
// Copyright (C) 2018-2024 Intel Corporation | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
#pragma once | ||
|
||
#include "openvino/pass/matcher_pass.hpp" | ||
#include "transformations_visibility.hpp" | ||
|
||
namespace ov { | ||
namespace pass { | ||
/** | ||
* @ingroup ov_transformation_common_api | ||
* @brief Converts Squeeze v15 to Squeeze v0. | ||
*/ | ||
class TRANSFORMATIONS_API ConvertSqueeze15ToSqueeze0 : public MatcherPass { | ||
public: | ||
OPENVINO_RTTI("ConvertSqueeze15ToSqueeze0", "0"); | ||
ConvertSqueeze15ToSqueeze0(); | ||
}; | ||
|
||
} // namespace pass | ||
} // 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
40 changes: 40 additions & 0 deletions
40
...common/transformations/src/transformations/op_conversions/convert_squeeze15_downgrade.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-2024 Intel Corporation | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
#include "transformations/op_conversions/convert_squeeze15_downgrade.hpp" | ||
|
||
#include "itt.hpp" | ||
#include "openvino/core/rt_info.hpp" | ||
#include "openvino/op/squeeze.hpp" | ||
#include "openvino/pass/pattern/op/wrap_type.hpp" | ||
#include "transformations/utils/utils.hpp" | ||
|
||
ov::pass::ConvertSqueeze15ToSqueeze0::ConvertSqueeze15ToSqueeze0() { | ||
MATCHER_SCOPE(ConvertSqueeze15ToSqueeze0); | ||
|
||
const auto& squeeze_v15_pattern = pattern::wrap_type<ov::op::v15::Squeeze>(); | ||
|
||
const matcher_pass_callback callback = [OV_CAPTURE_CPY_AND_THIS](pattern::Matcher& m) { | ||
const auto& squeeze_v15 = ov::as_type_ptr<ov::op::v15::Squeeze>(m.get_match_root()); | ||
if (!squeeze_v15 || transformation_callback(squeeze_v15)) { | ||
return false; | ||
} | ||
std::shared_ptr<op::v0::Squeeze> squeeze_v0; | ||
if (squeeze_v15->get_input_size() == 1) { | ||
squeeze_v0 = std::make_shared<op::v0::Squeeze>(squeeze_v15->input_value(0)); | ||
} else if (squeeze_v15->get_input_size() == 2 && !squeeze_v15->get_allow_axis_skip()) { | ||
squeeze_v0 = std::make_shared<op::v0::Squeeze>(squeeze_v15->input_value(0), squeeze_v15->input_value(1)); | ||
} else { | ||
return false; | ||
} | ||
squeeze_v0->set_friendly_name(squeeze_v15->get_friendly_name()); | ||
copy_runtime_info(squeeze_v15, squeeze_v0); | ||
replace_node(squeeze_v15, squeeze_v0); | ||
|
||
return true; | ||
}; | ||
|
||
auto m = std::make_shared<pattern::Matcher>(squeeze_v15_pattern, matcher_name); | ||
register_matcher(m, callback); | ||
} |
99 changes: 99 additions & 0 deletions
99
src/common/transformations/tests/op_conversions/convert_squeeze15_downgrade_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,99 @@ | ||
// Copyright (C) 2018-2024 Intel Corporation | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
#include "transformations/op_conversions/convert_squeeze15_downgrade.hpp" | ||
|
||
#include <gtest/gtest.h> | ||
|
||
#include <memory> | ||
|
||
#include "common_test_utils/ov_test_utils.hpp" | ||
#include "openvino/opsets/opset1.hpp" | ||
#include "openvino/opsets/opset15.hpp" | ||
#include "openvino/pass/manager.hpp" | ||
#include "transformations/utils/utils.hpp" | ||
using namespace ov; | ||
using namespace testing; | ||
|
||
namespace { | ||
|
||
enum class IndicesMode { NONE, CONST, PARAM }; | ||
|
||
std::shared_ptr<ov::Model> create_v15_model(const IndicesMode indices_mode, | ||
const std::vector<int> indices_const_val, | ||
const bool allow_axis_skip) { | ||
const PartialShape data_shape{-1, {2, 5}, 1, {1, 5}, 4}; | ||
const auto& data = std::make_shared<ov::opset15::Parameter>(ov::element::f32, data_shape); | ||
ov::ParameterVector params = {data}; | ||
std::shared_ptr<op::v15::Squeeze> squeeze; | ||
if (indices_mode == IndicesMode::NONE) { | ||
squeeze = std::make_shared<ov::opset15::Squeeze>(data); | ||
} else if (indices_mode == IndicesMode::PARAM) { | ||
const auto& indices = | ||
std::make_shared<ov::opset15::Parameter>(ov::element::i32, PartialShape({data_shape.rank()})); | ||
params.push_back(indices); | ||
squeeze = std::make_shared<ov::opset15::Squeeze>(data, indices, allow_axis_skip); | ||
} else if (indices_mode == IndicesMode::CONST) { | ||
const auto& indices = | ||
ov::opset15::Constant::create(ov::element::i32, Shape({indices_const_val.size()}), indices_const_val); | ||
squeeze = std::make_shared<ov::opset15::Squeeze>(data, indices, allow_axis_skip); | ||
} | ||
squeeze->set_friendly_name("squeeze15"); | ||
return std::make_shared<ov::Model>(squeeze->outputs(), params); | ||
} | ||
|
||
std::shared_ptr<ov::Model> create_v1_model(const IndicesMode indices_mode, const std::vector<int> indices_const_val) { | ||
const PartialShape data_shape{-1, {2, 5}, 1, {1, 5}, 4}; | ||
const auto& data = std::make_shared<ov::opset1::Parameter>(ov::element::f32, data_shape); | ||
ov::ParameterVector params = {data}; | ||
std::shared_ptr<op::v0::Squeeze> squeeze; | ||
if (indices_mode == IndicesMode::NONE) { | ||
squeeze = std::make_shared<ov::opset1::Squeeze>(data); | ||
} else if (indices_mode == IndicesMode::PARAM) { | ||
const auto& indices = | ||
std::make_shared<ov::opset1::Parameter>(ov::element::i32, PartialShape({data_shape.rank()})); | ||
params.push_back(indices); | ||
squeeze = std::make_shared<ov::opset1::Squeeze>(data, indices); | ||
} else if (indices_mode == IndicesMode::CONST) { | ||
const auto& indices = | ||
ov::opset1::Constant::create(ov::element::i32, Shape({indices_const_val.size()}), indices_const_val); | ||
squeeze = std::make_shared<ov::opset1::Squeeze>(data, indices); | ||
} | ||
squeeze->set_friendly_name("squeeze15"); | ||
return std::make_shared<ov::Model>(squeeze->outputs(), params); | ||
} | ||
|
||
} // namespace | ||
|
||
TEST_F(TransformationTestsF, ConvertSqueeze15ToSqueeze1_no_indices) { | ||
manager.register_pass<ov::pass::ConvertSqueeze15ToSqueeze0>(); | ||
model = create_v15_model(IndicesMode::NONE, {}, false); | ||
model_ref = create_v1_model(IndicesMode::NONE, {}); | ||
comparator.enable(FunctionsComparator::CmpValues::CONST_VALUES); | ||
comparator.enable(FunctionsComparator::CmpValues::ATTRIBUTES); | ||
comparator.enable(FunctionsComparator::CmpValues::NAMES); | ||
} | ||
|
||
TEST_F(TransformationTestsF, ConvertSqueeze15ToSqueeze1_const_indices_no_skip) { | ||
manager.register_pass<ov::pass::ConvertSqueeze15ToSqueeze0>(); | ||
model = create_v15_model(IndicesMode::CONST, {0, -4, 3}, false); | ||
model_ref = create_v1_model(IndicesMode::CONST, {0, -4, 3}); | ||
comparator.enable(FunctionsComparator::CmpValues::CONST_VALUES); | ||
comparator.enable(FunctionsComparator::CmpValues::ATTRIBUTES); | ||
comparator.enable(FunctionsComparator::CmpValues::NAMES); | ||
} | ||
|
||
TEST_F(TransformationTestsF, ConvertSqueeze15ToSqueeze1_dynamic_indices_no_skip) { | ||
manager.register_pass<ov::pass::ConvertSqueeze15ToSqueeze0>(); | ||
model = create_v15_model(IndicesMode::PARAM, {}, false); | ||
model_ref = create_v1_model(IndicesMode::PARAM, {}); | ||
comparator.enable(FunctionsComparator::CmpValues::CONST_VALUES); | ||
comparator.enable(FunctionsComparator::CmpValues::ATTRIBUTES); | ||
comparator.enable(FunctionsComparator::CmpValues::NAMES); | ||
} | ||
|
||
TEST_F(TransformationTestsF, ConvertSqueeze15ToSqueeze1_unsupported_skip) { | ||
manager.register_pass<ov::pass::ConvertSqueeze15ToSqueeze0>(); | ||
model = create_v15_model(IndicesMode::PARAM, {}, true); | ||
} |