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.
Symbolic shape inference and graph optimizations (openvinotoolkit#19392)
* Symbolic shape inference and graph optimizations - Prepares a place in CommonOptimizations pipeline for symbolic optimizations - Introduces symbolic propagation and symbolic optimizations for ChainedMaximum, NopBroadcast and shape sub-graph optimization - Introduces utility runtime info for TableOfEquivalence passing and disabling of value invalidation during shape inference * Executes NgramFusion in a symbolic environment. Relaxes Ngram fusion pattern utilizing symbolic knowledge * Remove debug model visualization * rt_info copying to new Add operation * Fix visualization and place validation in nicer place in symbolic transformation * Fix Slice operation not to propagate labels if input and output dimension is fully dynamic * Covering Vladislav comments * Replace value invalidation followed by validation to revalidation since it does the same thing * Adding back invalidation of cached values to Symbolic Propagation pass * Fix StridedSlice label propagation. Code style * Update src/common/transformations/tests/symbolic_transformations/nop_broadcast.cpp
- Loading branch information
1 parent
8558476
commit c1a8380
Showing
33 changed files
with
1,359 additions
and
41 deletions.
There are no files selected for viewing
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
26 changes: 26 additions & 0 deletions
26
...mmon/transformations/include/transformations/symbolic_transformations/chained_maximum.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,26 @@ | ||
// Copyright (C) 2018-2023 Intel Corporation | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
#pragma once | ||
|
||
#include <openvino/pass/graph_rewrite.hpp> | ||
#include <transformations_visibility.hpp> | ||
|
||
namespace ov { | ||
namespace pass { | ||
class TRANSFORMATIONS_API ChainedMaximumOptimization; | ||
} // namespace pass | ||
} // namespace ov | ||
|
||
/** | ||
* @ingroup ie_transformation_common_api | ||
* @brief Optimizes graphs based on value labels / symbols | ||
* Maximum(Maximum(A, B), B) -> Maximum(A, B) | ||
* Maximum(Maximum(A, B), A) -> Maximum(A, B) | ||
*/ | ||
class ov::pass::ChainedMaximumOptimization : public ov::pass::MatcherPass { | ||
public: | ||
OPENVINO_RTTI("ChainedMaximumOptimization", "0"); | ||
ChainedMaximumOptimization(); | ||
}; |
37 changes: 37 additions & 0 deletions
37
...n/transformations/include/transformations/symbolic_transformations/label_optimization.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,37 @@ | ||
// Copyright (C) 2018-2023 Intel Corporation | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
#pragma once | ||
#include <openvino/pass/graph_rewrite.hpp> | ||
#include <openvino/pass/pass.hpp> | ||
#include <transformations_visibility.hpp> | ||
|
||
namespace ov { | ||
namespace pass { | ||
class TRANSFORMATIONS_API ApplyTableOfEquivalence; | ||
class TRANSFORMATIONS_API OptimizeLabelsUsedAsValues; | ||
} // namespace pass | ||
} // namespace ov | ||
|
||
/** | ||
* @ingroup ie_transformation_common_api | ||
* @brief Resets symbols / labels on output shapes and values according to table of symbol / label equivalence. It | ||
* allows to reduce number of labels used in the model and to disambiguate label values. | ||
*/ | ||
class ov::pass::ApplyTableOfEquivalence : public ov::pass::ModelPass { | ||
public: | ||
OPENVINO_RTTI("ApplyTableOfEquivalence", "0"); | ||
bool run_on_model(const std::shared_ptr<ov::Model>& m) override; | ||
}; | ||
|
||
/** | ||
* @ingroup ie_transformation_common_api | ||
* @brief Collects sources where each symbol / label initially appeared (on shape or shape sub-graph) and attaches all | ||
* value usages of this label to this initial source | ||
*/ | ||
class ov::pass::OptimizeLabelsUsedAsValues : public ov::pass::ModelPass { | ||
public: | ||
OPENVINO_RTTI("OptimizeLabelsUsedAsValues", "0"); | ||
bool run_on_model(const std::shared_ptr<ov::Model>& m) override; | ||
}; |
25 changes: 25 additions & 0 deletions
25
...common/transformations/include/transformations/symbolic_transformations/nop_broadcast.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,25 @@ | ||
// Copyright (C) 2018-2023 Intel Corporation | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
#pragma once | ||
|
||
#include <openvino/pass/graph_rewrite.hpp> | ||
#include <transformations_visibility.hpp> | ||
|
||
namespace ov { | ||
namespace pass { | ||
class TRANSFORMATIONS_API NopBroadcast; | ||
} // namespace pass | ||
} // namespace ov | ||
|
||
/** | ||
* @ingroup ie_transformation_common_api | ||
* @brief Optimizes out Broadcast(data, Maximum(shape, ones)) if labels on data and shape are equal | ||
* Use case with data being empty should not be considered here since original graph has Maximum with ones | ||
*/ | ||
class ov::pass::NopBroadcast : public ov::pass::MatcherPass { | ||
public: | ||
OPENVINO_RTTI("NopBroadcast", "0"); | ||
NopBroadcast(); | ||
}; |
50 changes: 50 additions & 0 deletions
50
...ansformations/include/transformations/symbolic_transformations/symbolic_optimizations.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,50 @@ | ||
// Copyright (C) 2018-2023 Intel Corporation | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
#pragma once | ||
|
||
#include <openvino/pass/graph_rewrite.hpp> | ||
#include <openvino/pass/manager.hpp> | ||
#include <openvino/pass/pass.hpp> | ||
#include <openvino/pass/pattern/matcher.hpp> | ||
#include <transformations_visibility.hpp> | ||
|
||
namespace ov { | ||
namespace pass { | ||
class TRANSFORMATIONS_API SymbolicOptimizations; | ||
class TRANSFORMATIONS_API SymbolicPropagation; | ||
} // namespace pass | ||
} // namespace ov | ||
|
||
/** | ||
* @ingroup ie_transformation_common_api | ||
* @brief Runs optimizations which are based on symbolic shape inference | ||
*/ | ||
class ov::pass::SymbolicOptimizations : public ov::pass::ModelPass { | ||
public: | ||
OPENVINO_RTTI("SymbolicOptimizations", "0"); | ||
explicit SymbolicOptimizations(bool full_run = true); | ||
bool run_on_model(const std::shared_ptr<ov::Model>& m) override; | ||
std::shared_ptr<ov::pass::Manager> get_manager() { | ||
return m_manager; | ||
}; | ||
|
||
private: | ||
std::shared_ptr<ov::pass::Manager> m_manager; | ||
}; | ||
|
||
/** | ||
* @ingroup ie_transformation_common_api | ||
* @brief Assigns labels / symbols to all tensors on shapes and values. Uses shape inference and other special rules to | ||
* propagate labels / symbols | ||
*/ | ||
class ov::pass::SymbolicPropagation : public ov::pass::ModelPass { | ||
public: | ||
OPENVINO_RTTI("SymbolicPropagation"); | ||
SymbolicPropagation(); | ||
bool run_on_model(const std::shared_ptr<ov::Model>& m) override; | ||
|
||
private: | ||
std::shared_ptr<ov::TableOfEquivalence> m_te; | ||
}; |
43 changes: 43 additions & 0 deletions
43
src/common/transformations/include/transformations/symbolic_transformations/utils.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,43 @@ | ||
// Copyright (C) 2018-2023 Intel Corporation | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
#pragma once | ||
|
||
#include <transformations_visibility.hpp> | ||
|
||
#include "openvino/core/descriptor/tensor.hpp" | ||
#include "openvino/core/dimension.hpp" | ||
#include "openvino/core/partial_shape.hpp" | ||
#include "openvino/core/type/element_type.hpp" | ||
|
||
namespace ov { | ||
namespace symbol { | ||
namespace util { | ||
|
||
/// \brief Collects labels from shape. Labels of static dimensions are guaranteed to be ov::no_labels | ||
/// | ||
/// \param shape Shape object to collect labels from | ||
/// \param labels TensorLabel object to collect labels to | ||
/// | ||
/// \return Status of collecting the labels (false if rank is static else true) | ||
TRANSFORMATIONS_API bool get_labels(const ov::PartialShape& shape, ov::TensorLabel& labels); | ||
|
||
/// \brief Collects labels from tensor of Output object | ||
/// | ||
/// \param output Output object to collect labels from | ||
/// \param labels TensorLabel object to collect labels to | ||
/// | ||
/// \return Status of collecting the labels (false if tensor has no labels else true) | ||
TRANSFORMATIONS_API bool get_labels(const ov::Output<ov::Node>& output, ov::TensorLabel& labels); | ||
|
||
/// \brief Compares | ||
/// | ||
/// \param lhs TensorLabel object to compare | ||
/// \param rhs TensorLabel object to compare | ||
/// | ||
/// \return true if labels are unique and equal between lhs and rhs else false | ||
TRANSFORMATIONS_API bool are_unique_and_equal_labels(const ov::TensorLabel& lhs, const ov::TensorLabel& rhs); | ||
} // namespace util | ||
} // namespace symbol | ||
} // 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
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
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
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
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
50 changes: 50 additions & 0 deletions
50
src/common/transformations/src/transformations/symbolic_transformations/chained_maximum.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,50 @@ | ||
// Copyright (C) 2018-2023 Intel Corporation | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
#include "transformations/symbolic_transformations/chained_maximum.hpp" | ||
|
||
#include <openvino/op/maximum.hpp> | ||
#include <openvino/pass/pattern/op/wrap_type.hpp> | ||
|
||
#include "itt.hpp" | ||
#include "openvino/core/dimension_tracker.hpp" | ||
#include "transformations/symbolic_transformations/utils.hpp" | ||
|
||
using namespace ov::symbol::util; | ||
|
||
ov::pass::ChainedMaximumOptimization::ChainedMaximumOptimization() { | ||
MATCHER_SCOPE(ChainedMaximumOptimization); | ||
auto A_input = pattern::any_input(); | ||
auto B_input = pattern::any_input(); | ||
auto C_input = pattern::any_input(); | ||
auto first_maximum = pattern::wrap_type<op::v1::Maximum>({A_input, B_input}); | ||
auto maximum = pattern::wrap_type<op::v1::Maximum>({first_maximum, C_input}); | ||
|
||
ov::matcher_pass_callback matcher_pass_callback = [=](pattern::Matcher& m) { | ||
const auto& vm = m.get_pattern_value_map(); | ||
|
||
auto A = vm.at(A_input), B = vm.at(B_input), C = vm.at(C_input); | ||
auto output_to_replace = vm.at(first_maximum); | ||
|
||
ov::TensorLabel A_labels, B_labels, C_labels; | ||
bool A_read = get_labels(A, A_labels); | ||
bool B_read = get_labels(B, B_labels); | ||
bool C_read = get_labels(C, C_labels); | ||
|
||
if (!A_read && !B_read && !C_read) | ||
return false; | ||
|
||
if (are_unique_and_equal_labels(A_labels, C_labels)) { | ||
// Matched Maximum(Maximum(A, B), C) with A == C -> Maximum(B, C) | ||
return ov::replace_output_update_name(output_to_replace, B); | ||
} else if (are_unique_and_equal_labels(B_labels, C_labels)) { | ||
// Matched Maximum(Maximum(A, B), C) with B == C -> Maximum(A, C) | ||
return ov::replace_output_update_name(output_to_replace, A); | ||
} | ||
return false; | ||
}; | ||
|
||
auto m = std::make_shared<pattern::Matcher>(maximum, matcher_name); | ||
register_matcher(m, matcher_pass_callback); | ||
} |
Oops, something went wrong.