xref: /aosp_15_r20/external/tensorflow/tensorflow/compiler/xla/protobuf_util.h (revision b6fb3261f9314811a0f4371741dbb8839866f948)
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 #ifndef TENSORFLOW_COMPILER_XLA_PROTOBUF_UTIL_H_
17 #define TENSORFLOW_COMPILER_XLA_PROTOBUF_UTIL_H_
18 
19 #include "absl/time/time.h"
20 #include "tensorflow/compiler/xla/statusor.h"
21 #include "tensorflow/compiler/xla/types.h"
22 #include "tensorflow/core/platform/protobuf.h"
23 
24 namespace xla {
25 namespace protobuf_util {
26 
27 // Returns true if m1 is equal to m2.
28 //
29 // WARNING: We use protocol buffer serialization and then check for
30 // equality of the serialized representation, which may miss some
31 // cases of equality.  However, for the purposes of the XLA code
32 // base, this form of equality checking is sufficient.
33 extern bool ProtobufEquals(const tensorflow::protobuf::Message& m1,
34                            const tensorflow::protobuf::Message& m2);
35 
36 // Return the hash of the message "m".
37 //
38 // WARNING: This uses the same serialization approach used by ProtobufEquals,
39 // so the WARNING for that function applies here.
40 size_t ProtobufHash(const tensorflow::protobuf::Message& m);
41 
42 // Wrappers for above methods so that they can be used in containers.
43 class ProtobufEqualsWrapper {
44  public:
operator()45   bool operator()(const tensorflow::protobuf::Message& m1,
46                   const tensorflow::protobuf::Message& m2) const {
47     return ProtobufEquals(m1, m2);
48   }
49 };
50 
51 class ProtobufHashWrapper {
52  public:
operator()53   size_t operator()(const tensorflow::protobuf::Message& m) const {
54     return ProtobufHash(m);
55   }
56 };
57 // Writes the given message in binary proto to the path formed by joining
58 // 'directory/file_name.pb'. The 'directory' is recursively created if it
59 // doesn't already exist, and the 'file_name' is sanitized by replacing
60 // illegal characters with underscore '_'.
61 //
62 // If 'full_name' is not null then it is set to the name of the file the
63 // protobuf was written to.
64 Status DumpProtoToDirectory(const tensorflow::protobuf::Message& message,
65                             const std::string& directory,
66                             const std::string& file_name,
67                             std::string* full_path = nullptr);
68 
69 // Registers a function that may either expand a dirpath or forward the original
70 // dirpath along as-is.
71 void RegisterDirectoryExpander(
72     const std::function<std::string(std::string)>& expander);
73 
74 }  // namespace protobuf_util
75 }  // namespace xla
76 
77 #endif  // TENSORFLOW_COMPILER_XLA_PROTOBUF_UTIL_H_
78