OpenVINO™ is an open source toolkit for optimizing and deploying AI inference
C++ aicomputer-visiondeep-learningdeploy-aidiffusion-modelsgenerative-aigood-first-issueinferencellm-inferencenatural-language-processingnlpopenvinooptimize-aiperformance-boostrecommendation-systemspeech-recognitionstable-diffusiontransformersyolo
[Transformations] SharedOpOptimization and SymbolicOptimization update (#27243)
Pawel Raasz884ac4a129ac
diff --git a/src/common/transformations/src/transformations/common_optimizations/shared_ops_optimization.cpp b/src/common/transformations/src/transformations/common_optimizations/shared_ops_optimization.cppindex 226093143e6..188d0b07684 100644--- a/src/common/transformations/src/transformations/common_optimizations/shared_ops_optimization.cpp+++ b/src/common/transformations/src/transformations/common_optimizations/shared_ops_optimization.cpp@@ -103,8 +103,11 @@ bool nodes_are_equal(const std::shared_ptr<Node>& lhs, const std::shared_ptr<Nodbool shared_node_optimization(const shared_ptr<Model>& model) {bool rewritten = false;-- for (const auto& op : model->get_ordered_ops()) {+ std::unordered_map<std::shared_ptr<ov::Node>, size_t> index_map;+ const auto& order = model->get_ordered_ops();+ for (size_t i = 0; i < order.size(); ++i)+ index_map[order[i]] = i;+ for (const auto& op : order) {// Recursively apply transformation for sub-graph based operationsif (auto multi_subgraph_op = dynamic_pointer_cast<op::util::MultiSubGraphOp>(op)) {for (const auto& sub_graph : multi_subgraph_op->get_functions()) {@@ -124,6 +127,13 @@ bool shared_node_optimization(const shared_ptr<Model>& model) {auto& shared_nodes = item.second;if (shared_nodes.size() < 2)continue;+ // sort shared_nodes so that root would be the earliest in the topological order+ // it is critical for continuous application of this optimization+ std::sort(shared_nodes.begin(),+ shared_nodes.end(),+ [&index_map](const std::shared_ptr<ov::Node>& a, const std::shared_ptr<ov::Node>& b) {+ return index_map[a] < index_map[b];+ });std::vector<bool> visited_nodes(shared_nodes.size(), false);for (size_t i = 0; i < visited_nodes.size(); ++i) {diff --git a/src/common/transformations/src/transformations/symbolic_transformations/symbol_optimization.cpp b/src/common/transformations/src/transformations/symbolic_transformations/symbol_optimization.cppindex 27790904f43..55f0794e0ee 100644--- a/src/common/transformations/src/transformations/symbolic_transformations/symbol_optimization.cpp+++ b/src/common/transformations/src/transformations/symbolic_transformations/symbol_optimization.cpp@@ -7,6 +7,7 @@#include "itt.hpp"#include "openvino/core/bound_evaluation_util.hpp"#include "openvino/core/rt_info.hpp"+#include "openvino/core/tensor_util.hpp"#include "openvino/core/validation_util.hpp"#include "openvino/op/add.hpp"#include "openvino/op/concat.hpp"@@ -354,12 +355,85 @@ void save_shape_sources(const std::shared_ptr<ov::Node>& op, STS_map& symbol_sha}}}++struct OutputValue {+ std::vector<ov::Any> value;++ bool operator==(const OutputValue& other) const {+ return value == other.value;+ }++ bool operator<(const OutputValue& other) const {+ return std::lexicographical_compare(+ std::begin(value),+ std::end(value),+ std::begin(other.value),+ std::end(other.value),+ [](const ov::Any& a, const ov::Any& b) {+ // each element is either a symbol or an integer. in case they differ any integer is less than a symbol.+ if (a.is<std::shared_ptr<ov::Symbol>>() && b.is<std::shared_ptr<ov::Symbol>>())+ return a.as<std::shared_ptr<ov::Symbol>>() < b.as<std::shared_ptr<ov::Symbol>>();+ if (a.is<int64_t>() && b.is<int64_t>())+ return a.as<int64_t>() < b.as<int64_t>();+ return a.is<int64_t>();+ });+ }++ static ov::optional<OutputValue> make(const ov::Output<ov::Node>& output) {+ auto symbols = output.get_tensor().get_value_symbol();+ if (symbols.empty() || symbols.size() == 1)+ return {};++ const auto& lower_value = ov::util::to_vector<int64_t>(output.get_tensor().get_lower_value());+ const auto& upper_value = ov::util::to_vector<int64_t>(output.get_tensor().get_upper_value());+ const auto& et = output.get_element_type();+ bool use_values = lower_value && upper_value && (et == ov::element::i64 || et == ov::element::i32);++ std::vector<ov::Any> symbols_as_any(symbols.size(), nullptr);+ for (size_t i = 0; i < symbols_as_any.size(); ++i) {+ if (use_values && lower_value->at(i) == upper_value->at(i))+ symbols_as_any[i] = lower_value->at(i);+ else if (symbols.at(i) != nullptr)+ symbols_as_any[i] = ov::symbol::ancestor_of(symbols.at(i));+ else+ return {};+ }+ return {OutputValue{std::move(symbols_as_any)}};+ }+};++void save_and_update_value_sources(const std::shared_ptr<ov::Node>& op,+ std::map<OutputValue, ov::Output<ov::Node>>& multi_symbol_source) {+ for (auto& output : op->outputs()) {+ if (output.get_tensor().get_value_symbol().size() < 2)+ continue; // singular values are handled by optimize_value_usage helper++ if (auto result = OutputValue::make(output)) {+ if (multi_symbol_source.count(*result)) {+ auto alternative_source = multi_symbol_source[*result];+ if (output.get_element_type() != alternative_source.get_element_type()) {+ auto convert = std::make_shared<ov::op::v0::Convert>(alternative_source, output.get_element_type());+ ov::copy_runtime_info(output.get_node_shared_ptr(), convert);+ alternative_source = convert->output(0);+ }+ if (output.get_partial_shape().is_dynamic() ||+ output.get_partial_shape() != alternative_source.get_partial_shape())+ continue;+ output.replace(alternative_source);+ } else {+ multi_symbol_source[*result] = output;+ }+ }+ }+}+} // namespacebool ov::pass::OptimizeSymbolsUsedAsValues::run_on_model(const std::shared_ptr<ov::Model>& m) {RUN_ON_FUNCTION_SCOPE(OptimizeSymbolsUsedAsValues);STS_map symbol_shape_source;STS_map symbol_value_source;+ std::map<OutputValue, ov::Output<ov::Node>> multi_symbol_source;for (const auto& op : topological_order(m)) {// Result has output port which has shared (during validate_and_infer_type) tensor with input port.// Transformations may replace input of Result. After replacement and before Result::validate_and_infer_type --@@ -375,6 +449,7 @@ bool ov::pass::OptimizeSymbolsUsedAsValues::run_on_model(const std::shared_ptr<ofor (auto& output : op->outputs())optimize_value_usage(output, symbol_shape_source, symbol_value_source);save_shape_sources(op, symbol_shape_source);+ save_and_update_value_sources(op, multi_symbol_source);}return true;}diff --git a/src/common/transformations/tests/common_optimizations/shared_ops_optimization.cpp b/src/common/transformations/tests/common_optimizations/shared_ops_optimization.cppindex 3f717726b75..dcba44313c8 100644--- a/src/common/transformations/tests/common_optimizations/shared_ops_optimization.cpp+++ b/src/common/transformations/tests/common_optimizations/shared_ops_optimization.cpp@@ -6,6 +6,7 @@#include <gtest/gtest.h>#include "common_test_utils/ov_test_utils.hpp"+#include "openvino/op/add.hpp"#include "openvino/op/broadcast.hpp"#include "openvino/op/ceiling.hpp"#include "openvino/op/concat.hpp"@@ -716,3 +717,57 @@ TEST_F(SharedTransformationTestsF, SharedMaxPool) {model_ref = std::make_shared<ov::Model>(OutputVector{concat}, ParameterVector{data});}}++TEST_F(SharedTransformationTestsF, TopologicalOrder) {+ {+ auto data = std::make_shared<v0::Parameter>(element::f32, PartialShape{-1, -1, -1, -1});++ auto shape_of = std::make_shared<v3::ShapeOf>(data);++ auto gather_0 = std::make_shared<v8::Gather>(shape_of,+ v0::Constant::create(element::i32, {1}, {0}),+ v0::Constant::create(element::i32, {}, {0}));++ auto gather_1 = std::make_shared<v8::Gather>(shape_of,+ v0::Constant::create(element::i32, {1}, {0}),+ v0::Constant::create(element::i32, {}, {0}));++ auto gather_2 = std::make_shared<v8::Gather>(shape_of,+ v0::Constant::create(element::i32, {1}, {0}),+ v0::Constant::create(element::i32, {}, {0}));++ auto add_0 = std::make_shared<v1::Add>(gather_0, gather_0);+ auto add_1 = std::make_shared<v1::Add>(gather_1, gather_1);+ auto add_2 = std::make_shared<v1::Add>(gather_2, gather_2);++ auto concat_0 =+ std::make_shared<v0::Concat>(OutputVector{gather_0, add_0, v0::Constant::create(element::i64, {1}, {0})},+ 0);+ auto concat_1 =+ std::make_shared<v0::Concat>(OutputVector{gather_1, add_1, v0::Constant::create(element::i64, {1}, {0})},+ 0);+ auto concat_2 =+ std::make_shared<v0::Concat>(OutputVector{gather_2, add_2, v0::Constant::create(element::i64, {1}, {0})},+ 0);++ auto concat = std::make_shared<v0::Concat>(OutputVector{concat_0, concat_1}, 0);+ auto output = std::make_shared<v0::Concat>(OutputVector{concat, concat_2}, 0);++ model = std::make_shared<ov::Model>(OutputVector{output}, ParameterVector{data});+ manager.register_pass<ov::pass::SharedOpOptimization>();+ }+ {+ auto data = std::make_shared<v0::Parameter>(element::f32, PartialShape{-1, -1, -1, -1});+ auto shape_of = std::make_shared<v3::ShapeOf>(data);+ auto gather_0 = std::make_shared<v8::Gather>(shape_of,+ v0::Constant::create(element::i32, {1}, {0}),+ v0::Constant::create(element::i32, {}, {0}));+ auto add_0 = std::make_shared<v1::Add>(gather_0, gather_0);+ auto concat_0 =+ std::make_shared<v0::Concat>(OutputVector{gather_0, add_0, v0::Constant::create(element::i64, {1}, {0})},+ 0);+ auto concat = std::make_shared<v0::Concat>(OutputVector{concat_0, concat_0}, 0);+ auto output = std::make_shared<v0::Concat>(OutputVector{concat, concat_0}, 0);+ model_ref = std::make_shared<ov::Model>(OutputVector{output}, ParameterVector{data});+ }+}diff --git a/src/common/transformations/tests/symbolic_transformations/symbol_optimization.cpp b/src/common/transformations/tests/symbolic_transformations/symbol_optimization.cppindex 590290fb19d..e4653ec084b 100644--- a/src/common/transformations/tests/symbolic_transformations/symbol_optimization.cpp+++ b/src/common/transformations/tests/symbolic_transformations/symbol_optimization.cpp@@ -9,11 +9,15 @@#include "common_test_utils/ov_test_utils.hpp"#include "openvino/op/add.hpp"#include "openvino/op/concat.hpp"+#include "openvino/op/convert.hpp"#include "openvino/op/gather.hpp"+#include "openvino/op/range.hpp"#include "openvino/op/reshape.hpp"#include "openvino/op/shape_of.hpp"+#include "openvino/op/squeeze.hpp"#include "openvino/pass/manager.hpp"#include "openvino/pass/visualize_tree.hpp"+#include "transformations/common_optimizations/shared_ops_optimization.hpp"#include "transformations/symbolic_transformations/symbolic_optimizations.hpp"#include "transformations/symbolic_transformations/utils.hpp"@@ -95,3 +99,152 @@ TEST_F(TransformationTestsF, ApplySymbolEquivalence_Concat_Values) {model_ref = make_shared<Model>(NodeVector{reshape}, ParameterVector{input_2, input_1});}}++Output<Node> get_dim_by_idx(const Output<Node>& source, const int64_t& idx, element::Type type = element::i64) {+ auto shape = make_shared<v3::ShapeOf>(source, type);+ auto gather = make_shared<v1::Gather>(shape,+ v0::Constant::create(element::i64, {}, {idx}),+ v0::Constant::create(element::i64, {}, {0}));+ return gather->output(0);+}++Output<Node> get_dim_by_idx(const Output<Node>& source,+ initializer_list<int64_t> idx,+ element::Type type = element::i64) {+ auto shape = make_shared<v3::ShapeOf>(source, type);+ auto gather = make_shared<v8::Gather>(shape,+ v0::Constant::create(element::i64, {idx.size()}, idx),+ v0::Constant::create(element::i64, {}, {0}));+ return gather->output(0);+}++TEST_F(TransformationTestsF, ValueOptimizationSingleValue) {+ {+ auto input = make_shared<v0::Parameter>(element::f32, PartialShape::dynamic(4));++ auto dim_0 = get_dim_by_idx(input, {-1}, element::i64);+ auto dim_1 = get_dim_by_idx(input, {3}, element::i32);+ auto dim_2 = get_dim_by_idx(input, -1, element::i32);++ auto reshape_0 = make_shared<v1::Reshape>(+ input,+ make_shared<v0::Concat>(OutputVector{v0::Constant::create(element::i64, {1}, {-1}), dim_0}, 0),+ false);+ auto reshape_1 = make_shared<v1::Reshape>(+ input,+ make_shared<v0::Concat>(OutputVector{v0::Constant::create(element::i32, {1}, {0}), dim_1}, 0),+ false);+ auto range = make_shared<v4::Range>(v0::Constant::create(element::i32, {}, {0}),+ dim_2,+ v0::Constant::create(element::i32, {}, {1}),+ element::i32);++ model = make_shared<Model>(NodeVector{reshape_0, reshape_1, range}, ParameterVector{input});++ manager.set_per_pass_validation(false);+ manager.register_pass<pass::SymbolicPropagation>();+ manager.register_pass<pass::OptimizeSymbolsUsedAsValues>();+ manager.register_pass<pass::SharedOpOptimization>();+ }+ {+ auto input = make_shared<v0::Parameter>(element::f32, PartialShape::dynamic(4));+ auto dim_1 = get_dim_by_idx(input, {3}, element::i32);+ auto dim_0 = std::make_shared<v0::Convert>(dim_1, element::i64);+ auto dim_2 = std::make_shared<v0::Squeeze>(dim_1);+ auto reshape_0 = make_shared<v1::Reshape>(+ input,+ make_shared<v0::Concat>(OutputVector{v0::Constant::create(element::i64, {1}, {-1}), dim_0}, 0),+ false);+ auto reshape_1 = make_shared<v1::Reshape>(+ input,+ make_shared<v0::Concat>(OutputVector{v0::Constant::create(element::i32, {1}, {0}), dim_1}, 0),+ false);+ auto range = make_shared<v4::Range>(v0::Constant::create(element::i32, {}, {0}),+ dim_2,+ v0::Constant::create(element::i32, {}, {1}),+ element::i32);++ model_ref = make_shared<Model>(NodeVector{reshape_0, reshape_1, range}, ParameterVector{input});+ }+ comparator.enable(FunctionsComparator::CmpValues::CONST_VALUES);+}++TEST_F(TransformationTestsF, ValueOptimizationDoubleValue) {+ {+ auto input = make_shared<v0::Parameter>(element::f32, PartialShape::dynamic(4));++ auto dim_0 = get_dim_by_idx(input, {-1, -2}, element::i64);+ auto dim_1 = get_dim_by_idx(input, {3, 2}, element::i32);++ auto reshape_0 = make_shared<v1::Reshape>(+ input,+ make_shared<v0::Concat>(OutputVector{v0::Constant::create(element::i64, {1}, {-1}), dim_0}, 0),+ false);+ auto reshape_1 = make_shared<v1::Reshape>(+ input,+ make_shared<v0::Concat>(OutputVector{v0::Constant::create(element::i32, {1}, {0}), dim_1}, 0),+ false);++ model = make_shared<Model>(NodeVector{reshape_0, reshape_1}, ParameterVector{input});++ manager.set_per_pass_validation(false);+ manager.register_pass<pass::SymbolicPropagation>();+ manager.register_pass<pass::OptimizeSymbolsUsedAsValues>();+ manager.register_pass<pass::SharedOpOptimization>();+ }+ {+ auto input = make_shared<v0::Parameter>(element::f32, PartialShape::dynamic(4));+ auto dim_0 = get_dim_by_idx(input, {3, 2}, element::i32);+ auto dim_1 = std::make_shared<v0::Convert>(dim_0, element::i64);++ auto reshape_0 = make_shared<v1::Reshape>(+ input,+ make_shared<v0::Concat>(OutputVector{v0::Constant::create(element::i64, {1}, {-1}), dim_1}, 0),+ false);+ auto reshape_1 = make_shared<v1::Reshape>(+ input,+ make_shared<v0::Concat>(OutputVector{v0::Constant::create(element::i32, {1}, {0}), dim_0}, 0),+ false);++ model_ref = make_shared<Model>(NodeVector{reshape_0, reshape_1}, ParameterVector{input});+ }+ comparator.enable(FunctionsComparator::CmpValues::CONST_VALUES);+}++TEST_F(TransformationTestsF, ValueOptimizationSymbolAndValue) {+ {+ auto input = make_shared<v0::Parameter>(element::f32, PartialShape({-1, -1, 4, -1}));++ auto dim_0 = get_dim_by_idx(input, {-1, -2}, element::i64);+ auto dim_1 = get_dim_by_idx(input, {3, 2}, element::i32);++ auto reshape_0 = make_shared<v1::Reshape>(+ input,+ make_shared<v0::Concat>(OutputVector{v0::Constant::create(element::i64, {1}, {-1}), dim_0}, 0),+ false);+ auto reshape_1 = make_shared<v1::Reshape>(+ input,+ make_shared<v0::Concat>(OutputVector{v0::Constant::create(element::i32, {1}, {-1}), dim_1}, 0),+ false);++ model = make_shared<Model>(NodeVector{reshape_0, reshape_1}, ParameterVector{input});++ manager.set_per_pass_validation(false);+ manager.register_pass<pass::SymbolicPropagation>();+ manager.register_pass<pass::OptimizeSymbolsUsedAsValues>();+ manager.register_pass<pass::SharedOpOptimization>();+ }+ {+ auto input = make_shared<v0::Parameter>(element::f32, PartialShape({-1, -1, 4, -1}));+ auto dim_0 = make_shared<v0::Concat>(+ OutputVector{v0::Constant::create(element::i32, {1}, {-1}), get_dim_by_idx(input, {3, 2}, element::i32)},+ 0);+ auto dim_1 = std::make_shared<v0::Convert>(dim_0, element::i64);++ auto reshape_0 = make_shared<v1::Reshape>(input, dim_1, false);+ auto reshape_1 = make_shared<v1::Reshape>(input, dim_0, false);++ model_ref = make_shared<Model>(NodeVector{reshape_0, reshape_1}, ParameterVector{input});+ }+ comparator.enable(FunctionsComparator::CmpValues::CONST_VALUES);+}diff --git a/src/plugins/intel_cpu/src/transformations/cpu_opset/common/pass/ngram_fusion.cpp b/src/plugins/intel_cpu/src/transformations/cpu_opset/common/pass/ngram_fusion.cppindex 3fe96815559..ed1e82c8c29 100644--- a/src/plugins/intel_cpu/src/transformations/cpu_opset/common/pass/ngram_fusion.cpp+++ b/src/plugins/intel_cpu/src/transformations/cpu_opset/common/pass/ngram_fusion.cpp@@ -124,19 +124,32 @@ ov::intel_cpu::NgramFusion::NgramFusion() {: ov::OutputVector{wrap_type<Constant>(), idces_m, wrap_type<Constant>()};auto idces_concat_m = wrap_type<Concat>(idces_concat_inputs);+ // Concat can be replaced by symbolic optimizations as it can find alternative source for this op.+ const auto concat_shape_with_cropped_symbol_m =+ [&cropped_shape_symbol](const ov::Output<ov::Node>& output) -> bool {+ const auto& symbols = output.get_tensor().get_value_symbol();+ return ov::pass::pattern::rank_equals(1)(output) && !symbols.empty() &&+ symbols[0] == cropped_shape_symbol;+ };+// left equal branchauto crop_left_bias_m = wrap_type<Constant>();auto crop_left_cropped_shape_m = std::make_shared<ov::pass::pattern::op::Or>(ov::OutputVector{wrap_type<Add>({cropped_shape_m, crop_left_bias_m}), cropped_shape_m});- auto idxes_crop_left_concat_m = wrap_type<Concat>({crop_left_cropped_shape_m, wrap_type<Constant>()});+ auto idxes_crop_left_concat_m = std::make_shared<ov::pass::pattern::op::Or>(+ ov::OutputVector{wrap_type<Concat>({crop_left_cropped_shape_m, wrap_type<Constant>()}),+ any_input(concat_shape_with_cropped_symbol_m)});auto idxes_crop_left_m = wrap_type<StridedSlice>({idces_concat_m, wrap_type<Constant>(), idxes_crop_left_concat_m, wrap_type<Constant>()});// right equal branchauto crop_right_bias_m = wrap_type<Constant>();auto crop_right_cropped_shape_m = std::make_shared<ov::pass::pattern::op::Or>(ov::OutputVector{wrap_type<Add>({cropped_shape_m, crop_right_bias_m}), cropped_shape_m});- auto idxes_crop_right_concat_m = wrap_type<Concat>({crop_right_cropped_shape_m, wrap_type<Constant>()});- auto idxes_crop_right_m = wrap_type<StridedSlice>({idces_concat_m, wrap_type<Constant>(), idxes_crop_right_concat_m, wrap_type<Constant>()});+ auto idxes_crop_right_concat_m = std::make_shared<ov::pass::pattern::op::Or>(+ ov::OutputVector{wrap_type<Concat>({crop_right_cropped_shape_m, wrap_type<Constant>()}),+ any_input(concat_shape_with_cropped_symbol_m)});+ auto idxes_crop_right_m = wrap_type<StridedSlice>(+ {idces_concat_m, wrap_type<Constant>(), idxes_crop_right_concat_m, wrap_type<Constant>()});auto equal_m = wrap_type<Equal>({idxes_crop_left_m, idxes_crop_right_m});auto condition_m = wrap_type<Reshape>({equal_m, any_input()});@@ -153,7 +166,7 @@ ov::intel_cpu::NgramFusion::NgramFusion() {auto then_m = wrap_type<StridedSlice>({padded_tokens_m, wrap_type<Constant>(), then_cropped_shape_m, wrap_type<Constant>()});// else branch- auto else_target_shape_concat_m = wrap_type<Concat>({cropped_shape_m, wrap_type<Constant>()});+ auto else_target_shape_concat_m = any_input(concat_shape_with_cropped_symbol_m);auto else_m = wrap_type<Broadcast>({wrap_type<Constant>(), else_target_shape_concat_m, wrap_type<Constant>()});auto select_m = wrap_type<Select>({condition_m, then_m, else_m});Matcher select_matcher(select_m);