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_DEPENDENCY_OPTIMIZER_H_ 17 #define TENSORFLOW_CORE_GRAPPLER_OPTIMIZERS_DEPENDENCY_OPTIMIZER_H_ 18 19 #include <unordered_set> 20 #include "tensorflow/core/grappler/optimizers/graph_optimizer.h" 21 #include "tensorflow/core/grappler/utils.h" 22 #include "tensorflow/core/protobuf/rewriter_config.pb.h" 23 24 namespace tensorflow { 25 namespace grappler { 26 27 // Optimize TF computations by removing control dependencies or re-arranging 28 // them to shorten the critical path for a model step or enable other 29 // optimizations, such as removing nodes that are effectively noops. 30 class DependencyOptimizer : public GraphOptimizer { 31 public: DependencyOptimizer()32 DependencyOptimizer() {} DependencyOptimizer(RewriterConfig::Toggle opt_level)33 explicit DependencyOptimizer(RewriterConfig::Toggle opt_level) {} ~DependencyOptimizer()34 ~DependencyOptimizer() override {} 35 name()36 string name() const override { return "dependency_optimizer"; }; 37 38 Status Optimize(Cluster* cluster, const GrapplerItem& item, 39 GraphDef* optimized_graph) override; 40 41 void Feedback(Cluster* cluster, const GrapplerItem& item, 42 const GraphDef& optimized_graph, double result) override; 43 44 private: 45 // Returns true if bypassing node does not increase the number of edges or 46 // number of edges crossing a device boundary. 47 bool BypassingNodeIsBeneficial( 48 const NodeDef& node, const std::vector<NodeDef*>& input_nodes, 49 const std::vector<NodeDef*>& output_nodes) const; 50 int NumEdgesIfBypassed(const NodeDef& node, 51 const std::vector<NodeDef*>& output_nodes) const; 52 // Returns true if node is not an Identity node or if it is an Identity 53 // that is safe to remove. 54 bool SafeToRemoveIdentity(const NodeDef& node) const; 55 // Returns true if it is safe to convert node to NoOp. 56 bool SafeToConvertToNoOp(const NodeDef& node) const; 57 // Removes all duplicate control dependencies. 58 void CleanControlInputs(); 59 // Builds a map from the &optimized_graph_->node(i) to i. 60 void BuildNodeToIdx(); 61 // Tries to optimize the node with the given index, possibly additional 62 // optimizations by inserting nodes in nodes_to_simplify, and pruning nodes by 63 // inserting them in nodes_to_delete. 64 void OptimizeNode(int node_idx, SetVector<int>* nodes_to_simplify, 65 std::set<int>* nodes_to_delete); 66 // Eliminates redundant control dependencies by computing the transitive 67 // reduction of the graph. 68 Status TransitiveReduction(); 69 // Main driver of dependency optimizations. 70 Status OptimizeDependencies(); 71 // Replaces multiple cross-device control edges from the same device with a 72 // single control edge. 73 void GroupCrossDeviceControlEdges(); 74 75 bool fetch_nodes_known_; 76 std::unordered_set<string> nodes_to_preserve_; 77 std::unique_ptr<NodeMap> node_map_; 78 std::unordered_map<const NodeDef*, int> node_to_idx_; 79 GraphDef* optimized_graph_; // Not owned. 80 }; 81 82 } // end namespace grappler 83 } // end namespace tensorflow 84 85 #endif // TENSORFLOW_CORE_GRAPPLER_OPTIMIZERS_DEPENDENCY_OPTIMIZER_H_ 86