-
Notifications
You must be signed in to change notification settings - Fork 74k
/
stack.cc
340 lines (295 loc) · 11.7 KB
/
stack.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
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
/* Copyright 2015 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/stack.h"
#include <limits.h>
#include <atomic>
#include <vector>
#include "tensorflow/core/common_runtime/device.h"
#include "tensorflow/core/framework/device_base.h"
#include "tensorflow/core/framework/op_kernel.h"
#include "tensorflow/core/framework/register_types.h"
#include "tensorflow/core/framework/resource_mgr.h"
#include "tensorflow/core/framework/tensor.h"
#include "tensorflow/core/framework/tensor_shape.h"
#include "tensorflow/core/framework/types.h"
#include "tensorflow/core/lib/core/errors.h"
#include "tensorflow/core/lib/core/refcount.h"
#include "tensorflow/core/lib/gtl/map_util.h"
#include "tensorflow/core/platform/logging.h"
#include "tensorflow/core/platform/macros.h"
#include "tensorflow/core/platform/mutex.h"
#include "tensorflow/core/platform/thread_annotations.h"
#include "tensorflow/core/platform/types.h"
namespace tensorflow {
class Stack : public ResourceBase {
public:
static std::atomic<int64_t> stack_counter;
struct TensorAndAllocation {
Tensor tensor;
AllocatorAttributes alloc_attrs;
bool swapped_to_cpu;
};
Stack(const DataType& elem_type, const string& stack_name, int max_size)
: elem_type_(elem_type),
stack_name_(stack_name),
max_size_(max_size),
closed_(false) {}
Status Push(const TensorAndAllocation& value) {
mutex_lock l(mu_);
TF_RETURN_IF_ERROR(CheckNotClosed());
int stack_size = stack_.size();
if (max_size_ >= 0 && stack_size >= max_size_) {
return errors::InvalidArgument("Stack[", stack_name_, "] overflowed ",
"its max_size (", max_size_, ")");
}
stack_.push_back(value);
return OkStatus();
}
Status Pop(TensorAndAllocation* value) {
mutex_lock l(mu_);
TF_RETURN_IF_ERROR(CheckNotClosed());
if (stack_.empty()) {
return errors::InvalidArgument("Stack[", stack_name_,
"] is empty when calling Pop().");
}
*value = stack_.back();
stack_.pop_back();
return OkStatus();
}
// We don't swap the first tensor on the stack and any subsequent tensors
// that share the buffer with the first tensor.
bool IsUsefulToSwap(const Tensor& tensor) const {
mutex_lock l(mu_);
if (stack_.empty()) {
return false;
}
const Tensor& first = stack_.front().tensor;
return !tensor.SharesBufferWith(first);
}
void Close() {
mutex_lock l(mu_);
stack_.clear();
closed_ = true;
}
DataType ElemType() { return elem_type_; }
string DebugString() const override {
mutex_lock l(mu_);
return strings::StrCat("Stack[", stack_name_, "]");
}
const string& stack_name() { return stack_name_; }
private:
friend class StackOp;
mutex* mu() { return &mu_; }
mutable mutex mu_;
DataType elem_type_;
const string stack_name_;
Tensor handle_;
int max_size_;
bool closed_ TF_GUARDED_BY(mu_);
std::vector<TensorAndAllocation> stack_ TF_GUARDED_BY(mu_);
Status CheckNotClosed() const TF_EXCLUSIVE_LOCKS_REQUIRED(mu_) {
if (closed_) {
return errors::InvalidArgument("Stack[", stack_name_,
"] has already been closed.");
}
return OkStatus();
}
};
Status GetStack(OpKernelContext* ctx, Stack** stack) {
if (ctx->input_dtype(0) == DT_RESOURCE) {
return LookupResource(ctx, HandleFromInput(ctx, 0), stack);
} else {
Tensor Tstack_handle = ctx->mutable_input(0, false);
if (Tstack_handle.NumElements() != 2) {
return errors::InvalidArgument(
"Stack handle must have two elements, but had shape: ",
Tstack_handle.shape().DebugString());
}
const string& container = Tstack_handle.flat<tstring>()(0);
const string& stack_name = Tstack_handle.flat<tstring>()(1);
string key = strings::StrCat(container, stack_name);
ResourceMgr* rm = ctx->resource_manager();
if (rm == nullptr) {
return errors::Internal("No resource manager.");
}
auto* step_container = ctx->step_container();
if (step_container == nullptr) {
return errors::Internal("No step container.");
}
TF_RETURN_IF_ERROR(step_container->Lookup(rm, key, stack));
return OkStatus();
}
}
std::atomic<int64_t> Stack::stack_counter{0};
// StackOp
StackOp::StackOp(OpKernelConstruction* context) : OpKernel(context) {
OP_REQUIRES_OK(context, context->GetAttr("elem_type", &elem_type_));
OP_REQUIRES_OK(context, context->GetAttr("stack_name", &stack_name_));
if (stack_name_.empty()) stack_name_ = name();
}
void StackOp::Compute(OpKernelContext* ctx) {
int32_t size = std::numeric_limits<int32>::max();
if (ctx->num_inputs() > 0) {
const Tensor* tensor_size;
OP_REQUIRES_OK(ctx, ctx->input("max_size", &tensor_size));
OP_REQUIRES(
ctx, TensorShapeUtils::IsScalar(tensor_size->shape()),
errors::InvalidArgument("Stack size must be a scalar, but had shape: ",
tensor_size->shape().DebugString()));
int32_t size_value = tensor_size->scalar<int32>()();
if (size_value >= 0) {
size = size_value;
}
}
static const char kContainer[] = "_stacks";
auto stack_id = Stack::stack_counter.fetch_add(1);
string stack_name = strings::StrCat(stack_name_, "_", stack_id);
// Store the handle in a per-step container.
ResourceMgr* rm = ctx->resource_manager();
OP_REQUIRES(ctx, rm != nullptr, errors::Internal("No resource manager."));
string key = strings::StrCat(kContainer, stack_name);
auto* step_container = ctx->step_container();
OP_REQUIRES(ctx, step_container != nullptr,
errors::Internal("No step container."));
Stack* stack = new Stack(elem_type_, stack_name, size);
OP_REQUIRES_OK(ctx, step_container->Create(rm, key, stack));
if (IsRefType(ctx->expected_output_dtype(0))) {
// Create the stack handle.
AllocatorAttributes alloc_attr;
alloc_attr.set_on_host(true);
OP_REQUIRES_OK(ctx, ctx->allocate_temp(tensorflow::DT_STRING,
tensorflow::TensorShape({2}),
&stack->handle_, alloc_attr));
auto handle = stack->handle_.flat<tstring>();
handle(0) = kContainer;
handle(1) = std::move(stack_name);
ctx->set_output_ref(0, stack->mu(), &stack->handle_);
} else {
Tensor* handle;
OP_REQUIRES_OK(ctx, ctx->allocate_output(0, TensorShape({}), &handle));
handle->flat<ResourceHandle>()(0) =
ctx->step_container()->MakeResourceHandle<Stack>(key, *ctx->device());
}
}
// StackPushOp
StackPushOp::StackPushOp(OpKernelConstruction* context, bool allow_swapping)
: AsyncOpKernel(context) {
if (allow_swapping) {
OP_REQUIRES_OK(context, context->GetAttr("swap_memory", &swap_memory_));
}
}
void StackPushOp::ComputeAsync(OpKernelContext* ctx, DoneCallback done) {
// Get the stack from the handle.
Stack* stack = nullptr;
OP_REQUIRES_OK_ASYNC(ctx, GetStack(ctx, &stack), done);
core::ScopedUnref unref(stack);
if (ctx->input_dtype(1) != stack->ElemType()) {
ctx->CtxFailure(errors::InvalidArgument("Must have type ",
stack->ElemType(), " but got ",
ctx->input_dtype(1)));
done();
return;
}
// Push the tensor onto the stack. Swap the tensor to CPU if instructed.
const Tensor& tensor = ctx->input(1);
AllocatorAttributes alloc_attrs = ctx->input_alloc_attr(1);
// For now, we use a simple heuristic for swapping: A GPU tensor is moved
// to CPU if the tensor has more than kCopyThreshold bytes and the GPU
// allocator says more than kOccupancy of the memory is in use.
static constexpr int kCopyThreshold = 2048;
static constexpr double kOccupancy = 0.7;
if (swap_memory_ && !alloc_attrs.on_host() &&
tensor.TotalBytes() > kCopyThreshold && stack->IsUsefulToSwap(tensor)) {
DeviceContext* device_ctxt = ctx->op_device_context();
auto device = static_cast<tensorflow::Device*>(ctx->device());
Allocator* allocator = device->GetAllocator(alloc_attrs);
absl::optional<AllocatorStats> stats = allocator->GetStats();
if (stats && *stats->bytes_limit &&
stats->bytes_in_use > (*stats->bytes_limit * kOccupancy)) {
// Asynchronously copy the tensor from GPU to CPU memory.
// TODO(yuanbyu): Swap the oldest tensor first.
AllocatorAttributes host_alloc_attrs;
host_alloc_attrs.set_gpu_compatible(true);
host_alloc_attrs.set_on_host(true);
Allocator* cpu_allocator = device->GetAllocator(host_alloc_attrs);
Tensor* cpu_tensor =
new Tensor(cpu_allocator, tensor.dtype(), tensor.shape());
device_ctxt->CopyDeviceTensorToCPU(
&tensor, "StackPush", device, cpu_tensor,
[cpu_tensor, stack, ctx, done](const Status& s) {
ctx->SetStatus(s);
if (s.ok()) {
AllocatorAttributes alloc_attrs = ctx->input_alloc_attr(1);
ctx->SetStatus(stack->Push({*cpu_tensor, alloc_attrs, true}));
}
if (ctx->status().ok()) {
ctx->set_output(0, *cpu_tensor);
}
done();
delete cpu_tensor;
});
return;
}
}
// Execute synchronously if not swapped.
OP_REQUIRES_OK_ASYNC(ctx, stack->Push({tensor, alloc_attrs, false}), done);
ctx->set_output(0, tensor);
done();
}
bool StackPushOp::IsExpensive() { return false; }
// StackPopOp
StackPopOp::StackPopOp(OpKernelConstruction* context)
: AsyncOpKernel(context) {}
void StackPopOp::ComputeAsync(OpKernelContext* ctx, DoneCallback done) {
// Get the stack from the handle.
Stack* stack = nullptr;
OP_REQUIRES_OK_ASYNC(ctx, GetStack(ctx, &stack), done);
core::ScopedUnref unref(stack);
// Pop the tensor. Transfer the tensor back to device if it was
// swapped out to CPU.
Stack::TensorAndAllocation value;
OP_REQUIRES_OK_ASYNC(ctx, stack->Pop(&value), done);
if (value.swapped_to_cpu) {
// Asynchronously copy the tensor back from CPU to GPU memory.
DeviceContext* device_ctxt = ctx->op_device_context();
Device* device = static_cast<Device*>(ctx->device());
Tensor* cpu_tensor = &value.tensor;
Allocator* gpu_allocator = device->GetAllocator(value.alloc_attrs);
Tensor* device_tensor =
new Tensor(gpu_allocator, cpu_tensor->dtype(), cpu_tensor->shape());
device_ctxt->CopyCPUTensorToDevice(
cpu_tensor, device, device_tensor,
[device_tensor, ctx, done](const Status& s) {
ctx->SetStatus(s);
if (s.ok()) {
ctx->set_output(0, *device_tensor);
}
done();
delete device_tensor;
});
} else {
// Execute synchronously if not swapped.
ctx->set_output(0, value.tensor);
done();
}
}
bool StackPopOp::IsExpensive() { return false; }
// StackCloseOp
StackCloseOp::StackCloseOp(OpKernelConstruction* context) : OpKernel(context) {}
void StackCloseOp::Compute(OpKernelContext* ctx) {
Stack* stack = nullptr;
OP_REQUIRES_OK(ctx, GetStack(ctx, &stack));
core::ScopedUnref unref(stack);
stack->Close();
}
bool StackCloseOp::IsExpensive() { return false; }
} // namespace tensorflow