aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/core/grappler/optimizers/graph_rewriter.h
blob: 4a5a150dc9234ffdeed9b991c828e8ec30befde8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/* Copyright 2017 The TensorFlow Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/

#ifndef TENSORFLOW_CORE_GRAPPLER_OPTIMIZERS_GRAPH_REWRITER_H_
#define TENSORFLOW_CORE_GRAPPLER_OPTIMIZERS_GRAPH_REWRITER_H_

#include <unordered_map>
#include <unordered_set>
#include "tensorflow/core/grappler/grappler_item.h"

namespace tensorflow {
namespace grappler {

// Tools and utilities to simplify common graph rewrites.
class GraphRewriter {
 public:
  GraphRewriter(const GrapplerItem& item);

  // Forward the inputs of original_node as needed to skip over the nodes that
  // are to be deleted. In other words, if I is an input of 'original_node', and
  // I doesn't belong to one of the nodes in 'nodes_to_delete', I will be an
  // input to 'new_node'. On the other hand, if I belong to a node that will be
  // deleted, I will be replaced with the inputs J of the deleted node (unless J
  // belong to nodes that will be deleted, in which case we'll look for
  // preserved inputs further down the graph).
  void ForwardInputs(const NodeDef& original_node,
                     const std::unordered_set<const NodeDef*>& nodes_to_delete,
                     NodeDef* new_node);

  // Returns true if at least one of the edges in the direct fanout of 'node' is
  // a control dependency edge.
  bool DrivesControlDependency(const NodeDef& node) const;

  // Returns true if at least one of the incident edges is a control dependency
  // edge.
  bool IsDrivenByControlDependency(const NodeDef& node) const;

  // Returns true if at least one of the nodes in the direct fanin or the direct
  // fanout (excluding control dependencies) of 'node' is a function.
  bool IsConnectedToFunction(const NodeDef& node) const;

  // Returns true if the node is driven by at least one node placed on another
  // device.
  bool IsDrivenByAnotherDevice(const NodeDef& node) const;

  // Returns true if the node has input from a stateful op.
  bool ReceivesRefValue(const NodeDef& node) const;

  // Returns true if the node is driven by a Switch node.
  bool IsDrivenBySwitch(const NodeDef& node) const;

  // Returns true if the node feeds a Merge node.
  bool FeedsMerge(const NodeDef& node) const;

  // Returns true if removal of this degree would increase edge count, i.e. if
  // in-degree * out-degree > in-degree + out-degree or if the condition could
  // not be verified.
  bool RemovalIncreasesEdgeCount(const NodeDef& node) const;

 private:
  void RecordConnectivity(const NodeDef& node,
                          const std::unordered_set<string>& function_names);
  void ForwardInputsInternal(
      const NodeDef& original_node,
      const std::unordered_set<const NodeDef*>& nodes_to_delete,
      bool add_as_control, NodeDef* new_node);

  struct NodeInfo {
    int out_degree = 0;
    const NodeDef* def;

    // These are filled in when the NodeInfo is built, but not that they
    // may be empty - if the op could not be loaded from the registry.
    DataTypeVector outputs;
  };

  std::unordered_map<string, std::unique_ptr<NodeInfo>> nodes_;
  std::unordered_map<string, const NodeDef*> optimized_nodes_;
  std::unordered_set<const NodeDef*> control_dependency_drivers_;
  std::unordered_set<const NodeDef*> function_neighbors_;
  std::unordered_set<const NodeDef*> cross_device_receivers_;
  std::unordered_set<const NodeDef*> ref_receivers_;
  std::unordered_set<const NodeDef*> switch_receivers_;
  std::unordered_set<const NodeDef*> merge_feeders_;
};

}  // end namespace grappler
}  // end namespace tensorflow

#endif  // TENSORFLOW_CORE_GRAPPLER_OPTIMIZERS_GRAPH_REWRITER_H_