1/* Copyright 2017 The TensorFlow Authors. All Rights Reserved.
2
3Licensed under the Apache License, Version 2.0 (the "License");
4you may not use this file except in compliance with the License.
5You may obtain a copy of the License at
6
7 http://www.apache.org/licenses/LICENSE-2.0
8
9Unless required by applicable law or agreed to in writing, software
10distributed under the License is distributed on an "AS IS" BASIS,
11WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12See the License for the specific language governing permissions and
13limitations 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
22namespace tensorflow {
23namespace grappler {
24
25int OpOutputPortIdToArgId(const NodeDef& node, const OpDef& op, int port_id) {
26 return OpPortIdToArgId(node, op.output_arg(), port_id);
27}
28
29int OpInputPortIdToArgId(const NodeDef& node, const OpDef& op, int port_id) {
30 return OpPortIdToArgId(node, op.input_arg(), port_id);
31}
32
33bool 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
39bool 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
44bool 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
49bool 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
54bool 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