• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "tensorflow/compiler/xla/status_macros.h"
19 #include "tensorflow/compiler/xla/types.h"
20 #include "tensorflow/compiler/xla/util.h"
21 #include "tensorflow/core/lib/io/path.h"
22 #include "tensorflow/core/platform/env.h"
23 #include "tensorflow/core/platform/mutex.h"
24 #include "tensorflow/core/platform/protobuf.h"
25 
26 namespace xla {
27 namespace protobuf_util {
28 
ProtobufEquals(const tensorflow::protobuf::Message & m1,const tensorflow::protobuf::Message & m2)29 bool ProtobufEquals(const tensorflow::protobuf::Message& m1,
30                     const tensorflow::protobuf::Message& m2) {
31   // This is a bit fast and loose, but avoids introducing a dependency on
32   // the much more complex protobuf::util::MessageDifferencer class.  For
33   // our purposes we just say that two protobufs are equal if their serialized
34   // representations are equal.
35   string serialized1, serialized2;
36   m1.AppendToString(&serialized1);
37   m2.AppendToString(&serialized2);
38   return (serialized1 == serialized2);
39 }
40 
DumpProtoToDirectory(const tensorflow::protobuf::Message & message,const string & directory,const string & file_name)41 Status DumpProtoToDirectory(const tensorflow::protobuf::Message& message,
42                             const string& directory, const string& file_name) {
43   tensorflow::Env* env = tensorflow::Env::Default();
44   TF_RETURN_IF_ERROR(env->RecursivelyCreateDir(directory));
45   string safe_file_name = SanitizeFileName(file_name) + ".pb";
46   const string path = tensorflow::io::JoinPath(directory, safe_file_name);
47   return tensorflow::WriteBinaryProto(env, path, message);
48 }
49 
50 }  // namespace protobuf_util
51 }  // namespace xla
52