1 /* Copyright 2015 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_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 25 namespace tensorflow { 26 27 class 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 51 GraphOptimizer(const OptimizerOptions& opts); 52 ~GraphOptimizer(); 53 54 // Applies optimization passes specified in 'opts' to 'graph'. 55 // Maybe replace *graph with a new graph object. 'device' is device 56 // on which the 'graph' will execute. It's passed to the optimizers 57 // so that they can respect constraints if any, that should be 58 // respected. 59 void Optimize(FunctionLibraryRuntime* runtime, Env* env, Device* device, 60 std::unique_ptr<Graph>* graph, 61 const Options& graph_optimizer_options); 62 // DEPRECATED: Consider passing a GraphOptimizer::Options object instead. 63 void Optimize( 64 FunctionLibraryRuntime* runtime, Env* env, Device* device, 65 std::unique_ptr<Graph>* graph, 66 const std::unordered_map<string, std::vector<PartialTensorShape>>* 67 shape_map, 68 const NodePredicate& cse_consider_fn = nullptr, 69 const NodePredicate& cf_consider_fn = nullptr); 70 options()71 const OptimizerOptions& options() { return opts_; } 72 73 private: 74 OptimizerOptions opts_; 75 76 TF_DISALLOW_COPY_AND_ASSIGN(GraphOptimizer); 77 }; 78 79 } // end namespace tensorflow 80 81 #endif // TENSORFLOW_CORE_COMMON_RUNTIME_GRAPH_OPTIMIZER_H_ 82