• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2015 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/common_runtime/function_body.h"
17 
18 #include "tensorflow/core/framework/node_def_util.h"
19 #include "tensorflow/core/graph/graph.h"
20 
21 namespace tensorflow {
22 
FunctionBody(const FunctionDef & f,DataTypeSlice arg_t,DataTypeSlice ret_t,Graph * g)23 FunctionBody::FunctionBody(const FunctionDef& f, DataTypeSlice arg_t,
24                            DataTypeSlice ret_t, Graph* g)
25     : fdef(f),
26       graph(g),
27       arg_types(arg_t.begin(), arg_t.end()),
28       ret_types(ret_t.begin(), ret_t.end()) {
29   // 1. Find regular Arg/Ret nodes.
30   this->arg_nodes.resize(arg_types.size());
31   this->ret_nodes.resize(ret_types.size());
32   for (Node* n : this->graph->op_nodes()) {
33     gtl::InlinedVector<Node*, 4>* node_vec;
34     if (n->type_string() == FunctionLibraryDefinition::kRetOp ||
35         n->type_string() == FunctionLibraryDefinition::kDeviceRetOp) {
36       node_vec = &this->ret_nodes;
37     } else if (n->type_string() == FunctionLibraryDefinition::kArgOp ||
38                n->type_string() == FunctionLibraryDefinition::kDeviceArgOp) {
39       node_vec = &this->arg_nodes;
40     } else {
41       continue;
42     }
43     int index;
44     TF_CHECK_OK(GetNodeAttr(n->attrs(), "index", &index));
45     CHECK_LE(0, index);
46     CHECK_LT(index, node_vec->size());
47     (*node_vec)[index] = n;
48   }
49   // 2. Find ControlRet nodes that must be always executed.
50   std::unordered_set<StringPiece, StringPieceHasher> control_ret_node_names;
51   for (const auto& control_ret : fdef.control_ret()) {
52     control_ret_node_names.insert(control_ret.second);
53   }
54   this->control_ret_nodes.reserve(control_ret_node_names.size());
55   for (Node* n : this->graph->op_nodes()) {
56     if (control_ret_node_names.count(n->name()) > 0) {
57       this->control_ret_nodes.push_back(n);
58     }
59   }
60 }
61 
~FunctionBody()62 FunctionBody::~FunctionBody() { delete this->graph; }
63 
64 }  // end namespace tensorflow
65