-
Notifications
You must be signed in to change notification settings - Fork 74k
/
map_kernels.h
255 lines (219 loc) · 8.84 KB
/
map_kernels.h
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
/* Copyright 2020 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.
==============================================================================*/
#ifndef TENSORFLOW_CORE_KERNELS_MAP_KERNELS_H_
#define TENSORFLOW_CORE_KERNELS_MAP_KERNELS_H_
#include "tensorflow/core/framework/op_kernel.h"
#include "tensorflow/core/kernels/tensor_map.h"
#include "tensorflow/core/util/batch_util.h"
#include "tensorflow/core/util/tensor_ops_util.h"
namespace tensorflow {
inline Status GetInputMap(OpKernelContext* ctx, int index,
const TensorMap** ret_map) {
if (!TensorShapeUtils::IsScalar(ctx->input(index).shape())) {
return errors::InvalidArgument("Input map must be a scalar. Saw: ",
ctx->input(index).shape().DebugString());
}
const TensorMap* map = ctx->input(index).scalar<Variant>()().get<TensorMap>();
if (map == nullptr) {
return errors::InvalidArgument(
"Input handle is not a map. Saw: '",
ctx->input(index).scalar<Variant>()().DebugString(), "'");
}
*ret_map = map;
return OkStatus();
}
// TODO(kattian): change into templated function
inline Status ForwardInputOrCreateNewMap(OpKernelContext* ctx,
int32_t input_index,
int32_t output_index,
const TensorMap& input_map,
TensorMap** output_map) {
// Attempt to forward the input tensor to the output if possible.
std::unique_ptr<Tensor> maybe_output = ctx->forward_input(
input_index, output_index, DT_VARIANT, TensorShape{},
ctx->input_memory_type(input_index), AllocatorAttributes());
Tensor* output_tensor;
if (maybe_output != nullptr && maybe_output->dtype() == DT_VARIANT &&
maybe_output->NumElements() == 1) {
output_tensor = maybe_output.get();
TensorMap* tmp_out = output_tensor->scalar<Variant>()().get<TensorMap>();
if (tmp_out == nullptr) {
return errors::InvalidArgument(
"Expected input ", input_index, " to be a TensorMap but saw ",
output_tensor->scalar<Variant>()().TypeName());
}
if (tmp_out->RefCountIsOne()) {
// Woohoo, forwarding succeeded!
ctx->set_output(output_index, *output_tensor);
*output_map = tmp_out;
return OkStatus();
}
}
// If forwarding is not possible allocate a new output tensor and copy
// the `input_map` to it.
AllocatorAttributes attr;
attr.set_on_host(true);
TF_RETURN_IF_ERROR(
ctx->allocate_output(output_index, {}, &output_tensor, attr));
output_tensor->scalar<Variant>()() = input_map.Copy();
*output_map = output_tensor->scalar<Variant>()().get<TensorMap>();
return OkStatus();
}
class EmptyTensorMap : public OpKernel {
public:
explicit EmptyTensorMap(OpKernelConstruction* ctx) : OpKernel(ctx) {}
void Compute(OpKernelContext* ctx) override {
Tensor* result;
AllocatorAttributes attr;
attr.set_on_host(true);
OP_REQUIRES_OK(ctx, ctx->allocate_output(0, TensorShape{}, &result, attr));
TensorMap empty;
result->scalar<Variant>()() = std::move(empty);
}
};
class TensorMapSize : public OpKernel {
public:
explicit TensorMapSize(OpKernelConstruction* ctx) : OpKernel(ctx) {}
~TensorMapSize() override {}
void Compute(OpKernelContext* ctx) override {
const TensorMap* map = nullptr;
OP_REQUIRES_OK(ctx, GetInputMap(ctx, 0, &map));
Tensor* result;
OP_REQUIRES_OK(ctx, ctx->allocate_output(0, TensorShape{}, &result));
result->scalar<int32>()() = map->tensors().size();
}
};
class TensorMapLookup : public OpKernel {
public:
explicit TensorMapLookup(OpKernelConstruction* ctx) : OpKernel(ctx) {}
~TensorMapLookup() override {}
void Compute(OpKernelContext* ctx) override {
const TensorKey& key = ctx->input(1);
const TensorMap* map = nullptr;
OP_REQUIRES_OK(ctx, GetInputMap(ctx, 0, &map));
OP_REQUIRES(
ctx, map->tensors().find(key) != map->tensors().end(),
errors::InvalidArgument("Trying to lookup non-existent key. Could not "
"find key \"" +
key.SummarizeValue(100) + "\"."));
ctx->set_output(0, map->tensors().find(key)->second);
}
};
class TensorMapInsert : public OpKernel {
public:
explicit TensorMapInsert(OpKernelConstruction* ctx) : OpKernel(ctx) {}
~TensorMapInsert() override {}
void Compute(OpKernelContext* ctx) override {
const TensorKey& key = ctx->input(1);
const Tensor& value = ctx->input(2);
const TensorMap* map = nullptr;
OP_REQUIRES_OK(ctx, GetInputMap(ctx, 0, &map));
TensorMap* output_map = nullptr;
OP_REQUIRES_OK(ctx,
ForwardInputOrCreateNewMap(ctx, 0, 0, *map, &output_map));
output_map->replace(key, value);
}
};
class TensorMapErase : public OpKernel {
public:
explicit TensorMapErase(OpKernelConstruction* ctx) : OpKernel(ctx) {}
void Compute(OpKernelContext* ctx) override {
const TensorKey& key = ctx->input(1);
const TensorMap* map = nullptr;
OP_REQUIRES_OK(ctx, GetInputMap(ctx, 0, &map));
OP_REQUIRES(
ctx, map->tensors().find(key) != map->tensors().end(),
errors::InvalidArgument("Trying to erase non-existent item. Could not "
"find key \"" +
key.SummarizeValue(100) + "\"."));
TensorMap* output_map = nullptr;
OP_REQUIRES_OK(ctx,
ForwardInputOrCreateNewMap(ctx, 0, 0, *map, &output_map));
output_map->tensors().erase(key);
}
};
class TensorMapHasKey : public OpKernel {
public:
explicit TensorMapHasKey(OpKernelConstruction* ctx) : OpKernel(ctx) {}
~TensorMapHasKey() override {}
void Compute(OpKernelContext* ctx) override {
const TensorKey& key = ctx->input(1);
const TensorMap* map = nullptr;
OP_REQUIRES_OK(ctx, GetInputMap(ctx, 0, &map));
Tensor* result;
OP_REQUIRES_OK(ctx, ctx->allocate_output(0, TensorShape{}, &result));
result->scalar<bool>()() = map->tensors().find(key) != map->tensors().end();
}
};
class TensorMapStackKeys : public OpKernel {
public:
explicit TensorMapStackKeys(OpKernelConstruction* ctx) : OpKernel(ctx) {
OP_REQUIRES_OK(ctx, ctx->GetAttr("key_dtype", &key_dtype_));
}
~TensorMapStackKeys() override {}
void Compute(OpKernelContext* ctx) override {
const TensorMap* map = nullptr;
OP_REQUIRES_OK(ctx, GetInputMap(ctx, 0, &map));
OP_REQUIRES(ctx, map->size() != 0,
errors::InvalidArgument(
"TensorMapStackKeys cannot be called on empty map."));
auto it = map->tensors().begin();
TensorShape output_shape = it->first.shape();
output_shape.InsertDim(0, map->tensors().size());
Tensor* result;
OP_REQUIRES_OK(ctx, ctx->allocate_output(0, output_shape, &result));
int i = 0;
size_t sz = map->tensors().size();
TensorShape key_shape = it->first.shape();
while (it != map->tensors().end() && i < sz) {
OP_REQUIRES(
ctx, it->first.dtype() == key_dtype_,
errors::InvalidArgument("Key does not match requested dtype."));
OP_REQUIRES(
ctx, it->first.shape() == key_shape,
errors::InvalidArgument("Keys must all have the same shape."));
OP_REQUIRES_OK(ctx, batch_util::CopyElementToSlice(it->first, result, i));
i++;
it++;
}
}
private:
DataType key_dtype_;
};
template <typename Device>
Status TensorMapBinaryAdd(OpKernelContext* ctx, const TensorMap& a,
const TensorMap& b, TensorMap* out) {
// Binary add returns a map containing the union of keys.
// Values with keys in the intersection are added.
out->tensors() = a.tensors();
for (const std::pair<TensorKey, Tensor>& p : b.tensors()) {
absl::flat_hash_map<TensorKey, Tensor>::iterator it =
out->tensors().find(p.first);
if (it != out->tensors().end()) {
Tensor out_tensor;
TF_RETURN_IF_ERROR(
BinaryAddTensors<Device>(ctx, p.second, it->second, &out_tensor));
it->second = out_tensor;
} else {
out->tensors().emplace(p.first, p.second);
}
}
return OkStatus();
}
template <typename Device>
Status TensorMapZerosLike(OpKernelContext* ctx, const TensorMap& x,
TensorMap* y) {
// Zeros like returns an empty map.
return OkStatus();
}
} // namespace tensorflow
#endif // TENSORFLOW_CORE_KERNELS_MAP_KERNELS_H_