-
Notifications
You must be signed in to change notification settings - Fork 3.3k
[Transformations] EliminateSequentialFakeQuantize #36075
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
Merged
Merged
Changes from all commits
Commits
Show all changes
36 commits
Select commit
Hold shift + click to select a range
136173d
init
mryzhov 55e072f
merge FQs
mryzhov b65caba
fixed tests
mryzhov f904754
Merge branch 'master' into fqs_remove
mryzhov dc88a91
replace transformation into separate file
mryzhov 989bed7
move tests
mryzhov 47ba7bd
clang fixes
mryzhov 1ee9f2c
fixed old test
mryzhov 36fc099
Merge branch 'master' into fqs_remove
mryzhov f94a673
Merge branch 'master' into fqs_remove
mryzhov c9f8d7e
Exclude fq_fuse_dynamic_shapes changes
mryzhov c233abd
Exclude nop_elimination changes
mryzhov 7372c89
Remove eliminate_sequential_fake_quantize pass
mryzhov b03e5ef
aligned the naming
mryzhov feed7e0
Rename FQEliminateSequential to FakeQuantizeEliminateSequential and a…
mryzhov ea02c5b
optimization
mryzhov 7aeba44
refactoring
mryzhov 1b49164
Reshaping support
mryzhov 66fcadf
reduce supported cases
mryzhov 92051f8
clang fixes
mryzhov 41b4b7e
Merge branch 'master' into fqs_remove
mryzhov d7ababd
Merge branch 'master' into fqs_remove
mryzhov 48f6e67
fixed tests
mryzhov 43bca8a
per_channel_fake_quantize test
mryzhov 739cc33
switch to modelpass
mryzhov e849ba2
Merge branch 'master' into fqs_remove
mryzhov e95f5d6
share have_same_fake_quantize_params()
mryzhov 9e3431c
excluded cases with different FQ shape constants
mryzhov 2136bc6
Merge branch 'master' into fqs_remove
mryzhov f105696
Potential fix for pull request finding
mryzhov 5ab9dec
Update src/common/transformations/include/transformations/common_opti…
mryzhov 6d43d85
use is_type_any_of
mryzhov d592883
Merge branch 'master' into fqs_remove
mryzhov f686324
Potential fix for pull request finding
mryzhov ef3f380
Merge branch 'master' into fqs_remove
mryzhov 8c18ea8
Merge branch 'master' into fqs_remove
mryzhov 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
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
81 changes: 81 additions & 0 deletions
81
.../transformations/include/transformations/common_optimizations/fq_eliminate_sequential.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,81 @@ | ||
| // Copyright (C) 2018-2026 Intel Corporation | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| // | ||
|
|
||
| #pragma once | ||
|
|
||
| #include "openvino/pass/pass.hpp" | ||
| #include "transformations_visibility.hpp" | ||
|
|
||
| namespace ov { | ||
| namespace pass { | ||
|
|
||
| class TRANSFORMATIONS_API FakeQuantizeEliminateSequential; | ||
|
|
||
| } // namespace pass | ||
| } // namespace ov | ||
|
|
||
| /** | ||
| * @ingroup ov_transformation_common_api | ||
| * @brief FakeQuantizeEliminateSequential removes a redundant second FakeQuantize from a sequential | ||
| * pair (FQ1 -> FQ2). The notation below is FQ(in_low, in_high, out_low, out_high, levels). | ||
| * | ||
| * FQ1 and FQ2 may be separated by one or several value-preserving ops. | ||
| * A matched FakeQuantize is applied element-wise (whether per-tensor or per-channel), so it | ||
| * commutes with such ops and the folding stays valid through the chain. | ||
| * | ||
| * FQ2 is removed (FQ1 kept) when it is identical to FQ1, i.e. it has the same levels, auto broadcast, | ||
| * and range constants (input_low/input_high/output_low/output_high). In that case FQ2 only re-applies | ||
| * the quantization already produced by FQ1 and is redundant. | ||
| * | ||
| * Before: | ||
| * ┌─────────┐ | ||
| * │ Data │ | ||
| * └────┬────┘ | ||
| * │ | ||
| * ┌────┴────┐ | ||
| * │ FQ1 │ | ||
| * └────┬────┘ | ||
| * │ | ||
| * ┌──────┴───────┐ | ||
| * │ Reshape / │ (optional, zero or more value-preserving ops) | ||
| * │ Transpose / │ | ||
| * │ Squeeze / │ | ||
| * │ Unsqueeze │ | ||
| * └──────┬───────┘ | ||
| * │ | ||
| * ┌────┴────┐ | ||
| * │ FQ2 │ (identical params to FQ1) | ||
| * └────┬────┘ | ||
| * │ | ||
| * ┌────┴────┐ | ||
| * │Consumer │ | ||
| * └─────────┘ | ||
| * | ||
| * After: | ||
| * ┌─────────┐ | ||
| * │ Data │ | ||
| * └────┬────┘ | ||
| * │ | ||
| * ┌────┴────┐ | ||
| * │ FQ1 │ | ||
| * └────┬────┘ | ||
| * │ | ||
| * ┌──────┴───────┐ | ||
| * │ Reshape / │ (optional, preserved) | ||
| * │ Transpose / │ | ||
| * │ Squeeze / │ | ||
| * │ Unsqueeze │ | ||
| * └──────┬───────┘ | ||
| * │ | ||
| * ┌────┴────┐ | ||
| * │Consumer │ | ||
| * └─────────┘ | ||
| * | ||
| * For example: FQ1(-1, 1, -1, 1, 256) -> FQ2(-1, 1, -1, 1, 256) => FQ1(-1, 1, -1, 1, 256) | ||
| */ | ||
| class ov::pass::FakeQuantizeEliminateSequential : public ov::pass::ModelPass { | ||
| public: | ||
| OPENVINO_MODEL_PASS_RTTI("FakeQuantizeEliminateSequential"); | ||
| bool run_on_model(const std::shared_ptr<ov::Model>& model) override; | ||
| }; |
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
|
mryzhov marked this conversation as resolved.
|
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
75 changes: 75 additions & 0 deletions
75
...mmon/transformations/src/transformations/common_optimizations/fq_eliminate_sequential.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,75 @@ | ||
| // Copyright (C) 2018-2026 Intel Corporation | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| // | ||
|
|
||
| #include "transformations/common_optimizations/fq_eliminate_sequential.hpp" | ||
|
|
||
| #include <memory> | ||
| #include <unordered_set> | ||
| #include <vector> | ||
|
|
||
| #include "itt.hpp" | ||
| #include "openvino/core/graph_util.hpp" | ||
| #include "openvino/core/model.hpp" | ||
| #include "openvino/op/fake_quantize.hpp" | ||
| #include "openvino/op/reshape.hpp" | ||
| #include "openvino/op/transpose.hpp" | ||
| #include "openvino/op/unsqueeze.hpp" | ||
| #include "openvino/op/util/op_types.hpp" | ||
| #include "openvino/op/util/squeeze_base.hpp" | ||
| #include "transformations/utils/utils.hpp" | ||
|
|
||
| namespace v0 = ov::op::v0; | ||
| namespace v1 = ov::op::v1; | ||
| namespace op_util = ov::op::util; | ||
|
|
||
| namespace ov::pass { | ||
|
|
||
| namespace { | ||
|
|
||
| bool is_value_preserving(const std::shared_ptr<ov::Node>& node) { | ||
| return ov::is_type_any_of<v1::Reshape, v1::Transpose, op_util::SqueezeBase, v0::Unsqueeze>(node); | ||
| } | ||
|
|
||
| } // namespace | ||
|
|
||
| bool FakeQuantizeEliminateSequential::run_on_model(const std::shared_ptr<ov::Model>& model) { | ||
| RUN_ON_MODEL_SCOPE(FakeQuantizeEliminateSequential); | ||
|
|
||
| bool eliminated = false; | ||
| for (const auto& op : model->get_ordered_ops()) { | ||
| auto fq1 = ov::as_type_ptr<v0::FakeQuantize>(op); | ||
| if (!fq1) { | ||
| continue; | ||
| } | ||
|
|
||
| // Walk forward from FQ1 following its consumers and collect the redundant FakeQuantizes. | ||
| // Traversal continues only through FQ1, value-preserving ops, and redundant FakeQuantizes, so | ||
| // branches into several consumers are handled as well. visit_path_forward queries a node's | ||
| // consumers right after the visitor runs, so the redundant FakeQuantizes are only collected | ||
| // here and detached afterwards to avoid dereferencing a freed node. | ||
| std::vector<std::shared_ptr<v0::FakeQuantize>> redundant_fqs; | ||
| std::unordered_set<ov::Node*> visited; | ||
| auto skip_node = [&](ov::Node* node) { | ||
| auto shared_node = node->shared_from_this(); | ||
| if (shared_node == fq1 || is_value_preserving(shared_node)) { | ||
| return false; | ||
| } | ||
| return !op_util::have_same_fake_quantize_params(fq1, ov::as_type_ptr<v0::FakeQuantize>(shared_node)); | ||
| }; | ||
| auto collect_redundant_fq = [&](ov::Node* node) { | ||
| if (auto fq2 = ov::as_type_ptr<v0::FakeQuantize>(node->shared_from_this()); fq2 && fq2 != fq1) { | ||
| redundant_fqs.push_back(fq2); | ||
| } | ||
| }; | ||
| op_util::visit_path_forward(fq1.get(), visited, collect_redundant_fq, skip_node); | ||
|
|
||
| for (const auto& fq2 : redundant_fqs) { | ||
| eliminated = replace_output_update_name(fq2->output(0), fq2->input_value(0)) || eliminated; | ||
| } | ||
|
mryzhov marked this conversation as resolved.
|
||
| } | ||
|
mryzhov marked this conversation as resolved.
|
||
|
|
||
| return eliminated; | ||
| } | ||
|
|
||
| } // namespace ov::pass | ||
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.