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/compiler/xla/protobuf_util.h"
17
18 #include <string>
19
20 #include "absl/hash/hash.h"
21 #include "tensorflow/compiler/xla/status_macros.h"
22 #include "tensorflow/compiler/xla/types.h"
23 #include "tensorflow/compiler/xla/util.h"
24 #include "tensorflow/core/lib/io/path.h"
25 #include "tensorflow/core/platform/env.h"
26 #include "tensorflow/core/platform/protobuf.h"
27
28 namespace xla {
29 namespace protobuf_util {
30
ProtobufEquals(const tensorflow::protobuf::Message & m1,const tensorflow::protobuf::Message & m2)31 bool ProtobufEquals(const tensorflow::protobuf::Message& m1,
32 const tensorflow::protobuf::Message& m2) {
33 // This is a bit fast and loose, but avoids introducing a dependency on
34 // the much more complex protobuf::util::MessageDifferencer class. For
35 // our purposes we just say that two protobufs are equal if their serialized
36 // representations are equal.
37 std::string serialized1, serialized2;
38 m1.AppendToString(&serialized1);
39 m2.AppendToString(&serialized2);
40 return (serialized1 == serialized2);
41 }
42
ProtobufHash(const tensorflow::protobuf::Message & m)43 size_t ProtobufHash(const tensorflow::protobuf::Message& m) {
44 // This is a bit fast and loose, but avoids introducing a dependency on
45 // the much more complex protobuf::util::MessageDifferencer class.
46 // We perform the hash on their serialized representation.
47 std::string serialized;
48 m.AppendToString(&serialized);
49 return absl::HashOf(serialized);
50 }
51
DumpProtoToDirectory(const tensorflow::protobuf::Message & message,const std::string & directory,const std::string & file_name,std::string * full_path)52 Status DumpProtoToDirectory(const tensorflow::protobuf::Message& message,
53 const std::string& directory,
54 const std::string& file_name,
55 std::string* full_path) {
56 tensorflow::Env* env = tensorflow::Env::Default();
57 TF_RETURN_IF_ERROR(env->RecursivelyCreateDir(directory));
58 std::string safe_file_name = SanitizeFileName(file_name) + ".pb";
59 std::string full_path_impl;
60 if (!full_path) {
61 full_path = &full_path_impl;
62 }
63 *full_path = tensorflow::io::JoinPath(directory, safe_file_name);
64 return tensorflow::WriteBinaryProto(env, *full_path, message);
65 }
66
67 } // namespace protobuf_util
68 } // namespace xla
69