Repository navigation
Expand file tree
/
Copy pathoptional_ops.cc
More file actions
197 lines (172 loc) · 8.07 KB
/
Copy pathoptional_ops.cc
File metadata and controls
197 lines (172 loc) · 8.07 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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/* 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.
==============================================================================*/
#define EIGEN_USE_THREADS
#include "tensorflow/core/kernels/data/optional_ops.h"
#include "tensorflow/core/common_runtime/dma_helper.h"
#include "tensorflow/core/framework/op_kernel.h"
#include "tensorflow/core/framework/variant_encode_decode.h"
#include "tensorflow/core/framework/variant_op_registry.h"
namespace tensorflow {
namespace data {
namespace {
static Status OptionalDeviceCopy(
const OptionalVariant& from, OptionalVariant* to,
const UnaryVariantOpRegistry::AsyncTensorDeviceCopyFn& copy) {
if (from.has_value()) {
const std::vector<Tensor>& from_values = from.get_values();
std::vector<Tensor> to_values;
to_values.reserve(from_values.size());
for (const Tensor& t : from_values) {
if (DMAHelper::CanUseDMA(&t) || t.dtype() == DT_VARIANT) {
// NOTE(skyewm): we're careful to make sure the lifetime of the 'to'
// Tensor passed to `copy` (i.e. to_values.back()) is the same as the
// returned 'to' OptionalVariant. This is because `copy` may spawn async
// callbacks that don't run until after this function returns and access
// the 'to' Tensor (e.g. BaseGPUDevice::MaybeCopyTensorToGPU).
to_values.emplace_back(t.dtype());
TF_RETURN_IF_ERROR(copy(t, &to_values.back()));
} else {
to_values.push_back(t);
}
}
*to = OptionalVariant(std::move(to_values));
} else {
*to = from;
}
return Status::OK();
}
#define REGISTER_OPTIONAL_COPY(DIRECTION) \
INTERNAL_REGISTER_UNARY_VARIANT_DEVICE_COPY_FUNCTION( \
OptionalVariant, DIRECTION, OptionalDeviceCopy)
REGISTER_OPTIONAL_COPY(VariantDeviceCopyDirection::HOST_TO_DEVICE);
REGISTER_OPTIONAL_COPY(VariantDeviceCopyDirection::DEVICE_TO_HOST);
REGISTER_OPTIONAL_COPY(VariantDeviceCopyDirection::DEVICE_TO_DEVICE);
REGISTER_UNARY_VARIANT_DECODE_FUNCTION(OptionalVariant,
kOptionalVariantTypeName);
} // namespace
void OptionalNoneOp::Compute(OpKernelContext* ctx) {
OP_REQUIRES_OK(ctx, WriteOptionalNoneToOutput(ctx, 0));
}
void OptionalFromValueOp::Compute(OpKernelContext* ctx) {
OpInputList components_input;
OP_REQUIRES_OK(ctx, ctx->input_list("components", &components_input));
std::vector<Tensor> components(components_input.begin(),
components_input.end());
OP_REQUIRES_OK(ctx,
WriteOptionalWithValueToOutput(ctx, 0, std::move(components)));
}
void OptionalHasValueOp::Compute(OpKernelContext* ctx) {
const Tensor* optional_input;
OP_REQUIRES_OK(ctx, ctx->input("optional", &optional_input));
OP_REQUIRES(ctx, TensorShapeUtils::IsScalar(optional_input->shape()),
errors::InvalidArgument(
"Input to OptionalHasValue must be a scalar tensor "
"containing an OptionalVariant object."));
const OptionalVariant* optional =
optional_input->scalar<Variant>()().get<OptionalVariant>();
OP_REQUIRES(
ctx, optional != nullptr,
errors::InvalidArgument(
"Input to OptionalHasValue must be an OptionalVariant object."));
Tensor* result;
OP_REQUIRES_OK(ctx, ctx->allocate_output(0, {}, &result));
result->scalar<bool>()() = optional->has_value();
}
void OptionalGetValueOp::Compute(OpKernelContext* ctx) {
const Tensor* optional_input;
OP_REQUIRES_OK(ctx, ctx->input("optional", &optional_input));
OP_REQUIRES(ctx, TensorShapeUtils::IsScalar(optional_input->shape()),
errors::InvalidArgument(
"Input to OptionalHasValue must be a scalar tensor "
"containing an OptionalVariant object."));
const OptionalVariant* optional =
optional_input->scalar<Variant>()().get<OptionalVariant>();
OP_REQUIRES(
ctx, optional != nullptr,
errors::InvalidArgument(
"Input to OptionalHasValue must be an OptionalVariant object."));
OP_REQUIRES(
ctx, optional->has_value(),
errors::InvalidArgument("The given optional does not have a value."));
const auto& components = optional->get_values();
OP_REQUIRES(
ctx, components.size() == output_types_.size(),
errors::InvalidArgument("The given optional has ", components.size(),
" components, expected ", output_types_.size()));
for (int i = 0; i < components.size(); ++i) {
OP_REQUIRES(ctx, components[i].dtype() == output_types_[i],
errors::InvalidArgument(
"The given optional does not match the expected type for "
"component ",
i, ". Expected: ", DataTypeString(output_types_[i]),
". Actual: ", DataTypeString(components[i].dtype()), "."));
OP_REQUIRES(ctx, output_shapes_[i].IsCompatibleWith(components[i].shape()),
errors::InvalidArgument(
"The given optional does not match the expected shape "
"for component ",
i, ". Expected: ", output_shapes_[i].DebugString(),
". Actual: ", components[i].shape().DebugString(), "."));
ctx->set_output(i, components[i]);
}
}
Status WriteOptionalWithValueToOutput(OpKernelContext* ctx, int output_index,
std::vector<Tensor> value) {
OptionalVariant v(std::move(value));
Tensor* variant_t;
AllocatorAttributes cpu_alloc;
cpu_alloc.set_on_host(true);
TF_RETURN_IF_ERROR(ctx->allocate_output(output_index, TensorShape({}),
&variant_t, cpu_alloc));
variant_t->scalar<Variant>()() = v;
return Status::OK();
}
Status WriteOptionalNoneToOutput(OpKernelContext* ctx, int output_index) {
OptionalVariant v;
Tensor* variant_t;
AllocatorAttributes cpu_alloc;
cpu_alloc.set_on_host(true);
TF_RETURN_IF_ERROR(ctx->allocate_output(output_index, TensorShape({}),
&variant_t, cpu_alloc));
variant_t->scalar<Variant>()() = v;
return Status::OK();
}
namespace {
REGISTER_KERNEL_BUILDER(Name("OptionalNone").Device(DEVICE_CPU).Priority(2),
OptionalNoneOp);
REGISTER_KERNEL_BUILDER(Name("OptionalNone").Device(DEVICE_GPU).Priority(1),
OptionalNoneOp);
REGISTER_KERNEL_BUILDER(
Name("OptionalFromValue").Device(DEVICE_CPU).Priority(2),
OptionalFromValueOp);
REGISTER_KERNEL_BUILDER(
Name("OptionalFromValue").Device(DEVICE_GPU).Priority(1),
OptionalFromValueOp);
REGISTER_KERNEL_BUILDER(Name("OptionalHasValue").Device(DEVICE_CPU).Priority(2),
OptionalHasValueOp);
REGISTER_KERNEL_BUILDER(Name("OptionalHasValue")
.Device(DEVICE_GPU)
.HostMemory("has_value")
.Priority(1),
OptionalHasValueOp);
REGISTER_KERNEL_BUILDER(Name("OptionalGetValue").Device(DEVICE_CPU).Priority(2),
OptionalGetValueOp);
REGISTER_KERNEL_BUILDER(Name("OptionalGetValue").Device(DEVICE_GPU).Priority(1),
OptionalGetValueOp);
} // namespace
REGISTER_UNARY_VARIANT_UNARY_OP_FUNCTION(ZEROS_LIKE_VARIANT_UNARY_OP,
DEVICE_CPU, OptionalVariant,
OptionalZerosLike<CPUDevice>);
REGISTER_UNARY_VARIANT_BINARY_OP_FUNCTION(ADD_VARIANT_BINARY_OP, DEVICE_CPU,
OptionalVariant,
OptionalBinaryAdd<CPUDevice>);
} // namespace data
} // namespace tensorflow