-
Notifications
You must be signed in to change notification settings - Fork 3.2k
[GPU] Add ReduceFCDimensions transformation #36212
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
Open
mdvoretc-intel
wants to merge
6
commits into
openvinotoolkit:master
Choose a base branch
from
mdvoretc-intel:fc_reduce_dim
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+238
−37
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
a5126ce
[GPU] Introduce a transformation to reduce FullyConnected activation …
mdvoretc-intel 30ba6b5
Add tests, fix metadata transfer
mdvoretc-intel e9a0981
Remove the squeeze/unsqueeze from ConvertWeightCompressedConv1x1ToMatmul
mdvoretc-intel 2d0d79d
Refactor negative tests
mdvoretc-intel 4b79836
Remove missed code
mdvoretc-intel 8dd786d
Adjust to dynamic weight shapes
mdvoretc-intel 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 hidden or 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 hidden or 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
61 changes: 61 additions & 0 deletions
61
src/plugins/intel_gpu/src/plugin/transformations/reduce_fc_dimensions.cpp
This file contains hidden or 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,61 @@ | ||
| // Copyright (C) 2018-2026 Intel Corporation | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| // | ||
|
|
||
| #include "reduce_fc_dimensions.hpp" | ||
| #include "intel_gpu/op/fully_connected.hpp" | ||
| #include "intel_gpu/op/placeholder.hpp" | ||
| #include "openvino/core/graph_util.hpp" | ||
| #include "openvino/core/rt_info.hpp" | ||
| #include "openvino/op/reshape.hpp" | ||
| #include "openvino/pass/pattern/op/any.hpp" | ||
| #include "openvino/pass/pattern/op/pattern.hpp" | ||
| #include "openvino/pass/pattern/op/wrap_type.hpp" | ||
| #include "transformations/utils/utils.hpp" | ||
|
|
||
| namespace ov::intel_gpu { | ||
|
|
||
| ReduceFCDimensions::ReduceFCDimensions() { | ||
| auto activations_m = ov::pass::pattern::any_input(ov::pass::pattern::shape_matches("[1, 1, ?, ?]")); | ||
| auto weights_m = ov::pass::pattern::any_input(ov::pass::pattern::shape_matches("[?, ?]")); | ||
| auto no_bias_m = ov::pass::pattern::wrap_type<op::Placeholder>(); | ||
| auto fc_m = ov::pass::pattern::wrap_type<op::FullyConnected>({activations_m, weights_m, no_bias_m}); | ||
|
|
||
| ov::matcher_pass_callback callback = [OV_CAPTURE_CPY_AND_THIS](ov::pass::pattern::Matcher& m) { | ||
| const auto& pattern_map = m.get_pattern_value_map(); | ||
|
|
||
| auto activations = pattern_map.at(activations_m).get_node_shared_ptr(); | ||
| auto weights = pattern_map.at(weights_m).get_node_shared_ptr(); | ||
| auto no_bias = pattern_map.at(no_bias_m).get_node_shared_ptr(); | ||
| auto fc = pattern_map.at(fc_m).get_node_shared_ptr(); | ||
|
|
||
| auto wei_pshape = weights->get_output_partial_shape(0); | ||
| // Do not apply in case of dynamic weight shape | ||
| if (wei_pshape.is_dynamic()) { | ||
| return false; | ||
| } | ||
| auto squeeze_const = | ||
| std::make_shared<ov::op::v0::Constant>(ov::element::i64, ov::Shape{3}, std::vector<int64_t>{1, -1, wei_pshape[1].get_length()}); | ||
| auto squeeze = std::make_shared<ov::op::v1::Reshape>(activations, squeeze_const, false); | ||
| ov::copy_runtime_info(activations, squeeze); | ||
| squeeze->set_friendly_name(activations->get_friendly_name() + "_squeeze"); | ||
|
|
||
| auto fc_new = fc->clone_with_new_inputs({squeeze, weights, no_bias}); | ||
| ov::copy_runtime_info(fc, fc_new); | ||
|
|
||
| auto unsqueeze_const = | ||
| std::make_shared<ov::op::v0::Constant>(ov::element::i64, ov::Shape{4}, std::vector<int64_t>{1, 1, -1, wei_pshape[0].get_length()}); | ||
| ov::copy_runtime_info(fc, unsqueeze_const); | ||
| auto unsqueeze = std::make_shared<ov::op::v1::Reshape>(fc_new, unsqueeze_const, false); | ||
| unsqueeze->set_friendly_name(fc->get_friendly_name() + "_unsqueeze"); | ||
| ov::copy_runtime_info(fc, unsqueeze); | ||
|
|
||
| ov::replace_node(fc, unsqueeze); | ||
| return true; | ||
| }; | ||
|
|
||
| auto m = std::make_shared<ov::pass::pattern::Matcher>(fc_m, "ReduceFCDimensions"); | ||
| this->register_matcher(m, callback); | ||
| } | ||
|
|
||
| } // namespace ov::intel_gpu |
17 changes: 17 additions & 0 deletions
17
src/plugins/intel_gpu/src/plugin/transformations/reduce_fc_dimensions.hpp
This file contains hidden or 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,17 @@ | ||
| // Copyright (C) 2018-2026 Intel Corporation | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| // | ||
|
|
||
| #pragma once | ||
|
|
||
| #include "openvino/pass/graph_rewrite.hpp" | ||
|
|
||
| namespace ov::intel_gpu { | ||
|
|
||
| class ReduceFCDimensions : public ov::pass::MatcherPass { | ||
| public: | ||
| OPENVINO_MATCHER_PASS_RTTI("ReduceFCDimensions"); | ||
| ReduceFCDimensions(); | ||
| }; | ||
|
|
||
| } // namespace ov::intel_gpu |
This file contains hidden or 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
156 changes: 156 additions & 0 deletions
156
src/plugins/intel_gpu/tests/unit/transformations/reduce_fc_dimensions_test.cpp
This file contains hidden or 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,156 @@ | ||
| // Copyright (C) 2018-2026 Intel Corporation | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| // | ||
|
|
||
| #include <memory> | ||
|
|
||
| #include "common_test_utils/ov_test_utils.hpp" | ||
| #include "intel_gpu/op/fully_connected.hpp" | ||
| #include "intel_gpu/op/placeholder.hpp" | ||
| #include "openvino/core/model.hpp" | ||
| #include "openvino/op/add.hpp" | ||
| #include "openvino/op/constant.hpp" | ||
| #include "openvino/op/convert.hpp" | ||
| #include "openvino/op/multiply.hpp" | ||
| #include "openvino/op/parameter.hpp" | ||
| #include "openvino/op/reshape.hpp" | ||
| #include "openvino/op/result.hpp" | ||
| #include "openvino/pass/manager.hpp" | ||
| #include "plugin/transformations/reduce_fc_dimensions.hpp" | ||
|
|
||
| using namespace testing; | ||
| using namespace ov::intel_gpu; | ||
|
|
||
| namespace ov { | ||
| namespace test { | ||
| namespace intel_gpu { | ||
|
|
||
| // Regular case, transformation should trigger | ||
| TEST_F(TransformationTestsF, ReduceFCDimensions1) { | ||
| { | ||
| auto input1 = std::make_shared<ov::op::v0::Parameter>(ov::element::f32, ov::PartialShape{1, 1, -1, 16}); | ||
| auto weights_const = ov::op::v0::Constant::create(ov::element::u8, ov::Shape{32, 16}, {1}); | ||
| auto convert = std::make_shared<ov::op::v0::Convert>(weights_const, ov::element::f32); | ||
| auto scale_const = ov::op::v0::Constant::create(ov::element::f32, ov::Shape{32, 1}, {1}); | ||
| auto scale = std::make_shared<ov::op::v1::Multiply>(convert, scale_const); | ||
| auto no_bias = std::make_shared<ov::intel_gpu::op::Placeholder>(); | ||
| auto fc = std::make_shared<ov::intel_gpu::op::FullyConnected>(input1, scale, no_bias); | ||
|
|
||
| model = std::make_shared<ov::Model>(ov::OutputVector{fc}, ov::ParameterVector{input1}); | ||
| manager.register_pass<ReduceFCDimensions>(); | ||
| } | ||
| { | ||
| auto input1 = std::make_shared<ov::op::v0::Parameter>(ov::element::f32, ov::PartialShape{1, 1, -1, 16}); | ||
| auto squeeze_const = ov::op::v0::Constant::create(ov::element::i64, ov::Shape{3}, {1, -1, 16}); | ||
| auto squeeze = std::make_shared<ov::op::v1::Reshape>(input1, squeeze_const, false); | ||
| auto weights_const = ov::op::v0::Constant::create(ov::element::u8, ov::Shape{32, 16}, {1}); | ||
| auto convert = std::make_shared<ov::op::v0::Convert>(weights_const, ov::element::f32); | ||
| auto scale_const = ov::op::v0::Constant::create(ov::element::f32, ov::Shape{32, 1}, {1}); | ||
| auto scale = std::make_shared<ov::op::v1::Multiply>(convert, scale_const); | ||
| auto no_bias = std::make_shared<ov::intel_gpu::op::Placeholder>(); | ||
| auto fc = std::make_shared<ov::intel_gpu::op::FullyConnected>(squeeze, scale, no_bias); | ||
| auto unsqueeze_const = ov::op::v0::Constant::create(ov::element::i64, ov::Shape{4}, {1, 1, -1, 32}); | ||
| auto unsqueeze = std::make_shared<ov::op::v1::Reshape>(fc, unsqueeze_const, false); | ||
|
|
||
| model_ref = std::make_shared<ov::Model>(ov::OutputVector{unsqueeze}, ov::ParameterVector{input1}); | ||
| } | ||
| } | ||
|
|
||
| // Incorrect input size, transformation should not trigger | ||
| TEST_F(TransformationTestsF, ReduceFCDimensions2) { | ||
| { | ||
| auto input1 = std::make_shared<ov::op::v0::Parameter>(ov::element::f32, ov::PartialShape{1, 4, -1, 16}); | ||
| auto weights_const = ov::op::v0::Constant::create(ov::element::u8, ov::Shape{32, 16}, {1}); | ||
| auto convert = std::make_shared<ov::op::v0::Convert>(weights_const, ov::element::f32); | ||
| auto scale_const = ov::op::v0::Constant::create(ov::element::f32, ov::Shape{32, 1}, {1}); | ||
| auto scale = std::make_shared<ov::op::v1::Multiply>(convert, scale_const); | ||
| auto no_bias = std::make_shared<ov::intel_gpu::op::Placeholder>(); | ||
| auto fc = std::make_shared<ov::intel_gpu::op::FullyConnected>(input1, scale, no_bias); | ||
|
|
||
| model = std::make_shared<ov::Model>(ov::OutputVector{fc}, ov::ParameterVector{input1}); | ||
| manager.register_pass<ReduceFCDimensions>(); | ||
| } | ||
| { | ||
| model_ref = model->clone(); | ||
| } | ||
| } | ||
|
|
||
| // Bias present, transformation should not trigger | ||
| TEST_F(TransformationTestsF, ReduceFCDimensions3) { | ||
| { | ||
| auto input1 = std::make_shared<ov::op::v0::Parameter>(ov::element::f32, ov::PartialShape{1, 1, -1, 16}); | ||
| auto weights_const = ov::op::v0::Constant::create(ov::element::u8, ov::Shape{32, 16}, {1}); | ||
| auto convert = std::make_shared<ov::op::v0::Convert>(weights_const, ov::element::f32); | ||
| auto scale_const = ov::op::v0::Constant::create(ov::element::f32, ov::Shape{32, 1}, {1}); | ||
| auto scale = std::make_shared<ov::op::v1::Multiply>(convert, scale_const); | ||
| auto bias = ov::op::v0::Constant::create(ov::element::f32, ov::Shape{1, 1, 1, 32}, {1.0}); | ||
| auto fc = std::make_shared<ov::intel_gpu::op::FullyConnected>(input1, scale, bias); | ||
|
|
||
| model = std::make_shared<ov::Model>(ov::OutputVector{fc}, ov::ParameterVector{input1}); | ||
| manager.register_pass<ReduceFCDimensions>(); | ||
| } | ||
| { | ||
| model_ref = model->clone(); | ||
| } | ||
| } | ||
|
|
||
| // 3D weight, transformation should not trigger | ||
| TEST_F(TransformationTestsF, ReduceFCDimensions4) { | ||
| { | ||
| auto input1 = std::make_shared<ov::op::v0::Parameter>(ov::element::f32, ov::PartialShape{1, 1, -1, 16}); | ||
| auto weights_const = ov::op::v0::Constant::create(ov::element::u8, ov::Shape{4, 32, 16}, {1}); | ||
| auto convert = std::make_shared<ov::op::v0::Convert>(weights_const, ov::element::f32); | ||
| auto scale_const = ov::op::v0::Constant::create(ov::element::f32, ov::Shape{4, 32, 1}, {1}); | ||
| auto scale = std::make_shared<ov::op::v1::Multiply>(convert, scale_const); | ||
| auto no_bias = std::make_shared<ov::intel_gpu::op::Placeholder>(); | ||
| auto fc = std::make_shared<ov::intel_gpu::op::FullyConnected>(input1, scale, no_bias); | ||
|
|
||
| model = std::make_shared<ov::Model>(ov::OutputVector{fc}, ov::ParameterVector{input1}); | ||
| manager.register_pass<ReduceFCDimensions>(); | ||
| } | ||
| { | ||
| model_ref = model->clone(); | ||
| } | ||
| } | ||
|
|
||
| // Dynamic result dim, transformation should not trigger | ||
| TEST_F(TransformationTestsF, ReduceFCDimensions5) { | ||
| { | ||
| auto input1 = std::make_shared<ov::op::v0::Parameter>(ov::element::f32, ov::PartialShape{1, 1, -1, 16}); | ||
| auto weights_param = std::make_shared<ov::op::v0::Parameter>(ov::element::u8, ov::PartialShape{-1, 16}); | ||
| auto convert = std::make_shared<ov::op::v0::Convert>(weights_param, ov::element::f32); | ||
| auto scale_param = std::make_shared<ov::op::v0::Parameter>(ov::element::f32, ov::PartialShape{-1, 1}); | ||
| auto scale = std::make_shared<ov::op::v1::Multiply>(convert, scale_param); | ||
| auto no_bias = std::make_shared<ov::intel_gpu::op::Placeholder>(); | ||
| auto fc = std::make_shared<ov::intel_gpu::op::FullyConnected>(input1, scale, no_bias); | ||
|
|
||
| model = std::make_shared<ov::Model>(ov::OutputVector{fc}, ov::ParameterVector{input1, weights_param, scale_param}); | ||
| manager.register_pass<ReduceFCDimensions>(); | ||
| } | ||
| { | ||
| model_ref = model->clone(); | ||
| } | ||
| } | ||
|
|
||
| // Dynamic inner dim, transformation should not trigger | ||
| TEST_F(TransformationTestsF, ReduceFCDimensions6) { | ||
| { | ||
| auto input1 = std::make_shared<ov::op::v0::Parameter>(ov::element::f32, ov::PartialShape{1, 1, 10, -1}); | ||
| auto weights_param = std::make_shared<ov::op::v0::Parameter>(ov::element::u8, ov::PartialShape{32, -1}); | ||
| auto convert = std::make_shared<ov::op::v0::Convert>(weights_param, ov::element::f32); | ||
| auto scale_const = ov::op::v0::Constant::create(ov::element::f32, ov::Shape{32, 1}, {1}); | ||
| auto scale = std::make_shared<ov::op::v1::Multiply>(convert, scale_const); | ||
| auto no_bias = std::make_shared<ov::intel_gpu::op::Placeholder>(); | ||
| auto fc = std::make_shared<ov::intel_gpu::op::FullyConnected>(input1, scale, no_bias); | ||
|
|
||
| model = std::make_shared<ov::Model>(ov::OutputVector{fc}, ov::ParameterVector{input1, weights_param}); | ||
| manager.register_pass<ReduceFCDimensions>(); | ||
| } | ||
| { | ||
| model_ref = model->clone(); | ||
| } | ||
| } | ||
|
|
||
| } // namespace intel_gpu | ||
| } // namespace test | ||
| } // namespace ov |
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.
Judging by the comment, this change is only useful for
non-IMMADdevices, does it make sense in thesupports_immadcase?If not, then
transformations_pipeline.cppcontains information about device, and transformation may be disabled in this caseThere 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.
Added the check.