1 /* Copyright 2018 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 #ifndef TENSORFLOW_CORE_GRAPPLER_OPTIMIZERS_SCOPED_ALLOCATOR_OPTIMIZER_H_ 16 #define TENSORFLOW_CORE_GRAPPLER_OPTIMIZERS_SCOPED_ALLOCATOR_OPTIMIZER_H_ 17 18 #include <atomic> 19 #include <unordered_map> 20 #include <unordered_set> 21 #include <vector> 22 #include "tensorflow/core/grappler/optimizers/graph_optimizer.h" 23 #include "tensorflow/core/protobuf/rewriter_config.pb.h" 24 25 namespace tensorflow { 26 namespace grappler { 27 class Graph; 28 class GraphProperties; 29 class NodeMap; 30 class ScopedAllocatorOptimizer; 31 32 // An Optimizer that introduces ScopedAllocators in order to reduce data 33 // movement and consolidate some kinds of Ops. 34 class ScopedAllocatorOptimizer : public GraphOptimizer { 35 public: 36 ScopedAllocatorOptimizer(RewriterConfig::Toggle opt_level, 37 const ScopedAllocatorOptions& opts); 38 ~ScopedAllocatorOptimizer() override; 39 name()40 string name() const override { return "scoped_allocator_optimizer"; } 41 42 Status Optimize(Cluster* cluster, const GrapplerItem& item, 43 GraphDef* optimized_graph) override; 44 Feedback(Cluster * cluster,const GrapplerItem & item,const GraphDef & optimized_graph,double result)45 void Feedback(Cluster* cluster, const GrapplerItem& item, 46 const GraphDef& optimized_graph, double result) override {} 47 48 // Map from an Op name to a vector of Nodes with that Op. 49 typedef std::unordered_map<string, std::vector<NodeDef*>> DevOpOccurrences; 50 // Map from a device name to a DevOpOccurrences map. 51 typedef std::unordered_map<string, DevOpOccurrences> GraphOpOccurrences; 52 typedef std::unordered_set<string> OpNameSet; 53 54 Status ProcessGraphDef(GraphDef* graph, 55 const GraphProperties& graph_properties); 56 57 // Populates *occs by grouping Nodes with common Ops, according to 58 // their assigned devices. 59 void FindOpOccurrences(GraphDef* graph, const OpNameSet& op_names, 60 GraphOpOccurrences* occs); 61 62 // Returns a new, unused scope_id to be assigned to a ScopedAllocator that 63 // will allocate num_fields (> 0) separate tensors. 64 int NewScopedAllocatorId(int num_fields); 65 node_map()66 NodeMap* node_map() { return node_map_.get(); } 67 68 // Appends values to the attr value under name in node_def, if present. 69 // If not present does an assignment. 70 static void ExtendNodeAttr(StringPiece name, const std::vector<int32>& values, 71 NodeDef* node_def); 72 73 // Class that knows how to do graph rewriting for a particular kind of Op in 74 // order to take advantage of a ScopedAllocator. 75 class Rewriter { 76 public: ~Rewriter()77 virtual ~Rewriter() {} 78 79 virtual Status Rewrite(ScopedAllocatorOptimizer* paopti, 80 int64 invocation_count, GraphDef* graph, 81 const string& op_name, 82 const std::vector<NodeDef*>& nodes, 83 bool* applied) = 0; 84 SetGraphProperties(const GraphProperties & graph_properties)85 void SetGraphProperties(const GraphProperties& graph_properties) { 86 graph_properties_ = &graph_properties; 87 CHECK(graph_properties_); 88 } 89 90 protected: 91 const GraphProperties* graph_properties_; 92 }; 93 94 private: 95 Rewriter* GetRewriter(const string& op_name); 96 97 Status OrderNodeSet(std::vector<NodeDef*>* nodes) const; 98 99 RewriterConfig::Toggle opt_level_; 100 std::unordered_set<string> nodes_to_preserve_; 101 OpNameSet op_name_set_; 102 std::unordered_map<string, Rewriter*> rewriters_; 103 std::vector<Rewriter*> to_delete_; 104 int next_sa_id_ = 1; 105 std::unique_ptr<NodeMap> node_map_; 106 }; 107 108 } // namespace grappler 109 } // namespace tensorflow 110 #endif // TENSORFLOW_CORE_GRAPPLER_OPTIMIZERS_SCOPED_ALLOCATOR_OPTIMIZER_H_ 111