1 /* Copyright 2018 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 #include "tensorflow/core/lib/strings/proto_serialization.h"
16
17 #include <cstring>
18
19 #include "absl/memory/memory.h"
20 #include "absl/strings/string_view.h"
21 #include "tensorflow/core/lib/gtl/inlined_vector.h"
22 #include "tensorflow/core/lib/hash/hash.h"
23 #include "tensorflow/core/platform/logging.h"
24 #include "tensorflow/core/platform/macros.h"
25
26 namespace tensorflow {
27 namespace {
28
29 // Helper for deterministic serialization.
30 class DeterministicSerializer {
31 public:
DeterministicSerializer(const protobuf::MessageLite & msg)32 explicit DeterministicSerializer(const protobuf::MessageLite& msg)
33 : DeterministicSerializer(msg, msg.ByteSizeLong()) {}
34
DeterministicSerializer(const protobuf::MessageLite & msg,size_t size)35 DeterministicSerializer(const protobuf::MessageLite& msg, size_t size)
36 : size_(size) {
37 char* ptr = space_;
38 if (size_ > sizeof(space_)) {
39 ptr = new char[size_];
40 alloc_.reset(ptr);
41 }
42 bool ok = SerializeToBufferDeterministic(msg, ptr, size_);
43 DCHECK(ok);
44 }
45
size() const46 size_t size() const { return size_; }
data() const47 const char* data() const { return alloc_ == nullptr ? space_ : alloc_.get(); }
48
49 private:
50 // Avoid InlinedVector since it causes 2x slowdown in the compilation
51 // of graphs containing large tensors in debug mode.
52 static constexpr int kInlinedBufferSize = 256;
53 const size_t size_;
54 std::unique_ptr<char[]> alloc_;
55 char space_[kInlinedBufferSize];
56 };
57 } // namespace
58
SerializeToStringDeterministic(const protobuf::MessageLite & msg,string * result)59 bool SerializeToStringDeterministic(const protobuf::MessageLite& msg,
60 string* result) {
61 const size_t size = msg.ByteSizeLong();
62 DCHECK_LE(size, static_cast<size_t>(INT_MAX));
63 *result = string(size, '\0');
64 return SerializeToBufferDeterministic(msg, const_cast<char*>(result->data()),
65 result->size());
66 }
67
SerializeToBufferDeterministic(const protobuf::MessageLite & msg,char * buffer,size_t size)68 bool SerializeToBufferDeterministic(const protobuf::MessageLite& msg,
69 char* buffer, size_t size) {
70 DCHECK(msg.ByteSizeLong() == size && size <= static_cast<size_t>(INT_MAX));
71 protobuf::io::ArrayOutputStream array_stream(buffer, size);
72 protobuf::io::CodedOutputStream output_stream(&array_stream);
73 output_stream.SetSerializationDeterministic(true);
74 msg.SerializeWithCachedSizes(&output_stream);
75 return !output_stream.HadError() && size == output_stream.ByteCount();
76 }
77
AreSerializedProtosEqual(const protobuf::MessageLite & x,const protobuf::MessageLite & y)78 bool AreSerializedProtosEqual(const protobuf::MessageLite& x,
79 const protobuf::MessageLite& y) {
80 const size_t size = x.ByteSizeLong();
81 if (size != y.ByteSizeLong()) return false;
82 if (size == 0) return true;
83 DeterministicSerializer x_serialized(x, size);
84 DeterministicSerializer y_serialized(y, size);
85 return memcmp(x_serialized.data(), y_serialized.data(), size) == 0;
86 }
87
DeterministicProtoHash64(const protobuf::MessageLite & proto,uint64 seed)88 uint64 DeterministicProtoHash64(const protobuf::MessageLite& proto,
89 uint64 seed) {
90 DeterministicSerializer serialized(proto);
91 return Hash64(serialized.data(), serialized.size(), seed);
92 }
93
DeterministicProtoHash64(const protobuf::MessageLite & proto)94 uint64 DeterministicProtoHash64(const protobuf::MessageLite& proto) {
95 DeterministicSerializer serialized(proto);
96 return Hash64(serialized.data(), serialized.size());
97 }
98
99 } // namespace tensorflow
100