• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2017 The TensorFlow Authors. All Rights Reserved.
2 
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6 
7     http://www.apache.org/licenses/LICENSE-2.0
8 
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15 
16 #ifndef TENSORFLOW_CORE_GRAPPLER_OPTIMIZERS_META_OPTIMIZER_H_
17 #define TENSORFLOW_CORE_GRAPPLER_OPTIMIZERS_META_OPTIMIZER_H_
18 
19 #include "tensorflow/core/common_runtime/device_set.h"
20 #include "tensorflow/core/framework/device_base.h"
21 #include "tensorflow/core/framework/function.h"
22 #include "tensorflow/core/graph/graph.h"
23 #include "tensorflow/core/grappler/grappler_item.h"
24 #include "tensorflow/core/grappler/optimizers/graph_optimizer.h"
25 #include "tensorflow/core/grappler/verifiers/graph_verifier.h"
26 #include "tensorflow/core/lib/core/status.h"
27 #include "tensorflow/core/protobuf/config.pb.h"
28 #include "tensorflow/core/protobuf/rewriter_config.pb.h"
29 #include "tensorflow/core/protobuf/verifier_config.pb.h"
30 
31 namespace tensorflow {
32 namespace grappler {
33 
34 // Run the other grappler optimizers based on the specified rewriter config.
35 class MetaOptimizer : public GraphOptimizer {
36  public:
37   MetaOptimizer(DeviceBase* cpu_device, const ConfigProto& cfg);
38   ~MetaOptimizer() override = default;
39 
name()40   string name() const override { return "meta_optimizer"; };
41 
42   Status Optimize(Cluster* cluster, const GrapplerItem& item,
43                   GraphDef* optimized_graph) override;
44 
45   void PrintResult();
46 
47   void Feedback(Cluster* cluster, const GrapplerItem& item,
48                 const GraphDef& optimized_graph, double result) override;
49 
50  private:
51   std::unique_ptr<GraphOptimizer> MakeNewOptimizer(
52       const string& optimizer) const;
53 
54   // Initialize active optimizers from RewriterConfig toggles.
55   Status InitializeOptimizers(
56       std::vector<std::unique_ptr<GraphOptimizer>>* optimizers) const;
57   // Initialize active optimizers from RewriterConfig optimizer names.
58   Status InitializeOptimizersByName(
59       std::vector<std::unique_ptr<GraphOptimizer>>* optimizers) const;
60   // Initialize active optimizers from RewriterConfig.custom_optimizers.
61   Status InitializeCustomGraphOptimizers(
62       const std::set<string>& pre_initialized_optimizers,
63       std::vector<std::unique_ptr<GraphOptimizer>>* optimizers) const;
64   // Returns the config for a custom graph optimizer. Null if none was found.
65   const RewriterConfig::CustomGraphOptimizer* GetCustomGraphOptimizerConfig(
66       const string& name) const;
67 
68   // Initialiaze active verifiers from the RewriterConfig toggles.
69   void InitializeVerifiers(
70       std::vector<std::unique_ptr<GraphVerifier>>* inter_optimizer_verifiers,
71       std::vector<std::unique_ptr<GraphVerifier>>* post_optimization_verifiers)
72       const;
73 
74   // Run optimization pass over a single GrapplerItem. Meta optimizer might run
75   // multiple such passes: 1) for the main graph 2) for the function library
76   Status OptimizeGraph(Cluster* cluster, const GrapplerItem& item,
77                        GraphDef* optimized_graph);
78 
79   DeviceBase* const cpu_device_;  // may be NULL
80   ConfigProto config_proto_;
81   RewriterConfig& cfg_;
82 
83   struct OptimizerResult {
84     string optimizer_name;
85     string result;
86   };
87 
88   struct GraphOptimizationResult {
GraphOptimizationResultGraphOptimizationResult89     explicit GraphOptimizationResult(const string& id) : id(id) {}
90     string id;
91     std::vector<OptimizerResult> results;
92   };
93 
94   Status RunOptimizer(GraphOptimizer* optimizer, Cluster* cluster,
95                       GrapplerItem* optimized_item, GraphDef* optimized_graph,
96                       GraphOptimizationResult* optimization_result);
97 
98   std::vector<GraphOptimizationResult> optimization_results_;
99 };
100 
101 bool MetaOptimizerEnabled(const ConfigProto& cfg);
102 
103 // Run the meta optimizer.
104 //
105 // If <cpu_device> is non-null, it is the device to be used for executing ops
106 // during constant folding; if NULL, a new device is created for doing constant
107 // folding. For performance, it is recommended to pass in an existing cpu_device
108 // when possible.
109 Status RunMetaOptimizer(const GrapplerItem& item, const ConfigProto& cfg,
110                         DeviceBase* cpu_device, Cluster* cluster,
111                         GraphDef* optimized_graph);
112 
113 // Wrapper around RunMetaOptimizer convenient for optimizing
114 // function graphs.
115 //
116 // Runs grappler optimizations on `g` based on `config_proto`.
117 // `ret_node_names`: a vector of node names whose outputs are returned,
118 //    aka fetches. when `g` represent a function, these are _Retval nodes.
119 // `lib`: function library to use with `g`.
120 // `device_set`: the set of devices that graph can refer to.
121 // `cpu_device`: the CPU device.
122 // `config_proto`: Grapper configuration.
123 // `grappler_item_id': Grappler item id (e.g. optimized function name).
124 // `optimization_options`: Grappler optimization constraints that are known only
125 //    at runtime.
126 //
127 // **g is a graph constructed based on the runtime library 'lib'.
128 // OptimizeGraph mutates **g extensively and replaces '*g' with a
129 // complete copy. Therefore, the caller should not keep any references
130 // to nodes *g.
131 Status OptimizeGraph(
132     std::vector<string> ret_node_names, std::vector<string> keep_node_names,
133     FunctionLibraryDefinition* lib, const DeviceSet& device_set,
134     Device* cpu_device, const ConfigProto& config_proto,
135     const string& grappler_item_id,
136     const GrapplerItem::OptimizationOptions& optimization_options,
137     std::unique_ptr<tensorflow::Graph>* g);
138 
139 }  // namespace grappler
140 }  // namespace tensorflow
141 
142 #endif  // TENSORFLOW_CORE_GRAPPLER_OPTIMIZERS_META_OPTIMIZER_H_
143