HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
graph_transformer_utils.h
Go to the documentation of this file.
1 // Copyright (c) Microsoft Corporation. All rights reserved.
2 // Licensed under the MIT License.
3 
4 #pragma once
5 
6 #include <string>
7 #include <memory>
8 #include <unordered_map>
9 #include <unordered_set>
10 #include <vector>
11 
13 #include "core/framework/session_options.h"
14 #include "core/framework/tensor.h"
17 
18 #if !defined(ORT_MINIMAL_BUILD)
21 #endif
22 
23 #if !defined(ORT_MINIMAL_BUILD) || defined(ORT_EXTENDED_MINIMAL_BUILD)
24 #include "core/optimizer/selectors_actions/selector_action_transformer_apply_contexts.h"
25 #endif
26 
27 namespace onnxruntime {
28 class IExecutionProvider;
29 
30 namespace optimizer_utils {
31 
32 #if !defined(ORT_MINIMAL_BUILD)
33 
34 /** Generates all predefined rules for this level.
35  If rules_to_enable is not empty, it returns the intersection of predefined rules and rules_to_enable.
36  TODO: This is visible for testing at the moment, but we should rather make it private. */
39  const InlinedHashSet<std::string>& rules_to_disable = {});
40 
41 /** Given a TransformerLevel, this method generates a name for the rule-based graph transformer of that level. */
43 
44 /** Generates all rule-based transformers for this level. */
45 std::unique_ptr<RuleBasedGraphTransformer> GenerateRuleBasedGraphTransformer(
46  TransformerLevel level,
47  const InlinedHashSet<std::string>& rules_to_disable,
48  const InlinedHashSet<std::string_view>& compatible_execution_providers);
49 
50 /** Generates all predefined (both rule-based and non-rule-based) transformers for this level.
51  Any transformers or rewrite rules named in rules_and_transformers_to_disable will be excluded. */
53  TransformerLevel level,
54  const SessionOptions& session_options,
55  const IExecutionProvider& execution_provider /*required by constant folding*/,
56  const InlinedHashSet<std::string>& rules_and_transformers_to_disable = {},
57  concurrency::ThreadPool* intra_op_thread_pool = nullptr,
58  std::unordered_map<std::string, std::unique_ptr<Tensor>>* p_buffered_tensors = nullptr);
59 
60 #endif // !defined(ORT_MINIMAL_BUILD)
61 
62 #if !defined(ORT_MINIMAL_BUILD) || defined(ORT_EXTENDED_MINIMAL_BUILD)
63 
64 /** Generates all predefined transformers which can be used to provide runtime optimizations for this level
65  in a minimal build.
66  Any transformers or rewrite rules named in rules_and_transformers_to_disable will be excluded.
67 
68  This is a distinct function from GenerateTransformers() because:
69  - An ORT format model used in a minimal build will have been pre-optimized to at least level 1 when created, so
70  level 1 transformers are not included.
71  - In a minimal build we have limited optimization/Graph capabilities
72  - Graph::Resolve is not available so the transformer must keep the Graph in a valid state
73  - Limited graph_utils capabilities are included
74  - Only a small subset of transformers support storing/replaying runtime optimizations with an ORT format model
75  - this capability is provided by the SelectionActionTransformer infrastructure
76  - the logic to determine the set of nodes a transformer should modify is captured during creation of the ORT
77  format model
78  - this information is saved in the ORT format model
79  - only the logic to modify the set of nodes is included in the minimal build
80  - The QDQFinalCleanupTransformer and NhwcTransformer transformers are also supported in a minimal build
81 */
83  TransformerLevel level,
84  const SessionOptions& session_options,
85  const SatApplyContextVariant& apply_context,
86  const IExecutionProvider& cpu_execution_provider,
87  const InlinedHashSet<std::string>& rules_and_transformers_to_disable = {},
88  concurrency::ThreadPool* intra_op_thread_pool = nullptr,
89  std::unordered_map<std::string, std::unique_ptr<Tensor>>* p_buffered_tensors = nullptr);
90 
91 #endif // !defined(ORT_MINIMAL_BUILD) || defined(ORT_EXTENDED_MINIMAL_BUILD)
92 
93 } // namespace optimizer_utils
94 } // namespace onnxruntime
GLint level
Definition: glcorearb.h:108
std::unique_ptr< RuleBasedGraphTransformer > GenerateRuleBasedGraphTransformer(TransformerLevel level, const InlinedHashSet< std::string > &rules_to_disable, const InlinedHashSet< std::string_view > &compatible_execution_providers)
absl::InlinedVector< T, N, Allocator > InlinedVector
std::string GenerateRuleBasedTransformerName(TransformerLevel level)
InlinedVector< std::unique_ptr< RewriteRule > > GenerateRewriteRules(TransformerLevel level, const InlinedHashSet< std::string > &rules_to_disable={})
InlinedVector< std::unique_ptr< GraphTransformer > > GenerateTransformersForMinimalBuild(TransformerLevel level, const SessionOptions &session_options, const SatApplyContextVariant &apply_context, const IExecutionProvider &cpu_execution_provider, const InlinedHashSet< std::string > &rules_and_transformers_to_disable={}, concurrency::ThreadPool *intra_op_thread_pool=nullptr, std::unordered_map< std::string, std::unique_ptr< Tensor >> *p_buffered_tensors=nullptr)
InlinedVector< std::unique_ptr< GraphTransformer > > GenerateTransformers(TransformerLevel level, const SessionOptions &session_options, const IExecutionProvider &execution_provider, const InlinedHashSet< std::string > &rules_and_transformers_to_disable={}, concurrency::ThreadPool *intra_op_thread_pool=nullptr, std::unordered_map< std::string, std::unique_ptr< Tensor >> *p_buffered_tensors=nullptr)