1 /* Copyright 2015 The TensorFlow Authors. All Rights Reserved.
2
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6
7 http://www.apache.org/licenses/LICENSE-2.0
8
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15
16 #include "tensorflow/core/framework/resource_handle.h"
17 #include "tensorflow/core/framework/resource_handle.pb.h"
18 #include "tensorflow/core/lib/strings/strcat.h"
19
20 namespace tensorflow {
21
ResourceHandle()22 ResourceHandle::ResourceHandle() {}
23
ResourceHandle(const ResourceHandleProto & proto)24 ResourceHandle::ResourceHandle(const ResourceHandleProto& proto) {
25 FromProto(proto);
26 }
27
~ResourceHandle()28 ResourceHandle::~ResourceHandle() {}
29
AsProto(ResourceHandleProto * proto) const30 void ResourceHandle::AsProto(ResourceHandleProto* proto) const {
31 proto->set_device(device());
32 proto->set_container(container());
33 proto->set_name(name());
34 proto->set_hash_code(hash_code());
35 proto->set_maybe_type_name(maybe_type_name());
36 for (const auto& dtype_and_shape_pair : dtypes_and_shapes_) {
37 auto dtype_and_shape = proto->add_dtypes_and_shapes();
38 dtype_and_shape->set_dtype(dtype_and_shape_pair.dtype);
39 dtype_and_shape_pair.shape.AsProto(dtype_and_shape->mutable_shape());
40 }
41 }
42
FromProto(const ResourceHandleProto & proto)43 void ResourceHandle::FromProto(const ResourceHandleProto& proto) {
44 set_device(proto.device());
45 set_container(proto.container());
46 set_name(proto.name());
47 set_hash_code(proto.hash_code());
48 set_maybe_type_name(proto.maybe_type_name());
49 std::vector<DtypeAndPartialTensorShape> dtypes_and_shapes;
50 for (const auto& dtype_and_shape : proto.dtypes_and_shapes()) {
51 DataType dtype = dtype_and_shape.dtype();
52 PartialTensorShape shape(dtype_and_shape.shape());
53 dtypes_and_shapes.push_back(DtypeAndPartialTensorShape{dtype, shape});
54 }
55 dtypes_and_shapes_ = std::move(dtypes_and_shapes);
56 }
57
SerializeAsString() const58 string ResourceHandle::SerializeAsString() const {
59 ResourceHandleProto proto;
60 AsProto(&proto);
61 return proto.SerializeAsString();
62 }
63
ParseFromString(const string & s)64 bool ResourceHandle::ParseFromString(const string& s) {
65 ResourceHandleProto proto;
66 const bool status = proto.ParseFromString(s);
67 if (status) FromProto(proto);
68 return status;
69 }
70
DebugString() const71 string ResourceHandle::DebugString() const {
72 return strings::StrCat("device: ", device(), " container: ", container(),
73 " name: ", name(), " hash_code: ", hash_code(),
74 " maybe_type_name: ", maybe_type_name());
75 }
76
ProtoDebugString(const ResourceHandle & handle)77 string ProtoDebugString(const ResourceHandle& handle) {
78 return handle.DebugString();
79 }
80
EncodeResourceHandleList(const ResourceHandle * p,int64 n,std::unique_ptr<port::StringListEncoder> e)81 void EncodeResourceHandleList(const ResourceHandle* p, int64 n,
82 std::unique_ptr<port::StringListEncoder> e) {
83 ResourceHandleProto proto;
84 for (int i = 0; i < n; ++i) {
85 p[i].AsProto(&proto);
86 e->Append(proto);
87 }
88 e->Finalize();
89 }
90
DecodeResourceHandleList(std::unique_ptr<port::StringListDecoder> d,ResourceHandle * ps,int64 n)91 bool DecodeResourceHandleList(std::unique_ptr<port::StringListDecoder> d,
92 ResourceHandle* ps, int64 n) {
93 std::vector<uint32> sizes(n);
94 if (!d->ReadSizes(&sizes)) return false;
95
96 ResourceHandleProto proto;
97 for (int i = 0; i < n; ++i) {
98 if (!proto.ParseFromArray(d->Data(sizes[i]), sizes[i])) {
99 return false;
100 }
101 ps[i].FromProto(proto);
102 }
103 return true;
104 }
105
106 } // namespace tensorflow
107