Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/tensorflow/python/saved_model/fingerprinting.py: 33%

46 statements  

« prev     ^ index     » next       coverage.py v7.4.0, created at 2024-01-03 07:57 +0000

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"""Methods for SavedModel fingerprinting. 

16 

17This module contains classes and functions for reading the SavedModel 

18fingerprint. 

19""" 

20 

21from tensorflow.core.protobuf import fingerprint_pb2 

22from tensorflow.python.saved_model.pywrap_saved_model import fingerprinting as fingerprinting_pywrap 

23from tensorflow.python.util.tf_export import tf_export 

24 

25 

26@tf_export("saved_model.experimental.Fingerprint", v1=[]) 

27class Fingerprint(object): 

28 """The SavedModel fingerprint. 

29 

30 Each attribute of this class is named after a field name in the 

31 FingerprintDef proto and contains the value of the respective field in the 

32 protobuf. 

33 

34 Attributes: 

35 saved_model_checksum: A uint64 containing the `saved_model_checksum`. 

36 graph_def_program_hash: A uint64 containing `graph_def_program_hash`. 

37 signature_def_hash: A uint64 containing the `signature_def_hash`. 

38 saved_object_graph_hash: A uint64 containing the `saved_object_graph_hash`. 

39 checkpoint_hash: A uint64 containing the`checkpoint_hash`. 

40 version: An int32 containing the producer field of the VersionDef. 

41 """ 

42 

43 def __init__( 

44 self, 

45 saved_model_checksum=None, 

46 graph_def_program_hash=None, 

47 signature_def_hash=None, 

48 saved_object_graph_hash=None, 

49 checkpoint_hash=None, 

50 version=None, 

51 ): 

52 """Initializes the instance based on values in the SavedModel fingerprint. 

53 

54 Args: 

55 saved_model_checksum: Value of the`saved_model_checksum`. 

56 graph_def_program_hash: Value of the `graph_def_program_hash`. 

57 signature_def_hash: Value of the `signature_def_hash`. 

58 saved_object_graph_hash: Value of the `saved_object_graph_hash`. 

59 checkpoint_hash: Value of the `checkpoint_hash`. 

60 version: Value of the producer field of the VersionDef. 

61 """ 

62 self.saved_model_checksum = saved_model_checksum 

63 self.graph_def_program_hash = graph_def_program_hash 

64 self.signature_def_hash = signature_def_hash 

65 self.saved_object_graph_hash = saved_object_graph_hash 

66 self.checkpoint_hash = checkpoint_hash 

67 self.version = version 

68 

69 @classmethod 

70 def from_proto(cls, proto): 

71 """Constructs Fingerprint object from protocol buffer message.""" 

72 if isinstance(proto, bytes): 

73 proto = fingerprint_pb2.FingerprintDef.FromString(proto) 

74 try: 

75 return Fingerprint( 

76 proto.saved_model_checksum, 

77 proto.graph_def_program_hash, 

78 proto.signature_def_hash, 

79 proto.saved_object_graph_hash, 

80 proto.checkpoint_hash, 

81 proto.version) 

82 except AttributeError as e: 

83 raise ValueError( 

84 f"Given proto could not be deserialized as fingerprint." 

85 f"{e}") from None 

86 

87 def __eq__(self, other): 

88 if (isinstance(other, Fingerprint) or 

89 isinstance(other, fingerprint_pb2.FingerprintDef)): 

90 try: 

91 return ( 

92 self.saved_model_checksum == other.saved_model_checksum and 

93 self.graph_def_program_hash == other.graph_def_program_hash and 

94 self.signature_def_hash == other.signature_def_hash and 

95 self.saved_object_graph_hash == other.saved_object_graph_hash and 

96 self.checkpoint_hash == other.checkpoint_hash) 

97 except AttributeError: 

98 pass 

99 return False 

100 

101 def __str__(self): 

102 return "\n".join([ 

103 f"SavedModel Fingerprint", 

104 f" saved_model_checksum: {self.saved_model_checksum}", 

105 f" graph_def_program_hash: {self.graph_def_program_hash}", 

106 f" signature_def_hash: {self.signature_def_hash}", 

107 f" saved_object_graph_hash: {self.saved_object_graph_hash}", 

108 f" checkpoint_hash: {self.checkpoint_hash}" 

109 ]) 

110 

111 def __repr__(self): 

112 return (f"Fingerprint({self.saved_model_checksum}, " 

113 f"{self.graph_def_program_hash}, " 

114 f"{self.signature_def_hash}, " 

115 f"{self.saved_object_graph_hash}, " 

116 f"{self.checkpoint_hash})") 

117 

118 def singleprint(self): 

119 """Canonical fingerprinting ID for a SavedModel. 

120 

121 Uniquely identifies a SavedModel based on the regularized fingerprint 

122 attributes. (saved_model_checksum is sensitive to immaterial changes and 

123 thus non-deterministic.) 

124 

125 Returns: 

126 The string concatenation of `graph_def_program_hash`, 

127 `signature_def_hash`, `saved_object_graph_hash`, and `checkpoint_hash` 

128 fingerprint attributes (separated by '/'). 

129 

130 Raises: 

131 ValueError: If the fingerprint fields cannot be used to construct the 

132 singleprint. 

133 """ 

134 try: 

135 return fingerprinting_pywrap.Singleprint(self.graph_def_program_hash, 

136 self.signature_def_hash, 

137 self.saved_object_graph_hash, 

138 self.checkpoint_hash) 

139 except (TypeError, fingerprinting_pywrap.FingerprintException) as e: 

140 raise ValueError( 

141 f"Encounted invalid fingerprint values when constructing singleprint." 

142 f"graph_def_program_hash: {self.graph_def_program_hash}" 

143 f"signature_def_hash: {self.signature_def_hash}" 

144 f"saved_object_graph_hash: {self.saved_object_graph_hash}" 

145 f"checkpoint_hash: {self.checkpoint_hash}" 

146 f"{e}") from None 

147 

148 

149@tf_export("saved_model.experimental.read_fingerprint", v1=[]) 

150def read_fingerprint(export_dir): 

151 """Reads the fingerprint of a SavedModel in `export_dir`. 

152 

153 Returns a `tf.saved_model.experimental.Fingerprint` object that contains 

154 the values of the SavedModel fingerprint, which is persisted on disk in the 

155 `fingerprint.pb` file in the `export_dir`. 

156 

157 Read more about fingerprints in the SavedModel guide at 

158 https://www.tensorflow.org/guide/saved_model. 

159 

160 Args: 

161 export_dir: The directory that contains the SavedModel. 

162 

163 Returns: 

164 A `tf.saved_model.experimental.Fingerprint`. 

165 

166 Raises: 

167 FileNotFoundError: If no or an invalid fingerprint is found. 

168 """ 

169 try: 

170 fingerprint = fingerprinting_pywrap.ReadSavedModelFingerprint(export_dir) 

171 except fingerprinting_pywrap.FileNotFoundException as e: 

172 raise FileNotFoundError(f"SavedModel Fingerprint Error: {e}") from None # pylint: disable=raise-missing-from 

173 except fingerprinting_pywrap.FingerprintException as e: 

174 raise RuntimeError(f"SavedModel Fingerprint Error: {e}") from None # pylint: disable=raise-missing-from 

175 return Fingerprint.from_proto( 

176 fingerprint_pb2.FingerprintDef().FromString(fingerprint))