1 /* Copyright 2017 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/variant_tensor_data.h"
17 #include "tensorflow/core/framework/tensor.h"
18 #include "tensorflow/core/framework/tensor.pb.h"
19 #include "tensorflow/core/lib/strings/strcat.h"
20
21 namespace tensorflow {
22
VariantTensorData(VariantTensorDataProto proto)23 VariantTensorData::VariantTensorData(VariantTensorDataProto proto) {
24 FromProto(std::move(proto));
25 }
26
tensors_size() const27 int VariantTensorData::tensors_size() const { return tensors_.size(); }
28
tensors(int index) const29 const Tensor& VariantTensorData::tensors(int index) const {
30 return tensors_[index];
31 }
32
tensors() const33 const std::vector<Tensor>& VariantTensorData::tensors() const {
34 return tensors_;
35 }
36
add_tensors()37 Tensor* VariantTensorData::add_tensors() {
38 tensors_.emplace_back();
39 return &(tensors_[tensors_.size() - 1]);
40 }
41
42 template <typename... TensorConstructorArgs>
add_tensor(TensorConstructorArgs &&...args)43 Tensor* VariantTensorData::add_tensor(TensorConstructorArgs&&... args) {
44 tensors_.emplace_back(std::forward<TensorConstructorArgs>(args)...);
45 return &tensors_.back();
46 }
47
ToProto(VariantTensorDataProto * proto) const48 void VariantTensorData::ToProto(VariantTensorDataProto* proto) const {
49 proto->set_type_name(type_name());
50 proto->set_metadata(metadata_);
51 proto->clear_tensors();
52 for (const auto& tensor : tensors_) {
53 tensor.AsProtoField(proto->mutable_tensors()->Add());
54 }
55 }
56
FromProto(VariantTensorDataProto proto)57 bool VariantTensorData::FromProto(VariantTensorDataProto proto) {
58 // TODO(ebrevdo): Do this lazily.
59 set_type_name(proto.type_name());
60 set_metadata(proto.metadata());
61 for (const auto& tensor : proto.tensors()) {
62 Tensor tmp;
63 if (!tmp.FromProto(tensor)) return false;
64 tensors_.push_back(tmp);
65 }
66 return true;
67 }
68
FromConstProto(const VariantTensorDataProto & proto)69 bool VariantTensorData::FromConstProto(const VariantTensorDataProto& proto) {
70 set_type_name(proto.type_name());
71 set_metadata(proto.metadata());
72 for (const auto& tensor : proto.tensors()) {
73 Tensor tmp;
74 if (!tmp.FromProto(tensor)) return false;
75 tensors_.push_back(tmp);
76 }
77 return true;
78 }
79
SerializeAsString() const80 string VariantTensorData::SerializeAsString() const {
81 VariantTensorDataProto proto;
82 ToProto(&proto);
83 return proto.SerializeAsString();
84 }
85
SerializeToString(string * buf)86 bool VariantTensorData::SerializeToString(string* buf) {
87 VariantTensorDataProto proto;
88 ToProto(&proto);
89 return proto.SerializeToString(buf);
90 }
91
ParseFromString(string s)92 bool VariantTensorData::ParseFromString(string s) {
93 VariantTensorDataProto proto;
94 const bool status = proto.ParseFromString(s);
95 if (status) FromProto(std::move(proto));
96 return status;
97 }
98
DebugString() const99 string VariantTensorData::DebugString() const {
100 string repeated_field = "";
101 for (const auto& t : tensors_) {
102 repeated_field =
103 strings::StrCat(repeated_field, " tensors: ", t.DebugString());
104 }
105 return strings::StrCat("type_name: ", type_name(), " metadata: ", metadata_,
106 repeated_field);
107 }
108
ProtoDebugString(const VariantTensorData & object)109 string ProtoDebugString(const VariantTensorData& object) {
110 return object.DebugString();
111 }
112
113 } // namespace tensorflow
114