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 #include "tensorflow/core/framework/attr_value.pb.h" 18 #include "tensorflow/core/grappler/utils.h" 19 20 namespace tensorflow { 21 namespace grappler { 22 23 namespace { 24 int OpPortIdToArgId(const NodeDef& node, 25 const protobuf::RepeatedPtrField<OpDef::ArgDef>& args, 26 int port_id) { 27 for (int arg_id = 0; arg_id < args.size(); ++arg_id) { 28 if (port_id < 0) { 29 return -1; 30 } else if (port_id == 0) { 31 return arg_id; 32 } 33 34 // Default is 1 port per arg. 35 int n = 1; 36 37 const auto& arg = args.Get(arg_id); 38 if (!arg.number_attr().empty()) { 39 n = node.attr().at(arg.number_attr()).i(); 40 } else if (!arg.type_list_attr().empty()) { 41 n = node.attr().at(arg.type_list_attr()).list().type_size(); 42 } 43 44 if (n < 0) { 45 // This should never happen. 46 DCHECK_GE(n, 0); 47 return -1; 48 } else if (port_id < n) { 49 return arg_id; 50 } 51 port_id -= n; 52 } 53 54 return -1; 55 } 56 } // end namespace 57 58 int OpOutputPortIdToArgId(const NodeDef& node, const OpDef& op, int port_id) { 59 return OpPortIdToArgId(node, op.output_arg(), port_id); 60 } 61 62 int OpInputPortIdToArgId(const NodeDef& node, const OpDef& op, int port_id) { 63 return OpPortIdToArgId(node, op.input_arg(), port_id); 64 } 65 66 bool HasSingleFanoutNode(const GraphView& graph_view, const NodeDef* node, 67 int port) { 68 const auto output = GraphView::OutputPort(node, port); 69 return graph_view.GetFanout(output).size() <= 1; 70 } 71 72 bool HasFanouts(const GraphView& graph_view, const NodeDef* node, int port) { 73 const auto output = GraphView::OutputPort(node, port); 74 return !graph_view.GetFanout(output).empty(); 75 } 76 77 bool HasControlFanin(const GraphView& graph_view, const NodeDef* node) { 78 const auto control_port = GraphView::InputPort(node, Graph::kControlSlot); 79 return !graph_view.GetFanin(control_port).empty(); 80 } 81 82 bool HasControlFanout(const GraphView& graph_view, const NodeDef* node) { 83 const auto control_port = GraphView::OutputPort(node, Graph::kControlSlot); 84 return !graph_view.GetFanout(control_port).empty(); 85 } 86 87 bool HasControlFaninOrFanout(const GraphView& graph_view, const NodeDef* node) { 88 return HasControlFanin(graph_view, node) || 89 HasControlFanout(graph_view, node); 90 } 91 92 } // end namespace grappler 93 } // end namespace tensorflow 94