-
Notifications
You must be signed in to change notification settings - Fork 2.8k
[LPT] QDQ stripping #32266
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
v-Golubev
wants to merge
27
commits into
openvinotoolkit:master
Choose a base branch
from
v-Golubev:vg/lpt/qdq_stripping
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.
+375
−72
Open
[LPT] QDQ stripping #32266
Changes from all commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
bbe32a5
Adapt ConvertQuantizeDequantize for reusage in QDQStripping
v-Golubev 1955d55
QDQStripping initial implementation
v-Golubev 17d77ab
Finalize first QDQ Stripping implementation
v-Golubev e4f6d6a
Clang format
v-Golubev 1640fdb
set replace_with_clamp to true
v-Golubev d6130ae
Avoid main LPT pipeline in case of non-u8 activations quantization
v-Golubev 5824c91
WIP: some fixes + debug info
v-Golubev b4c320a
Logging extended
v-Golubev e771761
added graphs serialization
v-Golubev 8d46d49
Further debug logging extending
v-Golubev dbba54b
ConvertQuantizeDequantize: ignore consumers_count check
v-Golubev 95de481
Cleanup
v-Golubev 8faa050
Introduced REPLACE_QDQ_WITH_CLAMP env variable
v-Golubev b181306
Added QDQStrippingTest
v-Golubev 0f7aa0a
Warning fixed
v-Golubev cbc6318
Compilation error fix
v-Golubev 0e403c2
Transformation pipeline minor corrections
v-Golubev 5211740
QDQStrippingTest extending
v-Golubev 0bb9870
Added clarification comment to ConvertQuantizeDequantize
v-Golubev 6a6d9b8
Corrected LPT tests instances
v-Golubev 5032aea
codestyle
v-Golubev 68a9e3b
[GPU] Keep old behavior for non-i16 models
v-Golubev 060e17d
Review comments applied
v-Golubev 4736a76
Test data generation is adapted to the case when clamp is not inserted
v-Golubev 5b25b47
Fixed undef
v-Golubev fd48bf4
Remove unnecessary log message
maxnick 2471227
Isolate QDQ stripping changes to make sure that the feature doesn't a…
v-Golubev 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
31 changes: 31 additions & 0 deletions
31
src/common/low_precision_transformations/include/low_precision/qdq_stripping.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,31 @@ | ||
| // Copyright (C) 2018-2025 Intel Corporation | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| // | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <memory> | ||
| #include <set> | ||
|
|
||
| #include "lpt_visibility.hpp" | ||
| #include "openvino/pass/matcher_pass.hpp" | ||
| #include "quantization_details.hpp" | ||
|
|
||
| namespace ov { | ||
| namespace pass { | ||
| namespace low_precision { | ||
|
|
||
| /** | ||
| * @ingroup ov_transformation_common_api | ||
| * @brief FQStrippingTransformation strips FakeQuantize operations with specified levels | ||
| * by replacing them with Clamp operations. | ||
| */ | ||
| class LP_TRANSFORMATIONS_API FQStrippingTransformation : public ov::pass::MatcherPass { | ||
| public: | ||
| OPENVINO_RTTI("FQStrippingTransformation", "0", MatcherPass); | ||
| FQStrippingTransformation(const std::set<size_t>& levels_to_strip, bool replace_with_clamp); | ||
| }; | ||
|
|
||
| } // namespace low_precision | ||
| } // namespace pass | ||
| } // namespace ov |
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
92 changes: 92 additions & 0 deletions
92
src/common/low_precision_transformations/src/qdq_stripping.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,92 @@ | ||
| // Copyright (C) 2018-2025 Intel Corporation | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| // | ||
|
|
||
| #include "low_precision/qdq_stripping.hpp" | ||
|
|
||
| #include <memory> | ||
|
|
||
| #include "itt.hpp" | ||
| #include "low_precision/common/ie_lpt_exception.hpp" | ||
| #include "low_precision/lpt_itt.hpp" | ||
| #include "low_precision/network_helper.hpp" | ||
| #include "openvino/core/except.hpp" | ||
| #include "openvino/core/type.hpp" | ||
| #include "openvino/op/clamp.hpp" | ||
| #include "openvino/op/constant.hpp" | ||
| #include "openvino/op/equal.hpp" | ||
| #include "openvino/op/fake_quantize.hpp" | ||
| #include "openvino/pass/pattern/op/wrap_type.hpp" | ||
| #include "openvino/util/log.hpp" | ||
| #include "transformations/utils/utils.hpp" | ||
|
|
||
| namespace ov { | ||
| namespace pass { | ||
| namespace low_precision { | ||
|
|
||
| FQStrippingTransformation::FQStrippingTransformation(const std::set<size_t>& levels_to_strip, bool replace_with_clamp) { | ||
| MATCHER_SCOPE(FQStrippingTransformation); | ||
| auto is_scalar = [](const Output<Node>& output) -> bool { | ||
| return ov::shape_size(output.get_shape()) == 1; | ||
| }; | ||
| auto input_low_m = pattern::wrap_type<ov::op::v0::Constant>(is_scalar); | ||
| auto input_high_m = pattern::wrap_type<ov::op::v0::Constant>(is_scalar); | ||
| auto output_low_m = pattern::wrap_type<ov::op::v0::Constant>(is_scalar); | ||
| auto output_high_m = pattern::wrap_type<ov::op::v0::Constant>(is_scalar); | ||
| auto fq_m = pattern::wrap_type<ov::op::v0::FakeQuantize>( | ||
| {pattern::any_input(), input_low_m, input_high_m, output_low_m, output_high_m}); | ||
|
|
||
| ov::graph_rewrite_callback callback = [OV_CAPTURE_CPY_AND_THIS](pattern::Matcher& m) { | ||
| const auto& pattern_map = m.get_pattern_value_map(); | ||
| auto node = ov::as_type_ptr<ov::op::v0::FakeQuantize>(pattern_map.at(fq_m).get_node_shared_ptr()); | ||
| if (!node) { | ||
| return false; | ||
| } | ||
|
|
||
| const size_t levels = node->get_levels(); | ||
| if (!levels_to_strip.count(levels)) { | ||
| return false; | ||
| } | ||
|
|
||
| auto input = node->get_input_node_shared_ptr(0); | ||
| auto input_low = ov::as_type_ptr<ov::op::v0::Constant>(pattern_map.at(input_low_m).get_node_shared_ptr()); | ||
| auto input_high = ov::as_type_ptr<ov::op::v0::Constant>(pattern_map.at(input_high_m).get_node_shared_ptr()); | ||
| auto output_low = ov::as_type_ptr<ov::op::v0::Constant>(pattern_map.at(output_low_m).get_node_shared_ptr()); | ||
| auto output_high = ov::as_type_ptr<ov::op::v0::Constant>(pattern_map.at(output_high_m).get_node_shared_ptr()); | ||
|
|
||
| if (!input_low || !input_high || !output_low || !output_high) { | ||
| return false; | ||
| } | ||
| auto constants_are_equal = [](const std::shared_ptr<ov::op::v0::Constant>& lhs, | ||
| const std::shared_ptr<ov::op::v0::Constant>& rhs) -> bool { | ||
| auto equal = | ||
| ov::as_type_ptr<ov::op::v0::Constant>(ov::op::util::make_try_fold<ov::op::v1::Equal>(lhs, rhs)); | ||
| OPENVINO_ASSERT(equal && ov::shape_size(equal->get_shape()) == 1, | ||
| "constants_are_equal expects scalar constant as a comparison result"); | ||
| return equal->get_vector<bool>()[0]; | ||
| }; | ||
|
|
||
| if (!constants_are_equal(input_low, output_low) || !constants_are_equal(input_high, output_high)) { | ||
| return false; | ||
| } | ||
|
|
||
| bool res = false; | ||
| if (replace_with_clamp) { | ||
| auto clamp = std::make_shared<ov::op::v0::Clamp>(input->output(0), | ||
| output_low->cast_vector<double>()[0], | ||
| output_high->cast_vector<double>()[0]); | ||
| res = replace_node_update_name(node, clamp); | ||
| } else { | ||
| res = replace_output_update_name(node->output(0), node->input_value(0)); | ||
| } | ||
| OPENVINO_ASSERT(res, "FQ stripping failed"); | ||
| return res; | ||
| }; | ||
|
|
||
| auto m = std::make_shared<ov::pass::pattern::Matcher>(fq_m, matcher_name); | ||
| this->register_matcher(m, callback); | ||
| } | ||
|
|
||
| } // namespace low_precision | ||
| } // namespace pass | ||
| } // namespace ov |
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
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.