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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
|
/* Copyright 2017 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/process_function_library_runtime.h"
#include <utility>
#include "tensorflow/core/common_runtime/function.h"
#include "tensorflow/core/common_runtime/rendezvous_util.h"
#include "tensorflow/core/lib/gtl/map_util.h"
#include "tensorflow/core/util/device_name_utils.h"
namespace tensorflow {
const char ProcessFunctionLibraryRuntime::kDefaultFLRDevice[] = "null";
Status ProcessFunctionLibraryRuntime::FunctionData::DistributedInit(
DistributedFunctionLibraryRuntime* parent, const string& function_name,
const FunctionLibraryDefinition& lib_def, AttrSlice attrs,
const FunctionLibraryRuntime::InstantiateOptions& options) {
mutex_lock l(mu_);
if (!init_started_) {
init_started_ = true;
init_result_ = parent->Instantiate(function_name, lib_def, attrs, options,
&local_handle_);
}
return init_result_;
}
ProcessFunctionLibraryRuntime::ProcessFunctionLibraryRuntime(
const DeviceMgr* device_mgr, Env* env, int graph_def_version,
const FunctionLibraryDefinition* lib_def,
const OptimizerOptions& optimizer_options,
DistributedFunctionLibraryRuntime* parent)
: device_mgr_(device_mgr),
lib_def_(lib_def),
next_handle_(0),
parent_(parent) {
if (device_mgr == nullptr) {
flr_map_[nullptr] =
NewFunctionLibraryRuntime(nullptr, env, nullptr, graph_def_version,
lib_def, optimizer_options, this);
return;
}
for (Device* d : device_mgr->ListDevices()) {
flr_map_[d] =
NewFunctionLibraryRuntime(device_mgr, env, d, graph_def_version,
lib_def, optimizer_options, this);
}
}
ProcessFunctionLibraryRuntime::ProcessFunctionLibraryRuntime(
const DeviceMgr* device_mgr, Env* env, int graph_def_version,
const FunctionLibraryDefinition* lib_def,
const OptimizerOptions& optimizer_options,
CustomKernelCreator custom_kernel_creator,
DistributedFunctionLibraryRuntime* parent)
: device_mgr_(device_mgr),
lib_def_(lib_def),
next_handle_(0),
parent_(parent) {
if (device_mgr == nullptr) {
flr_map_[nullptr] = NewFunctionLibraryRuntime(
nullptr, env, nullptr, graph_def_version, lib_def, optimizer_options,
std::move(custom_kernel_creator), this);
return;
}
for (Device* d : device_mgr->ListDevices()) {
flr_map_[d] = NewFunctionLibraryRuntime(
device_mgr, env, d, graph_def_version, lib_def, optimizer_options,
custom_kernel_creator, this);
}
}
/* static */
Status ProcessFunctionLibraryRuntime::SendTensors(
const string& source_device, const string& target_device,
const string& key_prefix, int64 src_incarnation,
gtl::ArraySlice<Tensor> tensors_to_send, DeviceContext* device_context,
const std::vector<AllocatorAttributes>& alloc_attrs,
Rendezvous* rendezvous) {
std::vector<string> keys;
for (int i = 0; i < tensors_to_send.size(); ++i) {
string name = strings::StrCat(key_prefix, i);
string key = Rendezvous::CreateKey(source_device, src_incarnation,
target_device, name, FrameAndIter(0, 0));
keys.push_back(key);
}
TF_RETURN_IF_ERROR(SendTensorsToRendezvous(
rendezvous, device_context, alloc_attrs, keys, tensors_to_send));
return Status::OK();
}
/* static */
void ProcessFunctionLibraryRuntime::ReceiveTensorsAsync(
const string& source_device, const string& target_device,
const string& key_prefix, int64 src_incarnation, int64 num_tensors,
DeviceContext* device_context,
const std::vector<AllocatorAttributes>& alloc_attrs, Rendezvous* rendezvous,
std::vector<Tensor>* received_tensors, const StatusCallback& done) {
std::vector<string> keys;
for (int64 i = 0; i < num_tensors; ++i) {
string name = strings::StrCat(key_prefix, i);
string key = Rendezvous::CreateKey(source_device, src_incarnation,
target_device, name, FrameAndIter(0, 0));
keys.push_back(key);
}
RecvOutputsFromRendezvousAsync(
rendezvous, device_context, alloc_attrs, keys, received_tensors,
[done](const Status& status) { done(status); });
}
Status ProcessFunctionLibraryRuntime::GetDeviceIncarnation(
const string& device_name, int64* incarnation) {
FunctionLibraryRuntime* flr = GetFLR(device_name);
if (flr == nullptr) {
return errors::InvalidArgument("Device name: ", device_name, " not found");
}
*incarnation = flr->device()->attributes().incarnation();
return Status::OK();
}
Status ProcessFunctionLibraryRuntime::GetDeviceContext(
const string& device_name, DeviceContext** device_context) {
*device_context = nullptr;
FunctionLibraryRuntime* flr = GetFLR(device_name);
if (flr == nullptr) {
return errors::InvalidArgument("Device name: ", device_name, " not found.");
}
Device* device = flr->device();
string device_type = device->parsed_name().type;
if (device_type == "CPU") return Status::OK();
if (device_type == "GPU") {
auto* dev_info = flr->device()->tensorflow_gpu_device_info();
if (dev_info) {
*device_context = dev_info->default_context;
return Status::OK();
}
}
return errors::Internal("Device type: ", device_type,
" is currently unsupported for remote ",
"function executions");
}
FunctionLibraryRuntime* ProcessFunctionLibraryRuntime::GetFLR(
const string& device_name) const {
Device* device = nullptr;
if (device_name != kDefaultFLRDevice) {
if (!device_mgr_->LookupDevice(device_name, &device).ok()) {
VLOG(1) << "Could not find device: " << device_name;
return nullptr;
}
}
const auto& iter = flr_map_.find(device);
if (iter == flr_map_.end()) {
LOG(ERROR) << "Could not find device: " << device_name;
return nullptr;
}
return iter->second.get();
}
FunctionLibraryRuntime::Handle ProcessFunctionLibraryRuntime::AddHandle(
const string& function_key, const string& device_name,
FunctionLibraryRuntime::LocalHandle local_handle) {
mutex_lock l(mu_);
FunctionLibraryRuntime::Handle h =
gtl::FindWithDefault(table_, function_key, kInvalidHandle);
if (h != kInvalidHandle) {
if (function_data_.count(h) != 0) return h;
}
h = next_handle_;
FunctionData* fd = new FunctionData(device_name, local_handle);
function_data_[h] = std::unique_ptr<FunctionData>(fd);
table_[function_key] = h;
next_handle_++;
return h;
}
FunctionLibraryRuntime::Handle ProcessFunctionLibraryRuntime::GetHandle(
const string& function_key) const {
mutex_lock l(mu_);
FunctionLibraryRuntime::Handle h =
gtl::FindWithDefault(table_, function_key, kInvalidHandle);
if (h != kInvalidHandle) {
if (function_data_.count(h) == 0) return kInvalidHandle;
}
return h;
}
bool ProcessFunctionLibraryRuntime::IsInstantiatedOnDevice(
const string& device_name, FunctionLibraryRuntime::Handle handle) {
return GetHandleOnDevice(device_name, handle) != kInvalidHandle;
}
FunctionLibraryRuntime::LocalHandle
ProcessFunctionLibraryRuntime::GetHandleOnDevice(
const string& device_name, FunctionLibraryRuntime::Handle handle) {
mutex_lock l(mu_);
if (function_data_.count(handle) == 0) {
return kInvalidLocalHandle;
}
FunctionData* function_data = function_data_[handle].get();
if (function_data->target_device() != device_name) {
return kInvalidLocalHandle;
}
return function_data->local_handle();
}
string ProcessFunctionLibraryRuntime::GetDeviceName(
FunctionLibraryRuntime::Handle handle) {
mutex_lock l(mu_);
CHECK_EQ(1, function_data_.count(handle));
FunctionData* function_data = function_data_[handle].get();
return function_data->target_device();
}
Status ProcessFunctionLibraryRuntime::Instantiate(
const string& function_name, AttrSlice attrs,
const FunctionLibraryRuntime::InstantiateOptions& options,
FunctionLibraryRuntime::Handle* handle) {
*handle = kInvalidHandle;
FunctionLibraryRuntime* flr = GetFLR(options.target);
if (flr != nullptr) {
return flr->Instantiate(function_name, attrs, options, handle);
}
if (parent_ == nullptr) {
return errors::Internal(
"Currently don't support instantiating functions on device: ",
options.target);
}
string function_key = Canonicalize(function_name, attrs);
FunctionData* f;
{
mutex_lock l(mu_);
FunctionLibraryRuntime::Handle h =
gtl::FindWithDefault(table_, function_key, kInvalidHandle);
if (h == kInvalidHandle || function_data_.count(h) == 0) {
h = next_handle_;
FunctionData* fd = new FunctionData(options.target, kInvalidHandle);
function_data_[h] = std::unique_ptr<FunctionData>(fd);
table_[function_key] = h;
next_handle_++;
}
f = function_data_[h].get();
*handle = h;
}
TF_RETURN_IF_ERROR(
f->DistributedInit(parent_, function_name, *lib_def_, attrs, options));
return Status::OK();
}
Status ProcessFunctionLibraryRuntime::RemoveHandle(
FunctionLibraryRuntime::Handle handle) {
mutex_lock l(mu_);
function_data_.erase(handle);
return Status::OK();
}
Status ProcessFunctionLibraryRuntime::ReleaseHandle(
FunctionLibraryRuntime::Handle handle) {
FunctionLibraryRuntime* flr = nullptr;
string target_device;
{
mutex_lock l(mu_);
CHECK_EQ(1, function_data_.count(handle)) << " handle: " << handle;
target_device = function_data_[handle]->target_device();
}
flr = GetFLR(target_device);
if (flr != nullptr) {
return flr->ReleaseHandle(handle);
}
return errors::InvalidArgument("Handle not found: ", handle);
}
void ProcessFunctionLibraryRuntime::Run(
const FunctionLibraryRuntime::Options& opts,
FunctionLibraryRuntime::Handle handle, gtl::ArraySlice<Tensor> args,
std::vector<Tensor>* rets, FunctionLibraryRuntime::DoneCallback done) {
if (!opts.remote_execution) {
done(errors::InvalidArgument(
"ProcessFunctionLibraryRuntime::Run should only be called when there ",
"is a remote execution."));
return;
}
FunctionLibraryRuntime* flr = nullptr;
string target_device;
FunctionLibraryRuntime::LocalHandle local_handle;
{
mutex_lock l(mu_);
if (function_data_.count(handle) == 0) {
done(errors::NotFound("Handle: ", handle, " not found."));
return;
}
target_device = function_data_[handle]->target_device();
local_handle = function_data_[handle]->local_handle();
}
flr = GetFLR(target_device);
if (flr != nullptr) {
auto rendezvous = opts.rendezvous;
string source_device = opts.source_device;
DeviceContext* device_context;
Status s = GetDeviceContext(source_device, &device_context);
if (!s.ok()) {
done(s);
return;
}
int64 src_incarnation, target_incarnation;
s = GetDeviceIncarnation(source_device, &src_incarnation);
s.Update(GetDeviceIncarnation(target_device, &target_incarnation));
if (!s.ok()) {
done(s);
return;
}
// Send the args over to the target device.
s = SendTensors(source_device, target_device, "arg_", src_incarnation, args,
device_context, opts.args_alloc_attrs, rendezvous);
if (!s.ok()) {
done(s);
return;
}
const std::vector<AllocatorAttributes>& rets_alloc_attrs =
opts.rets_alloc_attrs;
std::vector<Tensor>* remote_rets = new std::vector<Tensor>;
flr->Run(opts, handle, args, remote_rets,
[source_device, target_device, target_incarnation, rendezvous,
device_context, rets_alloc_attrs, remote_rets, rets,
done](const Status& status) {
if (!status.ok()) {
delete remote_rets;
done(status);
return;
}
int64 num_returns = remote_rets->size();
delete remote_rets;
// Now receive the return values from the target.
ReceiveTensorsAsync(target_device, source_device, "ret_",
target_incarnation, num_returns,
device_context, rets_alloc_attrs, rendezvous,
rets, done);
});
return;
}
if (parent_ != nullptr) {
parent_->Run(opts, local_handle, args, rets, done);
return;
}
done(errors::Internal("Could not find device"));
}
Status ProcessFunctionLibraryRuntime::Clone(
Env* env, int graph_def_version, const OptimizerOptions& optimizer_options,
CustomKernelCreator custom_kernel_creator,
std::unique_ptr<FunctionLibraryDefinition>* out_lib_def,
std::unique_ptr<ProcessFunctionLibraryRuntime>* out_pflr) {
out_lib_def->reset(new FunctionLibraryDefinition(*lib_def_));
out_pflr->reset(new ProcessFunctionLibraryRuntime(
device_mgr_, env, graph_def_version, out_lib_def->get(),
optimizer_options, std::move(custom_kernel_creator), parent_));
return Status::OK();
}
} // namespace tensorflow
|