xref: /aosp_15_r20/external/tensorflow/tensorflow/core/grappler/graph_view.cc (revision b6fb3261f9314811a0f4371741dbb8839866f948)
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 #include "tensorflow/core/grappler/graph_view.h"
17 
18 #include "tensorflow/core/framework/attr_value.pb.h"
19 #include "tensorflow/core/framework/node_def_util.h"
20 #include "tensorflow/core/grappler/utils.h"
21 
22 namespace tensorflow {
23 namespace grappler {
24 
OpOutputPortIdToArgId(const NodeDef & node,const OpDef & op,int port_id)25 int OpOutputPortIdToArgId(const NodeDef& node, const OpDef& op, int port_id) {
26   return OpPortIdToArgId(node, op.output_arg(), port_id);
27 }
28 
OpInputPortIdToArgId(const NodeDef & node,const OpDef & op,int port_id)29 int OpInputPortIdToArgId(const NodeDef& node, const OpDef& op, int port_id) {
30   return OpPortIdToArgId(node, op.input_arg(), port_id);
31 }
32 
HasSingleFanoutNode(const GraphView & graph_view,const NodeDef * node,int port)33 bool HasSingleFanoutNode(const GraphView& graph_view, const NodeDef* node,
34                          int port) {
35   const auto output = GraphView::OutputPort(node, port);
36   return graph_view.GetFanout(output).size() <= 1;
37 }
38 
HasFanouts(const GraphView & graph_view,const NodeDef * node,int port)39 bool HasFanouts(const GraphView& graph_view, const NodeDef* node, int port) {
40   const auto output = GraphView::OutputPort(node, port);
41   return !graph_view.GetFanout(output).empty();
42 }
43 
HasControlFanin(const GraphView & graph_view,const NodeDef * node)44 bool HasControlFanin(const GraphView& graph_view, const NodeDef* node) {
45   const auto control_port = GraphView::InputPort(node, Graph::kControlSlot);
46   return !graph_view.GetFanin(control_port).empty();
47 }
48 
HasControlFanout(const GraphView & graph_view,const NodeDef * node)49 bool HasControlFanout(const GraphView& graph_view, const NodeDef* node) {
50   const auto control_port = GraphView::OutputPort(node, Graph::kControlSlot);
51   return !graph_view.GetFanout(control_port).empty();
52 }
53 
HasControlFaninOrFanout(const GraphView & graph_view,const NodeDef * node)54 bool HasControlFaninOrFanout(const GraphView& graph_view, const NodeDef* node) {
55   return HasControlFanin(graph_view, node) ||
56          HasControlFanout(graph_view, node);
57 }
58 
59 }  // end namespace grappler
60 }  // end namespace tensorflow
61