1/* Copyright 2015 The TensorFlow Authors. All Rights Reserved.
2
3Licensed under the Apache License, Version 2.0 (the "License");
4you may not use this file except in compliance with the License.
5You may obtain a copy of the License at
6
7 http://www.apache.org/licenses/LICENSE-2.0
8
9Unless required by applicable law or agreed to in writing, software
10distributed under the License is distributed on an "AS IS" BASIS,
11WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12See the License for the specific language governing permissions and
13limitations under the License.
14==============================================================================*/
15
16#ifndef TENSORFLOW_CORE_COMMON_RUNTIME_GRAPH_OPTIMIZER_H_
17#define TENSORFLOW_CORE_COMMON_RUNTIME_GRAPH_OPTIMIZER_H_
18
19#include "tensorflow/core/framework/function.h"
20#include "tensorflow/core/graph/graph.h"
21#include "tensorflow/core/lib/core/status.h"
22#include "tensorflow/core/platform/env.h"
23#include "tensorflow/core/protobuf/config.pb.h"
24
25namespace tensorflow {
26
27class GraphOptimizer {
28 public:
29 using NodePredicate = std::function<bool(const Node*)>;
30
31 struct Options {
32 // If not null it maps from nodes in graph to partially-known
33 // shapes of their outputs, and may be used, e.g., in the constant folding
34 // pass. The use of shape_map implies that the mapping from node name to the
35 // vector of partial shapes of its outputs is stable, i.e., no optimization
36 // pass may replace a node with a different node of the same name that has a
37 // different number of outputs, or outputs with different known shapes.
38 // TODO(b/65453533) introduce a unique way to name nodes in a graph.
39 std::unordered_map<string, std::vector<PartialTensorShape>>* shape_map =
40 nullptr;
41
42 // If not null then only nodes for which cse_consider_fn returns true will
43 // be considered for CSE.
44 NodePredicate cse_consider_fn = nullptr;
45
46 // If not null then only nodes for which cf_consider_fn returns true will be
47 // considered for CF.
48 NodePredicate cf_consider_fn = nullptr;
49
50 // If true, multi-device functions will be inlined if
51 // opts_.do_function_inlining() is true.
52 bool inline_multi_device_functions = false;
53
54 // If true, functions in implementation selection group will be inlined if
55 // opts_.do_function_inlining() is true.
56 bool inline_impl_selection_group_functions = false;
57
58 // If true all functions will be inlined with a single device function
59 // body placer strategy.
60 bool inline_with_single_device_body_placer = false;
61
62 // If true, the _noinline attribute on functions and callers is ignored.
63 bool ignore_noinline = false;
64 };
65
66 explicit GraphOptimizer(const OptimizerOptions& opts);
67 ~GraphOptimizer();
68
69 // Applies optimization passes specified in 'opts' to 'graph'.
70 // Maybe replace *graph with a new graph object. 'device' is device
71 // on which the 'graph' will execute. It's passed to the optimizers
72 // so that they can respect constraints if any, that should be
73 // respected.
74 void Optimize(FunctionLibraryRuntime* runtime, Env* env, const Device* device,
75 std::unique_ptr<Graph>* graph,
76 const Options& graph_optimizer_options);
77
78 const OptimizerOptions& options() { return opts_; }
79
80 private:
81 OptimizerOptions opts_;
82
83 TF_DISALLOW_COPY_AND_ASSIGN(GraphOptimizer);
84};
85
86// Applies graph rewrite optimization such as inlining, dead code
87// removal, etc.
88//
89// **g is a graph constructed based on the runtime library 'lib'.
90// OptimizeGraph mutates **g extensively and replaces '*g' with a
91// complete copy. Therefore, the caller should not keep any references
92// to nodes *g.
93void OptimizeGraph(FunctionLibraryRuntime* lib, std::unique_ptr<Graph>* g,
94 const GraphOptimizer::Options& graph_optimizer_options);
95void OptimizeGraph(FunctionLibraryRuntime* lib, std::unique_ptr<Graph>* g);
96
97} // end namespace tensorflow
98
99#endif // TENSORFLOW_CORE_COMMON_RUNTIME_GRAPH_OPTIMIZER_H_
100