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 #ifndef TENSORFLOW_CORE_LIB_STRINGS_PROTO_SERIALIZATION_H_ 16 #define TENSORFLOW_CORE_LIB_STRINGS_PROTO_SERIALIZATION_H_ 17 18 #include "tensorflow/core/platform/protobuf.h" 19 20 namespace tensorflow { 21 22 // Wrapper around protocol buffer serialization that requests deterministic 23 // serialization, in particular for Map fields, which serialize in a random 24 // order by default. Returns true on success. 25 // Serialization is guaranteed to be deterministic for a given binary only. 26 // See the following for more details: 27 // https://github.com/google/protobuf/blob/a1bb147e96b6f74db6cdf3c3fcb00492472dbbfa/src/google/protobuf/io/coded_stream.h#L834 28 bool SerializeToStringDeterministic(const protobuf::MessageLite& msg, 29 string* result); 30 31 // As above, but takes a pre-allocated buffer wrapped by result. 32 // PRECONDITION: size == msg.ByteSizeLong() && size <= INT_MAX. 33 bool SerializeToBufferDeterministic(const protobuf::MessageLite& msg, 34 char* buffer, size_t size); 35 36 // Returns true if serializing x and y using 37 // SerializeToBufferDeterministic() yields identical strings. 38 bool AreSerializedProtosEqual(const protobuf::MessageLite& x, 39 const protobuf::MessageLite& y); 40 41 // Computes Hash64 of the output of SerializeToBufferDeterministic(). 42 uint64 DeterministicProtoHash64(const protobuf::MessageLite& proto); 43 uint64 DeterministicProtoHash64(const protobuf::MessageLite& proto, 44 uint64 seed); 45 46 } // namespace tensorflow 47 48 #endif // TENSORFLOW_CORE_LIB_STRINGS_PROTO_SERIALIZATION_H_ 49