diff options
author | Igor Ganichev <iga@google.com> | 2017-09-25 23:49:12 -0700 |
---|---|---|
committer | TensorFlower Gardener <gardener@tensorflow.org> | 2017-09-25 23:52:52 -0700 |
commit | 12116d724cbefe4f49d85ba7dc96b6419ae9c865 (patch) | |
tree | b9403e9c0a89bc79778e352e53fad97bd550d24c /tensorflow/core/lib/hash | |
parent | 45db52ddbd98b898ac72aca00658bab52b8431c4 (diff) |
Move SerializeToStringDeterministic to a header file
It is a useful function for computing hash values.
PiperOrigin-RevId: 170013135
Diffstat (limited to 'tensorflow/core/lib/hash')
-rw-r--r-- | tensorflow/core/lib/hash/hash.cc | 11 | ||||
-rw-r--r-- | tensorflow/core/lib/hash/hash.h | 10 |
2 files changed, 21 insertions, 0 deletions
diff --git a/tensorflow/core/lib/hash/hash.cc b/tensorflow/core/lib/hash/hash.cc index dc9d300d00..ed9b4df37a 100644 --- a/tensorflow/core/lib/hash/hash.cc +++ b/tensorflow/core/lib/hash/hash.cc @@ -126,4 +126,15 @@ uint64 Hash64(const char* data, size_t n, uint64 seed) { return h; } +bool SerializeToStringDeterministic(const protobuf::MessageLite& msg, + string* result) { + const size_t size = msg.ByteSizeLong(); + *result = string(size, '\0'); + protobuf::io::ArrayOutputStream array_stream(&(*result)[0], size); + protobuf::io::CodedOutputStream output_stream(&array_stream); + output_stream.SetSerializationDeterministic(true); + msg.SerializeWithCachedSizes(&output_stream); + return !output_stream.HadError() && size == output_stream.ByteCount(); +} + } // namespace tensorflow diff --git a/tensorflow/core/lib/hash/hash.h b/tensorflow/core/lib/hash/hash.h index 77b8031598..0fb12966af 100644 --- a/tensorflow/core/lib/hash/hash.h +++ b/tensorflow/core/lib/hash/hash.h @@ -24,6 +24,7 @@ limitations under the License. #include <string> #include "tensorflow/core/lib/core/stringpiece.h" +#include "tensorflow/core/platform/protobuf.h" #include "tensorflow/core/platform/types.h" namespace tensorflow { @@ -84,6 +85,15 @@ struct hash<std::pair<T, U>> { } }; +// Wrapper around protocol buffer serialization that requests deterministic +// serialization, in particular for Map fields, which serialize in a random +// order by default. Returns true on success. +// Serialization is guaranteed to be deterministic for a given binary only. +// See the following for more details: +// https://github.com/google/protobuf/blob/a1bb147e96b6f74db6cdf3c3fcb00492472dbbfa/src/google/protobuf/io/coded_stream.h#L834 +bool SerializeToStringDeterministic(const protobuf::MessageLite& msg, + string* result); + } // namespace tensorflow #endif // TENSORFLOW_LIB_HASH_HASH_H_ |