xref: /aosp_15_r20/external/tensorflow/tensorflow/cc/saved_model/fingerprinting.h (revision b6fb3261f9314811a0f4371741dbb8839866f948)
1 /* Copyright 2022 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_CC_SAVED_MODEL_FINGERPRINTING_H_
17 #define TENSORFLOW_CC_SAVED_MODEL_FINGERPRINTING_H_
18 
19 #include <string>
20 
21 #include "google/protobuf/map.h"
22 #include "tensorflow/core/framework/graph.pb.h"
23 #include "tensorflow/core/platform/types.h"
24 #include "tensorflow/core/protobuf/fingerprint.pb.h"
25 #include "tensorflow/core/protobuf/meta_graph.pb.h"
26 
27 namespace tensorflow::fingerprinting {
28 
29 // Computes the Fingerprint64 hash of the GraphDef.
30 uint64 ComputeHash(const GraphDef& graph_def);
31 
32 // Sorts and computes the Fingerprint64 hash of the SignatureDefs.
33 uint64 RegularizeAndHashSignatureDefs(
34     const google::protobuf::Map<std::string, SignatureDef>& signature_def_map);
35 
36 // Canonicalizes and computes the Fingerprint64 hash of the SavedObjectGraph.
37 uint64 RegularizeAndHashSavedObjectGraph(
38     const SavedObjectGraph& object_graph_def);
39 
40 // Creates a FingerprintDef proto from a MetaGraph and the checkpoint meta file
41 // (.index) in `export_dir`.
42 FingerprintDef CreateFingerprintDef(const MetaGraphDef& metagraph,
43                                     absl::string_view export_dir);
44 
45 // Canonicalizes the GraphDef in order to remove sources of non-determinism.
46 void CanonicalizeGraphDef(GraphDef& graph_def);
47 
48 }  // namespace tensorflow::fingerprinting
49 
50 #endif  // TENSORFLOW_CC_SAVED_MODEL_FINGERPRINTING_H_
51