forked from tensorflow/tensorflow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraph_runner.cc
More file actions
183 lines (152 loc) · 6.34 KB
/
Copy pathgraph_runner.cc
File metadata and controls
183 lines (152 loc) · 6.34 KB
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/* Copyright 2016 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.
==============================================================================*/
#include "tensorflow/core/common_runtime/graph_runner.h"
#include "tensorflow/core/common_runtime/device_factory.h"
#include "tensorflow/core/common_runtime/executor.h"
#include "tensorflow/core/common_runtime/function.h"
#include "tensorflow/core/common_runtime/memory_types.h"
#include "tensorflow/core/common_runtime/rendezvous_mgr.h"
#include "tensorflow/core/framework/log_memory.h"
#include "tensorflow/core/framework/op_kernel.h"
#include "tensorflow/core/graph/algorithm.h"
#include "tensorflow/core/graph/graph_constructor.h"
#include "tensorflow/core/graph/node_builder.h"
#include "tensorflow/core/graph/subgraph.h"
#include "tensorflow/core/lib/core/threadpool.h"
#include "tensorflow/core/lib/strings/strcat.h"
#include "tensorflow/core/public/session_options.h"
namespace tensorflow {
namespace {
std::unique_ptr<Device> GetCPUDevice(Env* env) {
std::vector<Device*> devices;
SessionOptions session_options;
session_options.env = env;
Status s = DeviceFactory::GetFactory(DEVICE_CPU)
->CreateDevices(session_options, "", &devices);
if (s.ok() && !devices.empty()) {
return std::unique_ptr<Device>(devices[0]);
}
return nullptr;
}
// A simple rendezvous class.
// Assumes a single sender and a single receiver, no duplicate sends, and no
// sends of dead tensors.
class SimpleRendezvous : public Rendezvous {
public:
explicit SimpleRendezvous() {}
Status Send(const ParsedKey& parsed, const Args& send_args, const Tensor& val,
const bool is_dead) override {
if (is_dead) {
return errors::Internal("Send of a dead tensor");
}
mutex_lock l(mu_);
string edge_name = parsed.edge_name.ToString();
if (table_.count(edge_name) > 0) {
return errors::Internal("Send of an already sent tensor");
}
table_[edge_name] = val;
return Status::OK();
}
void RecvAsync(const ParsedKey& parsed, const Args& recv_args,
DoneCallback done) override {
Tensor tensor;
Status status = Status::OK();
{
string key = parsed.edge_name.ToString();
mutex_lock l(mu_);
if (table_.count(key) <= 0) {
status = errors::Internal("Did not find key ", key);
} else {
tensor = table_[key];
}
}
done(status, Args{}, recv_args, tensor, false);
}
void StartAbort(const Status& status) override {}
private:
typedef std::unordered_map<string, Tensor> Table;
mutex mu_;
Table table_ GUARDED_BY(mu_);
};
} // namespace
// static
Status GraphRunner::Run(Graph* graph, FunctionLibraryRuntime* function_library,
Env* env, const NamedTensorList& inputs,
const std::vector<string>& output_names,
std::vector<Tensor>* outputs) {
// TODO(vrv): Instead of copying the entire graph, consider modifying
// the existing graph, and then removing those removed edges.
// prior to returning.
std::unique_ptr<Graph> graph_to_run(new Graph(graph->op_registry()));
CopyGraph(*graph, graph_to_run.get());
std::unique_ptr<Device> device = GetCPUDevice(env);
if (!device) {
return errors::NotFound("Cannot find a device for GraphRunner.");
}
SimpleRendezvous* rendez = new SimpleRendezvous;
core::ScopedUnref rendez_unref(rendez);
// Extract the input names and keys, and feed in the inputs.
std::vector<string> input_names;
for (const auto& in : inputs) {
const string& tensor_name = in.first;
input_names.emplace_back(tensor_name);
string full_key = Rendezvous::CreateKey("/cpu:0", 1, "/cpu:1", tensor_name,
FrameAndIter(0, 0));
Rendezvous::ParsedKey parsed;
TF_RETURN_IF_ERROR(Rendezvous::ParseKey(full_key, &parsed));
TF_RETURN_IF_ERROR(rendez->Send(parsed, Rendezvous::Args(), in.second,
false /* is_dead */));
}
// Call RewriteGraphForExecution
TF_RETURN_IF_ERROR(subgraph::RewriteGraphForExecution(
graph_to_run.get(), input_names, output_names, {} /* target nodes */,
device->attributes()));
// Create the local executor and the Rendezvous for fetching back the
// constants.
// Run operators on the local thread. We should not need concurrency here; we
// should not be running expensive operators.
auto runner = [](Executor::Args::Closure c) { c(); };
// Take ownership and pass to NewLocalExecutor
Graph* g = graph_to_run.release();
LocalExecutorParams params;
params.device = device.get();
params.function_library = function_library;
params.create_kernel = [&device, g](const NodeDef& ndef, OpKernel** kernel) {
return CreateNonCachedKernel(device.get(), nullptr, ndef,
g->versions().producer(), kernel);
};
params.delete_kernel = [](OpKernel* kernel) { delete kernel; };
Executor* executor;
TF_RETURN_IF_ERROR(NewLocalExecutor(params, g, &executor));
std::unique_ptr<Executor> executor_unref(executor);
Executor::Args args;
// NOTE: we could take a step id as an argument, but currently
// there is no need since we never trace the running of a graph
// called via this method.
args.step_id = LogMemory::CONSTANT_FOLDING_STEP_ID;
args.runner = runner;
args.rendezvous = rendez;
// Run the graph.
TF_RETURN_IF_ERROR(executor->Run(args));
outputs->resize(output_names.size());
for (size_t i = 0; i < output_names.size(); ++i) {
const string& output_key = Rendezvous::CreateKey(
"/cpu:0", 1, "/cpu:1", output_names[i], FrameAndIter(0, 0));
Rendezvous::ParsedKey parsed;
TF_RETURN_IF_ERROR(Rendezvous::ParseKey(output_key, &parsed));
bool is_dead;
TF_RETURN_IF_ERROR(
rendez->Recv(parsed, Rendezvous::Args(), &(*outputs)[i], &is_dead));
}
return Status::OK();
}
} // namespace tensorflow