xref: /aosp_15_r20/external/tensorflow/tensorflow/core/grappler/optimizers/scoped_allocator_optimizer.h (revision b6fb3261f9314811a0f4371741dbb8839866f948)
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_set>
20 #include <vector>
21 
22 #include "absl/container/flat_hash_map.h"
23 #include "absl/container/flat_hash_set.h"
24 #include "tensorflow/core/grappler/optimizers/graph_optimizer.h"
25 #include "tensorflow/core/grappler/utils.h"
26 #include "tensorflow/core/protobuf/rewriter_config.pb.h"
27 
28 namespace tensorflow {
29 class Graph;
30 
31 namespace grappler {
32 class GraphProperties;
33 class NodeMap;
34 class ScopedAllocatorOptimizer;
35 
36 // An Optimizer that introduces ScopedAllocators in order to reduce data
37 // movement and consolidate some kinds of Ops.
38 class ScopedAllocatorOptimizer : public GraphOptimizer {
39  public:
40   ScopedAllocatorOptimizer(RewriterConfig::Toggle opt_level,
41                            const ScopedAllocatorOptions& opts);
42   ~ScopedAllocatorOptimizer() override;
43 
name()44   string name() const override { return "scoped_allocator_optimizer"; }
45 
UsesFunctionLibrary()46   bool UsesFunctionLibrary() const override { return true; }
47 
48   Status Optimize(Cluster* cluster, const GrapplerItem& item,
49                   GraphDef* optimized_graph) override;
50 
51   // Map from an Op name to a vector of Nodes with that Op.
52   typedef absl::flat_hash_map<string, std::vector<NodeDef*>> DevOpOccurrences;
53   // Map from a device name to a DevOpOccurrences map.
54   typedef absl::flat_hash_map<string, DevOpOccurrences> GraphOpOccurrences;
55   typedef absl::flat_hash_set<string> OpNameSet;
56 
57   Status ProcessGraphDef(GraphDef* graph,
58                          const GraphProperties& graph_properties);
59 
60   // Populates *occs by grouping Nodes with common Ops, according to
61   // their assigned devices.
62   void FindOpOccurrences(GraphDef* graph, const OpNameSet& op_names,
63                          GraphOpOccurrences* occs);
64 
65   // Returns a new, unused scope_id to be assigned to a ScopedAllocator that
66   // will allocate num_fields (> 0) separate tensors.
67   int NewScopedAllocatorId(int num_fields);
68 
69   // Returns a new, unused id to be assigned to an IdentityOp used in this graph
70   // rewrite.
71   Status NewIdentityId(int* id);
72 
node_map()73   NodeMap* node_map() { return node_map_.get(); }
74 
repeated_outputs()75   const absl::flat_hash_set<string>& repeated_outputs() {
76     return repeated_outputs_;
77   }
78 
79   // Appends values to the attr value under name in node_def, if present.
80   // If not present does an assignment.
81   static void ExtendNodeAttr(StringPiece name, const std::vector<int32>& values,
82                              NodeDef* node_def);
83 
84   // Class that knows how to do graph rewriting for a particular kind of Op in
85   // order to take advantage of a ScopedAllocator.
86   class Rewriter {
87    public:
~Rewriter()88     virtual ~Rewriter() {}
89 
90     virtual Status Rewrite(ScopedAllocatorOptimizer* paopti,
91                            int64_t invocation_count, GraphDef* graph,
92                            const string& op_name,
93                            const std::vector<NodeDef*>& nodes,
94                            bool* applied) = 0;
95 
SetGraphProperties(const GraphProperties & graph_properties)96     void SetGraphProperties(const GraphProperties& graph_properties) {
97       graph_properties_ = &graph_properties;
98       CHECK(graph_properties_);
99     }
100 
101    protected:
102     const GraphProperties* graph_properties_;
103   };
104 
105  private:
106   Rewriter* GetRewriter(const string& op_name);
107 
108   Status OrderNodeSet(std::vector<NodeDef*>* nodes) const;
109 
110   RewriterConfig::Toggle opt_level_;
111   std::unordered_set<string> nodes_to_preserve_;
112   OpNameSet op_name_set_;
113   absl::flat_hash_map<string, Rewriter*> rewriters_;
114   std::vector<Rewriter*> to_delete_;
115   int next_sa_id_ = 1;
116   int next_identity_id_ = 1;
117   std::unique_ptr<NodeMap> node_map_;
118   // Keeps track of outputs, i.e. a node and an output index, that are inputs to
119   // more than one op groups that are candidates for scoped allocator
120   // optimization.
121   absl::flat_hash_set<string> repeated_outputs_;
122 };
123 
124 }  // namespace grappler
125 }  // namespace tensorflow
126 #endif  // TENSORFLOW_CORE_GRAPPLER_OPTIMIZERS_SCOPED_ALLOCATOR_OPTIMIZER_H_
127