1/* Copyright 2020 The TensorFlow Authors. All Rights Reserved.
2
3Licensed under the Apache License, Version 2.0 (the "License");
4you may not use this file except in compliance with the License.
5You may obtain a copy of the License at
6
7 http://www.apache.org/licenses/LICENSE-2.0
8
9Unless required by applicable law or agreed to in writing, software
10distributed under the License is distributed on an "AS IS" BASIS,
11WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12See the License for the specific language governing permissions and
13limitations under the License.
14==============================================================================*/
15
16#include "tensorflow/core/kernels/tensor_map.h"
17
18#include "tensorflow/core/framework/tensor_shape.h"
19#include "tensorflow/core/framework/tensor_shape.pb.h"
20#include "tensorflow/core/framework/variant_op_registry.h"
21#include "tensorflow/core/lib/core/coding.h"
22
23namespace tensorflow {
24
25TensorMap::~TensorMap() {
26 if (tensors_) tensors_->Unref();
27}
28
29void TensorMap::Encode(VariantTensorData* data) const {
30 data->set_type_name(TypeName());
31
32 absl::flat_hash_map<TensorKey, Tensor>::const_iterator map_it =
33 tensors().begin();
34 while (map_it != tensors().end()) {
35 Tensor k = map_it->first;
36 Tensor v = map_it->second;
37 // TODO: k should also not be DT_RESOURCE or DT_VARIANT
38 CHECK_NE(k.dtype(), DT_INVALID);
39 CHECK_NE(v.dtype(), DT_INVALID);
40 *data->add_tensors() = k;
41 *data->add_tensors() = v;
42 map_it++;
43 }
44}
45
46static Status TensorMapDeviceCopy(
47 const TensorMap& from, TensorMap* to,
48 const UnaryVariantOpRegistry::AsyncTensorDeviceCopyFn& copy) {
49 for (const std::pair<TensorKey, Tensor>& p : from.tensors()) {
50 TensorKey to_key(p.first.dtype());
51 Tensor to_val(p.second.dtype());
52 TF_RETURN_IF_ERROR(copy(p.first, &to_key));
53 TF_RETURN_IF_ERROR(copy(p.second, &to_val));
54 to->tensors().emplace(to_key, to_val);
55 }
56 return OkStatus();
57}
58
59#define REGISTER_LIST_COPY(DIRECTION) \
60 INTERNAL_REGISTER_UNARY_VARIANT_DEVICE_COPY_FUNCTION(TensorMap, DIRECTION, \
61 TensorMapDeviceCopy)
62
63REGISTER_LIST_COPY(VariantDeviceCopyDirection::HOST_TO_DEVICE);
64REGISTER_LIST_COPY(VariantDeviceCopyDirection::DEVICE_TO_HOST);
65REGISTER_LIST_COPY(VariantDeviceCopyDirection::DEVICE_TO_DEVICE);
66
67REGISTER_UNARY_VARIANT_DECODE_FUNCTION(TensorMap, TensorMap::kTypeName);
68
69bool TensorMap::Decode(const VariantTensorData& data) {
70 // TODO(srbs): Change the signature to Decode(VariantTensorData data) so
71 // that we do not have to copy each tensor individually below. This would
72 // require changing VariantTensorData::tensors() as well.
73 std::vector<Tensor>::const_iterator tensors_it = data.tensors().begin();
74
75 while (tensors_it != data.tensors().end()) {
76 if (std::next(tensors_it) == data.tensors().end()) {
77 return false;
78 }
79 tensors().emplace(tensors_it[0], tensors_it[1]);
80 tensors_it += 2;
81 }
82 return true;
83}
84
85const char TensorMap::kTypeName[] = "tensorflow::TensorMap";
86
87} // namespace tensorflow
88