-
Notifications
You must be signed in to change notification settings - Fork 74k
/
partitioned_function_ops.cc
293 lines (265 loc) · 12 KB
/
partitioned_function_ops.cc
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
/* Copyright 2018 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/kernels/partitioned_function_ops.h"
#include "absl/strings/match.h"
#include "tensorflow/core/common_runtime/function.h"
#include "tensorflow/core/common_runtime/input_colocation_exemption_registry.h"
#include "tensorflow/core/common_runtime/rendezvous_mgr.h"
#include "tensorflow/core/framework/attr_value.pb.h"
#include "tensorflow/core/framework/function.h"
#include "tensorflow/core/framework/op_kernel.h"
#include "tensorflow/core/framework/tensor.h"
#include "tensorflow/core/framework/types.h"
#include "tensorflow/core/framework/types.pb.h"
#include "tensorflow/core/lib/random/random.h"
#include "tensorflow/core/lib/strings/strcat.h"
#include "tensorflow/core/platform/errors.h"
#include "tensorflow/core/profiler/lib/traceme.h"
#include "tensorflow/core/protobuf/config.pb.h"
#include "tensorflow/core/protobuf/rewriter_config.pb.h"
#ifndef IS_MOBILE_PLATFORM
#include "tensorflow/core/grappler/optimizers/meta_optimizer.h"
#endif
#if GOOGLE_CUDA || TENSORFLOW_USE_ROCM
#include "tensorflow/compiler/xla/stream_executor/stream.h"
#endif // GOOGLE_CUDA || TENSORFLOW_USE_ROCM
namespace tensorflow {
PartitionedCallOp::PartitionedCallOp(OpKernelConstruction* ctx)
: AsyncOpKernel(ctx),
func_(new NameAttrList),
config_proto_(new ConfigProto),
shared_rendezvous_(false) {
OP_REQUIRES_OK(
ctx, ctx->GetAttr(FunctionLibraryDefinition::kFuncAttr, func_.get()));
string deprecated_config_serialized;
OP_REQUIRES_OK(ctx, ctx->GetAttr("config", &deprecated_config_serialized));
string config_proto_serialized;
OP_REQUIRES_OK(ctx, ctx->GetAttr("config_proto", &config_proto_serialized));
OP_REQUIRES(
ctx,
deprecated_config_serialized.empty() || config_proto_serialized.empty(),
errors::InvalidArgument("Provided both 'config' and 'config_proto' but "
"only one should be provided. Note the "
"'config' option is deprecated."));
if (!deprecated_config_serialized.empty()) {
OP_REQUIRES(ctx,
config_proto_->mutable_graph_options()
->mutable_rewrite_options()
->ParseFromString(deprecated_config_serialized),
errors::InvalidArgument("Unable to parse config string as "
"tensorflow::RewriteOptions proto."));
} else {
OP_REQUIRES(
ctx, config_proto_->ParseFromString(config_proto_serialized),
errors::InvalidArgument("Unable to parse config_proto string as "
"tensorflow::ConfigProto proto."));
}
OP_REQUIRES_OK(ctx, ctx->GetAttr("executor_type", &executor_type_));
}
PartitionedCallOp::~PartitionedCallOp() {
for (const auto& it : handles_) {
Status status = it.first->ReleaseHandle(it.second);
if (!status.ok()) {
LOG(INFO) << "Ignoring error while destructing PartitionedCallOp: "
<< status.ToString();
}
}
}
void PartitionedCallOp::ComputeAsync(OpKernelContext* ctx, DoneCallback done) {
FunctionLibraryRuntime* lib = ctx->function_library();
OP_REQUIRES_ASYNC(ctx, lib != nullptr,
errors::Internal("No function library is provided."), done);
// The function body's graph is placed and partitioned the first time
// `ComputeAsync` is invoked; every subsequent invocation calls each
// of the function shards yielded by partitioning.
//
// The partitioning step yields a set of devices on which to run the
// function, and exactly one function shard is created for each device
// Inputs and outputs are pinned to the local device, for simplicity.
//
// TODO(akshayka): Support re-sharding the function on subsequent calls,
// via, e.g., virtual device annotations and a list of device names
// supplied through an attribute.
//
// TODO(akshayka): Add a fastpath for functions that execute on a single
// device.
FunctionLibraryRuntime::Handle handle;
// If we are instantiating the function, we can efficiently extract the
// inputs while instantiating. Else, we extract them separately below.
std::vector<Tensor> inputs;
bool inputs_extracted = false;
{
mutex_lock l(mu_);
auto it = handles_.find(lib);
if (it == handles_.end()) {
OP_REQUIRES_OK_ASYNC(ctx, Instantiate(lib, ctx, &inputs, &handle), done);
inputs_extracted = true;
handles_[lib] = handle;
} else {
handle = it->second;
}
}
if (!inputs_extracted) {
OpInputList args;
OP_REQUIRES_OK_ASYNC(ctx, ctx->input_list("args", &args), done);
inputs.reserve(args.size());
for (const Tensor& tensor : args) {
inputs.push_back(tensor);
}
}
RunFunction(handle, inputs, lib, ctx, done);
}
Status PartitionedCallOp::FillOutputDevices(
const FunctionLibraryRuntime& lib, const Device& cpu_device,
AttrSlice attrs, FunctionLibraryRuntime::InstantiateOptions* opts) {
const FunctionLibraryDefinition* flib = lib.GetFunctionLibraryDefinition();
const FunctionDef* fdef = flib->Find(func_->name());
if (fdef == nullptr) {
return errors::NotFound("Failed to find definition for function \"",
func_->name(), "\"");
}
auto func_attrs = fdef->attr();
auto attr = func_attrs.find(FunctionLibraryDefinition::kSharedRendezvousAttr);
if (attr != func_attrs.end() && attr->second.b()) {
shared_rendezvous_ = true;
}
bool is_type_list;
for (const OpDef::ArgDef& ret_def : fdef->signature().output_arg()) {
DataTypeVector dtypes;
TF_RETURN_IF_ERROR(ArgNumType(attrs, ret_def, &is_type_list, &dtypes));
for (DataType dtype : dtypes) {
if (dtype == DT_RESOURCE) {
// Resource memory type is HOST_MEMORY, however the actual resource
// might be allocated on a device. We leave output device for resource
// outputs empty, and rely on a Placer and colocation constraints to
// infer correct placement for the function output.
opts->output_devices.push_back("");
} else {
opts->output_devices.push_back(opts->target);
}
}
}
return OkStatus();
}
Status PartitionedCallOp::Instantiate(FunctionLibraryRuntime* lib,
OpKernelContext* ctx,
std::vector<Tensor>* inputs,
FunctionLibraryRuntime::Handle* handle) {
FunctionLibraryRuntime::InstantiateOptions opts;
const auto* config = (ctx->function_library())
? ctx->function_library()->config_proto()
: nullptr;
if (config) {
opts.config_proto = *config;
}
#ifndef IS_MOBILE_PLATFORM
// Android tf library does not include grappler.
grappler::GrapplerItem::OptimizationOptions optimization_options;
// Tensorflow 2.0 in eager mode with automatic control dependencies will
// prune all nodes that are not in the transitive fanin of the fetch nodes.
// However because the function will be executed via FunctionLibraryRuntime,
// and current function implementation does not prune stateful and dataset
// ops, we rely on Grappler to do the correct graph pruning.
optimization_options.allow_pruning_stateful_and_dataset_ops = true;
// All the nested function calls will be executed and optimized via
// PartitionedCallOp, there is no need to optimize functions now.
optimization_options.optimize_function_library = false;
opts.optimize_graph_fn =
std::bind(grappler::OptimizeGraph, std::placeholders::_1,
std::placeholders::_2, std::placeholders::_3,
std::placeholders::_4, std::placeholders::_5, *config_proto_,
func_->name(), optimization_options, std::placeholders::_6);
#endif
// In some contexts like running the graph to evaluate constants,
// the FLR won't have any device.
opts.target = lib->device() == nullptr ? "" : lib->device()->name();
opts.is_multi_device_function = true;
opts.graph_collector = ctx->graph_collector();
opts.executor_type = executor_type_;
OpInputList args;
TF_RETURN_IF_ERROR(ctx->input_list("args", &args));
Device* cpu_device;
TF_RETURN_IF_ERROR(lib->device_mgr()->LookupDevice("CPU:0", &cpu_device));
inputs->reserve(args.size());
for (const Tensor& tensor : args) {
inputs->push_back(tensor);
DataType dtype = tensor.dtype();
if (dtype == DT_RESOURCE) {
const ResourceHandle& handle = tensor.flat<ResourceHandle>()(0);
opts.input_devices.push_back(handle.device());
} else {
opts.input_devices.push_back(opts.target);
}
}
TF_RETURN_IF_ERROR(
FillOutputDevices(*lib, *cpu_device, AttrSlice(&func_->attr()), &opts));
TF_RETURN_IF_ERROR(
lib->Instantiate(func_->name(), AttrSlice(&func_->attr()), opts, handle));
return OkStatus();
}
void PartitionedCallOp::RunFunction(FunctionLibraryRuntime::Handle handle,
const std::vector<Tensor>& inputs,
FunctionLibraryRuntime* lib,
OpKernelContext* ctx, DoneCallback done) {
FunctionLibraryRuntime::Options run_opts;
ResourceMgr* resource_mgr = lib->device()->resource_manager();
ScopedStepContainer* step_container = new ScopedStepContainer(
run_opts.step_id, [resource_mgr](const string& name) {
resource_mgr->Cleanup(name).IgnoreError();
});
run_opts.step_container = step_container;
run_opts.cancellation_manager = ctx->cancellation_manager();
run_opts.stats_collector = ctx->stats_collector();
run_opts.collective_executor = ctx->collective_executor();
// TODO(akshayka): Consider selecting a runner on a per-device basis,
// i.e., using device-specific threadpools when available.
run_opts.runner = ctx->runner();
run_opts.run_all_kernels_inline = ctx->run_all_kernels_inline();
run_opts.source_device =
lib->device() == nullptr ? "" : lib->device()->name();
run_opts.allow_dead_tensors = true;
if (shared_rendezvous_) {
run_opts.rendezvous = ctx->rendezvous();
}
std::vector<Tensor>* rets = new std::vector<Tensor>;
const string& func_name = func_->name();
profiler::TraceMe trace_me("PartitionedCallOp");
lib->Run(run_opts, handle, inputs, rets,
[rets, done = std::move(done), ctx, func_name,
step_container](const Status& status) {
if (!status.ok()) {
const string function_and_msg =
strings::StrCat(errors::FormatFunctionForError(func_name),
" ", status.message());
ctx->SetStatus(
errors::CreateWithUpdatedMessage(status, function_and_msg));
} else {
for (int i = 0; i < rets->size(); ++i) {
ctx->set_output(i, (*rets)[i]);
}
}
delete rets;
delete step_container;
done();
});
}
REGISTER_KERNEL_BUILDER(Name("PartitionedCall").Device(DEVICE_CPU),
PartitionedCallOp);
REGISTER_KERNEL_BUILDER(Name("StatefulPartitionedCall").Device(DEVICE_CPU),
PartitionedCallOp);
REGISTER_KERNEL_BUILDER(Name("PartitionedCall").Device(DEVICE_DEFAULT),
PartitionedCallOp);
REGISTER_KERNEL_BUILDER(Name("StatefulPartitionedCall").Device(DEVICE_DEFAULT),
PartitionedCallOp);
REGISTER_INPUT_COLOCATION_EXEMPTION("PartitionedCall");
REGISTER_INPUT_COLOCATION_EXEMPTION("StatefulPartitionedCall");
} // namespace tensorflow