luclima commited on
Commit
2e29dde
·
verified ·
1 Parent(s): d2a4bca

Upload folder using huggingface_hub (part 47)

Browse files
This view is limited to 50 files because it contains too many changes.   See raw diff
Files changed (50) hide show
  1. .gitattributes +4 -0
  2. western_ai/usa/tensorflow/tensorflow/cc/saved_model/BUILD +682 -0
  3. western_ai/usa/tensorflow/tensorflow/cc/saved_model/README.md +1 -0
  4. western_ai/usa/tensorflow/tensorflow/cc/saved_model/bundle_v2.cc +234 -0
  5. western_ai/usa/tensorflow/tensorflow/cc/saved_model/bundle_v2.h +90 -0
  6. western_ai/usa/tensorflow/tensorflow/cc/saved_model/bundle_v2_test.cc +158 -0
  7. western_ai/usa/tensorflow/tensorflow/cc/saved_model/constants.h +82 -0
  8. western_ai/usa/tensorflow/tensorflow/cc/saved_model/experimental/public/BUILD +85 -0
  9. western_ai/usa/tensorflow/tensorflow/cc/saved_model/experimental/public/concrete_function.h +61 -0
  10. western_ai/usa/tensorflow/tensorflow/cc/saved_model/experimental/public/concrete_function_list.h +63 -0
  11. western_ai/usa/tensorflow/tensorflow/cc/saved_model/experimental/public/function_metadata.h +47 -0
  12. western_ai/usa/tensorflow/tensorflow/cc/saved_model/experimental/public/saved_model_api.h +155 -0
  13. western_ai/usa/tensorflow/tensorflow/cc/saved_model/experimental/public/signature_def_function.h +89 -0
  14. western_ai/usa/tensorflow/tensorflow/cc/saved_model/experimental/public/signature_def_function_metadata.h +47 -0
  15. western_ai/usa/tensorflow/tensorflow/cc/saved_model/experimental/tests/BUILD +28 -0
  16. western_ai/usa/tensorflow/tensorflow/cc/saved_model/experimental/tests/saved_model_api_test.cc +98 -0
  17. western_ai/usa/tensorflow/tensorflow/cc/saved_model/fingerprinting.cc +285 -0
  18. western_ai/usa/tensorflow/tensorflow/cc/saved_model/fingerprinting.h +47 -0
  19. western_ai/usa/tensorflow/tensorflow/cc/saved_model/fingerprinting_chunked_test.cc +52 -0
  20. western_ai/usa/tensorflow/tensorflow/cc/saved_model/fingerprinting_test.cc +210 -0
  21. western_ai/usa/tensorflow/tensorflow/cc/saved_model/fingerprinting_utils.cc +488 -0
  22. western_ai/usa/tensorflow/tensorflow/cc/saved_model/fingerprinting_utils.h +137 -0
  23. western_ai/usa/tensorflow/tensorflow/cc/saved_model/fingerprinting_utils_test.cc +396 -0
  24. western_ai/usa/tensorflow/tensorflow/cc/saved_model/fingerprinting_x_platform_utils.cc +36 -0
  25. western_ai/usa/tensorflow/tensorflow/cc/saved_model/fingerprinting_x_platform_utils.h +28 -0
  26. western_ai/usa/tensorflow/tensorflow/cc/saved_model/image_format/BUILD +39 -0
  27. western_ai/usa/tensorflow/tensorflow/cc/saved_model/image_format/README.md +21 -0
  28. western_ai/usa/tensorflow/tensorflow/cc/saved_model/image_format/internal_api.cc +143 -0
  29. western_ai/usa/tensorflow/tensorflow/cc/saved_model/image_format/internal_api.h +65 -0
  30. western_ai/usa/tensorflow/tensorflow/cc/saved_model/loader.cc +575 -0
  31. western_ai/usa/tensorflow/tensorflow/cc/saved_model/loader.h +150 -0
  32. western_ai/usa/tensorflow/tensorflow/cc/saved_model/loader_util.cc +96 -0
  33. western_ai/usa/tensorflow/tensorflow/cc/saved_model/loader_util.h +40 -0
  34. western_ai/usa/tensorflow/tensorflow/cc/saved_model/metrics.cc +324 -0
  35. western_ai/usa/tensorflow/tensorflow/cc/saved_model/metrics.h +147 -0
  36. western_ai/usa/tensorflow/tensorflow/cc/saved_model/metrics_test.cc +208 -0
  37. western_ai/usa/tensorflow/tensorflow/cc/saved_model/python/BUILD +18 -0
  38. western_ai/usa/tensorflow/tensorflow/cc/saved_model/python/loader.clif +4 -0
  39. western_ai/usa/tensorflow/tensorflow/cc/saved_model/reader.cc +163 -0
  40. western_ai/usa/tensorflow/tensorflow/cc/saved_model/reader.h +59 -0
  41. western_ai/usa/tensorflow/tensorflow/cc/saved_model/reader_test.cc +143 -0
  42. western_ai/usa/tensorflow/tensorflow/cc/saved_model/saved_model_bundle_lite_test.cc +266 -0
  43. western_ai/usa/tensorflow/tensorflow/cc/saved_model/saved_model_bundle_test.cc +405 -0
  44. western_ai/usa/tensorflow/tensorflow/cc/saved_model/saved_model_fuzz.cc +44 -0
  45. western_ai/usa/tensorflow/tensorflow/cc/saved_model/signature_constants.h +69 -0
  46. western_ai/usa/tensorflow/tensorflow/cc/saved_model/tag_constants.h +35 -0
  47. western_ai/usa/tensorflow/tensorflow/cc/saved_model/test_utils.h +53 -0
  48. western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/AssetModule/assets/test_asset.txt +1 -0
  49. western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/AssetModule/saved_model.pb +3 -0
  50. western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/AssetModule/variables/variables.data-00000-of-00001 +0 -0
.gitattributes CHANGED
@@ -1244,3 +1244,7 @@ western_ai/usa/stable-diffusion-webui/modules/Roboto-Regular.ttf filter=lfs diff
1244
  western_ai/usa/stable-diffusion-webui/modules/textual_inversion/test_embedding.png filter=lfs diff=lfs merge=lfs -text
1245
  western_ai/usa/stable-diffusion-webui/screenshot.png filter=lfs diff=lfs merge=lfs -text
1246
  western_ai/usa/tensorflow/tensorflow/c/testdata/tf_record filter=lfs diff=lfs merge=lfs -text
 
 
 
 
 
1244
  western_ai/usa/stable-diffusion-webui/modules/textual_inversion/test_embedding.png filter=lfs diff=lfs merge=lfs -text
1245
  western_ai/usa/stable-diffusion-webui/screenshot.png filter=lfs diff=lfs merge=lfs -text
1246
  western_ai/usa/tensorflow/tensorflow/c/testdata/tf_record filter=lfs diff=lfs merge=lfs -text
1247
+ western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/chunked_saved_model/chunked_model/saved_model.cpb filter=lfs diff=lfs merge=lfs -text
1248
+ western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/fuzz_generated/bad_node_attr/variables/variables.data-00000-of-00001 filter=lfs diff=lfs merge=lfs -text
1249
+ western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/fuzz_generated/const_with_no_value/variables/variables.data-00000-of-00001 filter=lfs diff=lfs merge=lfs -text
1250
+ western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/fuzz_generated/negative_shape/variables/variables.data-00000-of-00001 filter=lfs diff=lfs merge=lfs -text
western_ai/usa/tensorflow/tensorflow/cc/saved_model/BUILD ADDED
@@ -0,0 +1,682 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #Description:
2
+ # TensorFlow SavedModel.
3
+
4
+ load("@xla//third_party/rules_python/python:py_binary.bzl", "py_binary")
5
+ load(
6
+ "//tensorflow:tensorflow.bzl",
7
+ "if_android",
8
+ "if_google",
9
+ "if_mobile",
10
+ "if_not_mobile",
11
+ "if_not_windows_or_mac",
12
+ "tf_cc_test",
13
+ )
14
+ load("//tensorflow:tensorflow.default.bzl", "filegroup")
15
+ load(
16
+ "//tensorflow/core/platform:build_config_root.bzl",
17
+ "if_static",
18
+ )
19
+ load("//tensorflow/core/platform:rules_cc.bzl", "cc_library")
20
+ load(
21
+ "//tensorflow/security/fuzzing:tf_fuzzing.bzl",
22
+ "tf_cc_fuzz_test",
23
+ )
24
+
25
+ package(
26
+ # copybara:uncomment default_applicable_licenses = ["//tensorflow:license"],
27
+ default_visibility = ["//visibility:public"],
28
+ licenses = ["notice"],
29
+ )
30
+
31
+ exports_files([
32
+ "loader.h",
33
+ "testdata/chunked_saved_model/chunked_model/saved_model.cpb",
34
+ "testdata/chunked_saved_model/chunked_model/saved_model.pbtxt",
35
+ ])
36
+
37
+ cc_library(
38
+ name = "constants",
39
+ hdrs = ["constants.h"],
40
+ deps = ["@com_google_absl//absl/strings"],
41
+ )
42
+
43
+ cc_library(
44
+ name = "signature_constants",
45
+ hdrs = ["signature_constants.h"],
46
+ )
47
+
48
+ cc_library(
49
+ name = "tag_constants",
50
+ hdrs = ["tag_constants.h"],
51
+ )
52
+
53
+ # copybara:uncomment_begin(google-only)
54
+ # cc_library(
55
+ # name = "mobile_only_deps",
56
+ # visibility = ["//visibility:private"],
57
+ # deps = if_mobile(["//tensorflow/core:portable_tensorflow_lib"]),
58
+ # )
59
+ # copybara:uncomment_end
60
+
61
+ cc_library(
62
+ name = "reader",
63
+ srcs = ["reader.cc"],
64
+ hdrs = ["reader.h"],
65
+ deps = [
66
+ ":constants",
67
+ ":metrics",
68
+ ":util",
69
+ "//tensorflow/core:protos_all_cc",
70
+ "@com_google_absl//absl/status:statusor",
71
+ ] + if_google([
72
+ "//tensorflow/tools/proto_splitter:merge",
73
+ ]) + if_not_mobile([
74
+ # TODO(b/111634734): :lib and :protos_all contain dependencies that
75
+ # cannot be built on mobile platforms. Instead, include the appropriate
76
+ # tf_lib depending on the build platform.
77
+ "@com_google_absl//absl/memory:memory",
78
+ "//tensorflow/core:lib",
79
+ "//tensorflow/core/util/tensor_bundle:byteswaptensor",
80
+ ]),
81
+ )
82
+
83
+ tf_cc_test(
84
+ name = "reader_test",
85
+ srcs = ["reader_test.cc"],
86
+ data = [
87
+ ":saved_model_test_files",
88
+ ],
89
+ linkstatic = 1,
90
+ deps = [
91
+ ":constants",
92
+ ":metrics",
93
+ ":reader",
94
+ ":tag_constants",
95
+ "//tensorflow/core:lib",
96
+ "//tensorflow/core:test",
97
+ "//tensorflow/core/platform:resource_loader",
98
+ "@com_google_googletest//:gtest_main",
99
+ ],
100
+ )
101
+
102
+ cc_library(
103
+ name = "loader_default_deps",
104
+ visibility = ["//visibility:private"],
105
+ deps = if_static([
106
+ "//tensorflow/core:all_kernels",
107
+ "//tensorflow/core:direct_session",
108
+ ]) + if_google(if_static(["//tensorflow/core/platform/default/build_config:tensorflow_platform_specific"])),
109
+ )
110
+
111
+ cc_library(
112
+ name = "loader",
113
+ hdrs = ["loader.h"],
114
+ deps = [
115
+ ":loader_lite",
116
+ ] + select({
117
+ "//tensorflow:android": [],
118
+ "//tensorflow:ios": [],
119
+ "//tensorflow:tflite_converter": ["//tensorflow/core:direct_session"] + if_google([
120
+ "//tensorflow/core/platform/default/build_config:tensorflow_platform_specific",
121
+ ]),
122
+ "//conditions:default": [":loader_default_deps"],
123
+ }) + if_not_mobile([
124
+ "//tensorflow/core:core_cpu",
125
+ "//tensorflow/core:lib",
126
+ "//tensorflow/core:ops",
127
+ "//tensorflow/core:protos_all_cc",
128
+ ]) + if_android([
129
+ "//tensorflow/core:portable_tensorflow_lib",
130
+ ]),
131
+ )
132
+
133
+ cc_library(
134
+ name = "loader_lite",
135
+ hdrs = ["loader.h"],
136
+ deps = if_static([
137
+ ":loader_lite_impl",
138
+ ]) + if_not_mobile([
139
+ "//tensorflow/core:core_cpu",
140
+ "//tensorflow/core:lib",
141
+ "//tensorflow/core:protos_all_cc",
142
+ ]),
143
+ )
144
+
145
+ cc_library(
146
+ name = "loader_lite_impl",
147
+ srcs = ["loader.cc"],
148
+ hdrs = ["loader.h"],
149
+ deps = [
150
+ ":constants",
151
+ ":fingerprinting",
152
+ ":loader_util",
153
+ ":reader",
154
+ "@com_google_absl//absl/status",
155
+ "@com_google_absl//absl/strings",
156
+ ] + if_not_mobile([
157
+ ":metrics",
158
+ ":util",
159
+ "//tensorflow/core:core_cpu",
160
+ "//tensorflow/core:framework",
161
+ "//tensorflow/core:lib",
162
+ "//tensorflow/core:lib_internal",
163
+ "//tensorflow/core:protos_all_cc",
164
+ "//tensorflow/core/util/tensor_bundle:naming",
165
+ ]),
166
+ alwayslink = 1,
167
+ )
168
+
169
+ cc_library(
170
+ name = "bundle_v2",
171
+ srcs = ["bundle_v2.cc"],
172
+ hdrs = ["bundle_v2.h"],
173
+ deps = [
174
+ ":constants",
175
+ ":fingerprinting",
176
+ ":metrics",
177
+ ":reader",
178
+ ":util",
179
+ "//tensorflow/core:lib",
180
+ "//tensorflow/core:protos_all_cc",
181
+ "//tensorflow/core/platform:errors",
182
+ "//tensorflow/core/platform:strcat",
183
+ "//tensorflow/core/platform:tstring",
184
+ "//tensorflow/core/util/tensor_bundle",
185
+ "//tensorflow/core/util/tensor_bundle:byteswaptensor",
186
+ "@com_google_absl//absl/container:flat_hash_set",
187
+ "@com_google_absl//absl/log",
188
+ "@com_google_absl//absl/status",
189
+ "@jsoncpp_git//:jsoncpp",
190
+ "@tsl//tsl/platform:errors",
191
+ "@tsl//tsl/platform:statusor",
192
+ "@tsl//tsl/platform:strcat",
193
+ ],
194
+ )
195
+
196
+ cc_library(
197
+ name = "loader_util",
198
+ srcs = ["loader_util.cc"],
199
+ hdrs = ["loader_util.h"],
200
+ deps = [":constants"] + if_not_mobile([
201
+ "//tensorflow/core:lib",
202
+ "//tensorflow/core:lib_internal",
203
+ "//tensorflow/core:protos_all_cc",
204
+ ]),
205
+ )
206
+
207
+ tf_cc_test(
208
+ name = "bundle_v2_test",
209
+ srcs = ["bundle_v2_test.cc"],
210
+ data = [
211
+ ":saved_model_test_files",
212
+ ],
213
+ linkstatic = 1,
214
+ deps = [
215
+ ":bundle_v2",
216
+ ":metrics",
217
+ "//tensorflow/core:lib",
218
+ "//tensorflow/core:test",
219
+ "//tensorflow/core:test_main",
220
+ "//tensorflow/core/platform:test",
221
+ "//tensorflow/core/protobuf:for_core_protos_cc",
222
+ "@com_google_absl//absl/status",
223
+ "@com_google_absl//absl/status:statusor",
224
+ "@com_google_absl//absl/strings",
225
+ "@jsoncpp_git//:jsoncpp",
226
+ "@tsl//tsl/platform:statusor",
227
+ ],
228
+ )
229
+
230
+ tf_cc_test(
231
+ name = "saved_model_bundle_test",
232
+ srcs = ["saved_model_bundle_test.cc"],
233
+ data = [
234
+ ":saved_model_test_files",
235
+ ],
236
+ linkstatic = 1,
237
+ deps = [
238
+ ":constants",
239
+ ":loader",
240
+ ":metrics",
241
+ ":reader",
242
+ ":signature_constants",
243
+ ":tag_constants",
244
+ "//tensorflow/core:lib",
245
+ "//tensorflow/core:protos_all_cc",
246
+ "//tensorflow/core:tensorflow",
247
+ "//tensorflow/core:test",
248
+ "//tensorflow/core:test_main",
249
+ "//tensorflow/core:testlib",
250
+ ],
251
+ )
252
+
253
+ tf_cc_test(
254
+ name = "saved_model_bundle_lite_test",
255
+ srcs = ["saved_model_bundle_lite_test.cc"],
256
+ data = [
257
+ ":saved_model_test_files",
258
+ ],
259
+ linkstatic = 1,
260
+ deps = [
261
+ ":constants",
262
+ ":loader",
263
+ ":signature_constants",
264
+ ":tag_constants",
265
+ "//tensorflow/core:lib",
266
+ "//tensorflow/core:protos_all_cc",
267
+ "//tensorflow/core:tensorflow",
268
+ "//tensorflow/core:test",
269
+ "//tensorflow/core:test_main",
270
+ "//tensorflow/core:testlib",
271
+ "@com_google_absl//absl/status",
272
+ ],
273
+ )
274
+
275
+ # A subset of the TF2 saved models can be generated with this tool.
276
+ py_binary(
277
+ name = "testdata/generate_saved_models",
278
+ srcs = ["testdata/generate_saved_models.py"],
279
+ data = [
280
+ ":saved_model_asset_data",
281
+ ":saved_model_static_hashtable_asset_data",
282
+ ],
283
+ strict_deps = True,
284
+ deps = [
285
+ "//tensorflow/python/client:session",
286
+ "//tensorflow/python/compat:v2_compat",
287
+ "//tensorflow/python/eager:def_function",
288
+ "//tensorflow/python/framework:dtypes",
289
+ "//tensorflow/python/framework:ops",
290
+ "//tensorflow/python/framework:tensor_spec",
291
+ "//tensorflow/python/module",
292
+ "//tensorflow/python/ops:io_ops",
293
+ "//tensorflow/python/ops:lookup_ops",
294
+ "//tensorflow/python/ops:variables",
295
+ "//tensorflow/python/platform:client_testlib",
296
+ "//tensorflow/python/saved_model",
297
+ "//tensorflow/python/saved_model:save_options",
298
+ "//tensorflow/python/trackable:asset",
299
+ "@absl_py//absl:app",
300
+ ],
301
+ )
302
+
303
+ # copybara:uncomment_begin(google-only)
304
+ #
305
+ # py_binary(
306
+ # name = "testdata/generate_chunked_models",
307
+ # srcs = ["testdata/generate_chunked_models.py"],
308
+ # strict_deps = True,
309
+ # deps = [
310
+ # "//tensorflow/python/compat:v2_compat",
311
+ # "//tensorflow/python/eager:def_function",
312
+ # "//tensorflow/python/framework:constant_op",
313
+ # "//tensorflow/python/lib/io:file_io",
314
+ # "//tensorflow/python/module",
315
+ # "//tensorflow/python/saved_model:loader",
316
+ # "//tensorflow/python/saved_model:save",
317
+ # "//tensorflow/python/saved_model:save_options",
318
+ # "//tensorflow/python/util:compat",
319
+ # "//tensorflow/tools/proto_splitter:constants",
320
+ # "//tensorflow/tools/proto_splitter/python:saved_model",
321
+ # "//third_party/py/numpy",
322
+ # "@absl_py//absl:app",
323
+ # "@absl_py//absl/flags",
324
+ # ],
325
+ # )
326
+ #
327
+ # copybara:uncomment_end
328
+
329
+ # TODO(b/32673259): add a test to continuously validate these files.
330
+ filegroup(
331
+ name = "saved_model_test_files",
332
+ srcs = glob([
333
+ "testdata/AssetModule/**",
334
+ "testdata/half_plus_two_pbtxt/**",
335
+ "testdata/half_plus_two_main_op/**",
336
+ "testdata/half_plus_two/**",
337
+ "testdata/half_plus_two_v2/**",
338
+ "testdata/x_plus_y_v2_debuginfo/**",
339
+ "testdata/CyclicModule/**",
340
+ "testdata/StaticHashTableModule/**",
341
+ "testdata/VarsAndArithmeticObjectGraph/**",
342
+ "testdata/fuzz_generated/**",
343
+ "testdata/SimpleV1Model/**",
344
+ "testdata/OptimizerSlotVariableModule/**",
345
+ "testdata/chunked_saved_model/**",
346
+ ]),
347
+ )
348
+
349
+ filegroup(
350
+ name = "saved_model_fingerprinting_test_files",
351
+ srcs = glob([
352
+ "testdata/bert2/**",
353
+ "testdata/bert1/**",
354
+ "testdata/half_plus_two_pbtxt/**",
355
+ ]),
356
+ )
357
+
358
+ alias(
359
+ name = "saved_model_half_plus_two",
360
+ actual = ":saved_model_test_files",
361
+ )
362
+
363
+ filegroup(
364
+ name = "saved_model_asset_data",
365
+ srcs = [
366
+ "testdata/test_asset.txt",
367
+ ],
368
+ )
369
+
370
+ filegroup(
371
+ name = "saved_model_static_hashtable_asset_data",
372
+ srcs = [
373
+ "testdata/static_hashtable_asset.txt",
374
+ ],
375
+ )
376
+
377
+ exports_files(
378
+ glob([
379
+ "testdata/half_plus_two_pbtxt/**",
380
+ "testdata/half_plus_two_main_op/**",
381
+ "testdata/half_plus_two/**",
382
+ "testdata/half_plus_two_v2/**",
383
+ "testdata/x_plus_y_v2_debuginfo/**",
384
+ "testdata/CyclicModule/**",
385
+ "testdata/VarsAndArithmeticObjectGraph/**",
386
+ "testdata/fuzz_generated/**",
387
+ ]),
388
+ )
389
+
390
+ # Linked directly into ":tensorflow_framework".
391
+ cc_library(
392
+ name = "metrics_impl",
393
+ srcs = [
394
+ "metrics.cc",
395
+ "metrics.h",
396
+ ],
397
+ visibility = [
398
+ "//tensorflow:__pkg__",
399
+ "//tensorflow/python:__pkg__",
400
+ ],
401
+ deps = [
402
+ "//tensorflow/core:protos_all_cc",
403
+ "@com_google_absl//absl/status",
404
+ "@com_google_absl//absl/status:statusor",
405
+ "@com_google_absl//absl/strings",
406
+ "@jsoncpp_git//:jsoncpp",
407
+ ] + if_not_mobile([
408
+ "//tensorflow/core:lib",
409
+ ]) + if_android([
410
+ "//tensorflow/core:portable_tensorflow_lib_lite",
411
+ ]),
412
+ alwayslink = True,
413
+ )
414
+
415
+ cc_library(
416
+ name = "metrics",
417
+ hdrs = ["metrics.h"],
418
+ visibility = [
419
+ "//tensorflow/cc/saved_model/image_format:__subpackages__",
420
+ "//tensorflow/python/saved_model:__subpackages__",
421
+ ],
422
+ deps = if_static([
423
+ ":metrics_impl",
424
+ ]) + if_not_mobile([
425
+ "//tensorflow/core:lib",
426
+ ]) + if_android([
427
+ "//tensorflow/core:portable_tensorflow_lib_lite",
428
+ ]) + [
429
+ "//tensorflow/core/protobuf:for_core_protos_cc",
430
+ "@com_google_absl//absl/status",
431
+ "@com_google_absl//absl/status:statusor",
432
+ "@com_google_absl//absl/strings",
433
+ ],
434
+ )
435
+
436
+ tf_cc_test(
437
+ name = "metrics_test",
438
+ size = "small",
439
+ srcs = ["metrics_test.cc"],
440
+ deps = [
441
+ ":metrics",
442
+ "//tensorflow/core:protos_all_cc",
443
+ "//tensorflow/core:test",
444
+ "//tensorflow/core:test_main",
445
+ "//tensorflow/core/platform:status_matchers",
446
+ "@com_google_absl//absl/status",
447
+ "@com_google_googletest//:gtest",
448
+ "@jsoncpp_git//:jsoncpp",
449
+ "@tsl//tsl/platform:statusor",
450
+ ],
451
+ )
452
+
453
+ cc_library(
454
+ name = "util",
455
+ srcs = ["util.cc"],
456
+ hdrs = ["util.h"],
457
+ deps = [
458
+ "//tensorflow/core:framework",
459
+ "//tensorflow/core/framework:tensor_proto_cc",
460
+ "//tensorflow/core/platform:errors",
461
+ "//tensorflow/core/platform:status",
462
+ "//tensorflow/core/protobuf:for_core_protos_cc",
463
+ ] + if_not_mobile(["//tensorflow/core:lib"]) + if_android(["//tensorflow/core:portable_tensorflow_lib_lite"]),
464
+ )
465
+
466
+ cc_library(
467
+ name = "test_utils",
468
+ testonly = True,
469
+ hdrs = ["test_utils.h"],
470
+ visibility = ["//visibility:public"],
471
+ deps = [
472
+ "//tensorflow/core:test",
473
+ "//tensorflow/core/platform:protobuf",
474
+ ],
475
+ )
476
+
477
+ tf_cc_test(
478
+ name = "util_test",
479
+ size = "small",
480
+ srcs = ["util_test.cc"],
481
+ deps = [
482
+ ":test_utils",
483
+ ":util",
484
+ "//tensorflow/core:lib",
485
+ "//tensorflow/core:test",
486
+ "//tensorflow/core:test_main",
487
+ "//tensorflow/core/framework:tensor_shape_proto_cc",
488
+ "//tensorflow/core/framework:tensor_testutil",
489
+ "//tensorflow/core/protobuf:for_core_protos_cc",
490
+ "@tsl//tsl/platform:status_matchers",
491
+ ],
492
+ )
493
+
494
+ # Linked directly into ":tensorflow_framework".
495
+ cc_library(
496
+ name = "fingerprinting_impl",
497
+ srcs = [
498
+ "fingerprinting.cc",
499
+ "fingerprinting.h",
500
+ ],
501
+ visibility = [
502
+ "//tensorflow:__pkg__",
503
+ "//tensorflow/python:__pkg__",
504
+ ],
505
+ deps = [
506
+ ":constants",
507
+ ":fingerprinting_x_platform_utils",
508
+ "//tensorflow/core:protos_all_cc",
509
+ "//tensorflow/core/graph/regularization:simple_delete",
510
+ "//tensorflow/core/graph/regularization:util",
511
+ "//tensorflow/core/util/tensor_bundle:naming",
512
+ "@com_google_absl//absl/container:btree",
513
+ "@com_google_absl//absl/log",
514
+ "@com_google_absl//absl/status",
515
+ "@com_google_absl//absl/status:statusor",
516
+ "@com_google_absl//absl/strings",
517
+ "@com_google_absl//absl/strings:str_format",
518
+ "@tsl//tsl/platform:protobuf",
519
+ "@tsl//tsl/platform:types",
520
+ ] + if_not_mobile([
521
+ "//tensorflow/core:lib",
522
+ ]) + if_android([
523
+ "//tensorflow/core:portable_tensorflow_lib_lite",
524
+ ]) + if_not_windows_or_mac([
525
+ ":fingerprinting_utils",
526
+ "//tensorflow/tools/proto_splitter/cc:util",
527
+ ]),
528
+ alwayslink = True,
529
+ )
530
+
531
+ cc_library(
532
+ name = "fingerprinting",
533
+ hdrs = ["fingerprinting.h"],
534
+ visibility = [
535
+ "//learning/brain/contrib/hub/server/ingestion:__subpackages__",
536
+ "//learning/brain/contrib/tpu_modeling:__subpackages__",
537
+ "//learning/gemini/deployment/disaggregation/wiz_cc:__subpackages__",
538
+ "//learning/metadata/artifactoid/cc:__subpackages__",
539
+ "//learning/tfx/pipeline/util:__subpackages__",
540
+ "//tensorflow/core/tfrt:__subpackages__",
541
+ "//tensorflow/python/saved_model:__subpackages__",
542
+ ],
543
+ deps = if_static([
544
+ ":fingerprinting_impl",
545
+ "@com_google_absl//absl/strings",
546
+ "@com_google_absl//absl/status:statusor",
547
+ "//tensorflow/core:protos_all_cc",
548
+ ]) + if_not_mobile([
549
+ "//tensorflow/core:lib",
550
+ ]) + if_android([
551
+ "//tensorflow/core:portable_tensorflow_lib_lite",
552
+ ]),
553
+ )
554
+
555
+ cc_library(
556
+ name = "fingerprinting_utils",
557
+ srcs = ["fingerprinting_utils.cc"],
558
+ hdrs = ["fingerprinting_utils.h"],
559
+ visibility = ["//visibility:private"],
560
+ deps = [
561
+ ":constants",
562
+ ":fingerprinting_x_platform_utils",
563
+ "//tensorflow/core:lib",
564
+ "//tensorflow/core:protos_all_cc",
565
+ "//tensorflow/core/util/tensor_bundle:naming",
566
+ "//tensorflow/tools/proto_splitter:chunk_proto_cc",
567
+ "//tensorflow/tools/proto_splitter:merge",
568
+ "//tensorflow/tools/proto_splitter/cc:util",
569
+ "@com_google_absl//absl/status",
570
+ "@com_google_absl//absl/status:statusor",
571
+ "@com_google_absl//absl/strings",
572
+ "@com_google_absl//absl/strings:str_format",
573
+ "@riegeli//riegeli/bytes:fd_reader",
574
+ "@riegeli//riegeli/records:record_reader",
575
+ ],
576
+ alwayslink = True,
577
+ )
578
+
579
+ cc_library(
580
+ name = "fingerprinting_x_platform_utils",
581
+ srcs = ["fingerprinting_x_platform_utils.cc"],
582
+ hdrs = ["fingerprinting_x_platform_utils.h"],
583
+ deps = [
584
+ "@com_google_absl//absl/numeric:int128",
585
+ "@com_google_absl//absl/strings:str_format",
586
+ "@tsl//tsl/platform:random",
587
+ ],
588
+ )
589
+
590
+ tf_cc_test(
591
+ name = "fingerprinting_utils_test",
592
+ srcs = ["fingerprinting_utils_test.cc"],
593
+ data = [
594
+ "//tensorflow/tools/proto_splitter/testdata:many-field.cpb",
595
+ "//tensorflow/tools/proto_splitter/testdata:split-standard.cpb",
596
+ ],
597
+ tags = [
598
+ "no_mac", # b/291933687
599
+ "no_windows", # b/291001524
600
+ ],
601
+ deps = [
602
+ ":fingerprinting_utils",
603
+ "//tensorflow/core:protos_all_cc",
604
+ "//tensorflow/core/platform:path",
605
+ "//tensorflow/core/platform:protobuf",
606
+ "//tensorflow/tools/proto_splitter:chunk_proto_cc",
607
+ "//tensorflow/tools/proto_splitter/cc:util",
608
+ "//tensorflow/tools/proto_splitter/testdata:test_message_proto_cc",
609
+ "@com_google_absl//absl/status",
610
+ "@com_google_absl//absl/status:statusor",
611
+ "@com_google_absl//absl/strings",
612
+ "@com_google_googletest//:gtest_main",
613
+ "@tsl//tsl/platform:errors",
614
+ "@tsl//tsl/platform:protobuf",
615
+ "@tsl//tsl/platform:status_matchers",
616
+ "@tsl//tsl/platform:statusor",
617
+ "@tsl//tsl/platform:test",
618
+ "@xla//xla/tsl/lib/core:status_test_util",
619
+ ],
620
+ )
621
+
622
+ tf_cc_test(
623
+ name = "fingerprinting_chunked_test",
624
+ size = "small",
625
+ srcs = ["fingerprinting_chunked_test.cc"],
626
+ data = [
627
+ ":saved_model_fingerprinting_test_files",
628
+ ":saved_model_test_files",
629
+ ],
630
+ tags = [
631
+ "no_mac", # b/291933687
632
+ "no_windows", # b/291001524
633
+ ],
634
+ deps = [
635
+ ":fingerprinting",
636
+ "//tensorflow/core:protos_all_cc",
637
+ "//tensorflow/core:test",
638
+ "//tensorflow/core/platform:path",
639
+ "@com_google_absl//absl/container:flat_hash_set",
640
+ "@com_google_googletest//:gtest_main",
641
+ "@tsl//tsl/platform:statusor",
642
+ ],
643
+ )
644
+
645
+ # copybara:uncomment_end
646
+
647
+ tf_cc_test(
648
+ name = "fingerprinting_test",
649
+ size = "small",
650
+ srcs = ["fingerprinting_test.cc"],
651
+ data = [
652
+ ":saved_model_fingerprinting_test_files",
653
+ ":saved_model_test_files",
654
+ ],
655
+ deps = [
656
+ ":fingerprinting",
657
+ "//tensorflow/cc:cc_ops",
658
+ "//tensorflow/core:lib",
659
+ "//tensorflow/core:protos_all_cc",
660
+ "//tensorflow/core:test",
661
+ "//tensorflow/core:test_main",
662
+ "//tensorflow/core/protobuf:for_core_protos_cc",
663
+ "@com_google_absl//absl/numeric:int128",
664
+ "@com_google_absl//absl/status",
665
+ "@com_google_absl//absl/status:statusor",
666
+ "@com_google_absl//absl/strings",
667
+ "@com_google_googletest//:gtest",
668
+ ],
669
+ )
670
+
671
+ tf_cc_fuzz_test(
672
+ name = "saved_model_fuzz",
673
+ srcs = ["saved_model_fuzz.cc"],
674
+ deps = [
675
+ ":constants",
676
+ ":loader",
677
+ ":tag_constants",
678
+ "//tensorflow/core:core_cpu",
679
+ "//tensorflow/core:protos_all_cc",
680
+ "//tensorflow/core:tensorflow",
681
+ ],
682
+ )
western_ai/usa/tensorflow/tensorflow/cc/saved_model/README.md ADDED
@@ -0,0 +1 @@
 
 
1
+ <!--#include file="../../python/saved_model/README.md"-->
western_ai/usa/tensorflow/tensorflow/cc/saved_model/bundle_v2.cc ADDED
@@ -0,0 +1,234 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /* Copyright 2016 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/cc/saved_model/bundle_v2.h"
17
+
18
+ #include <memory>
19
+ #include <string>
20
+ #include <utility>
21
+
22
+ #include "absl/container/flat_hash_set.h"
23
+ #include "absl/log/log.h"
24
+ #include "absl/status/status.h"
25
+ #include "tensorflow/cc/saved_model/constants.h"
26
+ #include "tensorflow/cc/saved_model/fingerprinting.h"
27
+ #include "tensorflow/cc/saved_model/metrics.h"
28
+ #include "tensorflow/cc/saved_model/reader.h"
29
+ #include "tensorflow/core/framework/types.pb.h"
30
+ #include "tensorflow/core/platform/byte_order.h"
31
+ #include "tensorflow/core/platform/env.h"
32
+ #include "tensorflow/core/platform/errors.h"
33
+ #include "tensorflow/core/platform/path.h"
34
+ #include "tensorflow/core/platform/strcat.h"
35
+ #include "tensorflow/core/platform/tstring.h"
36
+ #include "tensorflow/core/protobuf/saved_model.pb.h"
37
+ #include "tensorflow/core/protobuf/saved_object_graph.pb.h"
38
+ #include "tensorflow/core/protobuf/trackable_object_graph.pb.h"
39
+ #include "tensorflow/core/util/tensor_bundle/byte_swap_tensor.h"
40
+ #include "tensorflow/core/util/tensor_bundle/tensor_bundle.h"
41
+ #include "tsl/platform/errors.h"
42
+ #include "tsl/platform/statusor.h"
43
+ #include "tsl/platform/strcat.h"
44
+
45
+ namespace tensorflow {
46
+ namespace {
47
+
48
+ using strings::StrCat;
49
+
50
+ // `tensorflow::SavedModelV2Bundle::Load` API label.
51
+ constexpr char kCCLoadBundleV2Label[] = "cc_load_bundle_v2";
52
+
53
+ absl::Status ReadCheckpointObjectGraph(BundleReader* bundle_reader,
54
+ TrackableObjectGraph* object_graph) {
55
+ Tensor object_graph_tensor;
56
+ TF_RETURN_WITH_CONTEXT_IF_ERROR(
57
+ bundle_reader->Lookup(kObjectGraphProtoKey, &object_graph_tensor),
58
+ "SavedModel checkpoint does not contain object graph.");
59
+ if (object_graph_tensor.dtype() != DT_STRING ||
60
+ object_graph_tensor.dims() != 0 ||
61
+ object_graph_tensor.NumElements() != 1) {
62
+ return absl::Status(
63
+ absl::StatusCode::kFailedPrecondition,
64
+ "SavedModel checkpoint object graph was not the correct type.");
65
+ }
66
+
67
+ if (!object_graph->ParseFromString(object_graph_tensor.scalar<tstring>()())) {
68
+ return absl::Status(
69
+ absl::StatusCode::kFailedPrecondition,
70
+ "SavedModel checkpoint object graph could not be deserialized.");
71
+ }
72
+ return absl::OkStatus();
73
+ }
74
+
75
+ } // namespace
76
+
77
+ absl::Status SavedModelV2Bundle::Load(const std::string& export_dir,
78
+ SavedModelV2Bundle* const bundle) {
79
+ metrics::SavedModelReadApi(kCCLoadBundleV2Label).IncrementBy(1);
80
+ SavedModel saved_model_proto;
81
+ TF_RETURN_IF_ERROR(ReadSavedModel(export_dir, &saved_model_proto));
82
+ metrics::SavedModelReadPath().Set(export_dir);
83
+
84
+ // Load MetaGraphDef.
85
+ // In version 2 SavedModels, there is only one MetaGraphDef.
86
+ if (saved_model_proto.meta_graphs_size() != 1) {
87
+ return absl::Status(
88
+ absl::StatusCode::kInvalidArgument,
89
+ strings::StrCat(
90
+ "SavedModelV2 should have exactly one MetaGraphDef but actually ",
91
+ "contains ", saved_model_proto.meta_graphs_size()));
92
+ }
93
+ bundle->meta_graph_def_ =
94
+ std::move(*saved_model_proto.mutable_meta_graphs(0));
95
+
96
+ // Correct the endiness of Tensor content on big-endian system
97
+ if (!port::kLittleEndian) {
98
+ TF_RETURN_IF_ERROR(
99
+ ByteSwapTensorContentInMetaGraphDef(&(bundle->meta_graph_def_)));
100
+ }
101
+
102
+ // Load GraphDebugInfo.
103
+ TF_RETURN_IF_ERROR(
104
+ ReadSavedModelDebugInfoIfPresent(export_dir, &bundle->debug_info_));
105
+
106
+ const std::string variables_dir =
107
+ io::JoinPath(export_dir, kSavedModelVariablesDirectory);
108
+ if (!Env::Default()->FileExists(variables_dir).ok()) {
109
+ LOG(INFO)
110
+ << "No checkpoint found, assuming this is a program-only SavedModel";
111
+ } else {
112
+ // Load the variables checkpoint reader.
113
+ const std::string variables_prefix =
114
+ io::JoinPath(variables_dir, kSavedModelVariablesFilename);
115
+ bundle->variable_reader_ =
116
+ std::make_unique<BundleReader>(Env::Default(), variables_prefix);
117
+ TF_RETURN_WITH_CONTEXT_IF_ERROR(
118
+ bundle->variable_reader_->status(),
119
+ "Unable to load SavedModel variables checkpoint from ",
120
+ variables_prefix);
121
+
122
+ // Deserialize the object graph proto from the tensor bundle.
123
+ TF_RETURN_IF_ERROR(ReadCheckpointObjectGraph(
124
+ bundle->variable_reader_.get(), &bundle->trackable_object_graph_));
125
+ }
126
+ // Read the fingerprint.
127
+ auto fingerprint_proto =
128
+ saved_model::fingerprinting::ReadSavedModelFingerprint(export_dir);
129
+ if (fingerprint_proto.ok()) {
130
+ metrics::SavedModelReadFingerprint().Set(
131
+ metrics::MakeFingerprintJson(fingerprint_proto.value()));
132
+
133
+ TF_ASSIGN_OR_RETURN(
134
+ std::string path_and_singleprint,
135
+ metrics::MakeSavedModelPathAndSingleprint(
136
+ export_dir, saved_model::fingerprinting::Singleprint(
137
+ fingerprint_proto.value())));
138
+ metrics::SavedModelReadPathAndSingleprint().Set(path_and_singleprint);
139
+ }
140
+
141
+ return absl::OkStatus();
142
+ }
143
+
144
+ absl::Status SavedModelV2Bundle::VisitObjectsToRestore(
145
+ RestoreObjectsCallback callback) {
146
+ if (saved_object_graph().nodes_size() == 0 ||
147
+ trackable_object_graph().nodes_size() == 0) {
148
+ return absl::OkStatus();
149
+ }
150
+
151
+ // Start from root nodes of both the SavedObjectGraph and TrackableObjectGraph
152
+ // and descend to leaves. Note that the TrackableObjectGraph can have cycles
153
+ // (as can the SavedObjectGraph).
154
+ // This is detected and cycle edges are skipped.
155
+ const SavedObject* root_saved_object = &saved_object_graph().nodes(0);
156
+ const TrackableObjectGraph::TrackableObject* root_trackable_object =
157
+ &trackable_object_graph().nodes(0);
158
+ absl::flat_hash_set<int> trackable_node_ids;
159
+ return RecurseObjectsToRestore(root_saved_object, 0, root_trackable_object,
160
+ std::string(), &trackable_node_ids,
161
+ std::move(callback));
162
+ }
163
+
164
+ absl::Status SavedModelV2Bundle::RecurseObjectsToRestore(
165
+ const SavedObject* saved_object, int saved_object_node_id,
166
+ const TrackableObjectGraph::TrackableObject* trackable_object,
167
+ std::string object_name, absl::flat_hash_set<int>* seen_trackable_node_ids,
168
+ RestoreObjectsCallback callback) {
169
+ // Callback if any attributes or slot variables.
170
+ // Note that the root is always excluded from the search (it can never
171
+ // be a restorable object). This matches some logic on the Python side.
172
+ if (saved_object_node_id != 0 &&
173
+ (trackable_object->attributes_size() > 0 ||
174
+ trackable_object->slot_variables_size() > 0)) {
175
+ TF_RETURN_WITH_CONTEXT_IF_ERROR(
176
+ callback(saved_object_node_id, *trackable_object), "Unable to restore ",
177
+ object_name);
178
+ }
179
+
180
+ for (const auto& trackable_child_ref : trackable_object->children()) {
181
+ const auto& local_name = trackable_child_ref.local_name();
182
+
183
+ // Compute the full child name.
184
+ std::string child_name;
185
+ if (object_name.empty()) {
186
+ child_name = local_name;
187
+ } else {
188
+ child_name = strings::StrCat(object_name, ".", local_name);
189
+ }
190
+
191
+ // Descend down the trackable graph.
192
+ int trackable_child_node_id = trackable_child_ref.node_id();
193
+ if (!seen_trackable_node_ids->insert(trackable_child_node_id).second) {
194
+ // Cycle or duplicate detected - ignore this branch.
195
+ continue;
196
+ }
197
+ if (trackable_child_node_id < 0 ||
198
+ trackable_child_node_id >= trackable_object_graph().nodes_size()) {
199
+ return errors::FailedPrecondition(
200
+ strings::StrCat("Illegal trackable child node id for ", child_name));
201
+ }
202
+ const auto* trackable_child =
203
+ &trackable_object_graph().nodes(trackable_child_node_id);
204
+
205
+ // Descend down the saved object graph.
206
+ int saved_child_node_id = -1;
207
+ const SavedObject* saved_child = nullptr;
208
+ for (const auto& saved_child_ref : saved_object->children()) {
209
+ if (saved_child_ref.local_name() == local_name) {
210
+ // Found.
211
+ saved_child_node_id = saved_child_ref.node_id();
212
+ if (saved_child_node_id >= 0 &&
213
+ saved_child_node_id < saved_object_graph().nodes_size()) {
214
+ saved_child = &saved_object_graph().nodes(saved_child_node_id);
215
+ }
216
+ break;
217
+ }
218
+ }
219
+
220
+ if (!saved_child) {
221
+ return absl::Status(
222
+ absl::StatusCode::kFailedPrecondition,
223
+ strings::StrCat("Could not find saved object to restore for ",
224
+ child_name));
225
+ }
226
+
227
+ TF_RETURN_IF_ERROR(RecurseObjectsToRestore(
228
+ saved_child, saved_child_node_id, trackable_child, child_name,
229
+ seen_trackable_node_ids, callback));
230
+ }
231
+ return absl::OkStatus();
232
+ }
233
+
234
+ } // namespace tensorflow
western_ai/usa/tensorflow/tensorflow/cc/saved_model/bundle_v2.h ADDED
@@ -0,0 +1,90 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /* Copyright 2016 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
+ // Helpers for loading the persistent representation of a SavedModelV2.
17
+ // Please note that this is depended on by code that does not make use of
18
+ // the full runtime and its dependencies should be restricted.
19
+
20
+ #ifndef TENSORFLOW_CC_SAVED_MODEL_BUNDLE_V2_H_
21
+ #define TENSORFLOW_CC_SAVED_MODEL_BUNDLE_V2_H_
22
+
23
+ #include <functional>
24
+ #include <memory>
25
+ #include <string>
26
+
27
+ #include "absl/container/flat_hash_set.h"
28
+ #include "tensorflow/core/framework/graph_debug_info.pb.h"
29
+ #include "tensorflow/core/lib/core/status.h"
30
+ #include "tensorflow/core/protobuf/meta_graph.pb.h"
31
+ #include "tensorflow/core/protobuf/saved_object_graph.pb.h"
32
+ #include "tensorflow/core/protobuf/trackable_object_graph.pb.h"
33
+ #include "tensorflow/core/util/tensor_bundle/tensor_bundle.h"
34
+
35
+ namespace tensorflow {
36
+
37
+ /// Represents a version 2 SavedModel that is loaded from storage (but not yet
38
+ /// loaded into an executable in-memory representation).
39
+ class SavedModelV2Bundle {
40
+ public:
41
+ using RestoreObjectsCallback = std::function<absl::Status(
42
+ int, const TrackableObjectGraph::TrackableObject&)>;
43
+
44
+ /// Loads persistent representations for a SavedModelV2 from the specified
45
+ /// export directory.
46
+ static absl::Status Load(const std::string& export_dir,
47
+ SavedModelV2Bundle* bundle);
48
+
49
+ /// MetaGraphDef from the loaded SavedModel.
50
+ MetaGraphDef& meta_graph_def() { return meta_graph_def_; }
51
+
52
+ /// SavedObjectGraph from the MetaGraphDef.
53
+ const SavedObjectGraph& saved_object_graph() {
54
+ return meta_graph_def().object_graph_def();
55
+ }
56
+
57
+ /// TrackableObjectGraph loaded from the variable_reader() checkpoint.
58
+ TrackableObjectGraph& trackable_object_graph() {
59
+ return trackable_object_graph_;
60
+ }
61
+
62
+ /// BundleReader for accessing the variables bundle.
63
+ BundleReader* variable_reader() { return variable_reader_.get(); }
64
+
65
+ /// The GraphDebugInfo (or nullptr if none).
66
+ GraphDebugInfo* debug_info() { return debug_info_.get(); }
67
+
68
+ /// Restores objects, invoking the callback with the node id in the
69
+ /// saved_object_graph() and the corresponding TrackableObject from the
70
+ /// trackable_object_graph(). The callback may use the variable_reader() but
71
+ /// must not modify the underlying saved_object_graph().
72
+ absl::Status VisitObjectsToRestore(RestoreObjectsCallback callback);
73
+
74
+ private:
75
+ absl::Status RecurseObjectsToRestore(
76
+ const SavedObject* saved_object, int saved_object_node_id,
77
+ const TrackableObjectGraph::TrackableObject* trackable_object,
78
+ std::string object_name,
79
+ absl::flat_hash_set<int>* seen_trackable_node_ids,
80
+ RestoreObjectsCallback callback);
81
+
82
+ MetaGraphDef meta_graph_def_;
83
+ TrackableObjectGraph trackable_object_graph_;
84
+ std::unique_ptr<BundleReader> variable_reader_;
85
+ std::unique_ptr<GraphDebugInfo> debug_info_;
86
+ };
87
+
88
+ } // namespace tensorflow
89
+
90
+ #endif // TENSORFLOW_CC_SAVED_MODEL_BUNDLE_V2_H_
western_ai/usa/tensorflow/tensorflow/cc/saved_model/bundle_v2_test.cc ADDED
@@ -0,0 +1,158 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /* Copyright 2018 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/cc/saved_model/bundle_v2.h"
17
+
18
+ #include <algorithm>
19
+ #include <string>
20
+ #include <tuple>
21
+ #include <utility>
22
+ #include <vector>
23
+
24
+ #include "absl/status/status.h"
25
+ #include "absl/strings/match.h"
26
+ #include "absl/strings/str_cat.h"
27
+ #include "json/json.h"
28
+ #include "json/reader.h"
29
+ #include "json/value.h"
30
+ #include "tensorflow/cc/saved_model/metrics.h"
31
+ #include "xla/tsl/lib/core/status_test_util.h"
32
+ #include "tensorflow/core/platform/path.h"
33
+ #include "tensorflow/core/platform/test.h"
34
+ #include "tensorflow/core/protobuf/trackable_object_graph.pb.h"
35
+ #include "tsl/platform/statusor.h"
36
+
37
+ namespace tensorflow {
38
+ namespace {
39
+
40
+ constexpr char kTestData[] = "cc/saved_model/testdata";
41
+
42
+ class BundleV2Test : public ::testing::Test {
43
+ protected:
44
+ BundleV2Test() {}
45
+
46
+ void RestoreVarsAndVerify(SavedModelV2Bundle* bundle,
47
+ std::vector<std::string> expected_names) {
48
+ // Collect saved_node_id, full_name, checkpoint_key into a vector.
49
+ using RestoredVarType = std::tuple<int, std::string, std::string>;
50
+ std::vector<RestoredVarType> restored_vars;
51
+ TF_ASSERT_OK(bundle->VisitObjectsToRestore(
52
+ [&](int saved_node_id,
53
+ const TrackableObjectGraph::TrackableObject& trackable_object)
54
+ -> absl::Status {
55
+ for (const auto& attr : trackable_object.attributes()) {
56
+ if (attr.name() == "VARIABLE_VALUE") {
57
+ restored_vars.emplace_back(saved_node_id, attr.full_name(),
58
+ attr.checkpoint_key());
59
+ }
60
+ }
61
+ return absl::OkStatus();
62
+ }));
63
+
64
+ // Should be one of each var name restored.
65
+ for (const auto& expected_name : expected_names) {
66
+ EXPECT_EQ(1, std::count_if(restored_vars.begin(), restored_vars.end(),
67
+ [&](RestoredVarType t) {
68
+ return std::get<1>(t) == expected_name;
69
+ }));
70
+ }
71
+
72
+ for (const auto& restored_var : restored_vars) {
73
+ // Each restored var should match a SavedObjectGraph node with the same
74
+ // variable name.
75
+ const auto& saved_node =
76
+ bundle->saved_object_graph().nodes(std::get<0>(restored_var));
77
+ EXPECT_EQ(std::get<1>(restored_var), saved_node.variable().name());
78
+
79
+ // And should be able to load it from the tensor_bundle.
80
+ Tensor value;
81
+ TF_ASSERT_OK(
82
+ bundle->variable_reader()->Lookup(std::get<2>(restored_var), &value));
83
+ }
84
+ }
85
+ };
86
+
87
+ TEST_F(BundleV2Test, LoadsVarsAndArithmeticObjectGraph) {
88
+ const std::string export_dir = io::JoinPath(
89
+ testing::TensorFlowSrcRoot(), kTestData, "VarsAndArithmeticObjectGraph");
90
+
91
+ SavedModelV2Bundle bundle;
92
+ TF_ASSERT_OK(SavedModelV2Bundle::Load(export_dir, &bundle));
93
+
94
+ // Ensure that there are nodes in the trackable_object_graph.
95
+ EXPECT_GT(bundle.trackable_object_graph().nodes_size(), 0);
96
+
97
+ RestoreVarsAndVerify(&bundle, {"variable_x", "variable_y", "child_variable"});
98
+ }
99
+
100
+ TEST_F(BundleV2Test, LoadsCyclicModule) {
101
+ const std::string export_dir =
102
+ io::JoinPath(testing::TensorFlowSrcRoot(), kTestData, "CyclicModule");
103
+
104
+ SavedModelV2Bundle bundle;
105
+ TF_ASSERT_OK(SavedModelV2Bundle::Load(export_dir, &bundle));
106
+
107
+ // Ensure that there are nodes in the trackable_object_graph.
108
+ EXPECT_GT(bundle.trackable_object_graph().nodes_size(), 0);
109
+
110
+ RestoreVarsAndVerify(&bundle, {"MyVariable"});
111
+ }
112
+
113
+ TEST_F(BundleV2Test, UpdatesMetrics) {
114
+ const std::string kCCLoadBundleV2Label = "cc_load_bundle_v2";
115
+ const int read_count = metrics::SavedModelReadCount("2").value();
116
+ const int api_count =
117
+ metrics::SavedModelReadApi(kCCLoadBundleV2Label).value();
118
+ const std::string export_dir = io::JoinPath(
119
+ testing::TensorFlowSrcRoot(), kTestData, "VarsAndArithmeticObjectGraph");
120
+
121
+ SavedModelV2Bundle bundle;
122
+ TF_ASSERT_OK(SavedModelV2Bundle::Load(export_dir, &bundle));
123
+
124
+ EXPECT_EQ(metrics::SavedModelReadCount("2").value(), read_count + 1);
125
+ EXPECT_EQ(metrics::SavedModelReadApi(kCCLoadBundleV2Label).value(),
126
+ api_count + 1);
127
+ // Check that the gauge contains the path and fingerprint.
128
+ EXPECT_EQ(metrics::SavedModelReadPath().value(), export_dir);
129
+
130
+ Json::Value fingerprint = Json::objectValue;
131
+ Json::Reader reader = Json::Reader();
132
+ reader.parse(metrics::SavedModelReadFingerprint().value(), fingerprint);
133
+ EXPECT_EQ(fingerprint["saved_model_checksum"].asUInt64(),
134
+ 15788619162413586750ULL);
135
+ EXPECT_EQ(fingerprint["graph_def_program_hash"].asUInt64(),
136
+ 706963557435316516ULL);
137
+ EXPECT_EQ(fingerprint["signature_def_hash"].asUInt64(),
138
+ 5693392539583495303ULL);
139
+ EXPECT_EQ(fingerprint["saved_object_graph_hash"].asUInt64(),
140
+ 12074714563970609759ULL);
141
+ EXPECT_EQ(fingerprint["checkpoint_hash"].asUInt64(), 10788359570789890102ULL);
142
+
143
+ TF_ASSERT_OK_AND_ASSIGN(
144
+ auto path_and_singleprint,
145
+ metrics::ParseSavedModelPathAndSingleprint(
146
+ metrics::SavedModelReadPathAndSingleprint().value()));
147
+ auto [path, singleprint] = path_and_singleprint;
148
+ EXPECT_TRUE(absl::StrContains(
149
+ path, absl::StrCat(kTestData, "/VarsAndArithmeticObjectGraph")));
150
+ EXPECT_EQ(singleprint,
151
+ "706963557435316516/" // graph_def_program_hash
152
+ "5693392539583495303/" // signature_def_hash
153
+ "12074714563970609759/" // saved_object_graph_hash
154
+ "10788359570789890102"); // checkpoint_hash
155
+ }
156
+
157
+ } // namespace
158
+ } // namespace tensorflow
western_ai/usa/tensorflow/tensorflow/cc/saved_model/constants.h ADDED
@@ -0,0 +1,82 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /* Copyright 2016 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_CONSTANTS_H_
17
+ #define TENSORFLOW_CC_SAVED_MODEL_CONSTANTS_H_
18
+
19
+ namespace tensorflow {
20
+
21
+ // SavedModel assets directory.
22
+ inline constexpr char kSavedModelAssetsDirectory[] = "assets";
23
+
24
+ // SavedModel assets.extra directory.
25
+ inline constexpr char kSavedModelAssetsExtraDirectory[] = "assets.extra";
26
+
27
+ // SavedModel assets key for graph collection-def.
28
+ inline constexpr char kSavedModelAssetsKey[] = "saved_model_assets";
29
+
30
+ /// SavedModel legacy init op collection key. Used in v1 SavedModels.
31
+ inline constexpr char kSavedModelLegacyInitOpKey[] = "legacy_init_op";
32
+
33
+ /// SavedModel main op collection key. Used in v1 SavedModels.
34
+ inline constexpr char kSavedModelMainOpKey[] = "saved_model_main_op";
35
+
36
+ // CollectionDef key for the SavedModel train op.
37
+ // Not exported while export_all_saved_models is experimental.
38
+ inline constexpr char kSavedModelTrainOpKey[] = "saved_model_train_op";
39
+
40
+ // Schema version for SavedModel.
41
+ inline constexpr int kSavedModelSchemaVersion = 1;
42
+
43
+ // SavedModel proto filename prefix.
44
+ inline constexpr char kSavedModelFilenamePrefix[] = "saved_model";
45
+ // SavedModel proto filename.
46
+ inline constexpr char kSavedModelFilenamePb[] = "saved_model.pb";
47
+
48
+ // SavedModel chunked proto filename.
49
+ inline constexpr char kSavedModelFilenameCpb[] = "saved_model.cpb";
50
+
51
+ // SavedModel text format proto filename.
52
+ inline constexpr char kSavedModelFilenamePbTxt[] = "saved_model.pbtxt";
53
+
54
+ // Subdirectory where debugging related files are written.
55
+ inline constexpr char kSavedModelDebugDirectory[] = "debug";
56
+
57
+ // File name for GraphDebugInfo protocol buffer which corresponds to the
58
+ // SavedModel.
59
+ inline constexpr char kSavedModelDebugInfoFilenamePb[] =
60
+ "saved_model_debug_info.pb";
61
+
62
+ // Directory in which to save the SavedModel variables.
63
+ inline constexpr char kSavedModelVariablesDirectory[] = "variables";
64
+
65
+ // SavedModel variables filename.
66
+ inline constexpr char kSavedModelVariablesFilename[] = "variables";
67
+
68
+ // SavedModel SignatureDef keys for the initialization and train ops. Used in
69
+ // V2 SavedModels.
70
+ inline constexpr char kSavedModelInitOpSignatureKey[] = "__saved_model_init_op";
71
+ inline constexpr char kSavedModelTrainOpSignatureKey[] =
72
+ "__saved_model_train_op";
73
+
74
+ // Key in the TensorBundle for the object graph proto.
75
+ inline constexpr char kObjectGraphProtoKey[] = "_CHECKPOINTABLE_OBJECT_GRAPH";
76
+
77
+ // Filename for the FingerprintDef protocol buffer.
78
+ inline constexpr char kFingerprintFilenamePb[] = "fingerprint.pb";
79
+
80
+ } // namespace tensorflow
81
+
82
+ #endif // TENSORFLOW_CC_SAVED_MODEL_CONSTANTS_H_
western_ai/usa/tensorflow/tensorflow/cc/saved_model/experimental/public/BUILD ADDED
@@ -0,0 +1,85 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Experimental C++ SavedModel Header Only APIs. See RFC
2
+ # https://github.com/tensorflow/community/pull/207
3
+
4
+ load("//tensorflow/core/platform:rules_cc.bzl", "cc_library")
5
+
6
+ package(
7
+ # copybara:uncomment default_applicable_licenses = ["//tensorflow:license"],
8
+ # This is intentionally public
9
+ default_visibility = [
10
+ "//visibility:public",
11
+ ],
12
+ licenses = ["notice"],
13
+ )
14
+
15
+ cc_library(
16
+ name = "concrete_function",
17
+ hdrs = [
18
+ "concrete_function.h",
19
+ ],
20
+ deps = [
21
+ ":function_metadata",
22
+ "//tensorflow/c/eager:c_api",
23
+ "//tensorflow/c/experimental/saved_model/public:concrete_function",
24
+ "//tensorflow/cc/experimental/base/public:status",
25
+ ],
26
+ )
27
+
28
+ cc_library(
29
+ name = "concrete_function_list",
30
+ hdrs = [
31
+ "concrete_function_list.h",
32
+ ],
33
+ deps = [
34
+ ":concrete_function",
35
+ "//tensorflow/c/experimental/saved_model/public:concrete_function_list",
36
+ ],
37
+ )
38
+
39
+ cc_library(
40
+ name = "function_metadata",
41
+ hdrs = [
42
+ "function_metadata.h",
43
+ ],
44
+ deps = [
45
+ "//tensorflow/c/experimental/saved_model/public:function_metadata",
46
+ ],
47
+ )
48
+
49
+ cc_library(
50
+ name = "saved_model_api",
51
+ hdrs = [
52
+ "saved_model_api.h",
53
+ ],
54
+ deps = [
55
+ ":concrete_function",
56
+ ":concrete_function_list",
57
+ ":signature_def_function",
58
+ "//tensorflow/c/experimental/saved_model/public:saved_model_api",
59
+ "//tensorflow/cc/experimental/base/public:runtime",
60
+ "//tensorflow/cc/experimental/base/public:status",
61
+ ],
62
+ )
63
+
64
+ cc_library(
65
+ name = "signature_def_function",
66
+ hdrs = [
67
+ "signature_def_function.h",
68
+ ],
69
+ deps = [
70
+ ":signature_def_function_metadata",
71
+ "//tensorflow/c/eager:c_api",
72
+ "//tensorflow/c/experimental/saved_model/public:signature_def_function",
73
+ "//tensorflow/cc/experimental/base/public:status",
74
+ ],
75
+ )
76
+
77
+ cc_library(
78
+ name = "signature_def_function_metadata",
79
+ hdrs = [
80
+ "signature_def_function_metadata.h",
81
+ ],
82
+ deps = [
83
+ "//tensorflow/c/experimental/saved_model/public:signature_def_function_metadata",
84
+ ],
85
+ )
western_ai/usa/tensorflow/tensorflow/cc/saved_model/experimental/public/concrete_function.h ADDED
@@ -0,0 +1,61 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /* Copyright 2020 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_EXPERIMENTAL_PUBLIC_CONCRETE_FUNCTION_H_
17
+ #define TENSORFLOW_CC_SAVED_MODEL_EXPERIMENTAL_PUBLIC_CONCRETE_FUNCTION_H_
18
+
19
+ #include <vector>
20
+
21
+ #include "tensorflow/c/eager/c_api.h"
22
+ #include "tensorflow/c/experimental/saved_model/public/concrete_function.h"
23
+ #include "tensorflow/cc/experimental/base/public/status.h"
24
+ #include "tensorflow/cc/saved_model/experimental/public/function_metadata.h"
25
+
26
+ namespace tensorflow {
27
+ namespace experimental {
28
+ namespace cc {
29
+
30
+ // ConcreteFunction is an executable "function" loaded from a SavedModelAPI.
31
+ class ConcreteFunction final {
32
+ public:
33
+ // TODO(bmzhao): Adding ConcreteFunction::Run in subsequent CL, since
34
+ // it depends on tensorflow::cc::Tensor and tensorflow::cc::TensorHandle
35
+
36
+ // Returns FunctionMetadata associated with this ConcreteFunction.
37
+ const FunctionMetadata* GetFunctionMetadata();
38
+
39
+ private:
40
+ friend class SavedModelAPI;
41
+ friend class ConcreteFunctionList;
42
+
43
+ // TODO(bmzhao): Consider adding a macro for wrapping/unwrapping
44
+ // when moving out of experimental.
45
+ static ConcreteFunction* wrap(TF_ConcreteFunction* p) {
46
+ return reinterpret_cast<ConcreteFunction*>(p);
47
+ }
48
+ static TF_ConcreteFunction* unwrap(ConcreteFunction* p) {
49
+ return reinterpret_cast<TF_ConcreteFunction*>(p);
50
+ }
51
+ };
52
+
53
+ inline const FunctionMetadata* ConcreteFunction::GetFunctionMetadata() {
54
+ return FunctionMetadata::wrap(TF_ConcreteFunctionGetMetadata(unwrap(this)));
55
+ }
56
+
57
+ } // namespace cc
58
+ } // namespace experimental
59
+ } // namespace tensorflow
60
+
61
+ #endif // TENSORFLOW_CC_SAVED_MODEL_EXPERIMENTAL_PUBLIC_CONCRETE_FUNCTION_H_
western_ai/usa/tensorflow/tensorflow/cc/saved_model/experimental/public/concrete_function_list.h ADDED
@@ -0,0 +1,63 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /* Copyright 2020 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_EXPERIMENTAL_PUBLIC_CONCRETE_FUNCTION_LIST_H_
17
+ #define TENSORFLOW_CC_SAVED_MODEL_EXPERIMENTAL_PUBLIC_CONCRETE_FUNCTION_LIST_H_
18
+
19
+ #include <vector>
20
+
21
+ #include "tensorflow/c/experimental/saved_model/public/concrete_function_list.h"
22
+ #include "tensorflow/cc/saved_model/experimental/public/concrete_function.h"
23
+
24
+ namespace tensorflow {
25
+ namespace experimental {
26
+ namespace cc {
27
+
28
+ // ConcreteFunctionList helps convert an opaque pointer to an array of
29
+ // ConcreteFunction pointers to a std::vector.
30
+ class ConcreteFunctionList {
31
+ public:
32
+ // Converts this object to a std::vector<ConcreteFunction*>
33
+ std::vector<ConcreteFunction*> ToVector();
34
+
35
+ private:
36
+ friend class SavedModelAPI;
37
+ // Wraps a TF_ConcreteFunctionList. Takes ownership of list.
38
+ explicit ConcreteFunctionList(TF_ConcreteFunctionList* list) : list_(list) {}
39
+
40
+ struct TFConcreteFunctionListDeleter {
41
+ void operator()(TF_ConcreteFunctionList* p) const {
42
+ TF_DeleteConcreteFunctionList(p);
43
+ }
44
+ };
45
+ std::unique_ptr<TF_ConcreteFunctionList, TFConcreteFunctionListDeleter> list_;
46
+ };
47
+
48
+ inline std::vector<ConcreteFunction*> ConcreteFunctionList::ToVector() {
49
+ int size = TF_ConcreteFunctionListSize(list_.get());
50
+ std::vector<ConcreteFunction*> result;
51
+ result.reserve(size);
52
+ for (int i = 0; i < size; ++i) {
53
+ result.push_back(
54
+ ConcreteFunction::wrap(TF_ConcreteFunctionListGet(list_.get(), i)));
55
+ }
56
+ return result;
57
+ }
58
+
59
+ } // namespace cc
60
+ } // namespace experimental
61
+ } // namespace tensorflow
62
+
63
+ #endif // TENSORFLOW_CC_SAVED_MODEL_EXPERIMENTAL_PUBLIC_CONCRETE_FUNCTION_LIST_H_
western_ai/usa/tensorflow/tensorflow/cc/saved_model/experimental/public/function_metadata.h ADDED
@@ -0,0 +1,47 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /* Copyright 2020 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_EXPERIMENTAL_PUBLIC_FUNCTION_METADATA_H_
17
+ #define TENSORFLOW_CC_SAVED_MODEL_EXPERIMENTAL_PUBLIC_FUNCTION_METADATA_H_
18
+
19
+ #include <memory>
20
+
21
+ #include "tensorflow/c/experimental/saved_model/public/function_metadata.h"
22
+
23
+ namespace tensorflow {
24
+ namespace experimental {
25
+ namespace cc {
26
+
27
+ // FunctionMetadata stores additional function information, including
28
+ // optional signaturedef feeds/fetches (for TF1-based ConcreteFunctions),
29
+ // a valid function path (for TF2-based ConcreteFunctions), and
30
+ // the types + number of inputs and outputs.
31
+ class FunctionMetadata final {
32
+ // TODO(bmzhao): Add getters here as necessary.
33
+ private:
34
+ friend class ConcreteFunction;
35
+ static FunctionMetadata* wrap(TF_FunctionMetadata* p) {
36
+ return reinterpret_cast<FunctionMetadata*>(p);
37
+ }
38
+ static TF_FunctionMetadata* unwrap(FunctionMetadata* p) {
39
+ return reinterpret_cast<TF_FunctionMetadata*>(p);
40
+ }
41
+ };
42
+
43
+ } // namespace cc
44
+ } // namespace experimental
45
+ } // namespace tensorflow
46
+
47
+ #endif // TENSORFLOW_CC_SAVED_MODEL_EXPERIMENTAL_PUBLIC_FUNCTION_METADATA_H_
western_ai/usa/tensorflow/tensorflow/cc/saved_model/experimental/public/saved_model_api.h ADDED
@@ -0,0 +1,155 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /* Copyright 2020 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_EXPERIMENTAL_PUBLIC_SAVED_MODEL_API_H_
17
+ #define TENSORFLOW_CC_SAVED_MODEL_EXPERIMENTAL_PUBLIC_SAVED_MODEL_API_H_
18
+
19
+ #include <memory>
20
+ #include <string>
21
+ #include <unordered_set>
22
+ #include <vector>
23
+
24
+ #include "tensorflow/c/experimental/saved_model/public/saved_model_api.h"
25
+ #include "tensorflow/cc/experimental/base/public/runtime.h"
26
+ #include "tensorflow/cc/experimental/base/public/status.h"
27
+ #include "tensorflow/cc/saved_model/experimental/public/concrete_function.h"
28
+ #include "tensorflow/cc/saved_model/experimental/public/concrete_function_list.h"
29
+ #include "tensorflow/cc/saved_model/experimental/public/signature_def_function.h"
30
+
31
+ namespace tensorflow {
32
+ namespace experimental {
33
+ namespace cc {
34
+
35
+ // SavedModelAPI offers a way to load Tensorflow Saved Models
36
+ // (https://www.tensorflow.org/guide/saved_model) and execute saved
37
+ // tf.functions or legacy SignatureDefs in a TF2-idiomatic fashion.
38
+ // See RFC 207
39
+ // (https://github.com/tensorflow/community/blob/master/rfcs/20200218-tf-c-saved-model.md)
40
+ // TODO(bmzhao): Add an e2e example here, once ConcreteFunction::Run is added.
41
+ class SavedModelAPI {
42
+ public:
43
+ // Load a SavedModel from `dirname`.
44
+ //
45
+ // Params:
46
+ // saved_model_path - A directory filepath that the SavedModel is at.
47
+ // runtime - A runtime used to load SavedModelAPI. `runtime` must outlive the
48
+ // returned TF_SavedModel pointer.
49
+ // tags - Optional set of tags. If tags = nullptr, we expect the SavedModel
50
+ // to contain a single Metagraph (as for those exported from TF2's
51
+ // `tf.saved_model.save`). If tags != nullptr, we load the metagraph
52
+ // matching the tags:
53
+ // https://github.com/tensorflow/tensorflow/blob/428cdeda09aef81e958eeb274b83d27ad635b57b/tensorflow/core/protobuf/meta_graph.proto#L50-L56
54
+ // status - Set to OK on success and an appropriate error on failure.
55
+ // Returns:
56
+ // If status is not OK, returns nullptr.
57
+ static std::unique_ptr<SavedModelAPI> Load(
58
+ const std::string& saved_model_path, const Runtime& runtime,
59
+ Status* status, const std::unordered_set<std::string>* tags = nullptr);
60
+
61
+ // Retrieve a function from the TF2 SavedModel via function path.
62
+ //
63
+ // Params:
64
+ // function_path - A string containing the path from the root saved python
65
+ // object to a tf.function method.
66
+ // status - Set to OK on success and an appropriate error on failure.
67
+ // Returns:
68
+ // If status is not OK, returns nullptr. Otherwise, returns a
69
+ // tensorflow::cc::ConcreteFunction pointer. The lifetime of this pointer
70
+ // is bound to SavedModelAPI it was loaded from.
71
+ ConcreteFunction* GetConcreteFunction(const std::string& function_path,
72
+ Status* status);
73
+
74
+ // Retrieve a function from the TF SavedModel via a SignatureDef key.
75
+ //
76
+ // Params:
77
+ // signature_def_key - String key of SignatureDef map of a SavedModel:
78
+ // https://github.com/tensorflow/tensorflow/blob/69b08900b1e991d84bce31f3b404f5ed768f339f/tensorflow/core/protobuf/meta_graph.proto#L89
79
+ // status - Set to OK on success and an appropriate error on failure.
80
+ // Returns:
81
+ // If status is not OK, returns nullptr. Otherwise, returns a
82
+ // tensorflow::cc::ConcreteFunction pointer. The lifetime of this pointer
83
+ // is bound to SavedModelAPI it was loaded from.
84
+ SignatureDefFunction* GetSignatureDefFunction(
85
+ const std::string& function_path, Status* status);
86
+
87
+ // SavedModelAPI is movable, but not copyable.
88
+ SavedModelAPI(SavedModelAPI&&) = default;
89
+ SavedModelAPI& operator=(SavedModelAPI&&) = default;
90
+
91
+ private:
92
+ SavedModelAPI(const SavedModelAPI&) = delete;
93
+ SavedModelAPI& operator=(const SavedModelAPI&) = delete;
94
+
95
+ explicit SavedModelAPI(TF_SavedModel* model) : saved_model_(model) {}
96
+ struct TFSavedModelDeleter {
97
+ void operator()(TF_SavedModel* p) const { TF_DeleteSavedModel(p); }
98
+ };
99
+ std::unique_ptr<TF_SavedModel, TFSavedModelDeleter> saved_model_;
100
+ };
101
+
102
+ inline std::unique_ptr<SavedModelAPI> SavedModelAPI::Load(
103
+ const std::string& saved_model_path, const Runtime& runtime, Status* status,
104
+ const std::unordered_set<std::string>* tags) {
105
+ TF_SavedModel* saved_model = nullptr;
106
+
107
+ if (tags == nullptr) {
108
+ saved_model =
109
+ TF_LoadSavedModel(saved_model_path.c_str(), runtime.GetTFEContext(),
110
+ status->GetTFStatus());
111
+ } else {
112
+ std::vector<const char*> tags_vector;
113
+ tags_vector.reserve(tags->size());
114
+ for (const std::string& tag : *tags) {
115
+ tags_vector.push_back(tag.c_str());
116
+ }
117
+ saved_model = TF_LoadSavedModelWithTags(
118
+ saved_model_path.c_str(), runtime.GetTFEContext(), tags_vector.data(),
119
+ tags_vector.size(), status->GetTFStatus());
120
+ }
121
+
122
+ if (!status->ok()) {
123
+ return nullptr;
124
+ }
125
+
126
+ // We can't use std::make_unique here because of its interaction with a
127
+ // private constructor: https://abseil.io/tips/134
128
+ return std::unique_ptr<SavedModelAPI>(new SavedModelAPI(saved_model));
129
+ }
130
+
131
+ inline ConcreteFunction* SavedModelAPI::GetConcreteFunction(
132
+ const std::string& function_path, Status* status) {
133
+ TF_ConcreteFunction* function = TF_GetSavedModelConcreteFunction(
134
+ saved_model_.get(), function_path.c_str(), status->GetTFStatus());
135
+ if (!status->ok()) {
136
+ return nullptr;
137
+ }
138
+ return ConcreteFunction::wrap(function);
139
+ }
140
+
141
+ inline SignatureDefFunction* SavedModelAPI::GetSignatureDefFunction(
142
+ const std::string& function_path, Status* status) {
143
+ TF_SignatureDefFunction* function = TF_GetSavedModelSignatureDefFunction(
144
+ saved_model_.get(), function_path.c_str(), status->GetTFStatus());
145
+ if (!status->ok()) {
146
+ return nullptr;
147
+ }
148
+ return SignatureDefFunction::wrap(function);
149
+ }
150
+
151
+ } // namespace cc
152
+ } // namespace experimental
153
+ } // namespace tensorflow
154
+
155
+ #endif // TENSORFLOW_CC_SAVED_MODEL_EXPERIMENTAL_PUBLIC_SAVED_MODEL_API_H_
western_ai/usa/tensorflow/tensorflow/cc/saved_model/experimental/public/signature_def_function.h ADDED
@@ -0,0 +1,89 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /* Copyright 2020 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_EXPERIMENTAL_PUBLIC_SIGNATURE_DEF_FUNCTION_H_
17
+ #define TENSORFLOW_CC_SAVED_MODEL_EXPERIMENTAL_PUBLIC_SIGNATURE_DEF_FUNCTION_H_
18
+
19
+ #include <vector>
20
+
21
+ #include "tensorflow/c/eager/c_api.h"
22
+ #include "tensorflow/c/experimental/saved_model/public/signature_def_function.h"
23
+ #include "tensorflow/cc/experimental/base/public/status.h"
24
+ #include "tensorflow/cc/saved_model/experimental/public/signature_def_function_metadata.h"
25
+
26
+ namespace tensorflow {
27
+ namespace experimental {
28
+ namespace cc {
29
+
30
+ // SignatureDefFunctions are functions that correspond to either:
31
+ // "signatures" saved from a TF2 SavedModel APIs:
32
+ // https://github.com/tensorflow/tensorflow/blob/8ce0600f58ed84a8c84a7bbdb014d1f09e44f4c8/tensorflow/python/saved_model/save.py#L830-L854
33
+ // Or the "SignatureDefMap" saved from TF1 SavedModel APIs:
34
+ // https://github.com/tensorflow/tensorflow/blob/8ce0600f58ed84a8c84a7bbdb014d1f09e44f4c8/tensorflow/python/saved_model/load_v1_in_v2_test.py#L170-L174
35
+ // In both cases, a SignatureDef is serialized as a SignatureDef protobuf:
36
+ // https://github.com/tensorflow/tensorflow/blob/8ce0600f58ed84a8c84a7bbdb014d1f09e44f4c8/tensorflow/core/protobuf/meta_graph.proto#L260-L330
37
+ // and represents a computation defined by a TF subgraph.
38
+ // These Signatures were primarily designed to be interoperable with the legacy
39
+ // TF 1 Session-based C++ SavedModelBundle loading APIs:
40
+ // https://github.com/tensorflow/tensorflow/blob/26c4ee0c833e74f94d0102d8b005c41a28b44445/tensorflow/cc/saved_model/loader.h#L96-L108
41
+ // SignatureDefFunctions have different semantics from regular TF2
42
+ // ConcreteFunctions, and are mainly intended provide a serving-friendly
43
+ // transition point from the TF1 Session API.
44
+ // First, SignatureDefFunctions have different calling conventions.
45
+ // SignatureDefFunctions' inputs and outputs are constrained to **flattened
46
+ // lists of TensorHandles only**. They do not support more exotic input/output
47
+ // types (like optionals, generators, etc). Additionally, this flattening means
48
+ // they will not preserve the exact interface of the original tf.function they
49
+ // were traced from, as things like composite tensors decay into their
50
+ // internal dense tensor representation.
51
+ // Second, all inputs and outputs are "named", and these names are load bearing
52
+ // (eg: they are part of the interface of tensorflow_serving):
53
+ // https://github.com/tensorflow/serving/blob/e0d247b2e4050713194b8fad0be24a0636df7209/tensorflow_serving/apis/predict.proto#L21
54
+ // https://github.com/tensorflow/serving/blob/e0d247b2e4050713194b8fad0be24a0636df7209/tensorflow_serving/apis/predict.proto#L39
55
+ // The name of each input/output is stored in the corresponding tf::Argument in
56
+ // SignatureDefFunctionMetadata::arguments(). Users must ensure the order of
57
+ // TensorHandles passed to the function matches with the order of named
58
+ // arguments. Similarly the name of the outputs is stored in
59
+ // SignatureDefFunctionMetadata::returns().
60
+ class SignatureDefFunction final {
61
+ public:
62
+ // Returns FunctionMetadata associated with this ConcreteFunction.
63
+ const SignatureDefFunctionMetadata* GetFunctionMetadata();
64
+
65
+ private:
66
+ friend class SavedModelAPI;
67
+ friend class ConcreteFunctionList;
68
+
69
+ // TODO(bmzhao): Consider adding a macro for wrapping/unwrapping
70
+ // when moving out of experimental.
71
+ static SignatureDefFunction* wrap(TF_SignatureDefFunction* p) {
72
+ return reinterpret_cast<SignatureDefFunction*>(p);
73
+ }
74
+ static TF_SignatureDefFunction* unwrap(SignatureDefFunction* p) {
75
+ return reinterpret_cast<TF_SignatureDefFunction*>(p);
76
+ }
77
+ };
78
+
79
+ inline const SignatureDefFunctionMetadata*
80
+ SignatureDefFunction::GetFunctionMetadata() {
81
+ return SignatureDefFunctionMetadata::wrap(
82
+ TF_SignatureDefFunctionGetMetadata(unwrap(this)));
83
+ }
84
+
85
+ } // namespace cc
86
+ } // namespace experimental
87
+ } // namespace tensorflow
88
+
89
+ #endif // TENSORFLOW_CC_SAVED_MODEL_EXPERIMENTAL_PUBLIC_SIGNATURE_DEF_FUNCTION_H_
western_ai/usa/tensorflow/tensorflow/cc/saved_model/experimental/public/signature_def_function_metadata.h ADDED
@@ -0,0 +1,47 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /* Copyright 2020 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_EXPERIMENTAL_PUBLIC_SIGNATURE_DEF_FUNCTION_METADATA_H_
17
+ #define TENSORFLOW_CC_SAVED_MODEL_EXPERIMENTAL_PUBLIC_SIGNATURE_DEF_FUNCTION_METADATA_H_
18
+
19
+ #include <memory>
20
+
21
+ #include "tensorflow/c/experimental/saved_model/public/signature_def_function_metadata.h"
22
+
23
+ namespace tensorflow {
24
+ namespace experimental {
25
+ namespace cc {
26
+
27
+ // SignatureDefFunctionMetadata stores additional information on each input
28
+ // and output's names, dtypes, and shape.
29
+ class SignatureDefFunctionMetadata final {
30
+ // TODO(bmzhao): Add getters here as necessary.
31
+ private:
32
+ friend class SignatureDefFunction;
33
+ static SignatureDefFunctionMetadata* wrap(
34
+ TF_SignatureDefFunctionMetadata* p) {
35
+ return reinterpret_cast<SignatureDefFunctionMetadata*>(p);
36
+ }
37
+ static TF_SignatureDefFunctionMetadata* unwrap(
38
+ SignatureDefFunctionMetadata* p) {
39
+ return reinterpret_cast<TF_SignatureDefFunctionMetadata*>(p);
40
+ }
41
+ };
42
+
43
+ } // namespace cc
44
+ } // namespace experimental
45
+ } // namespace tensorflow
46
+
47
+ #endif // TENSORFLOW_CC_SAVED_MODEL_EXPERIMENTAL_PUBLIC_SIGNATURE_DEF_FUNCTION_METADATA_H_
western_ai/usa/tensorflow/tensorflow/cc/saved_model/experimental/tests/BUILD ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Tests for the C++ header-only SavedModelAPI.
2
+ load("//tensorflow:tensorflow.bzl", "tf_cc_test")
3
+
4
+ package(
5
+ # copybara:uncomment default_applicable_licenses = ["//tensorflow:license"],
6
+ licenses = ["notice"],
7
+ )
8
+
9
+ tf_cc_test(
10
+ name = "saved_model_api_test",
11
+ srcs = [
12
+ "saved_model_api_test.cc",
13
+ ],
14
+ data = [
15
+ "//tensorflow/cc/saved_model:saved_model_half_plus_two",
16
+ ],
17
+ deps = [
18
+ "//tensorflow/c:tf_status_headers",
19
+ "//tensorflow/cc/experimental/base/public:runtime",
20
+ "//tensorflow/cc/experimental/base/public:runtime_builder",
21
+ "//tensorflow/cc/experimental/base/public:status",
22
+ "//tensorflow/cc/saved_model/experimental/public:saved_model_api",
23
+ "//tensorflow/core:lib",
24
+ "//tensorflow/core:test",
25
+ "//tensorflow/core:test_main",
26
+ "@com_google_absl//absl/strings:string_view",
27
+ ],
28
+ )
western_ai/usa/tensorflow/tensorflow/cc/saved_model/experimental/tests/saved_model_api_test.cc ADDED
@@ -0,0 +1,98 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /* Copyright 2020 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/cc/saved_model/experimental/public/saved_model_api.h"
17
+
18
+ #include <memory>
19
+ #include <string>
20
+ #include <unordered_set>
21
+
22
+ #include "absl/strings/string_view.h"
23
+ #include "tensorflow/c/tf_status.h"
24
+ #include "tensorflow/cc/experimental/base/public/runtime.h"
25
+ #include "tensorflow/cc/experimental/base/public/runtime_builder.h"
26
+ #include "tensorflow/cc/experimental/base/public/status.h"
27
+ #include "tensorflow/core/platform/path.h"
28
+ #include "tensorflow/core/platform/stringpiece.h"
29
+ #include "tensorflow/core/platform/test.h"
30
+
31
+
32
+ namespace {
33
+
34
+ using tensorflow::experimental::cc::Runtime;
35
+ using tensorflow::experimental::cc::RuntimeBuilder;
36
+ using tensorflow::experimental::cc::SavedModelAPI;
37
+ using tensorflow::experimental::cc::Status;
38
+
39
+ constexpr char kTestData[] = "cc/saved_model/testdata";
40
+
41
+ std::string SavedModelPath(absl::string_view saved_model_dir) {
42
+ return tensorflow::io::JoinPath(tensorflow::testing::TensorFlowSrcRoot(),
43
+ kTestData, saved_model_dir);
44
+ }
45
+
46
+ // This value parameterized test allows us to test both TFRT
47
+ // and non TFRT runtimes.
48
+ // https://github.com/google/googletest/blob/dcc92d0ab6c4ce022162a23566d44f673251eee4/googletest/docs/advanced.md#value-parameterized-tests
49
+ class CPPSavedModelAPITest : public ::testing::TestWithParam<bool> {};
50
+
51
+ TEST_P(CPPSavedModelAPITest, LoadsSavedModelWithTags) {
52
+ Status status;
53
+ RuntimeBuilder builder;
54
+ bool use_tfrt = GetParam();
55
+ if (use_tfrt) {
56
+ GTEST_SKIP(); // TODO(chky) : Enable this once TFRT is open sourced.
57
+ }
58
+
59
+ builder.SetUseTFRT(use_tfrt);
60
+ std::unique_ptr<Runtime> runtime = builder.Build(&status);
61
+ ASSERT_TRUE(status.ok()) << status.message();
62
+
63
+ std::string model_dir = SavedModelPath("VarsAndArithmeticObjectGraph");
64
+ std::unordered_set<std::string> tags = {"serve"};
65
+ std::unique_ptr<SavedModelAPI> model =
66
+ SavedModelAPI::Load(model_dir, *runtime, &status, &tags);
67
+
68
+ // TODO(bmzhao): Change this to expect TF_OK when loading is implemented.
69
+ // That unblocks writing other tests that require a TF_SavedModel*,
70
+ // like loading a ConcreteFunction. This test at least checks that the
71
+ // C API builds and can be minimally run.
72
+ EXPECT_EQ(status.code(), TF_UNIMPLEMENTED);
73
+ }
74
+
75
+ TEST_P(CPPSavedModelAPITest, LoadsSavedModel) {
76
+ Status status;
77
+ RuntimeBuilder builder;
78
+ bool use_tfrt = GetParam();
79
+ if (use_tfrt) {
80
+ GTEST_SKIP(); // TODO(chky) : Enable this once TFRT is open sourced.
81
+ }
82
+
83
+ builder.SetUseTFRT(use_tfrt);
84
+ std::unique_ptr<Runtime> runtime = builder.Build(&status);
85
+ ASSERT_TRUE(status.ok()) << status.message();
86
+
87
+ std::string model_dir = SavedModelPath("VarsAndArithmeticObjectGraph");
88
+ std::unique_ptr<SavedModelAPI> model =
89
+ SavedModelAPI::Load(model_dir, *runtime, &status);
90
+
91
+ EXPECT_EQ(status.code(), TF_OK) << status.message();
92
+ }
93
+
94
+ INSTANTIATE_TEST_SUITE_P(RuntimeAgnosticCPPSavedModelTests,
95
+ CPPSavedModelAPITest, ::testing::Bool());
96
+
97
+ } // namespace
98
+
western_ai/usa/tensorflow/tensorflow/cc/saved_model/fingerprinting.cc ADDED
@@ -0,0 +1,285 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ #include "tensorflow/cc/saved_model/fingerprinting.h"
17
+
18
+ #include <cstdint>
19
+ #include <string>
20
+
21
+ #include "absl/container/btree_map.h"
22
+ #include "absl/log/log.h"
23
+ #include "absl/status/status.h"
24
+ #include "absl/status/statusor.h"
25
+ #include "absl/strings/str_cat.h"
26
+ #include "absl/strings/string_view.h"
27
+ #include "absl/strings/strip.h"
28
+ #include "tensorflow/cc/saved_model/constants.h"
29
+ #include "tensorflow/cc/saved_model/fingerprinting_x_platform_utils.h"
30
+ #include "tensorflow/core/framework/versions.pb.h"
31
+ #include "tensorflow/core/graph/regularization/simple_delete.h"
32
+ #include "tensorflow/core/graph/regularization/util.h"
33
+ #include "tensorflow/core/platform/env.h"
34
+ #include "tensorflow/core/platform/file_system_helper.h"
35
+ #include "tensorflow/core/platform/fingerprint.h"
36
+ #include "tensorflow/core/platform/path.h"
37
+ #include "tensorflow/core/platform/protobuf.h" // IWYU pragma: keep
38
+ #include "tensorflow/core/protobuf/fingerprint.pb.h"
39
+ #include "tensorflow/core/protobuf/meta_graph.pb.h"
40
+ #include "tensorflow/core/protobuf/saved_model.pb.h"
41
+ #include "tensorflow/core/protobuf/saved_object_graph.pb.h"
42
+ #include "tensorflow/core/util/tensor_bundle/naming.h"
43
+ // b/291933687, b/291001524
44
+ #if !defined(PLATFORM_WINDOWS) && !defined(__APPLE__)
45
+ #include "tensorflow/cc/saved_model/fingerprinting_utils.h"
46
+ #include "tensorflow/tools/proto_splitter/cc/util.h"
47
+ #include "tsl/platform/errors.h"
48
+ #include "tsl/platform/statusor.h"
49
+ #endif
50
+ // IWYU pragma: no_include "third_party/protobuf/io/coded_stream.h"
51
+ // IWYU pragma: no_include "third_party/protobuf/io/zero_copy_stream_impl_lite.h"
52
+
53
+ namespace tensorflow::saved_model::fingerprinting {
54
+
55
+ namespace {
56
+
57
+ using ::tensorflow::protobuf::Map;
58
+ // NOLINTNEXTLINE: clang-tidy missing-includes false positive
59
+ using ::tensorflow::protobuf::io::CodedOutputStream;
60
+ // NOLINTNEXTLINE: clang-tidy missing-includes false positive
61
+ using ::tensorflow::protobuf::io::StringOutputStream;
62
+
63
+ // TODO(b/290063184): remove when USM is GA
64
+ uint64_t HashCheckpointIndexFile(absl::string_view model_dir) {
65
+ std::string meta_filename = MetaFilename(io::JoinPath(
66
+ model_dir, kSavedModelVariablesDirectory, kSavedModelVariablesFilename));
67
+ std::string data;
68
+ absl::Status read_status =
69
+ ReadFileToString(Env::Default(), meta_filename, &data);
70
+ if (read_status.ok()) {
71
+ return tensorflow::Fingerprint64(data);
72
+ } else {
73
+ LOG(WARNING) << "Failed to read checkpoint file: " << read_status;
74
+ return 0;
75
+ }
76
+ }
77
+
78
+ uint64_t HashSavedModel(const SavedModel& saved_model) {
79
+ std::string saved_model_serialized;
80
+ {
81
+ // Local scope guarantees coded stream will be trimmed (ensures
82
+ // serialization determinism).
83
+ // Unfortunately the saving process itself isn't deterministic, so the
84
+ // checksum may still change since the saved_model proto may be different.
85
+ StringOutputStream stream(&saved_model_serialized);
86
+ CodedOutputStream output(&stream);
87
+ output.SetSerializationDeterministic(true);
88
+ saved_model.SerializeToCodedStream(&output);
89
+ }
90
+ return tensorflow::Fingerprint64(saved_model_serialized);
91
+ }
92
+
93
+ uint64_t RegularizeAndHashSignatureDefs(
94
+ const Map<std::string, SignatureDef>& signature_def_map) {
95
+ // Sort `signature_def_map`, which is an unordered map from string keys to
96
+ // SignatureDefs.
97
+ absl::btree_map<std::string, SignatureDef> sorted_signature_defs;
98
+ sorted_signature_defs.insert(signature_def_map.begin(),
99
+ signature_def_map.end());
100
+ uint64_t result_hash = 0;
101
+ for (const auto& item : sorted_signature_defs) {
102
+ result_hash =
103
+ FingerprintCat64(result_hash, tensorflow::Fingerprint64(item.first));
104
+ std::string signature_def_serialized;
105
+ {
106
+ StringOutputStream stream(&signature_def_serialized);
107
+ CodedOutputStream output(&stream);
108
+ output.SetSerializationDeterministic(true);
109
+ item.second.SerializeToCodedStream(&output);
110
+ }
111
+ result_hash = FingerprintCat64(
112
+ result_hash, tensorflow::Fingerprint64(signature_def_serialized));
113
+ }
114
+ return result_hash;
115
+ }
116
+
117
+ // The SavedObjectGraph contains two parts: the list of nodes and the map of
118
+ // concrete functions. Regularization treats these two parts separately.
119
+ absl::StatusOr<uint64_t> RegularizeAndHashSavedObjectGraph(
120
+ const SavedObjectGraph& object_graph_def) {
121
+ // Sort `concrete_functions`, which is an unordered map from function names to
122
+ // SavedConcreteFunction, using the suffix UID of the function name. Assumes
123
+ // that the trackable children are listed in a deterministic order during
124
+ // serialization.
125
+ absl::btree_map<int64_t, std::string> uid_to_function_names;
126
+ for (const auto& [name, concrete_function] :
127
+ object_graph_def.concrete_functions()) {
128
+ // All valid function names should end in an UID.
129
+ TF_ASSIGN_OR_RETURN(int64_t uid, graph_regularization::GetSuffixUID(name));
130
+ uid_to_function_names.insert({uid, name});
131
+ }
132
+ uint64_t result_hash = 0;
133
+ for (const auto& [uid, function_name] : uid_to_function_names) {
134
+ // Hash the function name (with the UID stripped).
135
+ result_hash = FingerprintCat64(result_hash,
136
+ tensorflow::Fingerprint64(absl::StripSuffix(
137
+ function_name, std::to_string(uid))));
138
+ // Hash the serialized concrete function.
139
+ std::string concrete_function_serialized;
140
+ {
141
+ StringOutputStream stream(&concrete_function_serialized);
142
+ CodedOutputStream output(&stream);
143
+ output.SetSerializationDeterministic(true);
144
+ object_graph_def.concrete_functions()
145
+ .at(function_name)
146
+ .SerializeToCodedStream(&output);
147
+ }
148
+ result_hash = FingerprintCat64(
149
+ result_hash, tensorflow::Fingerprint64(concrete_function_serialized));
150
+ }
151
+ // TODO(b/241294832): Complete canonicalization of `object_graph_def.nodes`.
152
+ return result_hash;
153
+ }
154
+
155
+ void SetFingerprintUUID(FingerprintDef* fingerprint_def) {
156
+ // Assign a random UUID to the fingerprint. This can happen regardless of
157
+ // whether the SavedModel was read successfully. It serves as a unique
158
+ // identifier for the model even in situations where the SavedModel is
159
+ // non-existent or unreadable.
160
+ fingerprint_def->set_uuid(CreateRandomUUID());
161
+ }
162
+
163
+ // Creates a FingerprintDef proto from a SavedModel and the checkpoint meta file
164
+ // (.index) in `export_dir`.
165
+ absl::StatusOr<FingerprintDef> CreateFingerprintDefPb(
166
+ absl::string_view export_dir, std::string pb_file) {
167
+ // Version of the code that produced the fingerprint.
168
+ // Note corresponding version definition in
169
+ // FingerprintingUtils.cc:CreateFingerprintDefCpb() and in
170
+ // Fingerprinting.cc:CreateReducedFingerprintDef()
171
+ const int kFingerprintProducer = 4;
172
+
173
+ SavedModel saved_model;
174
+ TF_RETURN_IF_ERROR(ReadBinaryProto(Env::Default(), pb_file, &saved_model));
175
+
176
+ // Create a copy of `metagraph` which will be used and mutated for fingerprint
177
+ // computation.
178
+ FingerprintDef fingerprint_def;
179
+ MetaGraphDef* metagraph = saved_model.mutable_meta_graphs(0);
180
+ // Set fingerprint field #1.
181
+ fingerprint_def.set_saved_model_checksum(HashSavedModel(saved_model));
182
+ // Set fingerprint field #2.
183
+ graph_regularization::SimpleDelete(*metagraph->mutable_graph_def());
184
+ fingerprint_def.set_graph_def_program_hash(
185
+ graph_regularization::ComputeHash(metagraph->graph_def()));
186
+ // Set fingerprint field #3.
187
+ fingerprint_def.set_signature_def_hash(
188
+ RegularizeAndHashSignatureDefs(metagraph->signature_def()));
189
+ // Set fingerprint field #4.
190
+ TF_ASSIGN_OR_RETURN(
191
+ uint64_t object_graph_hash,
192
+ RegularizeAndHashSavedObjectGraph(metagraph->object_graph_def()));
193
+ fingerprint_def.set_saved_object_graph_hash(object_graph_hash);
194
+ // Set fingerprint field #5.
195
+ fingerprint_def.set_checkpoint_hash(HashCheckpointIndexFile(export_dir));
196
+ SetFingerprintUUID(&fingerprint_def);
197
+ // Set version of the fingerprint.
198
+ VersionDef* version = fingerprint_def.mutable_version();
199
+ version->set_producer(kFingerprintProducer);
200
+
201
+ return fingerprint_def;
202
+ }
203
+
204
+ absl::StatusOr<FingerprintDef> CreateReducedFingerprintDef() {
205
+ // Version of the code that produced the fingerprint.
206
+ // Note corresponding version definition in
207
+ // FingerprintingUtils.cc:CreateFingerprintDefCpb() and in
208
+ // Fingerprinting.cc:CreateFingerprintDefPb()
209
+ const int kFingerprintProducer = 5;
210
+
211
+ FingerprintDef fingerprint_def;
212
+ SetFingerprintUUID(&fingerprint_def);
213
+ // Set version of the fingerprint.
214
+ VersionDef* version = fingerprint_def.mutable_version();
215
+ version->set_producer(kFingerprintProducer);
216
+
217
+ return fingerprint_def;
218
+ }
219
+
220
+ } // namespace
221
+
222
+ absl::StatusOr<FingerprintDef> CreateFingerprintDef(
223
+ absl::string_view export_dir) {
224
+ std::string prefix = io::JoinPath(export_dir, kSavedModelFilenamePrefix);
225
+
226
+ #if !defined(PLATFORM_WINDOWS) && !defined(__APPLE__)
227
+ absl::StatusOr<bool> only_contains_pb =
228
+ tools::proto_splitter::OnlyContainsPb(prefix);
229
+ // TODO: b/455882293 - Enable reporting errors if the .[c]pb files are
230
+ // important but still missing.
231
+ if (only_contains_pb.ok()) {
232
+ if (*only_contains_pb) {
233
+ return CreateFingerprintDefPb(export_dir, absl::StrCat(prefix, ".pb"));
234
+ }
235
+ return CreateFingerprintDefCpb(export_dir, absl::StrCat(prefix, ".cpb"));
236
+ }
237
+ // At this point we have neither saved_model.pb nor saved_model.cpb.
238
+ return CreateReducedFingerprintDef(); // Only sets the UUID.
239
+ #else // The following runs on Windows and Mac.
240
+ absl::StatusOr<FingerprintDef> fingerprint_def =
241
+ CreateFingerprintDefPb(export_dir, absl::StrCat(prefix, ".pb"));
242
+ if (!fingerprint_def.ok()) {
243
+ return CreateReducedFingerprintDef();
244
+ }
245
+ return fingerprint_def;
246
+ #endif
247
+ }
248
+
249
+ absl::StatusOr<FingerprintDef> ReadSavedModelFingerprint(
250
+ absl::string_view export_dir) {
251
+ const std::string fingerprint_pb_path =
252
+ io::JoinPath(export_dir, kFingerprintFilenamePb);
253
+ TF_RETURN_IF_ERROR(Env::Default()->FileExists(fingerprint_pb_path));
254
+
255
+ FingerprintDef fingerprint_proto;
256
+ absl::Status result =
257
+ ReadBinaryProto(Env::Default(), fingerprint_pb_path, &fingerprint_proto);
258
+ if (!result.ok()) return result;
259
+
260
+ return fingerprint_proto;
261
+ }
262
+
263
+ std::string Singleprint(uint64_t graph_def_program_hash,
264
+ uint64_t signature_def_hash,
265
+ uint64_t saved_object_graph_hash,
266
+ uint64_t checkpoint_hash) {
267
+ return std::to_string(graph_def_program_hash) + "/" +
268
+ std::to_string(signature_def_hash) + "/" +
269
+ std::to_string(saved_object_graph_hash) + "/" +
270
+ std::to_string(checkpoint_hash);
271
+ }
272
+
273
+ std::string Singleprint(const FingerprintDef& fingerprint) {
274
+ return Singleprint(
275
+ fingerprint.graph_def_program_hash(), fingerprint.signature_def_hash(),
276
+ fingerprint.saved_object_graph_hash(), fingerprint.checkpoint_hash());
277
+ }
278
+
279
+ absl::StatusOr<std::string> Singleprint(absl::string_view export_dir) {
280
+ TF_ASSIGN_OR_RETURN(const FingerprintDef fingerprint_def,
281
+ ReadSavedModelFingerprint(export_dir));
282
+ return Singleprint(fingerprint_def);
283
+ }
284
+
285
+ } // namespace tensorflow::saved_model::fingerprinting
western_ai/usa/tensorflow/tensorflow/cc/saved_model/fingerprinting.h ADDED
@@ -0,0 +1,47 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 "absl/status/statusor.h"
22
+ #include "absl/strings/string_view.h"
23
+ #include "tensorflow/core/protobuf/fingerprint.pb.h"
24
+
25
+ namespace tensorflow::saved_model::fingerprinting {
26
+
27
+ // Creates a FingerprintDef proto from a SavedModel (regular or chunked) and the
28
+ // checkpoint meta file (.index) in `export_dir`.
29
+ absl::StatusOr<FingerprintDef> CreateFingerprintDef(
30
+ absl::string_view export_dir);
31
+
32
+ // Loads the `fingerprint.pb` from `export_dir`, returns an error if there is
33
+ // none.
34
+ absl::StatusOr<FingerprintDef> ReadSavedModelFingerprint(
35
+ absl::string_view export_dir);
36
+
37
+ // Canonical fingerprinting ID for a SavedModel.
38
+ std::string Singleprint(uint64_t graph_def_program_hash,
39
+ uint64_t signature_def_hash,
40
+ uint64_t saved_object_graph_hash,
41
+ uint64_t checkpoint_hash);
42
+ std::string Singleprint(const FingerprintDef& fingerprint);
43
+ absl::StatusOr<std::string> Singleprint(absl::string_view export_dir);
44
+
45
+ } // namespace tensorflow::saved_model::fingerprinting
46
+
47
+ #endif // TENSORFLOW_CC_SAVED_MODEL_FINGERPRINTING_H_
western_ai/usa/tensorflow/tensorflow/cc/saved_model/fingerprinting_chunked_test.cc ADDED
@@ -0,0 +1,52 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /* Copyright 2023 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 <cstdint>
17
+ #include <string>
18
+
19
+ #include <gmock/gmock.h>
20
+ #include <gtest/gtest.h>
21
+ #include "absl/container/flat_hash_set.h"
22
+ #include "tensorflow/cc/saved_model/fingerprinting.h"
23
+ #include "tensorflow/core/platform/path.h"
24
+ #include "tensorflow/core/platform/test.h"
25
+ #include "tensorflow/core/protobuf/fingerprint.pb.h"
26
+ #include "tsl/platform/statusor.h"
27
+
28
+ namespace tensorflow::saved_model::fingerprinting {
29
+
30
+ namespace {
31
+
32
+ TEST(FingerprintingTest, TestChunkedProto) {
33
+ const std::string export_dir =
34
+ io::JoinPath(testing::TensorFlowSrcRoot(), "cc/saved_model/testdata",
35
+ "chunked_saved_model/chunked_model");
36
+ TF_ASSERT_OK_AND_ASSIGN(FingerprintDef fingerprint_pb,
37
+ CreateFingerprintDef(export_dir));
38
+ EXPECT_GT(fingerprint_pb.saved_model_checksum(), 0);
39
+ // We test for multiple fingerprints due to non-determinism when building with
40
+ // different compilation_mode flag options.
41
+ EXPECT_THAT(absl::flat_hash_set<uint64_t>(
42
+ {906548630859202535U, 9562420523583756263U}),
43
+ ::testing::Contains(fingerprint_pb.graph_def_program_hash()));
44
+ EXPECT_EQ(fingerprint_pb.signature_def_hash(), 1043582354059066488U);
45
+ EXPECT_THAT(absl::flat_hash_set<uint64_t>(
46
+ {2766043449526180728U, 11894619660760763927U}),
47
+ ::testing::Contains(fingerprint_pb.saved_object_graph_hash()));
48
+ EXPECT_EQ(fingerprint_pb.checkpoint_hash(), 0);
49
+ }
50
+
51
+ } // namespace
52
+ } // namespace tensorflow::saved_model::fingerprinting
western_ai/usa/tensorflow/tensorflow/cc/saved_model/fingerprinting_test.cc ADDED
@@ -0,0 +1,210 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ #include "tensorflow/cc/saved_model/fingerprinting.h"
17
+
18
+ #include <string>
19
+
20
+ #include <gtest/gtest.h>
21
+ #include "absl/numeric/int128.h"
22
+ #include "absl/status/status.h"
23
+ #include "absl/status/statusor.h"
24
+ #include "absl/strings/numbers.h"
25
+ #include "absl/strings/string_view.h"
26
+ #include "tensorflow/core/framework/graph.pb.h"
27
+ #include "tensorflow/core/framework/versions.pb.h"
28
+ #include "tensorflow/core/platform/env.h"
29
+ #include "tensorflow/core/platform/path.h"
30
+ #include "tensorflow/core/platform/test.h"
31
+ #include "tensorflow/core/platform/types.h"
32
+ #include "tensorflow/core/protobuf/fingerprint.pb.h"
33
+ #include "tensorflow/core/protobuf/saved_model.pb.h"
34
+ #include "tsl/platform/statusor.h"
35
+
36
+ namespace tensorflow::saved_model::fingerprinting {
37
+
38
+ namespace {
39
+
40
+ absl::StatusOr<SavedModel> ReadSavedModel(absl::string_view file_dir) {
41
+ std::string file_path = io::JoinPath(file_dir, "saved_model.pb");
42
+ std::string serialized_saved_model;
43
+ auto status =
44
+ ReadFileToString(Env::Default(), file_path, &serialized_saved_model);
45
+ if (!status.ok()) {
46
+ return status;
47
+ }
48
+ SavedModel saved_model_pb;
49
+ saved_model_pb.ParseFromString(serialized_saved_model);
50
+ return saved_model_pb;
51
+ }
52
+
53
+ TEST(FingerprintingTest, TestCreateFingerprint) {
54
+ const std::string export_dir =
55
+ io::JoinPath(testing::TensorFlowSrcRoot(), "cc/saved_model/testdata",
56
+ "VarsAndArithmeticObjectGraph");
57
+ TF_ASSERT_OK_AND_ASSIGN(SavedModel saved_model_pb,
58
+ ReadSavedModel(export_dir));
59
+ TF_ASSERT_OK_AND_ASSIGN(FingerprintDef fingerprint_def,
60
+ CreateFingerprintDef(export_dir));
61
+
62
+ EXPECT_GT(fingerprint_def.saved_model_checksum(), 0);
63
+ EXPECT_EQ(fingerprint_def.graph_def_program_hash(), 10127142238652115842U);
64
+ EXPECT_EQ(fingerprint_def.signature_def_hash(), 15570736222402453744U);
65
+ EXPECT_EQ(fingerprint_def.saved_object_graph_hash(), 3678101440349108924U);
66
+
67
+ // The uuid is a random number (as string), but it should be a number > 0.
68
+ absl::uint128 uuid = 0;
69
+ EXPECT_TRUE(absl::SimpleAtoi(fingerprint_def.uuid(), &uuid))
70
+ << "String to Uint128 conversion failed. "
71
+ << "UUID from proto, and Uint128Max(): \n"
72
+ << fingerprint_def.uuid() << "\n"
73
+ << absl::Uint128Max();
74
+ EXPECT_GT(uuid, 0);
75
+
76
+ // TODO(b/242348400): The checkpoint hash is non-deterministic, so we cannot
77
+ // check its value here.
78
+ EXPECT_GT(fingerprint_def.checkpoint_hash(), 0);
79
+ }
80
+
81
+ TEST(FingerprintingTest, TestCreateFingerprintForPbtxtWorks) {
82
+ // This test ensures that we get a minimal fingerprint with an uuid, even
83
+ // when the SavedModel cannot be read.
84
+ const std::string export_dir =
85
+ io::JoinPath(testing::TensorFlowSrcRoot(), "cc/saved_model/testdata",
86
+ "half_plus_two_pbtxt");
87
+ TF_ASSERT_OK_AND_ASSIGN(FingerprintDef fingerprint_def,
88
+ CreateFingerprintDef(export_dir));
89
+
90
+ EXPECT_EQ(fingerprint_def.saved_model_checksum(), 0);
91
+ EXPECT_EQ(fingerprint_def.graph_def_program_hash(), 0);
92
+ EXPECT_EQ(fingerprint_def.signature_def_hash(), 0);
93
+ EXPECT_EQ(fingerprint_def.saved_object_graph_hash(), 0);
94
+ EXPECT_EQ(fingerprint_def.checkpoint_hash(), 0);
95
+
96
+ // The uuid is a random number (as string), but it should be a number > 0.
97
+ absl::uint128 uuid = 0;
98
+ EXPECT_TRUE(absl::SimpleAtoi(fingerprint_def.uuid(), &uuid))
99
+ << "String to Uint128 conversion failed. "
100
+ << "UUID from proto, and Uint128Max(): \n"
101
+ << fingerprint_def.uuid() << "\n"
102
+ << absl::Uint128Max();
103
+ EXPECT_GT(uuid, 0);
104
+ // version().producer()differs between WIN and non-WIN platforms.
105
+ EXPECT_GT(fingerprint_def.version().producer(), 3);
106
+ }
107
+
108
+ // Compare the fingerprints of two models saved by calling
109
+ // `tf.saved_model.save` twice in a row in the same program.
110
+ TEST(FingerprintingTest, TestCompareFingerprintForTwoModelSavedTwice) {
111
+ const std::string export_dir = io::JoinPath(
112
+ testing::TensorFlowSrcRoot(), "cc/saved_model/testdata", "bert1");
113
+
114
+ TF_ASSERT_OK_AND_ASSIGN(SavedModel saved_model_pb,
115
+ ReadSavedModel(export_dir));
116
+ TF_ASSERT_OK_AND_ASSIGN(FingerprintDef fingerprint_def,
117
+ CreateFingerprintDef(export_dir));
118
+
119
+ const std::string export_dir2 = io::JoinPath(
120
+ testing::TensorFlowSrcRoot(), "cc/saved_model/testdata", "bert2");
121
+ TF_ASSERT_OK_AND_ASSIGN(SavedModel saved_model_pb2,
122
+ ReadSavedModel(export_dir2));
123
+ TF_ASSERT_OK_AND_ASSIGN(FingerprintDef fingerprint_def2,
124
+ CreateFingerprintDef(export_dir2));
125
+
126
+ // While the saved_model serialization is deterministic, the model saving and
127
+ // proto construction is not. Therefore, we can't compare the two
128
+ // fingerprints' saved_model_checksums.
129
+ EXPECT_GT(fingerprint_def.saved_model_checksum(), 0);
130
+ EXPECT_GT(fingerprint_def2.saved_model_checksum(), 0);
131
+ EXPECT_EQ(fingerprint_def.graph_def_program_hash(),
132
+ fingerprint_def2.graph_def_program_hash());
133
+ EXPECT_EQ(fingerprint_def.signature_def_hash(),
134
+ fingerprint_def2.signature_def_hash());
135
+ EXPECT_EQ(fingerprint_def.saved_object_graph_hash(),
136
+ fingerprint_def2.saved_object_graph_hash());
137
+ EXPECT_NE(fingerprint_def.uuid(), fingerprint_def2.uuid());
138
+ }
139
+
140
+ TEST(FingerprintingTest, TestFingerprintComputationDoesNotMutateModel) {
141
+ const std::string export_dir = io::JoinPath(
142
+ testing::TensorFlowSrcRoot(), "cc/saved_model/testdata", "bert1");
143
+ TF_ASSERT_OK_AND_ASSIGN(SavedModel saved_model_pb,
144
+ ReadSavedModel(export_dir));
145
+ TF_ASSERT_OK_AND_ASSIGN(FingerprintDef fingerprint_def,
146
+ CreateFingerprintDef(export_dir));
147
+ TF_ASSERT_OK_AND_ASSIGN(FingerprintDef fingerprint_def2,
148
+ CreateFingerprintDef(export_dir));
149
+
150
+ EXPECT_EQ(fingerprint_def.saved_model_checksum(),
151
+ fingerprint_def2.saved_model_checksum());
152
+ }
153
+
154
+ TEST(FingerprintingTest, TestFingerprintHasVersion) {
155
+ const std::string export_dir = io::JoinPath(
156
+ testing::TensorFlowSrcRoot(), "cc/saved_model/testdata", "bert1");
157
+ TF_ASSERT_OK_AND_ASSIGN(SavedModel saved_model_pb,
158
+ ReadSavedModel(export_dir));
159
+ TF_ASSERT_OK_AND_ASSIGN(FingerprintDef fingerprint_def,
160
+ CreateFingerprintDef(export_dir));
161
+ EXPECT_EQ(fingerprint_def.version().producer(), 4);
162
+ }
163
+
164
+ TEST(FingerprintingTest, TestHashCheckpointForModelWithNoVariables) {
165
+ const std::string export_dir = io::JoinPath(
166
+ testing::TensorFlowSrcRoot(), "cc/saved_model/testdata", "bert1");
167
+ TF_ASSERT_OK_AND_ASSIGN(SavedModel saved_model_pb,
168
+ ReadSavedModel(export_dir));
169
+ TF_ASSERT_OK_AND_ASSIGN(FingerprintDef fingerprint_def,
170
+ CreateFingerprintDef(export_dir));
171
+ EXPECT_EQ(fingerprint_def.checkpoint_hash(), 0);
172
+ }
173
+
174
+ TEST(FingerprintingTest, TestReadValidFingerprint) {
175
+ const std::string export_dir =
176
+ io::JoinPath(testing::TensorFlowSrcRoot(), "cc/saved_model/testdata",
177
+ "VarsAndArithmeticObjectGraph");
178
+ TF_ASSERT_OK_AND_ASSIGN(FingerprintDef fingerprint_pb,
179
+ ReadSavedModelFingerprint(export_dir));
180
+ EXPECT_EQ(fingerprint_pb.saved_model_checksum(), 15788619162413586750u);
181
+ }
182
+
183
+ TEST(FingerprintingTest, TestReadNonexistentFingerprint) {
184
+ const std::string export_dir = io::JoinPath(
185
+ testing::TensorFlowSrcRoot(), "cc/saved_model/testdata", "AssetModule");
186
+ EXPECT_EQ(ReadSavedModelFingerprint(export_dir).status().code(),
187
+ absl::StatusCode::kNotFound);
188
+ }
189
+
190
+ TEST(FingerprintingTest, TestSingleprint) {
191
+ const std::string export_dir =
192
+ io::JoinPath(testing::TensorFlowSrcRoot(), "cc/saved_model/testdata",
193
+ "VarsAndArithmeticObjectGraph");
194
+ const std::string const_singleprint =
195
+ "706963557435316516/5693392539583495303/12074714563970609759/"
196
+ "10788359570789890102";
197
+ TF_ASSERT_OK_AND_ASSIGN(std::string singleprint, Singleprint(export_dir));
198
+ EXPECT_EQ(singleprint, const_singleprint);
199
+ TF_ASSERT_OK_AND_ASSIGN(FingerprintDef fingerprint_pb,
200
+ ReadSavedModelFingerprint(export_dir));
201
+ EXPECT_EQ(Singleprint(fingerprint_pb), const_singleprint);
202
+ EXPECT_EQ(Singleprint(fingerprint_pb.graph_def_program_hash(),
203
+ fingerprint_pb.signature_def_hash(),
204
+ fingerprint_pb.saved_object_graph_hash(),
205
+ fingerprint_pb.checkpoint_hash()),
206
+ const_singleprint);
207
+ }
208
+
209
+ } // namespace
210
+ } // namespace tensorflow::saved_model::fingerprinting
western_ai/usa/tensorflow/tensorflow/cc/saved_model/fingerprinting_utils.cc ADDED
@@ -0,0 +1,488 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /* Copyright 2023 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/cc/saved_model/fingerprinting_utils.h"
17
+
18
+ #include <algorithm>
19
+ #include <cstdint>
20
+ #include <memory>
21
+ #include <string>
22
+ #include <utility>
23
+ #include <vector>
24
+
25
+ #include "absl/status/status.h"
26
+ #include "absl/status/statusor.h"
27
+ #include "absl/strings/str_cat.h"
28
+ #include "absl/strings/string_view.h"
29
+ #include "riegeli/bytes/fd_reader.h" // from @riegeli
30
+ #include "riegeli/records/record_reader.h" // from @riegeli
31
+ #include "tensorflow/cc/saved_model/constants.h"
32
+ #include "tensorflow/cc/saved_model/fingerprinting_x_platform_utils.h"
33
+ #include "tensorflow/core/framework/function.pb.h"
34
+ #include "tensorflow/core/framework/graph.pb.h"
35
+ #include "tensorflow/core/framework/op_def.pb.h"
36
+ #include "tensorflow/core/framework/versions.pb.h"
37
+ #include "tensorflow/core/platform/env.h"
38
+ #include "tensorflow/core/platform/file_system_helper.h"
39
+ #include "tensorflow/core/platform/fingerprint.h"
40
+ #include "tensorflow/core/platform/path.h"
41
+ #include "tensorflow/core/protobuf/fingerprint.pb.h"
42
+ #include "tensorflow/core/protobuf/meta_graph.pb.h"
43
+ #include "tensorflow/core/protobuf/saved_model.pb.h"
44
+ #include "tensorflow/core/protobuf/saved_object_graph.pb.h"
45
+ #include "tensorflow/core/util/tensor_bundle/naming.h"
46
+ #include "tensorflow/tools/proto_splitter/cc/util.h"
47
+ #include "tensorflow/tools/proto_splitter/chunk.pb.h"
48
+ #include "tensorflow/tools/proto_splitter/merge.h"
49
+ #include "tsl/platform/errors.h"
50
+ #include "tsl/platform/statusor.h"
51
+ // IWYU pragma: no_include "third_party/protobuf/repeated_ptr_field.h"
52
+ // IWYU pragma: no_include "third_party/protobuf/io/coded_stream.h"
53
+ // IWYU pragma: no_include "third_party/protobuf/io/zero_copy_stream_impl_lite.h"
54
+
55
+ namespace tensorflow::saved_model::fingerprinting {
56
+
57
+ using ::tensorflow::proto_splitter::ChunkedField;
58
+ using ::tensorflow::proto_splitter::ChunkedMessage;
59
+ using ::tensorflow::proto_splitter::ChunkInfo;
60
+ using ::tensorflow::proto_splitter::ChunkMetadata;
61
+ using ::tensorflow::proto_splitter::FieldIndex;
62
+ using tools::proto_splitter::Field;
63
+ using tools::proto_splitter::FieldType;
64
+ using tools::proto_splitter::GetChunkMetadata;
65
+ using tools::proto_splitter::GetFieldTypes;
66
+ using tools::proto_splitter::GetMutableField;
67
+ using tools::proto_splitter::GetRiegeliReader;
68
+ using tools::proto_splitter::Merger;
69
+ using tools::proto_splitter::MutableFieldResult;
70
+ using tools::proto_splitter::ReadChunk;
71
+
72
+ namespace fingerprinting_utils_internal {
73
+
74
+ using ::tensorflow::protobuf::Map;
75
+ using ::tensorflow::protobuf::Message;
76
+ using ::tensorflow::protobuf::RepeatedPtrField;
77
+ // NOLINTNEXTLINE: clang-tidy missing-includes false positive
78
+ using ::tensorflow::protobuf::io::CodedOutputStream;
79
+ // NOLINTNEXTLINE: clang-tidy missing-includes false positive
80
+ using ::tensorflow::protobuf::io::StringOutputStream;
81
+
82
+ absl::StatusOr<int> fieldTagMatches(const RepeatedPtrField<FieldIndex>& a,
83
+ const RepeatedPtrField<FieldIndex>& b) {
84
+ int matches = 0;
85
+ for (int i = 0; i == matches && i < a.size() && i < b.size(); i++) {
86
+ switch (b[i].kind_case()) {
87
+ case ::tensorflow::proto_splitter::FieldIndex::KindCase::kField:
88
+ if (a.at(i).has_field() && a.at(i).field() == b.at(i).field()) {
89
+ matches += 1;
90
+ }
91
+ break;
92
+ case ::tensorflow::proto_splitter::FieldIndex::KindCase::kIndex:
93
+ if (a.at(i).has_index() && a.at(i).index() == b.at(i).index()) {
94
+ matches += 1;
95
+ }
96
+ break;
97
+ case ::tensorflow::proto_splitter::FieldIndex::KindCase::kMapKey:
98
+ if (a.at(i).has_map_key()) {
99
+ const ::tensorflow::proto_splitter::FieldIndex_MapKey& key =
100
+ b.at(i).map_key();
101
+ const ::tensorflow::proto_splitter::FieldIndex_MapKey& chunked_key =
102
+ a.at(i).map_key();
103
+ switch (key.type_case()) {
104
+ case ::tensorflow::proto_splitter::FieldIndex::MapKey::TypeCase::kS:
105
+ if (chunked_key.has_s() && chunked_key.s() == key.s()) {
106
+ matches += 1;
107
+ }
108
+ break;
109
+ case ::tensorflow::proto_splitter::FieldIndex::MapKey::TypeCase::
110
+ kBoolean:
111
+ if (chunked_key.has_boolean() &&
112
+ chunked_key.boolean() == key.boolean()) {
113
+ matches += 1;
114
+ }
115
+ break;
116
+ case ::tensorflow::proto_splitter::FieldIndex::MapKey::TypeCase::
117
+ kUi32:
118
+ if (chunked_key.has_ui32() && chunked_key.ui32() == key.ui32()) {
119
+ matches += 1;
120
+ }
121
+ break;
122
+ case ::tensorflow::proto_splitter::FieldIndex::MapKey::TypeCase::
123
+ kUi64:
124
+ if (chunked_key.has_ui64() && chunked_key.ui64() == key.ui64()) {
125
+ matches += 1;
126
+ }
127
+ break;
128
+ case ::tensorflow::proto_splitter::FieldIndex::MapKey::TypeCase::
129
+ kI32:
130
+ if (chunked_key.has_i32() && chunked_key.i32() == key.i32()) {
131
+ matches += 1;
132
+ }
133
+ break;
134
+ case ::tensorflow::proto_splitter::FieldIndex::MapKey::TypeCase::
135
+ kI64:
136
+ if (chunked_key.has_i64() && chunked_key.i64() == key.i64()) {
137
+ matches += 1;
138
+ }
139
+ break;
140
+ case ::tensorflow::proto_splitter::FieldIndex::MapKey::TypeCase::
141
+ TYPE_NOT_SET:
142
+ default:
143
+ return absl::FailedPreconditionError(
144
+ "Encountered unknown field_tag.map_key type.");
145
+ }
146
+ }
147
+ break;
148
+ case FieldIndex::KindCase::KIND_NOT_SET:
149
+ default:
150
+ return absl::FailedPreconditionError(
151
+ "Encountered unknown field_tag kind.");
152
+ }
153
+ }
154
+ return matches;
155
+ }
156
+
157
+ absl::StatusOr<::tensorflow::proto_splitter::ChunkedMessage>
158
+ PruneChunkedMessage(
159
+ const ::tensorflow::proto_splitter::ChunkedMessage& chunked_message,
160
+ riegeli::RecordReader<riegeli::FdReader<>>& reader,
161
+ std::vector<ChunkInfo> chunks_info,
162
+ std::vector<RepeatedPtrField<FieldIndex>> target_fields_list) {
163
+ ::tensorflow::proto_splitter::ChunkedMessage pruned_chunked_message;
164
+ if (chunked_message.has_chunk_index()) {
165
+ pruned_chunked_message.set_chunk_index(chunked_message.chunk_index());
166
+ }
167
+ // For each chunked_field, check if it matches any of the supplied
168
+ // target_fields, and copy over the relevant data.
169
+ for (const ChunkedField& chunked_field : chunked_message.chunked_fields()) {
170
+ for (const auto& target_fields : target_fields_list) {
171
+ TF_ASSIGN_OR_RETURN(
172
+ int matches,
173
+ fieldTagMatches(chunked_field.field_tag(), target_fields));
174
+ if (matches == chunked_field.field_tag_size()) {
175
+ // chunked_field_tags is an initial subsequence of target_fields, which
176
+ // means the chunked_field is relevant and the necessary data should be
177
+ // copied over.
178
+ auto cf = std::make_unique<proto_splitter::ChunkedField>();
179
+ cf->mutable_field_tag()->CopyFrom(chunked_field.field_tag());
180
+ TF_ASSIGN_OR_RETURN(
181
+ *cf->mutable_message(),
182
+ PruneChunkedMessage(chunked_field.message(), reader, chunks_info,
183
+ target_fields_list));
184
+ pruned_chunked_message.mutable_chunked_fields()->AddAllocated(
185
+ cf.release());
186
+ }
187
+ }
188
+ }
189
+ return pruned_chunked_message;
190
+ }
191
+
192
+ std::string SerializeProto(const Message& message) {
193
+ std::string serialized_message;
194
+ {
195
+ // local scope guarantees coded stream will be trimmed (ensures determinism)
196
+ StringOutputStream stream(&serialized_message);
197
+ CodedOutputStream output(&stream);
198
+ output.SetSerializationDeterministic(true);
199
+ message.SerializeToCodedStream(&output);
200
+ }
201
+ return serialized_message;
202
+ }
203
+
204
+ absl::StatusOr<uint64_t> HashFields(
205
+ const ChunkedMessage& chunked_message,
206
+ riegeli::RecordReader<riegeli::FdReader<>>& reader,
207
+ const std::vector<ChunkInfo>& chunks_info,
208
+ const RepeatedPtrField<FieldIndex>& field_tags, Message* merged_message) {
209
+ uint64_t field_checksum = 0;
210
+ // Find chunked_fields that match the field_tags.
211
+ for (const ChunkedField& chunked_field : chunked_message.chunked_fields()) {
212
+ const RepeatedPtrField<FieldIndex> chunked_field_tags =
213
+ chunked_field.field_tag();
214
+ const ChunkedMessage& chunked_message = chunked_field.message();
215
+ // Number of sequential field_tag matches.
216
+ TF_ASSIGN_OR_RETURN(int matches,
217
+ fieldTagMatches(chunked_field_tags, field_tags));
218
+
219
+ if (chunked_message.has_chunk_index() && matches == field_tags.size()) {
220
+ // chunked_field_tags are an exact match with field_tags. Hash referenced
221
+ // chunk.
222
+ TF_ASSIGN_OR_RETURN(
223
+ std::string chunk,
224
+ ReadChunk(reader, chunks_info[chunked_message.chunk_index()]));
225
+ field_checksum = FingerprintCat64(field_checksum, Fingerprint64(chunk));
226
+ } else if (matches == field_tags.size()) {
227
+ // chunked_field_tags are an exact match, but chunked_field is further
228
+ // broken down into separate chunked_fields (no chunk_index). Hash those
229
+ // chunked_fields.
230
+ TF_ASSIGN_OR_RETURN(uint64_t hash,
231
+ HashFields(chunked_message, reader, chunks_info,
232
+ field_tags, merged_message));
233
+ field_checksum = FingerprintCat64(field_checksum, hash);
234
+ } else if (chunked_message.has_chunk_index() &&
235
+ matches == chunked_field_tags.size()) {
236
+ // chunked_field_tags are a partial match (an initial segment/subsequence
237
+ // of field_tags). Merge chunk in, attempt to locate & hash the target
238
+ // field by recursing.
239
+ TF_ASSIGN_OR_RETURN(std::vector<Field> fields,
240
+ GetFieldTypes(chunked_field_tags));
241
+ for (const auto& field : fields) {
242
+ TF_ASSIGN_OR_RETURN(MutableFieldResult mfr,
243
+ GetMutableField(merged_message, field));
244
+ merged_message =
245
+ mfr.parent->GetReflection()->MutableMessage(mfr.parent, mfr.field);
246
+ }
247
+ TF_ASSIGN_OR_RETURN(
248
+ std::string chunk,
249
+ ReadChunk(reader, chunks_info[chunked_message.chunk_index()]));
250
+ merged_message->ParseFromString(chunk);
251
+ TF_ASSIGN_OR_RETURN(uint64_t hash,
252
+ HashFields(chunked_message, reader, chunks_info,
253
+ field_tags, merged_message));
254
+ field_checksum = FingerprintCat64(field_checksum, hash);
255
+ } else if (matches == chunked_field_tags.size()) {
256
+ // chunk_field_tags are a partial match, but chunked_field is broken down.
257
+ // Merge chunked_fields in, attempt to locate & hash target field.
258
+ for (const ChunkedField& cf : chunked_message.chunked_fields()) {
259
+ TF_ASSIGN_OR_RETURN(uint64_t hash,
260
+ HashFields(cf.message(), reader, chunks_info,
261
+ field_tags, merged_message));
262
+ field_checksum = FingerprintCat64(field_checksum, hash);
263
+ }
264
+ }
265
+ }
266
+ return field_checksum;
267
+ }
268
+
269
+ inline RepeatedPtrField<FieldIndex> GraphDefFieldTags() {
270
+ // SavedModel.meta_graphs[0].graph_def
271
+ FieldIndex meta_graph_field_tag;
272
+ meta_graph_field_tag.set_field(2);
273
+ FieldIndex meta_graph_index_field_tag;
274
+ meta_graph_index_field_tag.set_index(0);
275
+ FieldIndex graph_def_field_tag;
276
+ graph_def_field_tag.set_field(2);
277
+ RepeatedPtrField<FieldIndex> graph_def_field_tags;
278
+ graph_def_field_tags.Add(FieldIndex(meta_graph_field_tag));
279
+ graph_def_field_tags.Add(FieldIndex(meta_graph_index_field_tag));
280
+ graph_def_field_tags.Add(FieldIndex(graph_def_field_tag));
281
+
282
+ return graph_def_field_tags;
283
+ }
284
+
285
+ inline RepeatedPtrField<FieldIndex> SignatureDefFieldTags() {
286
+ // SavedModel.meta_graphs[0].signature_def
287
+ FieldIndex meta_graph_field_tag;
288
+ meta_graph_field_tag.set_field(2);
289
+ FieldIndex meta_graph_index_field_tag;
290
+ meta_graph_index_field_tag.set_index(0);
291
+ FieldIndex signature_def_field_tag;
292
+ signature_def_field_tag.set_field(5);
293
+ RepeatedPtrField<FieldIndex> signature_def_field_tags;
294
+ signature_def_field_tags.Add(FieldIndex(meta_graph_field_tag));
295
+ signature_def_field_tags.Add(FieldIndex(meta_graph_index_field_tag));
296
+ signature_def_field_tags.Add(FieldIndex(signature_def_field_tag));
297
+
298
+ return signature_def_field_tags;
299
+ }
300
+
301
+ inline RepeatedPtrField<FieldIndex> SavedObjectGraphFieldTags() {
302
+ // SavedModel.meta_graphs[0].object_graph_def
303
+ FieldIndex meta_graph_field_tag;
304
+ meta_graph_field_tag.set_field(2);
305
+ FieldIndex meta_graph_index_field_tag;
306
+ meta_graph_index_field_tag.set_index(0);
307
+ FieldIndex saved_object_graph_field_tag;
308
+ saved_object_graph_field_tag.set_field(7);
309
+ RepeatedPtrField<FieldIndex> saved_object_graph_field_tags;
310
+ saved_object_graph_field_tags.Add(FieldIndex(meta_graph_field_tag));
311
+ saved_object_graph_field_tags.Add(FieldIndex(meta_graph_index_field_tag));
312
+ saved_object_graph_field_tags.Add(FieldIndex(saved_object_graph_field_tag));
313
+
314
+ return saved_object_graph_field_tags;
315
+ }
316
+
317
+ absl::StatusOr<SavedModel> PrunedSavedModel(
318
+ absl::string_view export_dir,
319
+ riegeli::RecordReader<riegeli::FdReader<>>& reader,
320
+ const std::vector<ChunkInfo>& chunks_info, ChunkMetadata& chunk_metadata) {
321
+ SavedModel saved_model;
322
+ ChunkMetadata pruned_chunk_metadata;
323
+ pruned_chunk_metadata.mutable_chunks()->CopyFrom(chunk_metadata.chunks());
324
+ TF_ASSIGN_OR_RETURN(
325
+ *pruned_chunk_metadata.mutable_message(),
326
+ PruneChunkedMessage(chunk_metadata.message(), reader, chunks_info,
327
+ {GraphDefFieldTags(), SignatureDefFieldTags(),
328
+ SavedObjectGraphFieldTags()}));
329
+ // Read into saved_model.
330
+ TF_RETURN_IF_ERROR(
331
+ Merger::ReadPartial(io::JoinPath(export_dir, kSavedModelFilenamePrefix),
332
+ pruned_chunk_metadata, &saved_model));
333
+ return saved_model;
334
+ }
335
+
336
+ absl::StatusOr<uint64_t> HashMessage(
337
+ Message* message, const ChunkedMessage& chunked_message,
338
+ riegeli::RecordReader<riegeli::FdReader<>>& reader,
339
+ const std::vector<ChunkInfo>& chunks_info,
340
+ const RepeatedPtrField<FieldIndex>& field_tags) {
341
+ uint64_t total_message_hash = Fingerprint64(SerializeProto(*message));
342
+ TF_ASSIGN_OR_RETURN(
343
+ uint64_t message_hash,
344
+ HashFields(chunked_message, reader, chunks_info, field_tags, message));
345
+ return FingerprintCat64(total_message_hash, message_hash);
346
+ }
347
+
348
+ absl::StatusOr<uint64_t> HashGraphDef(
349
+ ::tensorflow::GraphDef* graph_def, const ChunkedMessage& chunked_message,
350
+ riegeli::RecordReader<riegeli::FdReader<>>& reader,
351
+ const std::vector<ChunkInfo>& chunks_info) {
352
+ // TODO(adamcogdell): here we assume that graph_def (top-level) is contained
353
+ // in a single chunk, which may not be the case
354
+ return HashMessage(graph_def, chunked_message, reader, chunks_info,
355
+ GraphDefFieldTags());
356
+ }
357
+
358
+ absl::StatusOr<uint64_t> HashSignatureDef(
359
+ const Map<std::string, ::tensorflow::SignatureDef>& signature_def_map,
360
+ const ChunkedMessage& chunked_message,
361
+ riegeli::RecordReader<riegeli::FdReader<>>& reader,
362
+ const std::vector<ChunkInfo>& chunks_info) {
363
+ uint64_t signature_def_hash = 0;
364
+ std::vector<std::pair<std::string, ::tensorflow::SignatureDef>>
365
+ signature_def_sorted(signature_def_map.begin(), signature_def_map.end());
366
+ std::sort(signature_def_sorted.begin(), signature_def_sorted.end(),
367
+ [](const std::pair<std::string, ::tensorflow::SignatureDef>& a,
368
+ const std::pair<std::string, ::tensorflow::SignatureDef>& b) {
369
+ return a.first < b.first;
370
+ });
371
+ for (const auto& signature_def : signature_def_sorted) {
372
+ uint64_t signature_def_pair_hash =
373
+ FingerprintCat64(Fingerprint64(signature_def.first),
374
+ Fingerprint64(SerializeProto(signature_def.second)));
375
+ signature_def_hash =
376
+ FingerprintCat64(signature_def_hash, signature_def_pair_hash);
377
+ SignatureDef signature_def_val = signature_def.second;
378
+ TF_ASSIGN_OR_RETURN(
379
+ uint64_t signature_def_entry_hash,
380
+ HashFields(chunked_message, reader, chunks_info,
381
+ SignatureDefFieldTags(), &signature_def_val));
382
+ signature_def_hash =
383
+ FingerprintCat64(signature_def_hash, signature_def_entry_hash);
384
+ }
385
+ return signature_def_hash;
386
+ }
387
+
388
+ absl::StatusOr<uint64_t> HashSavedObjectGraph(
389
+ ::tensorflow::SavedObjectGraph* saved_object_graph,
390
+ const ChunkedMessage& chunked_message,
391
+ riegeli::RecordReader<riegeli::FdReader<>>& reader,
392
+ const std::vector<ChunkInfo>& chunks_info) {
393
+ return HashMessage(saved_object_graph, chunked_message, reader, chunks_info,
394
+ SavedObjectGraphFieldTags());
395
+ }
396
+
397
+ } // namespace fingerprinting_utils_internal
398
+
399
+ using fingerprinting_utils_internal::HashFields;
400
+ using fingerprinting_utils_internal::HashGraphDef;
401
+ using fingerprinting_utils_internal::HashSavedObjectGraph;
402
+ using fingerprinting_utils_internal::HashSignatureDef;
403
+ using fingerprinting_utils_internal::PrunedSavedModel;
404
+ using fingerprinting_utils_internal::SerializeProto;
405
+
406
+ uint64_t HashCheckpointIndexFile(absl::string_view model_dir) {
407
+ std::string meta_filename = MetaFilename(io::JoinPath(
408
+ model_dir, kSavedModelVariablesDirectory, kSavedModelVariablesFilename));
409
+ std::string data;
410
+ absl::Status read_status =
411
+ ReadFileToString(Env::Default(), meta_filename, &data);
412
+ if (read_status.ok()) {
413
+ return tensorflow::Fingerprint64(data);
414
+ } else {
415
+ return 0;
416
+ }
417
+ }
418
+
419
+ absl::StatusOr<FingerprintDef> CreateFingerprintDefCpb(
420
+ absl::string_view export_dir, std::string cpb_file) {
421
+ // Version of the code that produced the fingerprint.
422
+ const int kFingerprintProducer = 2;
423
+
424
+ TF_ASSIGN_OR_RETURN(auto reader, GetRiegeliReader(cpb_file));
425
+
426
+ auto read_metadata = GetChunkMetadata(reader);
427
+ if (!read_metadata.ok()) {
428
+ reader.Close();
429
+ return absl::FailedPreconditionError(
430
+ absl::StrCat("Couldn't read ChunkMetadata from chunked proto.\n",
431
+ read_metadata.status().ToString()));
432
+ }
433
+ ChunkMetadata chunk_metadata = read_metadata.value();
434
+
435
+ std::vector<ChunkInfo> chunks_info = std::vector<ChunkInfo>(
436
+ chunk_metadata.chunks().begin(), chunk_metadata.chunks().end());
437
+
438
+ FingerprintDef fingerprint_def;
439
+ SavedModel saved_model;
440
+
441
+ // Set the saved_model_checksum.
442
+ TF_ASSIGN_OR_RETURN(uint64_t saved_model_hash,
443
+ HashFields(chunk_metadata.message(), reader, chunks_info,
444
+ {}, &saved_model));
445
+ saved_model_hash = FingerprintCat64(
446
+ saved_model_hash, Fingerprint64(SerializeProto(saved_model)));
447
+ fingerprint_def.set_saved_model_checksum(saved_model_hash);
448
+
449
+ // Fill saved_model with only relevant chunk(s).
450
+ TF_ASSIGN_OR_RETURN(
451
+ saved_model,
452
+ PrunedSavedModel(export_dir, reader, chunks_info, chunk_metadata));
453
+
454
+ TF_ASSIGN_OR_RETURN(
455
+ uint64_t graph_def_program_hash,
456
+ HashGraphDef(saved_model.mutable_meta_graphs(0)->mutable_graph_def(),
457
+ chunk_metadata.message(), reader, chunks_info));
458
+ fingerprint_def.set_graph_def_program_hash(graph_def_program_hash);
459
+
460
+ // TODO(adamcogdell): HashSignatureDef relies on the signatue_def map being
461
+ // populated with all of its entries, which may not be the case
462
+ TF_ASSIGN_OR_RETURN(
463
+ uint64_t signature_def_hash,
464
+ HashSignatureDef(saved_model.meta_graphs(0).signature_def(),
465
+ chunk_metadata.message(), reader, chunks_info));
466
+ fingerprint_def.set_signature_def_hash(signature_def_hash);
467
+
468
+ TF_ASSIGN_OR_RETURN(
469
+ uint64_t saved_object_graph_hash,
470
+ HashSavedObjectGraph(
471
+ saved_model.mutable_meta_graphs(0)->mutable_object_graph_def(),
472
+ chunk_metadata.message(), reader, chunks_info));
473
+ fingerprint_def.set_saved_object_graph_hash(saved_object_graph_hash);
474
+
475
+ fingerprint_def.set_checkpoint_hash(HashCheckpointIndexFile(export_dir));
476
+
477
+ // Assign a random UUID to the fingerprint.
478
+ fingerprint_def.set_uuid(fingerprinting::CreateRandomUUID());
479
+ reader.Close();
480
+
481
+ // Set version of the fingerprint.
482
+ VersionDef* version = fingerprint_def.mutable_version();
483
+ version->set_producer(kFingerprintProducer);
484
+
485
+ return fingerprint_def;
486
+ }
487
+
488
+ } // namespace tensorflow::saved_model::fingerprinting
western_ai/usa/tensorflow/tensorflow/cc/saved_model/fingerprinting_utils.h ADDED
@@ -0,0 +1,137 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /* Copyright 2023 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_UTILS_H_
17
+ #define TENSORFLOW_CC_SAVED_MODEL_FINGERPRINTING_UTILS_H_
18
+
19
+ #include <cstdint>
20
+ #include <string>
21
+ #include <vector>
22
+
23
+ #include "absl/status/statusor.h"
24
+ #include "absl/strings/string_view.h"
25
+ #include "riegeli/bytes/fd_reader.h" // from @riegeli
26
+ #include "riegeli/records/record_reader.h" // from @riegeli
27
+ #include "tensorflow/core/framework/graph.pb.h"
28
+ #include "tensorflow/core/platform/protobuf.h" // IWYU pragma: keep
29
+ #include "tensorflow/core/protobuf/fingerprint.pb.h"
30
+ #include "tensorflow/core/protobuf/meta_graph.pb.h"
31
+ #include "tensorflow/core/protobuf/saved_model.pb.h"
32
+ #include "tensorflow/core/protobuf/saved_object_graph.pb.h"
33
+ #include "tensorflow/tools/proto_splitter/chunk.pb.h"
34
+
35
+ namespace tensorflow::saved_model::fingerprinting {
36
+
37
+ namespace fingerprinting_utils_internal {
38
+
39
+ using ::tensorflow::protobuf::Map;
40
+ using ::tensorflow::protobuf::Message;
41
+ using ::tensorflow::protobuf::RepeatedPtrField;
42
+
43
+ // Number of sequential FieldIndex matches of `a` in `b`. (Length of initial
44
+ // subsequence.)
45
+ // Example: `a = {4, 2}`, `b = {4, 2, 1, 3}`, `fieldTagMatches(a, b) == 2`
46
+ absl::StatusOr<int> fieldTagMatches(
47
+ const RepeatedPtrField<::tensorflow::proto_splitter::FieldIndex>& a,
48
+ const RepeatedPtrField<::tensorflow::proto_splitter::FieldIndex>& b);
49
+
50
+ // Pull out the relevant data within `chunked_message`. A `chunked_field` is
51
+ // relevant if its `field_tags` are an initial subsequence any of the
52
+ // `target_fields` in the provided `target_fields_list`.
53
+ absl::StatusOr<::tensorflow::proto_splitter::ChunkedMessage>
54
+ PruneChunkedMessage(
55
+ const ::tensorflow::proto_splitter::ChunkedMessage& chunked_message,
56
+ riegeli::RecordReader<riegeli::FdReader<>>& reader,
57
+ std::vector<::tensorflow::proto_splitter::ChunkInfo> chunks_info,
58
+ std::vector<RepeatedPtrField<::tensorflow::proto_splitter::FieldIndex>>
59
+ target_fields_list);
60
+
61
+ // Deterministically serializes the proto `message`.
62
+ std::string SerializeProto(const Message& message);
63
+
64
+ // Uses metadata contained in `chunked_message` to hash fields within the
65
+ // data accessed by the `reader` using `chunks_info`.
66
+ absl::StatusOr<uint64_t> HashFields(
67
+ const ::tensorflow::proto_splitter::ChunkedMessage& chunked_message,
68
+ riegeli::RecordReader<riegeli::FdReader<>>& reader,
69
+ const std::vector<::tensorflow::proto_splitter::ChunkInfo>& chunks_info,
70
+ const RepeatedPtrField<::tensorflow::proto_splitter::FieldIndex>&
71
+ field_tags,
72
+ Message* merged_message);
73
+
74
+ // Gets the field tags for `graph_def`.::tensorflow
75
+ inline RepeatedPtrField<::tensorflow::proto_splitter::FieldIndex>
76
+ GraphDefFieldTags();
77
+
78
+ // Gets the field tags for `signature_def`.
79
+ inline RepeatedPtrField<::tensorflow::proto_splitter::FieldIndex>
80
+ SignatureDefFieldTags();
81
+
82
+ // Gets the field tags for `saved_object_graph`.
83
+ inline RepeatedPtrField<::tensorflow::proto_splitter::FieldIndex>
84
+ SavedObjectGraphFieldTags();
85
+
86
+ // Returns a `SavedModel` containing only fields (up to those) specified by
87
+ // `GraphDefFieldTags()`, `SignatureDefFieldTags()`, and
88
+ // `SavedObjectGraphFieldTags()`.
89
+ absl::StatusOr<tensorflow::SavedModel> PrunedSavedModel(
90
+ absl::string_view export_dir,
91
+ riegeli::RecordReader<riegeli::FdReader<>>& reader,
92
+ const std::vector<::tensorflow::proto_splitter::ChunkInfo>& chunks_info,
93
+ ::tensorflow::proto_splitter::ChunkMetadata& chunk_metadata);
94
+
95
+ // Hashes the contents of `message` specified by `field_tags`.
96
+ absl::StatusOr<uint64_t> HashMessage(
97
+ Message* message,
98
+ const ::tensorflow::proto_splitter::ChunkedMessage& chunked_message,
99
+ riegeli::RecordReader<riegeli::FdReader<>>& reader,
100
+ const std::vector<::tensorflow::proto_splitter::ChunkInfo>& chunks_info,
101
+ const RepeatedPtrField<::tensorflow::proto_splitter::FieldIndex>&
102
+ field_tags);
103
+
104
+ // Hashes the contents of `graph_def`.
105
+ absl::StatusOr<uint64_t> HashGraphDef(
106
+ tensorflow::GraphDef* graph_def,
107
+ const ::tensorflow::proto_splitter::ChunkedMessage& chunked_message,
108
+ riegeli::RecordReader<riegeli::FdReader<>>& reader,
109
+ const std::vector<::tensorflow::proto_splitter::ChunkInfo>& chunks_info);
110
+
111
+ // Hashes the contents of `signature_def`.
112
+ absl::StatusOr<uint64_t> HashSignatureDef(
113
+ const Map<std::string, ::tensorflow::SignatureDef>& signature_def_map,
114
+ const ::tensorflow::proto_splitter::ChunkedMessage& chunked_message,
115
+ riegeli::RecordReader<riegeli::FdReader<>>& reader,
116
+ const std::vector<::tensorflow::proto_splitter::ChunkInfo>& chunks_info);
117
+
118
+ // Hashes the contents of `saved_object_graph`.
119
+ absl::StatusOr<uint64_t> HashSavedObjectGraph(
120
+ tensorflow::SavedObjectGraph* saved_object_graph,
121
+ const ::tensorflow::proto_splitter::ChunkedMessage& chunked_message,
122
+ riegeli::RecordReader<riegeli::FdReader<>>& reader,
123
+ const std::vector<::tensorflow::proto_splitter::ChunkInfo>& chunks_info);
124
+
125
+ } // namespace fingerprinting_utils_internal
126
+
127
+ // Returns the hash of the checkpoint .index file, 0 if there is none.
128
+ uint64_t HashCheckpointIndexFile(absl::string_view model_dir);
129
+
130
+ // Creates a FingerprintDef proto from a chunked SavedModel and the checkpoint
131
+ // meta file (.index) in `export_dir`.
132
+ absl::StatusOr<FingerprintDef> CreateFingerprintDefCpb(
133
+ absl::string_view export_dir, std::string cpb_file);
134
+
135
+ } // namespace tensorflow::saved_model::fingerprinting
136
+
137
+ #endif // TENSORFLOW_CC_SAVED_MODEL_FINGERPRINTING_UTILS_H_
western_ai/usa/tensorflow/tensorflow/cc/saved_model/fingerprinting_utils_test.cc ADDED
@@ -0,0 +1,396 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /* Copyright 2023 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/cc/saved_model/fingerprinting_utils.h"
17
+
18
+ #include <cstdint>
19
+ #include <string>
20
+ #include <vector>
21
+
22
+ #include <gmock/gmock.h>
23
+ #include <gtest/gtest.h>
24
+ #include "absl/status/status.h"
25
+ #include "absl/status/statusor.h"
26
+ #include "absl/strings/str_cat.h"
27
+ #include "absl/strings/string_view.h"
28
+ #include "xla/tsl/lib/core/status_test_util.h"
29
+ #include "tensorflow/core/framework/graph.pb.h"
30
+ #include "tensorflow/core/platform/path.h"
31
+ #include "tensorflow/core/protobuf/meta_graph.pb.h"
32
+ #include "tensorflow/core/protobuf/saved_object_graph.pb.h"
33
+ #include "tensorflow/tools/proto_splitter/cc/util.h"
34
+ #include "tensorflow/tools/proto_splitter/chunk.pb.h"
35
+ #include "tensorflow/tools/proto_splitter/testdata/test_message.pb.h"
36
+ #include "tsl/platform/errors.h"
37
+ #include "tsl/platform/status_matchers.h"
38
+ #include "tsl/platform/statusor.h"
39
+ #include "tsl/platform/test.h"
40
+ // IWYU pragma: no_include "third_party/protobuf/io/zero_copy_stream_impl_lite.h"
41
+ // IWYU pragma: no_include "third_party/protobuf/util/message_differencer.h"
42
+
43
+ namespace tensorflow::saved_model::fingerprinting {
44
+
45
+ namespace {
46
+
47
+ using fingerprinting_utils_internal::fieldTagMatches;
48
+ using fingerprinting_utils_internal::HashFields;
49
+ using fingerprinting_utils_internal::HashGraphDef;
50
+ using fingerprinting_utils_internal::HashSavedObjectGraph;
51
+ using fingerprinting_utils_internal::HashSignatureDef;
52
+ using fingerprinting_utils_internal::PruneChunkedMessage;
53
+ using fingerprinting_utils_internal::SerializeProto;
54
+ using ::tensorflow::proto_splitter::ChunkedField;
55
+ using ::tensorflow::proto_splitter::ChunkedMessage;
56
+ using ::tensorflow::proto_splitter::ChunkInfo;
57
+ using ::tensorflow::proto_splitter::ChunkMetadata;
58
+ using ::tensorflow::proto_splitter::FieldIndex;
59
+ using ::tensorflow::proto_splitter_testdata::ManyFields;
60
+ using ::tensorflow::protobuf::Message;
61
+ using ::tensorflow::protobuf::RepeatedPtrField;
62
+ using ::tensorflow::protobuf::TextFormat;
63
+ // NOLINTNEXTLINE: clang-tidy missing-includes false positive
64
+ using ::tensorflow::protobuf::io::ArrayInputStream;
65
+ // NOLINTNEXTLINE: clang-tidy missing-includes false positive
66
+ using ::tensorflow::protobuf::util::MessageDifferencer;
67
+ using tools::proto_splitter::GetChunkMetadata;
68
+ using tools::proto_splitter::GetRiegeliReader;
69
+ using tsl::testing::IsOkAndHolds;
70
+ using tsl::testing::TensorFlowSrcRoot;
71
+
72
+ absl::Status ParseTextProto(absl::string_view text_proto,
73
+ Message* parsed_proto) {
74
+ TextFormat::Parser parser;
75
+ // Attempt to parse as text.
76
+ ArrayInputStream input_stream(text_proto.data(), text_proto.size());
77
+ if (parser.Parse(&input_stream, parsed_proto)) {
78
+ return absl::OkStatus();
79
+ }
80
+ parsed_proto->Clear();
81
+ return absl::InvalidArgumentError(
82
+ absl::StrCat("Could not parse text proto: ", text_proto));
83
+ }
84
+
85
+ absl::StatusOr<RepeatedPtrField<::tensorflow::proto_splitter::FieldIndex>>
86
+ ExtractFieldTags(absl::string_view chunked_field_text_proto) {
87
+ ChunkedField chunked_field;
88
+ TF_RETURN_IF_ERROR(ParseTextProto(chunked_field_text_proto, &chunked_field));
89
+ return chunked_field.field_tag();
90
+ }
91
+
92
+ TEST(FingerprintingTest, TestFieldTagMatchesInitialSubsequence) {
93
+ TF_ASSERT_OK_AND_ASSIGN(RepeatedPtrField<FieldIndex> field_tags,
94
+ ExtractFieldTags(R"pb(
95
+ field_tag { field: 2 }
96
+ field_tag { index: 1505 }
97
+ field_tag { field: 5 }
98
+ field_tag { map_key { ui32: 123 } }
99
+ )pb"));
100
+ RepeatedPtrField<FieldIndex> field_tags_sub;
101
+ field_tags_sub.CopyFrom(field_tags);
102
+ field_tags_sub.DeleteSubrange(2, 2);
103
+
104
+ EXPECT_THAT(fieldTagMatches(field_tags_sub, field_tags),
105
+ absl_testing::IsOkAndHolds(2));
106
+ }
107
+
108
+ TEST(FingerprintingTest, TestFieldTagMatchesNoninitialSubsequence) {
109
+ TF_ASSERT_OK_AND_ASSIGN(RepeatedPtrField<FieldIndex> field_tags,
110
+ ExtractFieldTags(R"pb(
111
+ field_tag { field: 2 }
112
+ field_tag { index: 1505 }
113
+ field_tag { field: 5 }
114
+ field_tag { map_key { ui32: 123 } }
115
+ )pb"));
116
+ RepeatedPtrField<FieldIndex> field_tags_sub;
117
+ field_tags_sub.CopyFrom(field_tags);
118
+ field_tags_sub.DeleteSubrange(0, 2);
119
+
120
+ EXPECT_THAT(fieldTagMatches(field_tags_sub, field_tags),
121
+ absl_testing::IsOkAndHolds(0));
122
+ }
123
+
124
+ TEST(FingerprintingTest, TestFieldTagMatchesIdenticalSubsequence) {
125
+ TF_ASSERT_OK_AND_ASSIGN(RepeatedPtrField<FieldIndex> field_tags,
126
+ ExtractFieldTags(R"pb(
127
+ field_tag { field: 2 }
128
+ field_tag { index: 1505 }
129
+ field_tag { field: 5 }
130
+ field_tag { map_key { ui32: 123 } }
131
+ )pb"));
132
+ RepeatedPtrField<FieldIndex> field_tags_sub;
133
+ field_tags_sub.CopyFrom(field_tags);
134
+
135
+ EXPECT_THAT(fieldTagMatches(field_tags_sub, field_tags),
136
+ absl_testing::IsOkAndHolds(4));
137
+ }
138
+
139
+ TEST(FingerprintingTest, TestFieldTagMatchesSuperSubsequence) {
140
+ TF_ASSERT_OK_AND_ASSIGN(RepeatedPtrField<FieldIndex> field_tags,
141
+ ExtractFieldTags(R"pb(
142
+ field_tag { field: 2 }
143
+ field_tag { index: 1505 }
144
+ field_tag { field: 5 }
145
+ field_tag { map_key { ui32: 123 } }
146
+ )pb"));
147
+ RepeatedPtrField<FieldIndex> field_tags_sub;
148
+ field_tags_sub.CopyFrom(field_tags);
149
+ field_tags_sub.Add()->set_field(6);
150
+
151
+ EXPECT_THAT(fieldTagMatches(field_tags_sub, field_tags),
152
+ absl_testing::IsOkAndHolds(4));
153
+ }
154
+
155
+ TEST(FingerprintingTest, TestPruneChunkedMessageSingleTarget) {
156
+ std::string cpb_file = io::JoinPath(
157
+ TensorFlowSrcRoot(), "tools/proto_splitter/testdata", "many-field.cpb");
158
+ TF_ASSERT_OK_AND_ASSIGN(auto reader, GetRiegeliReader(cpb_file));
159
+
160
+ auto read_metadata = GetChunkMetadata(reader);
161
+ if (!read_metadata.ok()) {
162
+ reader.Close();
163
+ TF_ASSERT_OK(read_metadata.status());
164
+ }
165
+ ChunkMetadata chunk_metadata = read_metadata.value();
166
+
167
+ std::vector<ChunkInfo> chunks_info = std::vector<ChunkInfo>(
168
+ chunk_metadata.chunks().begin(), chunk_metadata.chunks().end());
169
+
170
+ FieldIndex field_one_field_tag;
171
+ field_one_field_tag.set_field(1);
172
+ FieldIndex repeated_field_field_tag;
173
+ repeated_field_field_tag.set_field(2);
174
+ FieldIndex repeated_field_index_field_tag;
175
+ repeated_field_index_field_tag.set_index(1);
176
+ RepeatedPtrField<FieldIndex> target_field_tags;
177
+ target_field_tags.Add(FieldIndex(field_one_field_tag));
178
+ target_field_tags.Add(FieldIndex(repeated_field_field_tag));
179
+ target_field_tags.Add(FieldIndex(repeated_field_index_field_tag));
180
+
181
+ ChunkedMessage pruned_chunked_message;
182
+ TF_ASSERT_OK_AND_ASSIGN(
183
+ pruned_chunked_message,
184
+ PruneChunkedMessage(chunk_metadata.message(), reader, chunks_info,
185
+ {target_field_tags}));
186
+
187
+ std::string expected_pruned_chunked_message_text_proto = R"pb(
188
+ chunk_index: 0
189
+ chunked_fields {
190
+ field_tag { field: 1 }
191
+ message { chunk_index: 1 }
192
+ }
193
+ )pb";
194
+ ChunkedMessage expected_pruned_chunked_message;
195
+ TF_ASSERT_OK(ParseTextProto(expected_pruned_chunked_message_text_proto,
196
+ &expected_pruned_chunked_message));
197
+ ASSERT_TRUE(MessageDifferencer::Equals(pruned_chunked_message,
198
+ expected_pruned_chunked_message));
199
+ }
200
+
201
+ TEST(FingerprintingTest, TestPruneChunkedMessageMultiTarget) {
202
+ std::string cpb_file = io::JoinPath(
203
+ TensorFlowSrcRoot(), "tools/proto_splitter/testdata", "many-field.cpb");
204
+ TF_ASSERT_OK_AND_ASSIGN(auto reader, GetRiegeliReader(cpb_file));
205
+
206
+ auto read_metadata = GetChunkMetadata(reader);
207
+ if (!read_metadata.ok()) {
208
+ reader.Close();
209
+ TF_ASSERT_OK(read_metadata.status());
210
+ }
211
+ ChunkMetadata chunk_metadata = read_metadata.value();
212
+
213
+ std::vector<ChunkInfo> chunks_info = std::vector<ChunkInfo>(
214
+ chunk_metadata.chunks().begin(), chunk_metadata.chunks().end());
215
+
216
+ // ManyFields.field_one.repeated_field[1]
217
+ FieldIndex field_one_field_tag;
218
+ field_one_field_tag.set_field(1);
219
+ FieldIndex repeated_field_field_tag;
220
+ repeated_field_field_tag.set_field(2);
221
+ FieldIndex repeated_field_index_field_tag;
222
+ repeated_field_index_field_tag.set_index(1);
223
+ RepeatedPtrField<FieldIndex> target_one_field_tags;
224
+ target_one_field_tags.Add(FieldIndex(field_one_field_tag));
225
+ target_one_field_tags.Add(FieldIndex(repeated_field_field_tag));
226
+ target_one_field_tags.Add(FieldIndex(repeated_field_index_field_tag));
227
+
228
+ // ManyFields.nested_map_bool[true].string_field
229
+ FieldIndex nested_map_bool_field_tag;
230
+ nested_map_bool_field_tag.set_field(7);
231
+ FieldIndex nested_map_bool_mapkey_field_tag;
232
+ nested_map_bool_mapkey_field_tag.mutable_map_key()->set_boolean(true);
233
+ FieldIndex string_field_field_tag;
234
+ string_field_field_tag.set_field(3);
235
+ RepeatedPtrField<FieldIndex> target_two_field_tags;
236
+ target_two_field_tags.Add(FieldIndex(nested_map_bool_field_tag));
237
+ target_two_field_tags.Add(FieldIndex(nested_map_bool_mapkey_field_tag));
238
+ target_two_field_tags.Add(FieldIndex(string_field_field_tag));
239
+
240
+ ChunkedMessage pruned_chunked_message;
241
+ TF_ASSERT_OK_AND_ASSIGN(
242
+ pruned_chunked_message,
243
+ PruneChunkedMessage(chunk_metadata.message(), reader, chunks_info,
244
+ {target_one_field_tags, target_two_field_tags}));
245
+
246
+ std::string expected_pruned_chunked_message_text_proto = R"pb(
247
+ chunk_index: 0
248
+ chunked_fields {
249
+ field_tag { field: 1 }
250
+ message { chunk_index: 1 }
251
+ }
252
+ chunked_fields {
253
+ field_tag { field: 7 }
254
+ field_tag { map_key { boolean: true } }
255
+ message { chunk_index: 2 }
256
+ }
257
+ )pb";
258
+ ChunkedMessage expected_pruned_chunked_message;
259
+ TF_ASSERT_OK(ParseTextProto(expected_pruned_chunked_message_text_proto,
260
+ &expected_pruned_chunked_message));
261
+ ASSERT_TRUE(MessageDifferencer::Equals(pruned_chunked_message,
262
+ expected_pruned_chunked_message));
263
+ }
264
+
265
+ TEST(FingerprintingTest, TestPruneChunkedMessageNoTarget) {
266
+ std::string cpb_file = io::JoinPath(
267
+ TensorFlowSrcRoot(), "tools/proto_splitter/testdata", "many-field.cpb");
268
+ TF_ASSERT_OK_AND_ASSIGN(auto reader, GetRiegeliReader(cpb_file));
269
+
270
+ auto read_metadata = GetChunkMetadata(reader);
271
+ if (!read_metadata.ok()) {
272
+ reader.Close();
273
+ TF_ASSERT_OK(read_metadata.status());
274
+ }
275
+ ChunkMetadata chunk_metadata = read_metadata.value();
276
+
277
+ std::vector<ChunkInfo> chunks_info = std::vector<ChunkInfo>(
278
+ chunk_metadata.chunks().begin(), chunk_metadata.chunks().end());
279
+
280
+ ChunkedMessage pruned_chunked_message;
281
+ TF_ASSERT_OK_AND_ASSIGN(
282
+ pruned_chunked_message,
283
+ PruneChunkedMessage(chunk_metadata.message(), reader, chunks_info, {}));
284
+
285
+ std::string expected_pruned_chunked_message_text_proto = R"pb(
286
+ chunk_index: 0
287
+ )pb";
288
+ ChunkedMessage expected_pruned_chunked_message;
289
+ TF_ASSERT_OK(ParseTextProto(expected_pruned_chunked_message_text_proto,
290
+ &expected_pruned_chunked_message));
291
+ ASSERT_TRUE(MessageDifferencer::Equals(pruned_chunked_message,
292
+ expected_pruned_chunked_message));
293
+ }
294
+
295
+ TEST(FingerprintingTest, TestSerializeProto) {
296
+ std::string many_fields_text_proto = R"pb(
297
+ string_field: "abc123"
298
+ )pb";
299
+ ManyFields many_fields;
300
+ TF_ASSERT_OK(ParseTextProto(many_fields_text_proto, &many_fields));
301
+ ASSERT_EQ(SerializeProto(many_fields), many_fields.SerializeAsString());
302
+ }
303
+
304
+ TEST(FingerprintingTest, TestHashFieldsV2) {
305
+ std::string cpb_file = io::JoinPath(
306
+ TensorFlowSrcRoot(), "tools/proto_splitter/testdata", "many-field.cpb");
307
+ TF_ASSERT_OK_AND_ASSIGN(auto reader, GetRiegeliReader(cpb_file));
308
+
309
+ auto read_metadata = GetChunkMetadata(reader);
310
+ if (!read_metadata.ok()) {
311
+ reader.Close();
312
+ TF_ASSERT_OK(read_metadata.status());
313
+ }
314
+ ChunkMetadata chunk_metadata = read_metadata.value();
315
+
316
+ std::vector<ChunkInfo> chunks_info = std::vector<ChunkInfo>(
317
+ chunk_metadata.chunks().begin(), chunk_metadata.chunks().end());
318
+
319
+ ManyFields many_fields;
320
+ TF_ASSERT_OK_AND_ASSIGN(uint64_t many_fields_hash,
321
+ HashFields(chunk_metadata.message(), reader,
322
+ chunks_info, {}, &many_fields));
323
+ ASSERT_EQ(many_fields_hash, 14850154939410192811U);
324
+ }
325
+
326
+ TEST(FingerprintingTest, TestHashGraphDef) {
327
+ std::string cpb_file =
328
+ io::JoinPath(TensorFlowSrcRoot(), "tools/proto_splitter/testdata",
329
+ "split-standard.cpb");
330
+ TF_ASSERT_OK_AND_ASSIGN(auto reader, GetRiegeliReader(cpb_file));
331
+
332
+ auto read_metadata = GetChunkMetadata(reader);
333
+ if (!read_metadata.ok()) {
334
+ reader.Close();
335
+ TF_ASSERT_OK(read_metadata.status());
336
+ }
337
+ ChunkMetadata chunk_metadata = read_metadata.value();
338
+
339
+ std::vector<ChunkInfo> chunks_info = std::vector<ChunkInfo>(
340
+ chunk_metadata.chunks().begin(), chunk_metadata.chunks().end());
341
+
342
+ GraphDef graph_def;
343
+ EXPECT_THAT(
344
+ HashGraphDef(&graph_def, chunk_metadata.message(), reader, chunks_info),
345
+ absl_testing::IsOkAndHolds(16782272393894422524U));
346
+ }
347
+
348
+ TEST(FingerprintingTest, TestHashSignatureDef) {
349
+ std::string cpb_file =
350
+ io::JoinPath(TensorFlowSrcRoot(), "tools/proto_splitter/testdata",
351
+ "split-standard.cpb");
352
+ TF_ASSERT_OK_AND_ASSIGN(auto reader, GetRiegeliReader(cpb_file));
353
+
354
+ auto read_metadata = GetChunkMetadata(reader);
355
+ if (!read_metadata.ok()) {
356
+ reader.Close();
357
+ TF_ASSERT_OK(read_metadata.status());
358
+ }
359
+ ChunkMetadata chunk_metadata = read_metadata.value();
360
+
361
+ std::vector<ChunkInfo> chunks_info = std::vector<ChunkInfo>(
362
+ chunk_metadata.chunks().begin(), chunk_metadata.chunks().end());
363
+
364
+ ::tensorflow::protobuf::Map<std::string, SignatureDef> signature_def_map;
365
+ SignatureDef signature_def;
366
+ EXPECT_THAT(HashSignatureDef(signature_def_map, chunk_metadata.message(),
367
+ reader, chunks_info),
368
+ absl_testing::IsOkAndHolds(0));
369
+ }
370
+
371
+ TEST(FingerprintingTest, TestHashSavedObjectGraph) {
372
+ std::string cpb_file =
373
+ io::JoinPath(TensorFlowSrcRoot(), "tools/proto_splitter/testdata",
374
+ "split-standard.cpb");
375
+ TF_ASSERT_OK_AND_ASSIGN(auto reader, GetRiegeliReader(cpb_file));
376
+
377
+ auto read_metadata = GetChunkMetadata(reader);
378
+ if (!read_metadata.ok()) {
379
+ reader.Close();
380
+ TF_ASSERT_OK(read_metadata.status());
381
+ }
382
+ ChunkMetadata chunk_metadata = read_metadata.value();
383
+
384
+ std::vector<ChunkInfo> chunks_info = std::vector<ChunkInfo>(
385
+ chunk_metadata.chunks().begin(), chunk_metadata.chunks().end());
386
+
387
+ SavedObjectGraph saved_object_graph;
388
+ EXPECT_THAT(
389
+ HashSavedObjectGraph(&saved_object_graph, chunk_metadata.message(),
390
+ reader, chunks_info),
391
+ absl_testing::IsOkAndHolds(17454850744699451884U));
392
+ }
393
+
394
+ } // namespace
395
+
396
+ } // namespace tensorflow::saved_model::fingerprinting
western_ai/usa/tensorflow/tensorflow/cc/saved_model/fingerprinting_x_platform_utils.cc ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /* Copyright 2023 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/cc/saved_model/fingerprinting_x_platform_utils.h"
17
+
18
+ #include <string>
19
+
20
+ #include "absl/numeric/int128.h"
21
+ #include "absl/strings/str_format.h"
22
+ #include "tsl/platform/random.h"
23
+
24
+ // UINT64MAX is 18'446'744'073'709'551'615 (20 digits)
25
+ // UINT128MAX is 340'282'366'920'938'463'463'374'607'431'768'211'455 (39 dgts)
26
+ // After sqrt(INT64MAX) = 4'294'967'296 (4B models), it's 50% likely to be
27
+ // duplicates in the ID space. In comparison, sqrt(UINT128MAX) = UINT64MAX,
28
+ // meaning that we can continue generating unique IDs for a lot longer time
29
+ // if the UUID is generated from two random UINT64s. This can be replaced by
30
+ // random::New128() if that becomes available.
31
+ std::string tensorflow::saved_model::fingerprinting::CreateRandomUUID() {
32
+ absl::uint128 uuid_1 = tsl::random::New64();
33
+ absl::uint128 uuid_2 = tsl::random::New64();
34
+ absl::uint128 uuid_complete = (uuid_1 << 64) | uuid_2;
35
+ return absl::StrFormat("%020d", uuid_complete);
36
+ }
western_ai/usa/tensorflow/tensorflow/cc/saved_model/fingerprinting_x_platform_utils.h ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /* Copyright 2023 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_X_PLATFORM_UTILS_H_
17
+ #define TENSORFLOW_CC_SAVED_MODEL_FINGERPRINTING_X_PLATFORM_UTILS_H_
18
+
19
+ #include <string>
20
+
21
+ namespace tensorflow::saved_model::fingerprinting {
22
+
23
+ // Returns a random UUID (128 bits random) as a string.
24
+ std::string CreateRandomUUID();
25
+
26
+ } // namespace tensorflow::saved_model::fingerprinting
27
+
28
+ #endif // TENSORFLOW_CC_SAVED_MODEL_FINGERPRINTING_X_PLATFORM_UTILS_H_
western_ai/usa/tensorflow/tensorflow/cc/saved_model/image_format/BUILD ADDED
@@ -0,0 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # On-disk serialization format for TensorFlow models.
2
+
3
+ load(
4
+ "//tensorflow:tensorflow.bzl",
5
+ "if_not_windows_or_mac",
6
+ )
7
+ load("//tensorflow/core/platform:rules_cc.bzl", "cc_library")
8
+
9
+ package(
10
+ # copybara:uncomment default_applicable_licenses = ["//tensorflow:license"],
11
+ default_visibility = [
12
+ "//tensorflow/cc/experimental/tfa:__subpackages__",
13
+ "//tensorflow/cc/saved_model:__subpackages__",
14
+ "//tensorflow/tools/tfg_graph_transforms:__subpackages__",
15
+ ],
16
+ licenses = ["notice"],
17
+ )
18
+
19
+ cc_library(
20
+ name = "internal_api",
21
+ srcs = ["internal_api.cc"],
22
+ hdrs = ["internal_api.h"],
23
+ deps = [
24
+ "//tensorflow/cc/saved_model:metrics",
25
+ "//tensorflow/cc/saved_model:util",
26
+ "//tensorflow/core/platform",
27
+ "//tensorflow/core/platform:env",
28
+ "//tensorflow/core/protobuf:for_core_protos_cc",
29
+ "//tensorflow/tools/proto_splitter/cc:max_size",
30
+ "@com_google_absl//absl/log",
31
+ "@com_google_absl//absl/status",
32
+ "@com_google_absl//absl/status:statusor",
33
+ "@com_google_absl//absl/strings",
34
+ "@com_google_absl//absl/strings:cord",
35
+ ] + if_not_windows_or_mac([
36
+ "//tensorflow/tools/proto_splitter:merge",
37
+ "//tensorflow/tools/proto_splitter/cc:saved_model_splitter",
38
+ ]),
39
+ )
western_ai/usa/tensorflow/tensorflow/cc/saved_model/image_format/README.md ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # SavedModel Image Format
2
+
3
+ Everything related to the SavedModel Image format belongs in this directory.
4
+ If you are a TensorFlow Python user, you can try this format by setting the
5
+ `experimental_image_format` option:
6
+
7
+ ```
8
+ tf.savedmodel.save(
9
+ model, path,
10
+ options=tf.saved_model.SaveOptions(experimental_image_format=True)
11
+ )
12
+ ```
13
+
14
+ When this option is enabled, exported SavedModels with proto size > 2GB will
15
+ automatically save with the new format (`.cpb` instead of `.pb`).
16
+
17
+ <!-- **Compatibility** -->
18
+
19
+ The official TF APIs (TF1/TF2 python or C++ loading) have already been
20
+ integrated to handle the new format, but some downstream converters may not
21
+ have been updated.
western_ai/usa/tensorflow/tensorflow/cc/saved_model/image_format/internal_api.cc ADDED
@@ -0,0 +1,143 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /* Copyright 2023 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/cc/saved_model/image_format/internal_api.h"
17
+
18
+ #include <string>
19
+ #include <tuple>
20
+
21
+ #include "absl/log/log.h"
22
+ #include "absl/status/status.h"
23
+ #include "absl/strings/cord.h"
24
+ #include "absl/strings/str_cat.h"
25
+ #include "tensorflow/cc/saved_model/metrics.h"
26
+ #include "tensorflow/cc/saved_model/util.h"
27
+ #include "tensorflow/core/platform/env.h"
28
+ #include "tensorflow/core/platform/file_system_helper.h"
29
+ #include "tensorflow/core/protobuf/saved_model.pb.h"
30
+ #include "tensorflow/tools/proto_splitter/cc/max_size.h"
31
+ // TODO(b/291933687), TODO(b/291001524)
32
+ #if !defined(PLATFORM_WINDOWS) && !defined(__APPLE__)
33
+ #include "tensorflow/tools/proto_splitter/cc/saved_model_splitter.h"
34
+ #include "tensorflow/tools/proto_splitter/merge.h"
35
+ #endif
36
+ #define IS_OSS false
37
+ namespace tensorflow {
38
+ namespace image_format {
39
+
40
+ absl::Status ReadSavedModel(const std::string& file_prefix,
41
+ SavedModel* saved_model_proto) {
42
+ LOG(INFO) << "Reading SavedModel from: " << file_prefix;
43
+
44
+ #if defined(PLATFORM_WINDOWS) || defined(__APPLE__)
45
+ const std::string saved_model_pb_path = absl::StrCat(file_prefix, ".pb");
46
+ TF_ASSIGN_OR_RETURN(
47
+ bool saved_model_pb_exists,
48
+ internal::FileExists(Env::Default(), saved_model_pb_path));
49
+ if (saved_model_pb_exists) {
50
+ absl::Status result =
51
+ ReadBinaryProto(Env::Default(), saved_model_pb_path, saved_model_proto);
52
+ if (result.ok()) {
53
+ metrics::SavedModelReadCount(
54
+ saved_model::GetWriteVersion(*saved_model_proto))
55
+ .IncrementBy(1);
56
+ }
57
+ return result;
58
+ }
59
+ #endif
60
+
61
+ // TODO(b/295208714): add pbtxt support to Merger::Read
62
+ const std::string saved_model_pbtxt_path =
63
+ absl::StrCat(file_prefix, ".pbtxt");
64
+ auto saved_model_pbtxt_exists =
65
+ internal::FileExists(Env::Default(), saved_model_pbtxt_path);
66
+ if (saved_model_pbtxt_exists.value_or(false)) {
67
+ absl::Status result = ReadTextProto(Env::Default(), saved_model_pbtxt_path,
68
+ saved_model_proto);
69
+ if (result.ok()) {
70
+ metrics::SavedModelReadCount(
71
+ saved_model::GetWriteVersion(*saved_model_proto))
72
+ .IncrementBy(1);
73
+ }
74
+ return result;
75
+ }
76
+
77
+ #if !defined(PLATFORM_WINDOWS) && !defined(__APPLE__)
78
+ absl::Status result =
79
+ tools::proto_splitter::Merger::Read(file_prefix, saved_model_proto);
80
+ if (result.ok()) {
81
+ metrics::SavedModelReadCount(
82
+ saved_model::GetWriteVersion(*saved_model_proto))
83
+ .IncrementBy(1);
84
+ }
85
+ return result;
86
+ #endif
87
+
88
+ return absl::Status(
89
+ absl::StatusCode::kNotFound,
90
+ absl::StrCat("Could not find SavedModel .pb or .pbtxt at supplied "
91
+ "file prefix: ",
92
+ file_prefix,
93
+ ". Check that "
94
+ "the directory exists and that you have the right "
95
+ "permissions for accessing it."));
96
+ }
97
+
98
+ absl::Status WriteSavedModel(SavedModel* saved_model_proto,
99
+ const std::string& file_prefix) {
100
+ #if !defined(PLATFORM_WINDOWS) && !defined(__APPLE__)
101
+ tools::proto_splitter::SavedModelSplitter splitter(saved_model_proto);
102
+ return splitter.Write(file_prefix).status();
103
+ #else
104
+ return absl::UnimplementedError(
105
+ "WriteSavedModel not implemented for Windows or MacOS.");
106
+ #endif
107
+ }
108
+
109
+ absl::StatusOr<std::tuple<std::string, bool>> WriteSavedModelToString(
110
+ SavedModel* saved_model_proto) {
111
+ #if !defined(PLATFORM_WINDOWS) && !defined(__APPLE__)
112
+ tools::proto_splitter::SavedModelSplitter splitter(saved_model_proto);
113
+ return splitter.WriteToString();
114
+ #else
115
+ return absl::UnimplementedError(
116
+ "WriteSavedModelToString not implemented for Windows or MacOS.");
117
+ #endif
118
+ }
119
+
120
+ #if !IS_OSS
121
+ // TODO(b/311769337): Define the function unconditionally after tf oss
122
+ // dependency is updated to protobuf v22.x.
123
+ absl::StatusOr<std::tuple<absl::Cord, bool>> WriteSavedModelToCord(
124
+ SavedModel* saved_model_proto) {
125
+ tools::proto_splitter::SavedModelSplitter splitter(saved_model_proto);
126
+ return splitter.WriteToCord();
127
+ }
128
+ #endif
129
+
130
+ absl::Status WriteSavedModel(SavedModel* saved_model_proto,
131
+ const std::string& file_prefix,
132
+ int debug_max_size) {
133
+ #if !defined(PLATFORM_WINDOWS) && !defined(__APPLE__)
134
+ tools::proto_splitter::DebugSetMaxSize(debug_max_size);
135
+ return WriteSavedModel(saved_model_proto, file_prefix);
136
+ #else
137
+ return absl::UnimplementedError(
138
+ "WriteSavedModel not implemented for Windows or MacOS.");
139
+ #endif
140
+ }
141
+
142
+ } // namespace image_format
143
+ } // namespace tensorflow
western_ai/usa/tensorflow/tensorflow/cc/saved_model/image_format/internal_api.h ADDED
@@ -0,0 +1,65 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /* Copyright 2023 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_IMAGE_FORMAT_INTERNAL_API_H_
17
+ #define TENSORFLOW_CC_SAVED_MODEL_IMAGE_FORMAT_INTERNAL_API_H_
18
+
19
+ #include <string>
20
+ #include <tuple>
21
+
22
+ #include "absl/status/status.h"
23
+ #include "absl/status/statusor.h"
24
+ #include "absl/strings/cord.h"
25
+ #include "tensorflow/core/protobuf/saved_model.pb.h"
26
+
27
+ #define IS_OSS false
28
+
29
+ namespace tensorflow {
30
+ namespace image_format {
31
+
32
+ // Reads the SavedModel proto from {file_prefix}{.pb|.cpb}.
33
+ // Returns a failure status when the SavedModel file does not exist.
34
+ absl::Status ReadSavedModel(const std::string& file_prefix,
35
+ SavedModel* saved_model_proto);
36
+
37
+ // Writes the SavedModel proto to a file or to string. If the proto is < the
38
+ // protobuf maximum size, then it will be serialized as a `.pb` proto binary.
39
+ // When larger than the maximum size, the SavedModel proto is destructively
40
+ // separated into chunks and written to
41
+ // `.cpb` (chunked proto).
42
+ //
43
+ // Write SavedModel to {file_prefix}{.pb|.cpb}.
44
+ absl::Status WriteSavedModel(SavedModel* saved_model_proto,
45
+ const std::string& file_prefix);
46
+ // Writes the SavedModel proto to std::string
47
+ // The bool field record whether it's saved as a chunked protobuf (true) or
48
+ // regular protobuf (false)
49
+ absl::StatusOr<std::tuple<std::string, bool>> WriteSavedModelToString(
50
+ SavedModel* saved_model_proto);
51
+ #if !IS_OSS
52
+ absl::StatusOr<std::tuple<absl::Cord, bool>> WriteSavedModelToCord(
53
+ SavedModel* saved_model_proto);
54
+ #endif
55
+
56
+ // See above. The `debug_max_size` argument can be used to the maximum size to
57
+ // less than 2GB for testing purposes.
58
+ absl::Status WriteSavedModel(SavedModel* saved_model_proto,
59
+ const std::string& file_prefix,
60
+ int debug_max_size);
61
+
62
+ } // namespace image_format
63
+ } // namespace tensorflow
64
+
65
+ #endif // TENSORFLOW_CC_SAVED_MODEL_IMAGE_FORMAT_INTERNAL_API_H_
western_ai/usa/tensorflow/tensorflow/cc/saved_model/loader.cc ADDED
@@ -0,0 +1,575 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /* Copyright 2016 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/cc/saved_model/loader.h"
17
+
18
+ #include <memory>
19
+ #include <string>
20
+ #include <unordered_set>
21
+ #include <utility>
22
+
23
+ #include "absl/status/status.h"
24
+ #include "absl/strings/str_cat.h"
25
+ #include "absl/strings/str_join.h"
26
+ #include "tensorflow/cc/saved_model/constants.h"
27
+ #include "tensorflow/cc/saved_model/fingerprinting.h"
28
+ #include "tensorflow/cc/saved_model/loader_util.h"
29
+ #include "tensorflow/cc/saved_model/metrics.h"
30
+ #include "tensorflow/cc/saved_model/reader.h"
31
+ #include "tensorflow/cc/saved_model/util.h"
32
+ #include "tensorflow/core/framework/attr_value.pb.h"
33
+ #include "tensorflow/core/framework/function.pb.h"
34
+ #include "tensorflow/core/framework/graph_debug_info.pb.h"
35
+ #include "tensorflow/core/framework/node_def.pb.h"
36
+ #include "tensorflow/core/framework/op_def.pb.h"
37
+ #include "tensorflow/core/framework/tensor.pb.h"
38
+ #include "tensorflow/core/lib/io/path.h"
39
+ #include "tensorflow/core/lib/monitoring/counter.h"
40
+ #include "tensorflow/core/lib/monitoring/sampler.h"
41
+ #include "tensorflow/core/lib/strings/str_util.h"
42
+ #include "tensorflow/core/lib/strings/strcat.h"
43
+ #include "tensorflow/core/platform/env.h"
44
+ #include "tensorflow/core/platform/errors.h"
45
+ #include "tensorflow/core/platform/file_system_helper.h"
46
+ #include "tensorflow/core/platform/statusor.h"
47
+ #include "tensorflow/core/platform/threadpool_options.h"
48
+ #include "tensorflow/core/protobuf/meta_graph.pb.h"
49
+ #include "tensorflow/core/protobuf/saver.pb.h"
50
+ #include "tensorflow/core/public/session.h"
51
+ #include "tensorflow/core/public/session_options.h"
52
+ #include "tensorflow/core/util/tensor_bundle/naming.h"
53
+
54
+ namespace tensorflow {
55
+ namespace {
56
+
57
+ auto* load_attempt_count = monitoring::Counter<2>::New(
58
+ "/tensorflow/cc/saved_model/load_attempt_count",
59
+ "The number of times a SavedModel was successfully loaded.", "model_path",
60
+ "status");
61
+ auto* load_latency = monitoring::Counter<1>::New(
62
+ "/tensorflow/cc/saved_model/load_latency",
63
+ "Latency in microseconds for SavedModels that were successfully loaded.",
64
+ "model_path");
65
+ auto* load_latency_by_stage = monitoring::Sampler<2>::New(
66
+ {
67
+ "/tensorflow/cc/saved_model/load_latency_by_stage", // metric name
68
+ "Distribution of wall time spent (in microseconds) in each stage "
69
+ "(restore graph from disk, run init graph op, etc) when loading the "
70
+ "model",
71
+ "model_path",
72
+ "stage",
73
+ },
74
+ // Scale of 10, power of 1.8 with bucket count 37 (~258 minutes).
75
+ monitoring::Buckets::Exponential(10, 1.8, 37));
76
+
77
+ constexpr char kLoadAttemptFail[] = "fail";
78
+ constexpr char kLoadAttemptSuccess[] = "success";
79
+ // `tensorflow::LoadSavedModel` API label.
80
+ constexpr char kCCLoadLabel[] = "cc_load";
81
+
82
+ uint64 GetLatencyMicroseconds(const uint64 start_microseconds) {
83
+ const uint64 end_microseconds = EnvTime::NowMicros();
84
+ // Avoid clock skew.
85
+ if (end_microseconds < start_microseconds) return 0;
86
+ return end_microseconds - start_microseconds;
87
+ }
88
+
89
+ // Ensure that constant tensors loaded from the saved model have valid shape.
90
+ // Also ensure that constant nodes have a value assigned to them.
91
+ // TODO(b/154763635): this is temporary and will be replaced with a better audit
92
+ static absl::Status ValidateNode(const NodeDef& node) {
93
+ const auto node_iterator = node.attr().find("value");
94
+ if (node_iterator != node.attr().end()) {
95
+ AttrValue node_value = node_iterator->second;
96
+ if (node_value.has_tensor()) {
97
+ const PartialTensorShape node_shape(node_value.tensor().tensor_shape());
98
+ if (node_shape.num_elements() < 0) {
99
+ return absl::FailedPreconditionError(absl::StrCat(
100
+ "Saved model contains node \"", node.name(), "\" (op \"", node.op(),
101
+ "\") which initializes from a tensor with ",
102
+ node_shape.num_elements(), " elements"));
103
+ }
104
+ }
105
+ } else if (node.op() == "Const") {
106
+ return absl::FailedPreconditionError(absl::StrCat(
107
+ "Saved model contains node \"", node.name(),
108
+ "\" which is a constant tensor but no value has been provided"));
109
+ }
110
+ return absl::OkStatus();
111
+ }
112
+
113
+ static absl::Status ValidateFunctionNotRecursive(const FunctionDef& function) {
114
+ const auto& function_name = function.signature().name();
115
+ for (const auto& node : function.node_def()) {
116
+ if (node.op() == function_name) {
117
+ return absl::FailedPreconditionError(absl::StrCat(
118
+ "Function ", function_name,
119
+ " is self recursive and TensorFlow does not support this scenario."));
120
+ }
121
+ }
122
+
123
+ return absl::OkStatus();
124
+ }
125
+
126
+ static absl::Status ValidateSavedTensors(const GraphDef& graph_def) {
127
+ for (const auto& node : graph_def.node()) {
128
+ TF_RETURN_IF_ERROR(ValidateNode(node));
129
+ }
130
+
131
+ if (graph_def.has_library()) {
132
+ const FunctionDefLibrary& library = graph_def.library();
133
+ for (const auto& function : library.function()) {
134
+ for (const auto& node : function.node_def()) {
135
+ TF_RETURN_IF_ERROR(ValidateNode(node));
136
+ }
137
+
138
+ // Also check that there is no recursivity in the library
139
+ TF_RETURN_IF_ERROR(ValidateFunctionNotRecursive(function));
140
+ }
141
+ }
142
+
143
+ return absl::OkStatus();
144
+ }
145
+
146
+ Tensor CreateStringTensor(const string& value) {
147
+ Tensor tensor(DT_STRING, TensorShape({}));
148
+ tensor.scalar<tstring>()() = value;
149
+ return tensor;
150
+ }
151
+
152
+ void AddAssetsTensorsToInputs(const absl::string_view export_dir,
153
+ const std::vector<AssetFileDef>& asset_file_defs,
154
+ std::vector<std::pair<string, Tensor>>* inputs) {
155
+ if (asset_file_defs.empty()) {
156
+ return;
157
+ }
158
+ for (auto& asset_file_def : asset_file_defs) {
159
+ Tensor assets_file_path_tensor = CreateStringTensor(io::JoinPath(
160
+ export_dir, kSavedModelAssetsDirectory, asset_file_def.filename()));
161
+ inputs->push_back(
162
+ {asset_file_def.tensor_info().name(), assets_file_path_tensor});
163
+ }
164
+ }
165
+
166
+ // Like Session::Run(), but uses the Make/Run/ReleaseCallable() API to avoid
167
+ // leaving behind non-GC'ed state.
168
+ //
169
+ // Detailed motivation behind this approach, from ashankar@:
170
+ //
171
+ // Each call to Session::Run() that identifies a new subgraph (based on feeds
172
+ // and fetches) creates some datastructures that live as long as the session
173
+ // (the partitioned graph, associated executors etc.).
174
+ //
175
+ // A pathological case of this would be if say the initialization op
176
+ // (main_op/legacy_init_op) involves the use of a large constant. Then we
177
+ // allocate memory for that large constant that will just stick around till the
178
+ // session dies. With this Callable mechanism, that memory will be released
179
+ // right after ReleaseCallable returns.
180
+ //
181
+ // However, the resource manager state remains.
182
+ absl::Status RunOnce(const RunOptions& run_options,
183
+ const std::vector<std::pair<string, Tensor>>& inputs,
184
+ const std::vector<string>& output_tensor_names,
185
+ const std::vector<string>& target_node_names,
186
+ std::vector<Tensor>* outputs, RunMetadata* run_metadata,
187
+ Session* session) {
188
+ CallableOptions callable_options;
189
+ std::vector<Tensor> feed_tensors;
190
+ *callable_options.mutable_run_options() = run_options;
191
+ for (const auto& input : inputs) {
192
+ const string& name = input.first;
193
+ const Tensor& tensor = input.second;
194
+ callable_options.add_feed(name);
195
+ feed_tensors.push_back(tensor);
196
+ }
197
+ for (const string& output_tensor_name : output_tensor_names) {
198
+ callable_options.add_fetch(output_tensor_name);
199
+ }
200
+ for (const string& target_node_name : target_node_names) {
201
+ callable_options.add_target(target_node_name);
202
+ }
203
+
204
+ Session::CallableHandle callable_handle;
205
+ TF_RETURN_IF_ERROR(session->MakeCallable(callable_options, &callable_handle));
206
+ const absl::Status run_status = session->RunCallable(
207
+ callable_handle, feed_tensors, outputs, run_metadata);
208
+ // Be sure to call ReleaseCallable() regardless of the outcome of
209
+ // RunCallable().
210
+ session->ReleaseCallable(callable_handle).IgnoreError();
211
+ return run_status;
212
+ }
213
+
214
+ // RunInitOp will return OK if the initialization op was run successfully.
215
+ // An empty init_op_name indicates that there are no init ops to run.
216
+ absl::Status RunInitOp(const RunOptions& run_options, const string& export_dir,
217
+ const MetaGraphDef& meta_graph_def,
218
+ const std::vector<AssetFileDef>& asset_file_defs,
219
+ Session* session, const string& init_op_name) {
220
+ if (!init_op_name.empty()) {
221
+ LOG(INFO) << "Running initialization op on SavedModel bundle at path: "
222
+ << export_dir;
223
+ std::vector<std::pair<string, Tensor>> inputs;
224
+ AddAssetsTensorsToInputs(export_dir, asset_file_defs, &inputs);
225
+ RunMetadata run_metadata;
226
+ return RunOnce(run_options, inputs, {}, {init_op_name},
227
+ nullptr /* outputs */, &run_metadata, session);
228
+ }
229
+ return absl::OkStatus();
230
+ }
231
+
232
+ absl::Status RunRestore(const RunOptions& run_options, const string& export_dir,
233
+ const absl::string_view restore_op_name,
234
+ const absl::string_view variable_filename_const_op_name,
235
+ const std::vector<AssetFileDef>& asset_file_defs,
236
+ Session* session) {
237
+ LOG(INFO) << "Restoring SavedModel bundle.";
238
+ // Find path to variables to be restored in export directory.
239
+ const string variables_directory =
240
+ io::JoinPath(export_dir, kSavedModelVariablesDirectory);
241
+ // Check for saver checkpoints in v2 format. Models exported in the checkpoint
242
+ // v2 format will have a variables.index file. The corresponding
243
+ // variables are stored in the variables.data-?????-of-????? files.
244
+ const string variables_index_path = io::JoinPath(
245
+ variables_directory, MetaFilename(kSavedModelVariablesFilename));
246
+ TF_ASSIGN_OR_RETURN(
247
+ bool variables_index_exists,
248
+ internal::FileExists(Env::Default(), variables_index_path));
249
+ if (!variables_index_exists) {
250
+ LOG(INFO) << "The specified SavedModel has no variables; no checkpoints "
251
+ "were restored. File does not exist: "
252
+ << variables_index_path;
253
+ return absl::OkStatus();
254
+ }
255
+ const string variables_path =
256
+ io::JoinPath(variables_directory, kSavedModelVariablesFilename);
257
+
258
+ // Add variables to the graph.
259
+ Tensor variables_path_tensor(DT_STRING, TensorShape({}));
260
+ variables_path_tensor.scalar<tstring>()() = variables_path;
261
+
262
+ std::vector<std::pair<string, Tensor>> inputs = {
263
+ {string(variable_filename_const_op_name), variables_path_tensor}};
264
+
265
+ AddAssetsTensorsToInputs(export_dir, asset_file_defs, &inputs);
266
+
267
+ RunMetadata run_metadata;
268
+ return RunOnce(run_options, inputs, {}, {string(restore_op_name)},
269
+ nullptr /* outputs */, &run_metadata, session);
270
+ }
271
+
272
+ } // namespace
273
+
274
+ SavedModelBundleInterface::~SavedModelBundleInterface() = default;
275
+
276
+ absl::Status LoadMetagraphIntoSession(const SessionOptions& session_options,
277
+ const MetaGraphDef& meta_graph,
278
+ std::unique_ptr<Session>* session) {
279
+ Session* session_p = nullptr;
280
+ TF_RETURN_IF_ERROR(NewSession(session_options, &session_p));
281
+ session->reset(session_p);
282
+ TF_RETURN_IF_ERROR(ValidateSavedTensors(meta_graph.graph_def()));
283
+ return (*session)->Create(meta_graph.graph_def());
284
+ }
285
+
286
+ absl::Status LoadGraphDefIntoSession(const SessionOptions& session_options,
287
+ GraphDef graph_def,
288
+ std::unique_ptr<Session>* session) {
289
+ Session* session_p = nullptr;
290
+ TF_RETURN_IF_ERROR(NewSession(session_options, &session_p));
291
+ session->reset(session_p);
292
+ TF_RETURN_IF_ERROR(ValidateSavedTensors(graph_def));
293
+ return (*session)->Create(std::move(graph_def));
294
+ }
295
+
296
+ absl::Status LoadSavedModelInternal(const SessionOptions& session_options,
297
+ const RunOptions& run_options,
298
+ const string& export_dir,
299
+ const std::unordered_set<string>& tags,
300
+ SavedModelBundle* const bundle) {
301
+ TF_RETURN_IF_ERROR(ReadMetaGraphDefFromSavedModel(export_dir, tags,
302
+ &bundle->meta_graph_def));
303
+ TF_RETURN_IF_ERROR(
304
+ ReadSavedModelDebugInfoIfPresent(export_dir, &bundle->debug_info));
305
+ TF_RETURN_IF_ERROR(LoadMetagraphIntoSession(
306
+ session_options, bundle->meta_graph_def, &bundle->session));
307
+ TF_RETURN_IF_ERROR(RestoreSession(run_options, bundle->meta_graph_def,
308
+ export_dir, &bundle->session));
309
+ return absl::OkStatus();
310
+ }
311
+
312
+ namespace {
313
+ // Session wrapper that prevents calls to Session::Create(), Session::Extend(),
314
+ // and the deprecated partial-run methods.
315
+ //
316
+ // Limiting the available methods on a returned Session gives us the option
317
+ // to replace the Session with a cut-down implementation, without breaking any
318
+ // users.
319
+ class LiteSessionWrapper : public Session {
320
+ public:
321
+ explicit LiteSessionWrapper(std::unique_ptr<Session> wrapped)
322
+ : wrapped_(std::move(wrapped)) {}
323
+
324
+ absl::Status Create(const GraphDef& graph) override {
325
+ return absl::UnimplementedError("Session::Create()");
326
+ }
327
+ absl::Status Create(GraphDef&& graph) override {
328
+ return absl::UnimplementedError("Session::Create()");
329
+ }
330
+
331
+ absl::Status Extend(const GraphDef& graph) override {
332
+ return absl::UnimplementedError("Session::Extend()");
333
+ }
334
+ absl::Status Extend(GraphDef&& graph) override {
335
+ return absl::UnimplementedError("Session::Extend()");
336
+ }
337
+
338
+ absl::Status Run(const std::vector<std::pair<string, Tensor>>& inputs,
339
+ const std::vector<string>& output_tensor_names,
340
+ const std::vector<string>& target_node_names,
341
+ std::vector<Tensor>* outputs) override {
342
+ return wrapped_->Run(inputs, output_tensor_names, target_node_names,
343
+ outputs);
344
+ }
345
+
346
+ absl::Status Create(const RunOptions& run_options,
347
+ const GraphDef& graph) override {
348
+ return absl::UnimplementedError("Session::Create()");
349
+ }
350
+ absl::Status Extend(const RunOptions& run_options,
351
+ const GraphDef& graph) override {
352
+ return absl::UnimplementedError("Session::Extend()");
353
+ }
354
+ absl::Status Create(const RunOptions& run_options,
355
+ GraphDef&& graph) override {
356
+ return absl::UnimplementedError("Session::Create()");
357
+ }
358
+ absl::Status Extend(const RunOptions& run_options,
359
+ GraphDef&& graph) override {
360
+ return absl::UnimplementedError("Session::Extend()");
361
+ }
362
+ absl::Status Close(const RunOptions& run_options) override {
363
+ return wrapped_->Close(run_options);
364
+ }
365
+
366
+ absl::Status Run(const RunOptions& run_options,
367
+ const std::vector<std::pair<string, Tensor>>& inputs,
368
+ const std::vector<string>& output_tensor_names,
369
+ const std::vector<string>& target_node_names,
370
+ std::vector<Tensor>* outputs,
371
+ RunMetadata* run_metadata) override {
372
+ return wrapped_->Run(run_options, inputs, output_tensor_names,
373
+ target_node_names, outputs, run_metadata);
374
+ }
375
+
376
+ absl::Status Run(
377
+ const RunOptions& run_options,
378
+ const std::vector<std::pair<std::string, Tensor>>& inputs,
379
+ const std::vector<std::string>& output_tensor_names,
380
+ const std::vector<std::string>& target_tensor_names,
381
+ std::vector<Tensor>* outputs, RunMetadata* run_metadata,
382
+ const thread::ThreadPoolOptions& threadpool_options) override {
383
+ return wrapped_->Run(run_options, inputs, output_tensor_names,
384
+ target_tensor_names, outputs, run_metadata,
385
+ threadpool_options);
386
+ }
387
+
388
+ absl::Status PRunSetup(const std::vector<string>& input_names,
389
+ const std::vector<string>& output_names,
390
+ const std::vector<string>& target_nodes,
391
+ string* handle) override {
392
+ return absl::UnimplementedError("Session::PRunSetup()");
393
+ }
394
+
395
+ absl::Status PRun(const string& handle,
396
+ const std::vector<std::pair<string, Tensor>>& inputs,
397
+ const std::vector<string>& output_names,
398
+ std::vector<Tensor>* outputs) override {
399
+ return absl::UnimplementedError("Session::PRun()");
400
+ }
401
+
402
+ absl::Status ListDevices(std::vector<DeviceAttributes>* response) override {
403
+ return wrapped_->ListDevices(response);
404
+ }
405
+
406
+ absl::Status Close() override { return wrapped_->Close(); }
407
+
408
+ absl::Status LocalDeviceManager(const DeviceMgr** device_mgr) override {
409
+ return wrapped_->LocalDeviceManager(device_mgr);
410
+ }
411
+
412
+ absl::Status MakeCallable(const CallableOptions& callable_options,
413
+ CallableHandle* out_handle) override {
414
+ return wrapped_->MakeCallable(callable_options, out_handle);
415
+ }
416
+
417
+ absl::Status RunCallable(CallableHandle handle,
418
+ const std::vector<Tensor>& feed_tensors,
419
+ std::vector<Tensor>* fetch_tensors,
420
+ RunMetadata* run_metadata) override {
421
+ return wrapped_->RunCallable(handle, feed_tensors, fetch_tensors,
422
+ run_metadata);
423
+ }
424
+
425
+ absl::Status RunCallable(
426
+ CallableHandle handle, const std::vector<Tensor>& feed_tensors,
427
+ std::vector<Tensor>* fetch_tensors, RunMetadata* run_metadata,
428
+ const thread::ThreadPoolOptions& threadpool_options) override {
429
+ return wrapped_->RunCallable(handle, feed_tensors, fetch_tensors,
430
+ run_metadata, threadpool_options);
431
+ }
432
+
433
+ absl::Status ReleaseCallable(CallableHandle handle) override {
434
+ return wrapped_->ReleaseCallable(handle);
435
+ }
436
+
437
+ absl::Status Finalize() override { return wrapped_->Finalize(); }
438
+
439
+ private:
440
+ const std::unique_ptr<Session> wrapped_;
441
+ };
442
+ } // namespace
443
+
444
+ absl::Status LoadSavedModelInternal(const SessionOptions& session_options,
445
+ const RunOptions& run_options,
446
+ const string& export_dir,
447
+ const std::unordered_set<string>& tags,
448
+ SavedModelBundleLite* const bundle) {
449
+ MetaGraphDef meta_graph_def;
450
+ TF_RETURN_IF_ERROR(
451
+ ReadMetaGraphDefFromSavedModel(export_dir, tags, &meta_graph_def));
452
+ std::unique_ptr<Session> session;
453
+ TF_RETURN_IF_ERROR(LoadGraphDefIntoSession(
454
+ session_options, std::move(*meta_graph_def.mutable_graph_def()),
455
+ &session));
456
+ TF_RETURN_IF_ERROR(
457
+ RestoreSession(run_options, meta_graph_def, export_dir, &session));
458
+ *bundle = SavedModelBundleLite(
459
+ std::make_unique<LiteSessionWrapper>(std::move(session)),
460
+ std::move(*meta_graph_def.mutable_signature_def()));
461
+ return absl::OkStatus();
462
+ }
463
+
464
+ template <typename BundleType>
465
+ absl::Status LoadSavedModelGeneric(const SessionOptions& session_options,
466
+ const RunOptions& run_options,
467
+ const string& export_dir,
468
+ const std::unordered_set<string>& tags,
469
+ BundleType* const bundle) {
470
+ metrics::SavedModelReadApi(kCCLoadLabel).IncrementBy(1);
471
+ auto fingerprint_proto =
472
+ saved_model::fingerprinting::ReadSavedModelFingerprint(export_dir);
473
+ if (fingerprint_proto.ok()) {
474
+ // Set gauge cell with saved_model_checksum.
475
+ metrics::SavedModelReadFingerprint().Set(
476
+ std::to_string(fingerprint_proto->saved_model_checksum()));
477
+ }
478
+
479
+ // TODO(robson): Add tests for the counters.
480
+ const uint64 start_microseconds = Env::Default()->NowMicros();
481
+ const absl::Status status = LoadSavedModelInternal(
482
+ session_options, run_options, export_dir, tags, bundle);
483
+ auto log_and_count = [&](const string& status_str) {
484
+ LOG(INFO) << "SavedModel load for tags { " << absl::StrJoin(tags, " ")
485
+ << " }; Status: " << status_str << ": " << status << ". Took "
486
+ << GetLatencyMicroseconds(start_microseconds) << " microseconds.";
487
+ load_attempt_count->GetCell(export_dir, status_str)->IncrementBy(1);
488
+ };
489
+ if (status.ok()) {
490
+ log_and_count(kLoadAttemptSuccess);
491
+ metrics::SavedModelReadPath().Set(export_dir);
492
+ } else {
493
+ log_and_count(kLoadAttemptFail);
494
+ }
495
+ load_latency->GetCell(export_dir)
496
+ ->IncrementBy(GetLatencyMicroseconds(start_microseconds));
497
+ return status;
498
+ }
499
+
500
+ absl::Status LoadSavedModel(const SessionOptions& session_options,
501
+ const RunOptions& run_options,
502
+ const string& export_dir,
503
+ const std::unordered_set<string>& tags,
504
+ SavedModelBundle* const bundle) {
505
+ return LoadSavedModelGeneric<SavedModelBundle>(session_options, run_options,
506
+ export_dir, tags, bundle);
507
+ }
508
+
509
+ absl::Status RestoreSession(const RunOptions& run_options,
510
+ const MetaGraphDef& meta_graph,
511
+ const string& export_dir,
512
+ std::unique_ptr<Session>* session) {
513
+ const uint64 read_start_microseconds = Env::Default()->NowMicros();
514
+ std::vector<AssetFileDef> asset_file_defs;
515
+ TF_RETURN_IF_ERROR(internal::GetAssetFileDefs(meta_graph, &asset_file_defs));
516
+ if (meta_graph.has_saver_def()) {
517
+ TF_RETURN_IF_ERROR(RunRestore(run_options, export_dir,
518
+ meta_graph.saver_def().restore_op_name(),
519
+ meta_graph.saver_def().filename_tensor_name(),
520
+ asset_file_defs, session->get()));
521
+ }
522
+ // Record walltime spent in restoring graph from disk, but postpone metric
523
+ // increments until graph init finishes.
524
+ const uint64 restore_graph_walltime =
525
+ GetLatencyMicroseconds(read_start_microseconds);
526
+
527
+ const uint64 graph_init_start_microseconds = Env::Default()->NowMicros();
528
+ string init_op_name;
529
+ TF_RETURN_IF_ERROR(
530
+ internal::GetInitOp(export_dir, meta_graph, &init_op_name));
531
+ TF_RETURN_IF_ERROR(RunInitOp(run_options, export_dir, meta_graph,
532
+ asset_file_defs, session->get(), init_op_name));
533
+ load_latency_by_stage->GetCell(export_dir, "restore_graph")
534
+ ->Add(restore_graph_walltime);
535
+ // Record wall time spent in init op.
536
+ load_latency_by_stage->GetCell(export_dir, "init_graph")
537
+ ->Add(GetLatencyMicroseconds(graph_init_start_microseconds));
538
+ return absl::OkStatus();
539
+ }
540
+
541
+ absl::Status LoadSavedModel(const SessionOptions& session_options,
542
+ const RunOptions& run_options,
543
+ const string& export_dir,
544
+ const std::unordered_set<string>& tags,
545
+ SavedModelBundleLite* const bundle) {
546
+ SessionOptions rewritten_options(session_options);
547
+ // We disallow calls to Session::Extend() on the returned session, so we can
548
+ // reduce memory consumption by not storing the original GraphDef.
549
+ rewritten_options.config.mutable_experimental()
550
+ ->set_optimize_for_static_graph(true);
551
+ // Disallowing the `RunOptions.output_partition_graphs` option (typically used
552
+ // in debugging and tests) allows us to reduce memory consumption further by
553
+ // not storing the rewritten subgraph for each signature.
554
+ rewritten_options.config.mutable_experimental()
555
+ ->set_disable_output_partition_graphs(true);
556
+ // TODO(mrry): Consider specializing the session creation to reduce peak
557
+ // RAM consumption by using `Session::Create(GraphDef&&)`.
558
+ TF_RETURN_IF_ERROR(LoadSavedModelGeneric(rewritten_options, run_options,
559
+ export_dir, tags, bundle));
560
+ return absl::OkStatus();
561
+ }
562
+
563
+ bool MaybeSavedModelDirectory(const string& export_dir) {
564
+ const string saved_model_pb_path =
565
+ io::JoinPath(export_dir, kSavedModelFilenamePb);
566
+ const string saved_model_cpb_path =
567
+ io::JoinPath(export_dir, kSavedModelFilenameCpb);
568
+ const string saved_model_pbtxt_path =
569
+ io::JoinPath(export_dir, kSavedModelFilenamePbTxt);
570
+ return Env::Default()->FileExists(saved_model_pb_path).ok() ||
571
+ Env::Default()->FileExists(saved_model_cpb_path).ok() ||
572
+ Env::Default()->FileExists(saved_model_pbtxt_path).ok();
573
+ }
574
+
575
+ } // namespace tensorflow
western_ai/usa/tensorflow/tensorflow/cc/saved_model/loader.h ADDED
@@ -0,0 +1,150 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /* Copyright 2016 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
+ /// SavedModel loading functions and SavedModelBundle struct.
17
+
18
+ #ifndef TENSORFLOW_CC_SAVED_MODEL_LOADER_H_
19
+ #define TENSORFLOW_CC_SAVED_MODEL_LOADER_H_
20
+
21
+ #include <string>
22
+ #include <unordered_set>
23
+
24
+ #include "tensorflow/core/framework/graph_debug_info.pb.h"
25
+ #include "tensorflow/core/lib/core/status.h"
26
+ #include "tensorflow/core/protobuf/meta_graph.pb.h"
27
+ #include "tensorflow/core/public/session.h"
28
+
29
+ namespace tensorflow {
30
+
31
+ /// Represents a SavedModel that is loaded from storage.
32
+ class SavedModelBundleInterface {
33
+ public:
34
+ virtual ~SavedModelBundleInterface();
35
+
36
+ /// Returns the TensorFlow Session that can be used to interact with the
37
+ /// SavedModel.
38
+ virtual Session* GetSession() const = 0;
39
+
40
+ /// Returns a map from signature name to SignatureDef for all signatures in
41
+ /// in the SavedModel.
42
+ virtual const protobuf::Map<string, SignatureDef>& GetSignatures() const = 0;
43
+ };
44
+
45
+ /// SavedModel representation once the SavedModel is loaded from storage.
46
+ ///
47
+ /// NOTE: Prefer to use SavedModelBundleLite in new code, as it consumes less
48
+ /// RAM.
49
+ struct SavedModelBundle : public SavedModelBundleInterface {
50
+ /// A TensorFlow Session does not Close itself on destruction. To avoid
51
+ /// resource leaks, we explicitly call Close on Sessions that we create.
52
+ ~SavedModelBundle() override {
53
+ if (session) {
54
+ session->Close().IgnoreError();
55
+ }
56
+ }
57
+
58
+ SavedModelBundle() = default;
59
+
60
+ Session* GetSession() const override { return session.get(); }
61
+ const protobuf::Map<string, SignatureDef>& GetSignatures() const override {
62
+ return meta_graph_def.signature_def();
63
+ }
64
+
65
+ std::unique_ptr<Session> session;
66
+ MetaGraphDef meta_graph_def;
67
+ std::unique_ptr<GraphDebugInfo> debug_info;
68
+ };
69
+
70
+ // A version of SavedModelBundle that avoids storing a potentially large
71
+ // MetaGraphDef. Prefer to use SavedModelBundleLite in new code.
72
+ class SavedModelBundleLite : public SavedModelBundleInterface {
73
+ public:
74
+ SavedModelBundleLite() = default;
75
+ SavedModelBundleLite(SavedModelBundleLite&& other) = default;
76
+ SavedModelBundleLite& operator=(SavedModelBundleLite&& other) = default;
77
+
78
+ SavedModelBundleLite(std::unique_ptr<Session> session,
79
+ protobuf::Map<string, SignatureDef> signatures)
80
+ : session_(std::move(session)), signatures_(std::move(signatures)) {}
81
+
82
+ /// A TensorFlow Session does not Close itself on destruction. To avoid
83
+ /// resource leaks, we explicitly call Close on Sessions that we create.
84
+ ~SavedModelBundleLite() override {
85
+ if (session_) {
86
+ session_->Close().IgnoreError();
87
+ }
88
+ }
89
+
90
+ Session* GetSession() const override { return session_.get(); }
91
+ const protobuf::Map<string, SignatureDef>& GetSignatures() const override {
92
+ return signatures_;
93
+ }
94
+
95
+ private:
96
+ std::unique_ptr<Session> session_;
97
+ protobuf::Map<string, SignatureDef> signatures_;
98
+ };
99
+
100
+ // Restore variable and resources in the SavedModel export dir for the
101
+ // indicated metagraph.
102
+ // The recommended way to load a saved model is to call LoadSavedModel,
103
+ // which provides an already initialized Metagraph, Session, and DebugInfo.
104
+ absl::Status RestoreSession(const RunOptions& run_options,
105
+ const MetaGraphDef& meta_graph,
106
+ const string& export_dir,
107
+ std::unique_ptr<Session>* session);
108
+
109
+ // Initialize a session which wraps this metagraph.
110
+ // The recommended way to load a saved model is to call LoadSavedModel,
111
+ // which provides an already initialized Metagraph, Session, and DebugInfo.
112
+ absl::Status LoadMetagraphIntoSession(const SessionOptions& session_options,
113
+ const MetaGraphDef& meta_graph,
114
+ std::unique_ptr<Session>* session);
115
+
116
+ /// Loads a SavedModel from the specified export directory. The MetaGraphDef
117
+ /// to be loaded is identified by the supplied tags, corresponding exactly to
118
+ /// the set of tags used at SavedModel build time. Stores a SavedModel bundle in
119
+ /// *bundle with a session and the requested MetaGraphDef, if found.
120
+ ///
121
+ /// NOTE: Prefer the overload that takes a SavedModelBundleLite* in new code.
122
+ absl::Status LoadSavedModel(const SessionOptions& session_options,
123
+ const RunOptions& run_options,
124
+ const string& export_dir,
125
+ const std::unordered_set<string>& tags,
126
+ SavedModelBundle* bundle);
127
+
128
+ /// Loads a SavedModel from the specified export directory. The MetaGraphDef
129
+ /// to be loaded is identified by the supplied tags, corresponding exactly to
130
+ /// the set of tags used at SavedModel build time. Stores a SavedModel bundle
131
+ /// in *bundle with a session created from the requested MetaGraphDef if found.
132
+ ///
133
+ /// This overload creates a SavedModelBundleLite, which consumes less RAM than
134
+ /// an equivalent SavedModelBundle.
135
+ absl::Status LoadSavedModel(const SessionOptions& session_options,
136
+ const RunOptions& run_options,
137
+ const string& export_dir,
138
+ const std::unordered_set<string>& tags,
139
+ SavedModelBundleLite* bundle);
140
+
141
+ /// Checks whether the provided directory could contain a SavedModel. Note that
142
+ /// the method does not load any data by itself. If the method returns `false`,
143
+ /// the export directory definitely does not contain a SavedModel. If the method
144
+ /// returns `true`, the export directory may contain a SavedModel but provides
145
+ /// no guarantee that it can be loaded.
146
+ bool MaybeSavedModelDirectory(const std::string& export_dir);
147
+
148
+ } // namespace tensorflow
149
+
150
+ #endif // TENSORFLOW_CC_SAVED_MODEL_LOADER_H_
western_ai/usa/tensorflow/tensorflow/cc/saved_model/loader_util.cc ADDED
@@ -0,0 +1,96 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /* Copyright 2016 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/cc/saved_model/loader_util.h"
17
+
18
+ #include <vector>
19
+
20
+ #include "tensorflow/cc/saved_model/constants.h"
21
+ #include "tensorflow/core/lib/strings/strcat.h"
22
+ #include "tensorflow/core/platform/errors.h"
23
+ #include "tensorflow/core/platform/protobuf_internal.h"
24
+
25
+ namespace tensorflow {
26
+ namespace internal {
27
+
28
+ // A SavedModel may store the name of the initialization op to run in the
29
+ // in the SignatureDef (v2) or a collection (v1). If an init_op collection
30
+ // exists, then the collection must contain exactly one op.
31
+ absl::Status GetInitOp(const string& export_dir,
32
+ const MetaGraphDef& meta_graph_def,
33
+ string* init_op_name) {
34
+ const auto& sig_def_map = meta_graph_def.signature_def();
35
+ const auto& init_op_sig_it =
36
+ meta_graph_def.signature_def().find(kSavedModelInitOpSignatureKey);
37
+ if (init_op_sig_it != sig_def_map.end()) {
38
+ const auto& sig_def_outputs = init_op_sig_it->second.outputs();
39
+ const auto& sig_def_outputs_it =
40
+ sig_def_outputs.find(kSavedModelInitOpSignatureKey);
41
+ if (sig_def_outputs_it == sig_def_outputs.end()) {
42
+ return errors::FailedPrecondition("Could not find output ",
43
+ kSavedModelInitOpSignatureKey);
44
+ }
45
+ *init_op_name = sig_def_outputs_it->second.name();
46
+ return absl::OkStatus();
47
+ }
48
+
49
+ const auto& collection_def_map = meta_graph_def.collection_def();
50
+ string init_op_collection_key;
51
+ if (collection_def_map.find(kSavedModelMainOpKey) !=
52
+ collection_def_map.end()) {
53
+ init_op_collection_key = kSavedModelMainOpKey;
54
+ } else {
55
+ init_op_collection_key = kSavedModelLegacyInitOpKey;
56
+ }
57
+
58
+ const auto init_op_it = collection_def_map.find(init_op_collection_key);
59
+ if (init_op_it != collection_def_map.end()) {
60
+ if (init_op_it->second.node_list().value_size() != 1) {
61
+ return errors::FailedPrecondition(
62
+ strings::StrCat("Expected exactly one main op in : ", export_dir));
63
+ }
64
+ *init_op_name = init_op_it->second.node_list().value(0);
65
+ }
66
+ return absl::OkStatus();
67
+ }
68
+
69
+ absl::Status GetAssetFileDefs(const MetaGraphDef& meta_graph_def,
70
+ std::vector<AssetFileDef>* asset_file_defs) {
71
+ // With SavedModel v2, we write asset file def into metagraph instead of
72
+ // collection, so read from metagraph first.
73
+ if (meta_graph_def.asset_file_def_size() > 0) {
74
+ for (const auto& asset : meta_graph_def.asset_file_def()) {
75
+ asset_file_defs->push_back(asset);
76
+ }
77
+ return absl::OkStatus();
78
+ }
79
+ // Fall back to read from collection to be backward compatible with v1.
80
+ const auto& collection_def_map = meta_graph_def.collection_def();
81
+ const auto assets_it = collection_def_map.find(kSavedModelAssetsKey);
82
+ if (assets_it == collection_def_map.end()) {
83
+ return absl::OkStatus();
84
+ }
85
+ const auto& any_assets = assets_it->second.any_list().value();
86
+ for (const auto& any_asset : any_assets) {
87
+ AssetFileDef asset_file_def;
88
+ TF_RETURN_IF_ERROR(
89
+ ParseAny(any_asset, &asset_file_def, "tensorflow.AssetFileDef"));
90
+ asset_file_defs->push_back(asset_file_def);
91
+ }
92
+ return absl::OkStatus();
93
+ }
94
+
95
+ } // namespace internal
96
+ } // namespace tensorflow
western_ai/usa/tensorflow/tensorflow/cc/saved_model/loader_util.h ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /* Copyright 2016 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_LOADER_UTIL_H_
17
+ #define TENSORFLOW_CC_SAVED_MODEL_LOADER_UTIL_H_
18
+
19
+ #include <string>
20
+
21
+ #include "tensorflow/core/lib/core/status.h"
22
+ #include "tensorflow/core/protobuf/meta_graph.pb.h"
23
+
24
+ namespace tensorflow {
25
+ namespace internal {
26
+
27
+ // A SavedModel may store the name of the initialization op to run in the
28
+ // in the SignatureDef (v2) or a collection (v1). If an init_op collection
29
+ // exists, then the collection must contain exactly one op.
30
+ absl::Status GetInitOp(const string& export_dir,
31
+ const MetaGraphDef& meta_graph_def,
32
+ string* init_op_name);
33
+
34
+ absl::Status GetAssetFileDefs(const MetaGraphDef& meta_graph_def,
35
+ std::vector<AssetFileDef>* asset_file_defs);
36
+
37
+ } // namespace internal
38
+ } // namespace tensorflow
39
+
40
+ #endif // TENSORFLOW_CC_SAVED_MODEL_LOADER_UTIL_H_
western_ai/usa/tensorflow/tensorflow/cc/saved_model/metrics.cc ADDED
@@ -0,0 +1,324 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /* Copyright 2021 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/cc/saved_model/metrics.h"
17
+
18
+ #include <cstddef>
19
+ #include <cstdint>
20
+ #include <string>
21
+ #include <utility>
22
+
23
+ #include "absl/status/status.h"
24
+ #include "absl/status/statusor.h"
25
+ #include "absl/strings/str_cat.h"
26
+ #include "absl/strings/string_view.h"
27
+ #include "json/config.h"
28
+ #include "json/json.h"
29
+ #include "json/writer.h"
30
+ #include "tensorflow/core/lib/monitoring/counter.h"
31
+ #include "tensorflow/core/lib/monitoring/gauge.h"
32
+ #include "tensorflow/core/lib/monitoring/sampler.h"
33
+ #include "tensorflow/core/protobuf/fingerprint.pb.h"
34
+
35
+ namespace tensorflow {
36
+ namespace metrics {
37
+
38
+ namespace {
39
+
40
+ // Counter that tracks total number and `write_version` of SavedModels written.
41
+ auto* saved_model_write_counter = monitoring::Counter<1>::New(
42
+ "/tensorflow/core/saved_model/write/count",
43
+ "The number of SavedModels successfully written.", "write_version");
44
+
45
+ // Counter that tracks total number and `write_version` of SavedModels read.
46
+ auto* saved_model_read_counter = monitoring::Counter<1>::New(
47
+ "/tensorflow/core/saved_model/read/count",
48
+ "The number of SavedModels successfully loaded.", "write_version");
49
+
50
+ // Counter that tracks number of calls for each SavedModel write API. Summing
51
+ // across "api_label" is not expected to equal the ".../write/count" cell value
52
+ // because programs can invoke more than one API to save a single SM and
53
+ // because the API may error out before successfully writing a SM.
54
+ auto* saved_model_write_api = monitoring::Counter<1>::New(
55
+ "/tensorflow/core/saved_model/write/api",
56
+ "The API used to write the SavedModel.", "api_label");
57
+
58
+ // Counter that tracks number of calls for each SavedModel read API. Summing
59
+ // across "api_label" is not expected to equal the ".../read/count" cell value
60
+ // because programs can invoke more than one API to load a single SM and
61
+ // because the API may error out before successfully reading a SM.
62
+ auto* saved_model_read_api = monitoring::Counter<1>::New(
63
+ "/tensorflow/core/saved_model/read/api",
64
+ "The API used to load the SavedModel.", "api_label");
65
+
66
+ // Gauge that contains the fingerprint (saved_model_checksum) of the newly
67
+ // written SavedModel.
68
+ auto* saved_model_write_fingerprint = monitoring::Gauge<std::string, 0>::New(
69
+ "/tensorflow/core/saved_model/write/fingerprint",
70
+ "The fingerprint (saved_model_checksum) of the exported SavedModel.");
71
+
72
+ // Gauge that contains the path (saved_model_path) of the newly written
73
+ // SavedModel.
74
+ auto* saved_model_write_path = monitoring::Gauge<std::string, 0>::New(
75
+ "/tensorflow/core/saved_model/write/path",
76
+ "The path (saved_model_path) of the exported SavedModel.");
77
+
78
+ // Gauge that contains the path (saved_model_path) and the singleprint
79
+ // (concatenation of graph_def_program_hash, signature_def_hash,
80
+ // saved_object_graph_hash, and checkpoint_hash) of the newly written
81
+ // SavedModel.
82
+ auto* saved_model_write_path_and_singleprint =
83
+ monitoring::Gauge<std::string, 0>::New(
84
+ "/tensorflow/core/saved_model/write/path_and_singleprint",
85
+ "The path (saved_model_path) and singleprint (concatenation of "
86
+ "graph_def_program_hash, signature_def_hash, saved_object_graph_hash, "
87
+ "and checkpoint_hash) of the newly written SavedModel.");
88
+
89
+ // Gauge that contains the fingerprint (saved_model_checksum) of the loaded
90
+ // SavedModel.
91
+ auto* saved_model_read_fingerprint = monitoring::Gauge<std::string, 0>::New(
92
+ "/tensorflow/core/saved_model/read/fingerprint",
93
+ "The fingerprint (saved_model_checksum) of the loaded SavedModel.");
94
+
95
+ // Gauge that contains the path (saved_model_path) of the loaded SavedModel.
96
+ auto* saved_model_read_path = monitoring::Gauge<std::string, 0>::New(
97
+ "/tensorflow/core/saved_model/read/path",
98
+ "The path (saved_model_path) of the loaded SavedModel.");
99
+
100
+ // Gauge that contains the path (saved_model_path) and the singleprint
101
+ // (concatenation of graph_def_program_hash, signature_def_hash,
102
+ // saved_object_graph_hash, and checkpoint_hash) of the loaded SavedModel.
103
+ auto* saved_model_read_path_and_singleprint =
104
+ monitoring::Gauge<std::string, 0>::New(
105
+ "/tensorflow/core/saved_model/read/path_and_singleprint",
106
+ "The path (saved_model_path) and singleprint (concatenation of "
107
+ "graph_def_program_hash, signature_def_hash, saved_object_graph_hash, "
108
+ "and checkpoint_hash) of the loaded SavedModel.");
109
+
110
+ // Gauge that marks whether or not the fingerprint.pb file was found when
111
+ // loading the SavedModel.
112
+ // Can hold one of the following string values:
113
+ // - "FOUND"
114
+ // - "NOT_FOUND"
115
+ // - "ERROR"
116
+ auto* saved_model_found_fingerprint_on_load =
117
+ monitoring::Gauge<std::string, 0>::New(
118
+ "/tensorflow/core/saved_model/found_fingerprint_on_load",
119
+ "Whether or not the fingerprint.pb file was found when loading the "
120
+ "SavedModel.");
121
+
122
+ // Distribution of checkpoint write durations.
123
+ auto* checkpoint_write_durations = monitoring::Sampler<1>::New(
124
+ {
125
+ "/tensorflow/core/checkpoint/write/write_durations", // Metric name.
126
+ "Distribution of the wall time duration in microseconds of the "
127
+ "checkpoint write operation.", // Metric description.
128
+ "api_label" // Cell label.
129
+ },
130
+ // Scale of 1000, growth factor of 1.5 with upper bound of ~184 minutes.
131
+ monitoring::Buckets::Exponential(1000, 1.5, 41));
132
+
133
+ // Distribution of checkpoint read durations.
134
+ auto* checkpoint_read_durations = monitoring::Sampler<1>::New(
135
+ {
136
+ "/tensorflow/core/checkpoint/read/read_durations", // Metric name.
137
+ "Distribution of the wall time duration in microseconds of the "
138
+ "checkpoint read operation.", // Metric description.
139
+ "api_label" // Cell label.
140
+ },
141
+ // Scale of 1000, growth factor of 1.5 with upper bound of ~184 minutes.
142
+ monitoring::Buckets::Exponential(1000, 1.5, 41));
143
+
144
+ // Distribution of async checkpoint write durations.
145
+ auto* async_checkpoint_write_durations = monitoring::Sampler<1>::New(
146
+ {
147
+ "/tensorflow/core/checkpoint/write/async_write_durations", // Metric
148
+ // name.
149
+ "Distribution of the wall time duration in microseconds of the async "
150
+ "checkpoint write operation", // Metric description.
151
+ "api_label" // Cell label.
152
+ },
153
+ // Scale of 1000, growth factor of 1.5 with upper bound of ~184 minutes.
154
+ monitoring::Buckets::Exponential(1000, 1.5, 41));
155
+
156
+ // Counter that accumulates total time elapsed between module import time and
157
+ // the last successful Checkpoint write prior to job preemption or completion.
158
+ auto* checkpoint_training_time_saved = monitoring::Counter<1>::New(
159
+ "/tensorflow/core/checkpoint/write/training_time_saved",
160
+ "Total time in microseconds elapsed between two consecutive write "
161
+ "operations in a single job or between Checkpoint construction and the "
162
+ "first write operation.",
163
+ "api_label");
164
+
165
+ // Counter that records filesize (MB) of written checkpoint. Contains two cells:
166
+ // (api_label, filesize). Cardinality should not be an issue as the filesize
167
+ // should be equal among all checkpoints written per job.
168
+ auto* checkpoint_size = monitoring::Counter<2>::New(
169
+ "/tensorflow/core/checkpoint/write/checkpoint_size",
170
+ "Size of checkpoint (.index and sharded data files), rounded to the "
171
+ "nearest 100 MB.",
172
+ "api_label", "filesize");
173
+
174
+ } // namespace
175
+
176
+ // Counter that records how long it took to execute the checkpoint sharding
177
+ // callback in microseconds.
178
+ auto* sharding_callback_duration = monitoring::Counter<0>::New(
179
+ "/tensorflow/core/checkpoint/sharding/callback_duration",
180
+ "Sharding callback execution duration in microseconds.");
181
+
182
+ // Counter that records how many checkpoint shard files were written during
183
+ // saving.
184
+ auto* num_checkpoint_shards_written = monitoring::Counter<0>::New(
185
+ "/tensorflow/core/checkpoint/sharding/num_checkpoint_shards_written",
186
+ "Number of checkpoint shard files written during saving.");
187
+
188
+ // String gauge which describes the callback used to shard the checkpoint during
189
+ // saving.
190
+ auto* sharding_callback_description = monitoring::Gauge<std::string, 0>::New(
191
+ "/tensorflow/core/checkpoint/sharding/callback_description",
192
+ "Describes the callback used to shard the checkpoint during saving.");
193
+
194
+ monitoring::CounterCell& SavedModelWriteCount(absl::string_view write_version) {
195
+ return *saved_model_write_counter->GetCell(std::string(write_version));
196
+ }
197
+
198
+ monitoring::CounterCell& SavedModelReadCount(absl::string_view write_version) {
199
+ return *saved_model_read_counter->GetCell(std::string(write_version));
200
+ }
201
+
202
+ monitoring::CounterCell& SavedModelWriteApi(absl::string_view api_label) {
203
+ return *saved_model_write_api->GetCell(std::string(api_label));
204
+ }
205
+
206
+ monitoring::CounterCell& SavedModelReadApi(absl::string_view api_label) {
207
+ return *saved_model_read_api->GetCell(std::string(api_label));
208
+ }
209
+
210
+ monitoring::GaugeCell<std::string>& SavedModelReadFingerprint() {
211
+ return *saved_model_read_fingerprint->GetCell();
212
+ }
213
+
214
+ monitoring::GaugeCell<std::string>& SavedModelReadPath() {
215
+ return *saved_model_read_path->GetCell();
216
+ }
217
+
218
+ monitoring::GaugeCell<std::string>& SavedModelReadPathAndSingleprint() {
219
+ return *saved_model_read_path_and_singleprint->GetCell();
220
+ }
221
+
222
+ monitoring::GaugeCell<std::string>& SavedModelWriteFingerprint() {
223
+ return *saved_model_write_fingerprint->GetCell();
224
+ }
225
+
226
+ monitoring::GaugeCell<std::string>& SavedModelWritePath() {
227
+ return *saved_model_write_path->GetCell();
228
+ }
229
+
230
+ monitoring::GaugeCell<std::string>& SavedModelWritePathAndSingleprint() {
231
+ return *saved_model_write_path_and_singleprint->GetCell();
232
+ }
233
+
234
+ std::string MakeFingerprintJson(FingerprintDef fingerprint_def) {
235
+ Json::Value fingerprint = Json::objectValue;
236
+ fingerprint["saved_model_checksum"] =
237
+ Json::UInt64(fingerprint_def.saved_model_checksum());
238
+ fingerprint["graph_def_program_hash"] =
239
+ Json::UInt64(fingerprint_def.graph_def_program_hash());
240
+ fingerprint["signature_def_hash"] =
241
+ Json::UInt64(fingerprint_def.signature_def_hash());
242
+ fingerprint["saved_object_graph_hash"] =
243
+ Json::UInt64(fingerprint_def.saved_object_graph_hash());
244
+ fingerprint["checkpoint_hash"] =
245
+ Json::UInt64(fingerprint_def.checkpoint_hash());
246
+
247
+ Json::StreamWriterBuilder json_factory;
248
+ return Json::writeString(json_factory, fingerprint);
249
+ }
250
+
251
+ absl::StatusOr<std::string> MakeSavedModelPathAndSingleprint(
252
+ std::string path, std::string singleprint) {
253
+ if (path.empty()) {
254
+ return absl::InvalidArgumentError(
255
+ "Invalid path_and_singleprint argument. Empty path.");
256
+ }
257
+ if (singleprint.empty()) {
258
+ return absl::InvalidArgumentError(
259
+ "Invalid path_and_singleprint argument. Empty singleprint.");
260
+ }
261
+ return absl::StrCat(path, ":", singleprint);
262
+ }
263
+
264
+ absl::StatusOr<std::pair<std::string, std::string>>
265
+ ParseSavedModelPathAndSingleprint(std::string path_and_singleprint) {
266
+ size_t delimiter = path_and_singleprint.rfind(':');
267
+ if (delimiter == std::string::npos) {
268
+ return absl::InvalidArgumentError(
269
+ "Invalid path_and_singleprint argument. Found no delimeter.");
270
+ }
271
+ std::string path = path_and_singleprint.substr(0, delimiter);
272
+ if (path.empty()) {
273
+ return absl::InvalidArgumentError(
274
+ "Invalid path_and_singleprint argument. Empty path.");
275
+ }
276
+ std::string singleprint = path_and_singleprint.substr(delimiter + 1);
277
+ if (singleprint.empty()) {
278
+ return absl::InvalidArgumentError(
279
+ "Invalid path_and_singleprint argument. Empty singleprint.");
280
+ }
281
+ return std::pair<std::string, std::string>(path, singleprint);
282
+ }
283
+
284
+ monitoring::GaugeCell<std::string>& SavedModelFoundFingerprintOnLoad() {
285
+ return *saved_model_found_fingerprint_on_load->GetCell();
286
+ }
287
+
288
+ monitoring::SamplerCell& CheckpointReadDuration(absl::string_view api_label) {
289
+ return *checkpoint_read_durations->GetCell(std::string(api_label));
290
+ }
291
+
292
+ monitoring::SamplerCell& CheckpointWriteDuration(absl::string_view api_label) {
293
+ return *checkpoint_write_durations->GetCell(std::string(api_label));
294
+ }
295
+
296
+ monitoring::SamplerCell& AsyncCheckpointWriteDuration(
297
+ absl::string_view api_label) {
298
+ return *async_checkpoint_write_durations->GetCell(std::string(api_label));
299
+ }
300
+
301
+ monitoring::CounterCell& TrainingTimeSaved(absl::string_view api_label) {
302
+ return *checkpoint_training_time_saved->GetCell(std::string(api_label));
303
+ }
304
+
305
+ monitoring::CounterCell& CheckpointSize(absl::string_view api_label,
306
+ int64_t filesize) {
307
+ return *checkpoint_size->GetCell(std::string(api_label),
308
+ std::to_string(filesize));
309
+ }
310
+
311
+ monitoring::CounterCell& ShardingCallbackDuration() {
312
+ return *sharding_callback_duration->GetCell();
313
+ }
314
+
315
+ monitoring::CounterCell& NumCheckpointShardsWritten() {
316
+ return *num_checkpoint_shards_written->GetCell();
317
+ }
318
+
319
+ monitoring::GaugeCell<std::string>& ShardingCallbackDescription() {
320
+ return *sharding_callback_description->GetCell();
321
+ }
322
+
323
+ } // namespace metrics
324
+ } // namespace tensorflow
western_ai/usa/tensorflow/tensorflow/cc/saved_model/metrics.h ADDED
@@ -0,0 +1,147 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /* Copyright 2021 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
+ // APIs for accessing SavedModel and checkpoint metric objects.
16
+ //
17
+ // In order to collect the data from these metrics, please add the metrics to
18
+ // the provided monitoring platform. Unless configured with a user-specified
19
+ // monitoring platform, the data is not collected in OSS.
20
+
21
+ #ifndef TENSORFLOW_CC_SAVED_MODEL_METRICS_H_
22
+ #define TENSORFLOW_CC_SAVED_MODEL_METRICS_H_
23
+
24
+ #include <cstdint>
25
+ #include <string>
26
+ #include <utility>
27
+
28
+ #include "absl/status/statusor.h"
29
+ #include "absl/strings/string_view.h"
30
+ #include "tensorflow/core/lib/monitoring/counter.h"
31
+ #include "tensorflow/core/lib/monitoring/gauge.h"
32
+ #include "tensorflow/core/lib/monitoring/sampler.h"
33
+ #include "tensorflow/core/protobuf/fingerprint.pb.h"
34
+
35
+ namespace tensorflow {
36
+ namespace metrics {
37
+
38
+ const char kFingerprintFound[] = "FOUND";
39
+ const char kFingerprintNotFound[] = "NOT_FOUND";
40
+ const char kFingerprintError[] = "ERROR";
41
+
42
+ // Returns "/tensorflow/core/saved_model/write/count" cell. This metric
43
+ // has 1 field "write_version", which is equal to the
44
+ // `tensorflow::libexport::GetWriteVersion` of the protobuf and should be
45
+ // incremented when a SavedModel has been successfully written.
46
+ monitoring::CounterCell& SavedModelWriteCount(absl::string_view write_version);
47
+
48
+ // Returns "/tensorflow/core/saved_model/read/count" cell. This metric
49
+ // has 1 field "write_version", which is equal to the
50
+ // `tensorflow::libexport::GetWriteVersion` of the protobuf, and should be
51
+ // incremented when a SavedModel has been successfully read.
52
+ monitoring::CounterCell& SavedModelReadCount(absl::string_view write_version);
53
+
54
+ // Returns "/tensorflow/core/saved_model/write/api" cell. This metric has 1
55
+ // field "api_label" which corresponds to a SavedModel write API. The cell for
56
+ // `foo` should be incremented when the write API `foo` is called.
57
+ monitoring::CounterCell& SavedModelWriteApi(absl::string_view api_label);
58
+
59
+ // Returns "/tensorflow/core/saved_model/read/api" cell. This metric has 1
60
+ // field "api_label" which corresponds to a SavedModel read API. The cell for
61
+ // `foo` should be incremented when the read API `foo` is called.
62
+ monitoring::CounterCell& SavedModelReadApi(absl::string_view api_label);
63
+
64
+ // Returns "/tensorflow/core/saved_model/write/fingerprint" cell, which contains
65
+ // the saved_model_checksum of the SM's fingerprint when it is exported.
66
+ monitoring::GaugeCell<std::string>& SavedModelWriteFingerprint();
67
+
68
+ // Returns "/tensorflow/core/saved_model/write/path" cell, which contains
69
+ // the saved_model_path of the SM when it is exported.
70
+ monitoring::GaugeCell<std::string>& SavedModelWritePath();
71
+
72
+ // Returns "/tensorflow/core/saved_model/write/path_and_fingerprint" cell, which
73
+ // contains the path (saved_model_path) and fingerprint (concatenation of
74
+ // graph_def_program_hash, signature_def_hash, saved_object_graph_hash,
75
+ // and checkpoint_hash) of the SavedModel when it is exported.
76
+ monitoring::GaugeCell<std::string>& SavedModelWritePathAndSingleprint();
77
+
78
+ // Returns "/tensorflow/core/saved_model/read/fingerprint" cell, wich contains
79
+ // the saved_model_checksum of the SM's fingerprint when it is imported.
80
+ monitoring::GaugeCell<std::string>& SavedModelReadFingerprint();
81
+
82
+ // Returns "/tensorflow/core/saved_model/read/path" cell, wich contains
83
+ // the saved_model_path of the SM when it is imported.
84
+ monitoring::GaugeCell<std::string>& SavedModelReadPath();
85
+
86
+ // Returns "/tensorflow/core/saved_model/read/path_and_fingerprint" cell, which
87
+ // contains the path (saved_model_path) and singleprint (concatenation of
88
+ // graph_def_program_hash, signature_def_hash, saved_object_graph_hash,
89
+ // and checkpoint_hash) of the SavedModel when it is imported.
90
+ monitoring::GaugeCell<std::string>& SavedModelReadPathAndSingleprint();
91
+
92
+ // Returns the fingerprint as a Json string.
93
+ std::string MakeFingerprintJson(FingerprintDef fingerprint_def);
94
+
95
+ // Returns canonical string concatenation of path and singleprint.
96
+ absl::StatusOr<std::string> MakeSavedModelPathAndSingleprint(
97
+ std::string path, std::string singleprint);
98
+
99
+ // Returns path and singleprint as a pair, parsed canonically from the string
100
+ // metric.
101
+ absl::StatusOr<std::pair<std::string, std::string>>
102
+ ParseSavedModelPathAndSingleprint(std::string path_and_singleprint);
103
+
104
+ // Returns string status indicating whether or not the fingerprint.pb file was
105
+ // found when loading the SavedModel.
106
+ monitoring::GaugeCell<std::string>& SavedModelFoundFingerprintOnLoad();
107
+
108
+ // Returns "/tensorflow/core/checkpoint/read/read_durations" cell belonging to
109
+ // field `api_label`.
110
+ monitoring::SamplerCell& CheckpointReadDuration(absl::string_view api_label);
111
+
112
+ // Returns "/tensorflow/core/checkpoint/write/write_durations" cell belonging to
113
+ // field `api_label`.
114
+ monitoring::SamplerCell& CheckpointWriteDuration(absl::string_view api_label);
115
+
116
+ // Returns "/tensorflow/core/checkpoint/write/async_write_durations" cell
117
+ // belonging to field `api_label`.
118
+ monitoring::SamplerCell& AsyncCheckpointWriteDuration(
119
+ absl::string_view api_label);
120
+
121
+ // Returns "/tensorflow/core/checkpoint/write/training_time_saved" cell
122
+ // belonging to field `api_label`.
123
+ monitoring::CounterCell& TrainingTimeSaved(absl::string_view api_label);
124
+
125
+ // Returns "/tensorflow/core/checkpoint/write/checkpoint_size" cell
126
+ // belonging to field (`api_label`, `filesize`).
127
+ monitoring::CounterCell& CheckpointSize(absl::string_view api_label,
128
+ int64_t filesize);
129
+
130
+ // Returns "/tensorflow/core/checkpoint/sharding/callback_duration" cell which
131
+ // describes how long it took to execute the checkpoint sharding callback in
132
+ // microseconds.
133
+ monitoring::CounterCell& ShardingCallbackDuration();
134
+
135
+ // Returns "/tensorflow/core/checkpoint/sharding/num_checkpoint_shards_written"
136
+ // cell which describes how many checkpoint shard files were written during
137
+ // saving.
138
+ monitoring::CounterCell& NumCheckpointShardsWritten();
139
+
140
+ // Returns "/tensorflow/core/checkpoint/sharding/callback_description" cell
141
+ // which describes the callback used to shard the checkpoint during saving.
142
+ monitoring::GaugeCell<std::string>& ShardingCallbackDescription();
143
+
144
+ } // namespace metrics
145
+ } // namespace tensorflow
146
+
147
+ #endif // TENSORFLOW_CC_SAVED_MODEL_METRICS_H_
western_ai/usa/tensorflow/tensorflow/cc/saved_model/metrics_test.cc ADDED
@@ -0,0 +1,208 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /* Copyright 2021 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/cc/saved_model/metrics.h"
17
+
18
+ #include <string>
19
+
20
+ #include <gmock/gmock.h>
21
+ #include <gtest/gtest.h>
22
+ #include "absl/status/status.h"
23
+ #include "json/json.h"
24
+ #include "json/reader.h"
25
+ #include "tensorflow/core/platform/status_matchers.h"
26
+ #include "tensorflow/core/platform/test.h"
27
+ #include "tensorflow/core/protobuf/fingerprint.pb.h"
28
+ #include "tsl/platform/statusor.h"
29
+
30
+ namespace tensorflow {
31
+ namespace metrics {
32
+ // The value of the cells for each metric persists across tests.
33
+
34
+ TEST(MetricsTest, TestSavedModelWrite) {
35
+ EXPECT_EQ(SavedModelWriteApi("foo").value(), 0);
36
+ SavedModelWriteApi("foo").IncrementBy(1);
37
+ EXPECT_EQ(SavedModelWriteApi("foo").value(), 1);
38
+
39
+ EXPECT_EQ(SavedModelWriteCount("1").value(), 0);
40
+ SavedModelWriteCount("1").IncrementBy(1);
41
+ EXPECT_EQ(SavedModelWriteCount("1").value(), 1);
42
+ }
43
+
44
+ TEST(MetricsTest, TestSavedModelRead) {
45
+ SavedModelReadApi("bar").IncrementBy(1);
46
+ EXPECT_EQ(SavedModelReadApi("bar").value(), 1);
47
+ SavedModelReadCount("2").IncrementBy(1);
48
+ EXPECT_EQ(SavedModelReadCount("2").value(), 1);
49
+
50
+ SavedModelReadApi("baz").IncrementBy(1);
51
+ EXPECT_EQ(SavedModelReadApi("baz").value(), 1);
52
+ SavedModelReadCount("2").IncrementBy(1);
53
+ EXPECT_EQ(SavedModelReadCount("2").value(), 2);
54
+ }
55
+
56
+ TEST(MetricsTest, TestCheckpointRead) {
57
+ EXPECT_EQ(CheckpointReadDuration("foo").value().num(), 0);
58
+ CheckpointReadDuration("foo").Add(100);
59
+ EXPECT_EQ(CheckpointReadDuration("foo").value().num(), 1);
60
+ }
61
+
62
+ TEST(MetricsTest, TestCheckpointWrite) {
63
+ EXPECT_EQ(CheckpointWriteDuration("foo").value().num(), 0);
64
+ CheckpointWriteDuration("foo").Add(100);
65
+ EXPECT_EQ(CheckpointWriteDuration("foo").value().num(), 1);
66
+ }
67
+
68
+ TEST(MetricsTest, TestAsyncCheckpointWrite) {
69
+ EXPECT_EQ(AsyncCheckpointWriteDuration("foo").value().num(), 0);
70
+ AsyncCheckpointWriteDuration("foo").Add(100);
71
+ EXPECT_EQ(AsyncCheckpointWriteDuration("foo").value().num(), 1);
72
+ }
73
+
74
+ TEST(MetricsTest, TestTrainingTimeSaved) {
75
+ EXPECT_EQ(TrainingTimeSaved("foo").value(), 0);
76
+ TrainingTimeSaved("foo").IncrementBy(100);
77
+ EXPECT_EQ(TrainingTimeSaved("foo").value(), 100);
78
+ }
79
+
80
+ TEST(MetricsTest, TestCheckpointSize) {
81
+ EXPECT_EQ(CheckpointSize("foo", 10).value(), 0);
82
+ CheckpointSize("foo", 10).IncrementBy(1);
83
+ EXPECT_EQ(CheckpointSize("foo", 10).value(), 1);
84
+ }
85
+
86
+ TEST(MetricsTest, TestWriteFingerprint) {
87
+ EXPECT_EQ(SavedModelWriteFingerprint().value(), "");
88
+ SavedModelWriteFingerprint().Set("foo");
89
+ EXPECT_EQ(SavedModelWriteFingerprint().value(), "foo");
90
+ SavedModelWriteFingerprint().Set("bar");
91
+ EXPECT_EQ(SavedModelWriteFingerprint().value(), "bar");
92
+ }
93
+
94
+ TEST(MetricsTest, TestWritePath) {
95
+ EXPECT_EQ(SavedModelWritePath().value(), "");
96
+ SavedModelWritePath().Set("foo");
97
+ EXPECT_EQ(SavedModelWritePath().value(), "foo");
98
+ SavedModelWritePath().Set("bar");
99
+ EXPECT_EQ(SavedModelWritePath().value(), "bar");
100
+ }
101
+
102
+ TEST(MetricsTest, TestWritePathAndSingleprint) {
103
+ EXPECT_EQ(SavedModelWritePathAndSingleprint().value(), "");
104
+ SavedModelWritePathAndSingleprint().Set("foo");
105
+ EXPECT_EQ(SavedModelWritePathAndSingleprint().value(), "foo");
106
+ SavedModelWritePathAndSingleprint().Set("bar");
107
+ EXPECT_EQ(SavedModelWritePathAndSingleprint().value(), "bar");
108
+
109
+ EXPECT_EQ(
110
+ MakeSavedModelPathAndSingleprint("path", "singleprint").value_or(""),
111
+ "path:singleprint");
112
+ }
113
+
114
+ TEST(MetricsTest, TestInvalidMakePathAndSingleprint) {
115
+ EXPECT_THAT(MakeSavedModelPathAndSingleprint("", "singleprint"),
116
+ absl_testing::StatusIs(absl::StatusCode::kInvalidArgument));
117
+ EXPECT_THAT(MakeSavedModelPathAndSingleprint("path", ""),
118
+ absl_testing::StatusIs(absl::StatusCode::kInvalidArgument));
119
+ }
120
+
121
+ TEST(MetricsTest, TestReadFingerprint) {
122
+ EXPECT_EQ(SavedModelReadFingerprint().value(), "");
123
+ SavedModelReadFingerprint().Set("foo");
124
+ EXPECT_EQ(SavedModelReadFingerprint().value(), "foo");
125
+ SavedModelReadFingerprint().Set("bar");
126
+ EXPECT_EQ(SavedModelReadFingerprint().value(), "bar");
127
+ }
128
+
129
+ TEST(MetricsTest, TestReadPath) {
130
+ EXPECT_EQ(SavedModelReadPath().value(), "");
131
+ SavedModelReadPath().Set("foo");
132
+ EXPECT_EQ(SavedModelReadPath().value(), "foo");
133
+ SavedModelReadPath().Set("bar");
134
+ EXPECT_EQ(SavedModelReadPath().value(), "bar");
135
+ }
136
+
137
+ TEST(MetricsTest, TestReadPathAndSingleprint) {
138
+ EXPECT_EQ(SavedModelReadPathAndSingleprint().value(), "");
139
+ SavedModelReadPathAndSingleprint().Set("foo");
140
+ EXPECT_EQ(SavedModelReadPathAndSingleprint().value(), "foo");
141
+ SavedModelReadPathAndSingleprint().Set("bar");
142
+ EXPECT_EQ(SavedModelReadPathAndSingleprint().value(), "bar");
143
+
144
+ TF_ASSERT_OK_AND_ASSIGN(
145
+ auto path_singleprint,
146
+ ParseSavedModelPathAndSingleprint("path/model:name:singleprint"));
147
+ auto [path, singleprint] = path_singleprint;
148
+ EXPECT_EQ(path, "path/model:name");
149
+ EXPECT_EQ(singleprint, "singleprint");
150
+ }
151
+
152
+ TEST(MetricsTest, TestMakeFingerprintJson) {
153
+ FingerprintDef fingerprint;
154
+ fingerprint.set_saved_model_checksum(1);
155
+ fingerprint.set_graph_def_program_hash(2);
156
+ fingerprint.set_signature_def_hash(3);
157
+ fingerprint.set_saved_object_graph_hash(4);
158
+ fingerprint.set_checkpoint_hash(5);
159
+
160
+ std::string serialized_fingerprint_json = MakeFingerprintJson(fingerprint);
161
+
162
+ EXPECT_EQ(
163
+ serialized_fingerprint_json,
164
+ "{\n\t\"checkpoint_hash\" : 5,\n\t\"graph_def_program_hash\" : "
165
+ "2,\n\t\"saved_model_checksum\" : 1,\n\t\"saved_object_graph_hash\" : "
166
+ "4,\n\t\"signature_def_hash\" : 3\n}");
167
+
168
+ Json::Value fingerprint_json = Json::objectValue;
169
+ Json::Reader reader = Json::Reader();
170
+ reader.parse(serialized_fingerprint_json, fingerprint_json);
171
+ EXPECT_EQ(fingerprint_json["saved_model_checksum"].asUInt64(), 1);
172
+ EXPECT_EQ(fingerprint_json["graph_def_program_hash"].asUInt64(), 2);
173
+ EXPECT_EQ(fingerprint_json["signature_def_hash"].asUInt64(), 3);
174
+ EXPECT_EQ(fingerprint_json["saved_object_graph_hash"].asUInt64(), 4);
175
+ EXPECT_EQ(fingerprint_json["checkpoint_hash"].asUInt64(), 5);
176
+ }
177
+
178
+ TEST(MetricsTest, TestFoundFingerprintOnLoad) {
179
+ EXPECT_EQ(SavedModelFoundFingerprintOnLoad().value(), "");
180
+
181
+ SavedModelFoundFingerprintOnLoad().Set(kFingerprintFound);
182
+ EXPECT_EQ(SavedModelFoundFingerprintOnLoad().value(), "FOUND");
183
+ SavedModelFoundFingerprintOnLoad().Set(kFingerprintNotFound);
184
+ EXPECT_EQ(SavedModelFoundFingerprintOnLoad().value(), "NOT_FOUND");
185
+ SavedModelFoundFingerprintOnLoad().Set(kFingerprintError);
186
+ EXPECT_EQ(SavedModelFoundFingerprintOnLoad().value(), "ERROR");
187
+ }
188
+
189
+ TEST(MetricsTest, TestShardingCallbackDuration) {
190
+ EXPECT_EQ(ShardingCallbackDuration().value(), 0);
191
+ ShardingCallbackDuration().IncrementBy(100);
192
+ EXPECT_EQ(ShardingCallbackDuration().value(), 100);
193
+ }
194
+
195
+ TEST(MetricsTest, TestNumCheckpointShardsWritten) {
196
+ EXPECT_EQ(NumCheckpointShardsWritten().value(), 0);
197
+ NumCheckpointShardsWritten().IncrementBy(10);
198
+ EXPECT_EQ(NumCheckpointShardsWritten().value(), 10);
199
+ }
200
+
201
+ TEST(MetricsTest, TestShardingCallbackDescription) {
202
+ EXPECT_EQ(ShardingCallbackDescription().value(), "");
203
+ ShardingCallbackDescription().Set("foo");
204
+ EXPECT_EQ(ShardingCallbackDescription().value(), "foo");
205
+ }
206
+
207
+ } // namespace metrics
208
+ } // namespace tensorflow
western_ai/usa/tensorflow/tensorflow/cc/saved_model/python/BUILD ADDED
@@ -0,0 +1,18 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Description:
2
+ # CLIF wrappers for TensorFlow SavedModels.
3
+
4
+ load("//tensorflow/core/platform:build_config.bzl", "tf_py_clif_cc")
5
+
6
+ package(
7
+ # copybara:uncomment default_applicable_licenses = ["//tensorflow:license"],
8
+ default_visibility = ["//visibility:public"],
9
+ licenses = ["notice"],
10
+ )
11
+
12
+ tf_py_clif_cc(
13
+ name = "loader",
14
+ srcs = ["loader.clif"],
15
+ deps = [
16
+ "//tensorflow/cc/saved_model:loader",
17
+ ],
18
+ )
western_ai/usa/tensorflow/tensorflow/cc/saved_model/python/loader.clif ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ from "third_party/tensorflow/cc/saved_model/loader.h":
2
+ namespace `tensorflow`:
3
+ class SavedModelBundle:
4
+ def __init__(self)
western_ai/usa/tensorflow/tensorflow/cc/saved_model/reader.cc ADDED
@@ -0,0 +1,163 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /* Copyright 2018 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/cc/saved_model/reader.h"
17
+
18
+ #include <memory>
19
+ #include <string>
20
+ #include <unordered_set>
21
+ #include <utility>
22
+
23
+ #include "absl/memory/memory.h"
24
+ #include "absl/status/statusor.h"
25
+ #include "tensorflow/cc/saved_model/constants.h"
26
+ #include "tensorflow/cc/saved_model/metrics.h"
27
+ #include "tensorflow/cc/saved_model/util.h"
28
+ #include "tensorflow/core/framework/attr_value.pb.h"
29
+ #include "tensorflow/core/framework/function.pb.h"
30
+ #include "tensorflow/core/framework/graph.pb.h"
31
+ #include "tensorflow/core/framework/node_def.pb.h"
32
+ #include "tensorflow/core/framework/tensor.pb.h"
33
+ #include "tensorflow/core/lib/io/path.h"
34
+ #include "tensorflow/core/lib/strings/str_util.h"
35
+ #include "tensorflow/core/lib/strings/strcat.h"
36
+ #include "tensorflow/core/platform/env.h"
37
+ #include "tensorflow/core/platform/file_system_helper.h"
38
+ #include "tensorflow/core/platform/path.h"
39
+ #include "tensorflow/core/platform/statusor.h"
40
+ #include "tensorflow/core/platform/types.h"
41
+ #include "tensorflow/core/protobuf/meta_graph.pb.h"
42
+ #include "tensorflow/core/protobuf/saved_model.pb.h"
43
+ #include "tensorflow/core/util/tensor_bundle/byte_swap_tensor.h"
44
+ // Placeholder for protosplitter merger include.
45
+
46
+ #define IS_OSS true
47
+
48
+ namespace tensorflow {
49
+
50
+ absl::StatusOr<MetaGraphDef*> FindMetaGraphDef(
51
+ const std::unordered_set<string>& tags, SavedModel* saved_model_proto) {
52
+ LOG(INFO) << "Reading meta graph with tags { " << absl::StrJoin(tags, " ")
53
+ << " }";
54
+ for (MetaGraphDef& graph_def : *saved_model_proto->mutable_meta_graphs()) {
55
+ // Get tags from the graph_def.
56
+ std::unordered_set<string> graph_tags;
57
+ for (const string& tag : graph_def.meta_info_def().tags()) {
58
+ graph_tags.insert(tag);
59
+ }
60
+ // Match with the set of tags provided.
61
+ if (graph_tags == tags) {
62
+ MetaGraphDef* meta_graph_def = &graph_def;
63
+ // Correct the endiness of Tensor content on big-endian system
64
+ if (!port::kLittleEndian) {
65
+ TF_RETURN_IF_ERROR(ByteSwapTensorContentInMetaGraphDef(meta_graph_def));
66
+ }
67
+ return meta_graph_def;
68
+ }
69
+ }
70
+ return Status(
71
+ absl::StatusCode::kNotFound,
72
+ absl::StrCat(
73
+ "Could not find meta graph def matching supplied tags: { ",
74
+ absl::StrJoin(tags, " "),
75
+ " }. To inspect available tag-sets in the SavedModel, please "
76
+ "use the SavedModel CLI: `saved_model_cli`"));
77
+ }
78
+
79
+ // Reads the SavedModel proto from saved_model.pb in `export_dir`.
80
+ // Returns a failure status when the SavedModel file does not exist.
81
+ Status ReadSavedModel(absl::string_view export_dir,
82
+ SavedModel* saved_model_proto) {
83
+ LOG(INFO) << "Reading SavedModel from: " << export_dir;
84
+
85
+ if (IS_OSS) {
86
+ const std::string saved_model_pb_path =
87
+ io::JoinPath(export_dir, kSavedModelFilenamePb);
88
+ TF_ASSIGN_OR_RETURN(
89
+ bool saved_model_pb_exists,
90
+ internal::FileExists(Env::Default(), saved_model_pb_path));
91
+ if (saved_model_pb_exists) {
92
+ Status result = ReadBinaryProto(Env::Default(), saved_model_pb_path,
93
+ saved_model_proto);
94
+ if (result.ok()) {
95
+ metrics::SavedModelReadCount(
96
+ saved_model::GetWriteVersion(*saved_model_proto))
97
+ .IncrementBy(1);
98
+ }
99
+ return result;
100
+ }
101
+ }
102
+
103
+ const std::string saved_model_pbtxt_path =
104
+ io::JoinPath(export_dir, kSavedModelFilenamePbTxt);
105
+ auto saved_model_pbtxt_exists =
106
+ internal::FileExists(Env::Default(), saved_model_pbtxt_path);
107
+ if (saved_model_pbtxt_exists.value_or(false)) {
108
+ absl::Status result = ReadTextProto(Env::Default(), saved_model_pbtxt_path,
109
+ saved_model_proto);
110
+ if (result.ok()) {
111
+ metrics::SavedModelReadCount(
112
+ saved_model::GetWriteVersion(*saved_model_proto))
113
+ .IncrementBy(1);
114
+ }
115
+ return result;
116
+ }
117
+
118
+ if (!IS_OSS) {
119
+ // Only use Merger outside of OSS.
120
+ // Placeholder for protosplitter merger call.
121
+ }
122
+
123
+ return absl::Status(
124
+ absl::StatusCode::kNotFound,
125
+ absl::StrCat("Could not find SavedModel .pb or .pbtxt at supplied "
126
+ "export directory path: ",
127
+ export_dir,
128
+ ". Check that "
129
+ "the directory exists and that you have the right "
130
+ "permissions for accessing it."));
131
+ }
132
+
133
+ Status ReadMetaGraphDefFromSavedModel(absl::string_view export_dir,
134
+ const std::unordered_set<string>& tags,
135
+ MetaGraphDef* const meta_graph_def) {
136
+ SavedModel saved_model_proto;
137
+ TF_RETURN_IF_ERROR(ReadSavedModel(export_dir, &saved_model_proto));
138
+ TF_ASSIGN_OR_RETURN(MetaGraphDef * m,
139
+ FindMetaGraphDef(tags, &saved_model_proto));
140
+ *meta_graph_def = std::move(*m);
141
+ return absl::OkStatus();
142
+ }
143
+
144
+ absl::Status ReadSavedModelDebugInfoIfPresent(
145
+ absl::string_view export_dir,
146
+ std::unique_ptr<GraphDebugInfo>* debug_info_proto) {
147
+ LOG(INFO) << "Reading SavedModel debug info (if present) from: "
148
+ << export_dir;
149
+
150
+ const string debug_info_pb_path =
151
+ io::JoinPath(export_dir, "debug", "saved_model_debug_info.pb");
152
+ TF_ASSIGN_OR_RETURN(bool debug_info_pb_exists,
153
+ internal::FileExists(Env::Default(), debug_info_pb_path));
154
+ if (debug_info_pb_exists) {
155
+ GraphDebugInfo debug_info;
156
+ TF_RETURN_IF_ERROR(
157
+ ReadBinaryProto(Env::Default(), debug_info_pb_path, &debug_info));
158
+ *debug_info_proto = std::make_unique<GraphDebugInfo>(std::move(debug_info));
159
+ }
160
+ return absl::OkStatus();
161
+ }
162
+
163
+ } // namespace tensorflow
western_ai/usa/tensorflow/tensorflow/cc/saved_model/reader.h ADDED
@@ -0,0 +1,59 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /* Copyright 2018 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
+ /// Functions to read the SavedModel proto, or parts of it.
17
+
18
+ #ifndef TENSORFLOW_CC_SAVED_MODEL_READER_H_
19
+ #define TENSORFLOW_CC_SAVED_MODEL_READER_H_
20
+
21
+ #include <memory>
22
+ #include <unordered_set>
23
+
24
+ #include "absl/status/statusor.h"
25
+ #include "tensorflow/core/framework/graph_debug_info.pb.h"
26
+ #include "tensorflow/core/lib/core/status.h"
27
+ #include "tensorflow/core/platform/types.h"
28
+ #include "tensorflow/core/protobuf/meta_graph.pb.h"
29
+ #include "tensorflow/core/protobuf/saved_model.pb.h"
30
+
31
+ namespace tensorflow {
32
+ absl::Status ReadSavedModel(absl::string_view export_dir,
33
+ SavedModel* saved_model_proto);
34
+
35
+ // Finds and returns the MetaGraphDef (within the provided SavedModel) that
36
+ // matches the given set of tags. The lifetime of the returned MetaGraphDef is
37
+ // the same as the lifetime of `saved_model_proto`.
38
+ //
39
+ // FindMetaGraphDef returns a failure status when no MetaGraphDef matches the
40
+ // provided tags.
41
+ absl::StatusOr<MetaGraphDef*> FindMetaGraphDef(
42
+ const std::unordered_set<string>& tags, SavedModel* saved_model_proto);
43
+
44
+ // Reads the SavedModel proto from saved_model.pb(txt) in the given directory,
45
+ // finds the MetaGraphDef that matches the given set of tags and writes it to
46
+ // the `meta_graph_def` parameter. Returns a failure status when the SavedModel
47
+ // file does not exist or no MetaGraphDef matches the tags.
48
+ absl::Status ReadMetaGraphDefFromSavedModel(
49
+ absl::string_view export_dir, const std::unordered_set<string>& tags,
50
+ MetaGraphDef* meta_graph_def);
51
+
52
+ // Store debug info from the SavedModel export dir.
53
+ absl::Status ReadSavedModelDebugInfoIfPresent(
54
+ absl::string_view export_dir,
55
+ std::unique_ptr<GraphDebugInfo>* debug_info_proto);
56
+
57
+ } // namespace tensorflow
58
+
59
+ #endif // TENSORFLOW_CC_SAVED_MODEL_READER_H_
western_ai/usa/tensorflow/tensorflow/cc/saved_model/reader_test.cc ADDED
@@ -0,0 +1,143 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /* Copyright 2018 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/cc/saved_model/reader.h"
17
+
18
+ #include <gmock/gmock.h>
19
+ #include "tensorflow/cc/saved_model/constants.h"
20
+ #include "tensorflow/cc/saved_model/metrics.h"
21
+ #include "tensorflow/cc/saved_model/tag_constants.h"
22
+ #include "tensorflow/core/lib/core/status.h"
23
+ #include "tensorflow/core/lib/core/status_test_util.h"
24
+ #include "tensorflow/core/lib/io/path.h"
25
+ #include "tensorflow/core/lib/strings/str_util.h"
26
+ #include "tensorflow/core/platform/path.h"
27
+ #include "tensorflow/core/platform/resource_loader.h"
28
+
29
+ namespace tensorflow {
30
+ namespace {
31
+
32
+ string TestDataPbTxt() {
33
+ return io::JoinPath("tensorflow", "cc", "saved_model", "testdata",
34
+ "half_plus_two_pbtxt", "00000123");
35
+ }
36
+
37
+ string TestDataSharded() {
38
+ return io::JoinPath("tensorflow", "cc", "saved_model", "testdata",
39
+ "half_plus_two", "00000123");
40
+ }
41
+
42
+ string ChunkedSavedModel() {
43
+ return io::JoinPath("tensorflow", "cc", "saved_model", "testdata",
44
+ "chunked_saved_model", "chunked_model");
45
+ }
46
+
47
+ string NonChunkedSavedModel() {
48
+ return io::JoinPath("tensorflow", "cc", "saved_model", "testdata",
49
+ "chunked_saved_model", "non_chunked_model");
50
+ }
51
+
52
+ class ReaderTest : public ::testing::Test {
53
+ protected:
54
+ ReaderTest() {}
55
+
56
+ void CheckMetaGraphDef(const MetaGraphDef& meta_graph_def) {
57
+ const auto& tags = meta_graph_def.meta_info_def().tags();
58
+ EXPECT_TRUE(std::find(tags.begin(), tags.end(), kSavedModelTagServe) !=
59
+ tags.end());
60
+ EXPECT_NE(meta_graph_def.meta_info_def().tensorflow_version(), "");
61
+ EXPECT_EQ(
62
+ meta_graph_def.signature_def().at("serving_default").method_name(),
63
+ "tensorflow/serving/predict");
64
+ }
65
+ };
66
+
67
+ TEST_F(ReaderTest, TagMatch) {
68
+ MetaGraphDef meta_graph_def;
69
+
70
+ const string export_dir = GetDataDependencyFilepath(TestDataSharded());
71
+ TF_ASSERT_OK(ReadMetaGraphDefFromSavedModel(export_dir, {kSavedModelTagServe},
72
+ &meta_graph_def));
73
+ CheckMetaGraphDef(meta_graph_def);
74
+ }
75
+
76
+ TEST_F(ReaderTest, NoTagMatch) {
77
+ MetaGraphDef meta_graph_def;
78
+
79
+ const string export_dir = GetDataDependencyFilepath(TestDataSharded());
80
+ absl::Status st = ReadMetaGraphDefFromSavedModel(export_dir, {"missing-tag"},
81
+ &meta_graph_def);
82
+ EXPECT_FALSE(st.ok());
83
+ EXPECT_TRUE(absl::StrContains(
84
+ st.message(),
85
+ "Could not find meta graph def matching supplied tags: { missing-tag }"))
86
+ << st.message();
87
+ }
88
+
89
+ TEST_F(ReaderTest, NoTagMatchMultiple) {
90
+ MetaGraphDef meta_graph_def;
91
+
92
+ const string export_dir = GetDataDependencyFilepath(TestDataSharded());
93
+ absl::Status st = ReadMetaGraphDefFromSavedModel(
94
+ export_dir, {kSavedModelTagServe, "missing-tag"}, &meta_graph_def);
95
+ EXPECT_FALSE(st.ok());
96
+ EXPECT_TRUE(absl::StrContains(
97
+ st.message(), "Could not find meta graph def matching supplied tags: "))
98
+ << st.message();
99
+ }
100
+
101
+ TEST_F(ReaderTest, InvalidExportPath) {
102
+ MetaGraphDef meta_graph_def;
103
+
104
+ const string export_dir = GetDataDependencyFilepath("missing-path");
105
+ absl::Status st = ReadMetaGraphDefFromSavedModel(
106
+ export_dir, {kSavedModelTagServe}, &meta_graph_def);
107
+ EXPECT_FALSE(st.ok());
108
+ }
109
+
110
+ TEST_F(ReaderTest, ReadSavedModelDebugInfoIfPresent) {
111
+ const string export_dir = GetDataDependencyFilepath(TestDataSharded());
112
+ std::unique_ptr<GraphDebugInfo> debug_info_proto;
113
+ TF_ASSERT_OK(ReadSavedModelDebugInfoIfPresent(export_dir, &debug_info_proto));
114
+ }
115
+
116
+ TEST_F(ReaderTest, MetricsNotUpdatedFailedRead) {
117
+ MetaGraphDef meta_graph_def;
118
+ const int read_count_v1 = metrics::SavedModelReadCount("1").value();
119
+ const int read_count_v2 = metrics::SavedModelReadCount("2").value();
120
+
121
+ const string export_dir = GetDataDependencyFilepath("missing-path");
122
+ absl::Status st =
123
+ ReadMetaGraphDefFromSavedModel(export_dir, {"serve"}, &meta_graph_def);
124
+
125
+ EXPECT_FALSE(st.ok());
126
+ EXPECT_EQ(metrics::SavedModelReadCount("1").value(), read_count_v1);
127
+ EXPECT_EQ(metrics::SavedModelReadCount("2").value(), read_count_v2);
128
+ }
129
+
130
+ TEST_F(ReaderTest, MetricsUpdatedSuccessfulRead) {
131
+ MetaGraphDef meta_graph_def;
132
+ const int read_count_v1 = metrics::SavedModelReadCount("1").value();
133
+
134
+ const string export_dir = GetDataDependencyFilepath(TestDataSharded());
135
+ absl::Status st =
136
+ ReadMetaGraphDefFromSavedModel(export_dir, {"serve"}, &meta_graph_def);
137
+ EXPECT_EQ(metrics::SavedModelReadCount("1").value(), read_count_v1 + 1);
138
+ }
139
+
140
+ // Placeholder for protosplitter merger merge test.
141
+
142
+ } // namespace
143
+ } // namespace tensorflow
western_ai/usa/tensorflow/tensorflow/cc/saved_model/saved_model_bundle_lite_test.cc ADDED
@@ -0,0 +1,266 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /* Copyright 2016 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 "absl/status/status.h"
17
+ #include "tensorflow/cc/saved_model/constants.h"
18
+ #include "tensorflow/cc/saved_model/loader.h"
19
+ #include "tensorflow/cc/saved_model/signature_constants.h"
20
+ #include "tensorflow/cc/saved_model/tag_constants.h"
21
+ #include "tensorflow/core/example/example.pb.h"
22
+ #include "tensorflow/core/example/feature.pb.h"
23
+ #include "tensorflow/core/framework/tensor_testutil.h"
24
+ #include "tensorflow/core/lib/core/status.h"
25
+ #include "tensorflow/core/lib/core/status_test_util.h"
26
+ #include "tensorflow/core/lib/io/path.h"
27
+ #include "tensorflow/core/lib/strings/str_util.h"
28
+ #include "tensorflow/core/platform/test.h"
29
+ #include "tensorflow/core/protobuf/config.pb.h"
30
+
31
+ namespace tensorflow {
32
+ namespace {
33
+
34
+ constexpr char kTestDataPbTxt[] =
35
+ "cc/saved_model/testdata/half_plus_two_pbtxt/00000123";
36
+ constexpr char kTestDataMainOp[] =
37
+ "cc/saved_model/testdata/half_plus_two_main_op/00000123";
38
+ constexpr char kTestDataSharded[] =
39
+ "cc/saved_model/testdata/half_plus_two/00000123";
40
+ constexpr char kTestDataInitOpV2[] =
41
+ "cc/saved_model/testdata/half_plus_two_v2/00000123";
42
+
43
+ class LoaderTest : public ::testing::Test {
44
+ protected:
45
+ LoaderTest() {}
46
+
47
+ string MakeSerializedExample(float x) {
48
+ tensorflow::Example example;
49
+ auto* feature_map = example.mutable_features()->mutable_feature();
50
+ (*feature_map)["x"].mutable_float_list()->add_value(x);
51
+ return example.SerializeAsString();
52
+ }
53
+
54
+ void ValidateAssets(const string& export_dir,
55
+ const SavedModelBundleLite& bundle) {
56
+ const string asset_directory =
57
+ io::JoinPath(export_dir, kSavedModelAssetsDirectory);
58
+ const string asset_filename = "foo.txt";
59
+ const string asset_filepath = io::JoinPath(asset_directory, asset_filename);
60
+ TF_EXPECT_OK(Env::Default()->FileExists(asset_filepath));
61
+
62
+ std::vector<Tensor> path_outputs;
63
+ TF_ASSERT_OK(
64
+ bundle.GetSession()->Run({}, {"filename_tensor:0"}, {}, &path_outputs));
65
+ ASSERT_EQ(1, path_outputs.size());
66
+
67
+ test::ExpectTensorEqual<tstring>(
68
+ test::AsTensor<tstring>({"foo.txt"}, TensorShape({})), path_outputs[0]);
69
+ }
70
+
71
+ void CheckSavedModelBundle(const string& export_dir,
72
+ const SavedModelBundleLite& bundle) {
73
+ ValidateAssets(export_dir, bundle);
74
+ // Retrieve the regression signature from the bundle.
75
+ const auto& signature_def = bundle.GetSignatures().at("regress_x_to_y");
76
+
77
+ const string input_name = signature_def.inputs().at(kRegressInputs).name();
78
+ const string output_name =
79
+ signature_def.outputs().at(kRegressOutputs).name();
80
+
81
+ std::vector<tstring> serialized_examples;
82
+ for (float x : {0, 1, 2, 3}) {
83
+ serialized_examples.push_back(MakeSerializedExample(x));
84
+ }
85
+
86
+ // Validate the half plus two behavior.
87
+ Tensor input =
88
+ test::AsTensor<tstring>(serialized_examples, TensorShape({4}));
89
+ std::vector<Tensor> outputs;
90
+ TF_ASSERT_OK(bundle.GetSession()->Run({{input_name, input}}, {output_name},
91
+ {}, &outputs));
92
+ ASSERT_EQ(outputs.size(), 1);
93
+ test::ExpectTensorEqual<float>(
94
+ outputs[0],
95
+ test::AsTensor<float>({2, 2.5, 3, 3.5}, TensorShape({4, 1})));
96
+
97
+ // Validate the `output_partition_graphs` is not supported.
98
+ RunOptions run_options;
99
+ run_options.set_output_partition_graphs(true);
100
+ RunMetadata run_metadata;
101
+ absl::Status s =
102
+ bundle.GetSession()->Run(run_options, {{input_name, input}},
103
+ {output_name}, {}, &outputs, &run_metadata);
104
+ ASSERT_TRUE(absl::IsInvalidArgument(s));
105
+ }
106
+ };
107
+
108
+ // Test for resource leaks related to TensorFlow session closing requirements
109
+ // when loading and unloading large numbers of SavedModelBundles.
110
+ // TODO(sukritiramesh): Increase run iterations and move outside of the test
111
+ // suite.
112
+ TEST_F(LoaderTest, ResourceLeakTest) {
113
+ SavedModelBundleLite bundle;
114
+ SessionOptions session_options;
115
+ RunOptions run_options;
116
+
117
+ const string export_dir =
118
+ io::JoinPath(testing::TensorFlowSrcRoot(), kTestDataSharded);
119
+ for (int i = 0; i < 100; ++i) {
120
+ TF_ASSERT_OK(LoadSavedModel(session_options, run_options, export_dir,
121
+ {kSavedModelTagServe}, &bundle));
122
+ CheckSavedModelBundle(export_dir, bundle);
123
+ }
124
+ }
125
+
126
+ TEST_F(LoaderTest, ExtendFailsTest) {
127
+ SavedModelBundleLite bundle;
128
+ SessionOptions session_options;
129
+ RunOptions run_options;
130
+
131
+ const string export_dir =
132
+ io::JoinPath(testing::TensorFlowSrcRoot(), kTestDataSharded);
133
+ TF_ASSERT_OK(LoadSavedModel(session_options, run_options, export_dir,
134
+ {kSavedModelTagServe}, &bundle));
135
+ absl::Status s = bundle.GetSession()->Extend({});
136
+ ASSERT_TRUE(absl::IsUnimplemented(s));
137
+ }
138
+
139
+ TEST_F(LoaderTest, TagMatch) {
140
+ SavedModelBundleLite bundle;
141
+ SessionOptions session_options;
142
+ RunOptions run_options;
143
+
144
+ const string export_dir =
145
+ io::JoinPath(testing::TensorFlowSrcRoot(), kTestDataSharded);
146
+ TF_ASSERT_OK(LoadSavedModel(session_options, run_options, export_dir,
147
+ {kSavedModelTagServe}, &bundle));
148
+ CheckSavedModelBundle(export_dir, bundle);
149
+ }
150
+
151
+ TEST_F(LoaderTest, NoTagMatch) {
152
+ SavedModelBundleLite bundle;
153
+ RunOptions run_options;
154
+ SessionOptions session_options;
155
+
156
+ const string export_dir =
157
+ io::JoinPath(testing::TensorFlowSrcRoot(), kTestDataSharded);
158
+ absl::Status st = LoadSavedModel(session_options, run_options, export_dir,
159
+ {"missing-tag"}, &bundle);
160
+ EXPECT_FALSE(st.ok());
161
+ EXPECT_TRUE(absl::StrContains(
162
+ st.message(),
163
+ "Could not find meta graph def matching supplied tags: { missing-tag }"))
164
+ << st.message();
165
+ }
166
+
167
+ TEST_F(LoaderTest, NoTagMatchMultiple) {
168
+ SavedModelBundleLite bundle;
169
+ RunOptions run_options;
170
+ SessionOptions session_options;
171
+
172
+ const string export_dir =
173
+ io::JoinPath(testing::TensorFlowSrcRoot(), kTestDataSharded);
174
+ absl::Status st =
175
+ LoadSavedModel(session_options, run_options, export_dir,
176
+ {kSavedModelTagServe, "missing-tag"}, &bundle);
177
+ EXPECT_FALSE(st.ok());
178
+ EXPECT_TRUE(absl::StrContains(
179
+ st.message(), "Could not find meta graph def matching supplied tags: "))
180
+ << st.message();
181
+ }
182
+
183
+ TEST_F(LoaderTest, SessionCreationFailure) {
184
+ SavedModelBundleLite bundle;
185
+ // Use invalid SessionOptions to cause session creation to fail. Default
186
+ // options work, so provide an invalid value for the target field.
187
+ SessionOptions session_options;
188
+ constexpr char kInvalidTarget[] = "invalid target";
189
+ session_options.target = kInvalidTarget;
190
+ RunOptions run_options;
191
+
192
+ const string export_dir =
193
+ io::JoinPath(testing::TensorFlowSrcRoot(), kTestDataSharded);
194
+ absl::Status st = LoadSavedModel(session_options, run_options, export_dir,
195
+ {kSavedModelTagServe}, &bundle);
196
+ EXPECT_FALSE(st.ok());
197
+ EXPECT_TRUE(absl::StrContains(st.message(), kInvalidTarget)) << st.message();
198
+ }
199
+
200
+ TEST_F(LoaderTest, PbtxtFormat) {
201
+ SavedModelBundleLite bundle;
202
+ SessionOptions session_options;
203
+ RunOptions run_options;
204
+
205
+ const string export_dir =
206
+ io::JoinPath(testing::TensorFlowSrcRoot(), kTestDataPbTxt);
207
+ TF_ASSERT_OK(LoadSavedModel(session_options, run_options, export_dir,
208
+ {kSavedModelTagServe}, &bundle));
209
+ CheckSavedModelBundle(export_dir, bundle);
210
+ }
211
+
212
+ TEST_F(LoaderTest, MainOpFormat) {
213
+ SavedModelBundleLite bundle;
214
+ SessionOptions session_options;
215
+ RunOptions run_options;
216
+
217
+ const string export_dir =
218
+ io::JoinPath(testing::TensorFlowSrcRoot(), kTestDataMainOp);
219
+ TF_ASSERT_OK(LoadSavedModel(session_options, run_options, export_dir,
220
+ {kSavedModelTagServe}, &bundle));
221
+ CheckSavedModelBundle(export_dir, bundle);
222
+ }
223
+
224
+ TEST_F(LoaderTest, InvalidExportPath) {
225
+ SavedModelBundleLite bundle;
226
+ RunOptions run_options;
227
+ SessionOptions session_options;
228
+
229
+ const string export_dir =
230
+ io::JoinPath(testing::TensorFlowSrcRoot(), "missing-path");
231
+ absl::Status st = LoadSavedModel(session_options, run_options, export_dir,
232
+ {kSavedModelTagServe}, &bundle);
233
+ EXPECT_FALSE(st.ok());
234
+ }
235
+
236
+ TEST_F(LoaderTest, MaybeSavedModelDirectory) {
237
+ // Valid SavedModel directory.
238
+ const string export_dir =
239
+ io::JoinPath(testing::TensorFlowSrcRoot(), kTestDataSharded);
240
+ EXPECT_TRUE(MaybeSavedModelDirectory(export_dir));
241
+
242
+ // Directory that does not exist.
243
+ const string missing_export_dir =
244
+ io::JoinPath(testing::TensorFlowSrcRoot(), "missing-path");
245
+ EXPECT_FALSE(MaybeSavedModelDirectory(missing_export_dir));
246
+
247
+ // Directory that exists but is an invalid SavedModel location.
248
+ const string invalid_export_dir =
249
+ io::JoinPath(testing::TensorFlowSrcRoot(), "cc/saved_model");
250
+ EXPECT_FALSE(MaybeSavedModelDirectory(invalid_export_dir));
251
+ }
252
+
253
+ TEST_F(LoaderTest, SavedModelInitOpV2Format) {
254
+ SavedModelBundleLite bundle;
255
+ SessionOptions session_options;
256
+ RunOptions run_options;
257
+
258
+ const string export_dir =
259
+ io::JoinPath(testing::TensorFlowSrcRoot(), kTestDataInitOpV2);
260
+ TF_ASSERT_OK(LoadSavedModel(session_options, run_options, export_dir,
261
+ {kSavedModelTagServe}, &bundle));
262
+ CheckSavedModelBundle(export_dir, bundle);
263
+ }
264
+
265
+ } // namespace
266
+ } // namespace tensorflow
western_ai/usa/tensorflow/tensorflow/cc/saved_model/saved_model_bundle_test.cc ADDED
@@ -0,0 +1,405 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /* Copyright 2016 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/cc/saved_model/constants.h"
17
+ #include "tensorflow/cc/saved_model/loader.h"
18
+ #include "tensorflow/cc/saved_model/metrics.h"
19
+ #include "tensorflow/cc/saved_model/reader.h"
20
+ #include "tensorflow/cc/saved_model/signature_constants.h"
21
+ #include "tensorflow/cc/saved_model/tag_constants.h"
22
+ #include "tensorflow/core/example/example.pb.h"
23
+ #include "tensorflow/core/example/feature.pb.h"
24
+ #include "tensorflow/core/framework/tensor_testutil.h"
25
+ #include "tensorflow/core/lib/core/status.h"
26
+ #include "tensorflow/core/lib/core/status_test_util.h"
27
+ #include "tensorflow/core/lib/io/path.h"
28
+ #include "tensorflow/core/lib/strings/str_util.h"
29
+ #include "tensorflow/core/platform/test.h"
30
+ #include "tensorflow/core/protobuf/meta_graph.pb.h"
31
+
32
+ namespace tensorflow {
33
+ namespace {
34
+
35
+ constexpr char kTestDataPbTxt[] =
36
+ "cc/saved_model/testdata/half_plus_two_pbtxt/00000123";
37
+ constexpr char kTestDataMainOp[] =
38
+ "cc/saved_model/testdata/half_plus_two_main_op/00000123";
39
+ constexpr char kTestDataSharded[] =
40
+ "cc/saved_model/testdata/half_plus_two/00000123";
41
+ constexpr char kTestDataInitOpV2[] =
42
+ "cc/saved_model/testdata/half_plus_two_v2/00000123";
43
+ constexpr char kTestDataV2DebugInfo[] =
44
+ "cc/saved_model/testdata/x_plus_y_v2_debuginfo";
45
+ constexpr char kTestFuzzGeneratedNegativeShape[] =
46
+ "cc/saved_model/testdata/fuzz_generated/negative_shape";
47
+ constexpr char kTestFuzzGeneratedConstWithNoValue[] =
48
+ "cc/saved_model/testdata/fuzz_generated/const_with_no_value";
49
+ constexpr char kTestFuzzGeneratedBadNodeAttr[] =
50
+ "cc/saved_model/testdata/fuzz_generated/bad_node_attr";
51
+ constexpr char kTestCyclicModule[] = "cc/saved_model/testdata/CyclicModule";
52
+ constexpr char kTestSimpleV1Model[] = "cc/saved_model/testdata/SimpleV1Model";
53
+ constexpr char kVarsAndArithmeticObjectGraph[] =
54
+ "cc/saved_model/testdata/VarsAndArithmeticObjectGraph";
55
+ // This is the value in testdata/VarsAndArithmeticObjectGraph/fingerprint.pb
56
+ constexpr char kV2ModuleSavedModelChecksum[] = "15788619162413586750";
57
+
58
+ class LoaderTest : public ::testing::Test {
59
+ protected:
60
+ LoaderTest() {}
61
+
62
+ string MakeSerializedExample(float x) {
63
+ tensorflow::Example example;
64
+ auto* feature_map = example.mutable_features()->mutable_feature();
65
+ (*feature_map)["x"].mutable_float_list()->add_value(x);
66
+ return example.SerializeAsString();
67
+ }
68
+
69
+ void ValidateAssets(const string& export_dir,
70
+ const SavedModelBundle& bundle) {
71
+ const string asset_directory =
72
+ io::JoinPath(export_dir, kSavedModelAssetsDirectory);
73
+ const string asset_filename = "foo.txt";
74
+ const string asset_filepath = io::JoinPath(asset_directory, asset_filename);
75
+ TF_EXPECT_OK(Env::Default()->FileExists(asset_filepath));
76
+
77
+ std::vector<Tensor> path_outputs;
78
+ TF_ASSERT_OK(
79
+ bundle.session->Run({}, {"filename_tensor:0"}, {}, &path_outputs));
80
+ ASSERT_EQ(1, path_outputs.size());
81
+
82
+ test::ExpectTensorEqual<tstring>(
83
+ test::AsTensor<tstring>({"foo.txt"}, TensorShape({})), path_outputs[0]);
84
+ }
85
+
86
+ void CheckSavedModelBundle(const string& export_dir,
87
+ const SavedModelBundle& bundle) {
88
+ ValidateAssets(export_dir, bundle);
89
+ // Retrieve the regression signature from meta graph def.
90
+ const auto& signature_def = bundle.GetSignatures().at("regress_x_to_y");
91
+
92
+ const string input_name = signature_def.inputs().at(kRegressInputs).name();
93
+ const string output_name =
94
+ signature_def.outputs().at(kRegressOutputs).name();
95
+
96
+ std::vector<tstring> serialized_examples;
97
+ for (float x : {0, 1, 2, 3}) {
98
+ serialized_examples.push_back(MakeSerializedExample(x));
99
+ }
100
+
101
+ // Validate the half plus two behavior.
102
+ Tensor input =
103
+ test::AsTensor<tstring>(serialized_examples, TensorShape({4}));
104
+ std::vector<Tensor> outputs;
105
+ TF_ASSERT_OK(bundle.session->Run({{input_name, input}}, {output_name}, {},
106
+ &outputs));
107
+ ASSERT_EQ(outputs.size(), 1);
108
+ test::ExpectTensorEqual<float>(
109
+ outputs[0],
110
+ test::AsTensor<float>({2, 2.5, 3, 3.5}, TensorShape({4, 1})));
111
+ }
112
+ };
113
+
114
+ // Test for resource leaks related to TensorFlow session closing requirements
115
+ // when loading and unloading large numbers of SavedModelBundles.
116
+ // TODO(sukritiramesh): Increase run iterations and move outside of the test
117
+ // suite.
118
+ TEST_F(LoaderTest, ResourceLeakTest) {
119
+ SavedModelBundle bundle;
120
+ SessionOptions session_options;
121
+ RunOptions run_options;
122
+
123
+ const string export_dir =
124
+ io::JoinPath(testing::TensorFlowSrcRoot(), kTestDataSharded);
125
+ for (int i = 0; i < 100; ++i) {
126
+ TF_ASSERT_OK(LoadSavedModel(session_options, run_options, export_dir,
127
+ {kSavedModelTagServe}, &bundle));
128
+ CheckSavedModelBundle(export_dir, bundle);
129
+ }
130
+ }
131
+
132
+ TEST_F(LoaderTest, TagMatch) {
133
+ SavedModelBundle bundle;
134
+ SessionOptions session_options;
135
+ RunOptions run_options;
136
+
137
+ const string export_dir =
138
+ io::JoinPath(testing::TensorFlowSrcRoot(), kTestDataSharded);
139
+ TF_ASSERT_OK(LoadSavedModel(session_options, run_options, export_dir,
140
+ {kSavedModelTagServe}, &bundle));
141
+ CheckSavedModelBundle(export_dir, bundle);
142
+ }
143
+
144
+ TEST_F(LoaderTest, ReadMetaGraphFromSavedModel) {
145
+ SavedModelBundle bundle;
146
+ SessionOptions session_options;
147
+ RunOptions run_options;
148
+
149
+ const string export_dir =
150
+ io::JoinPath(testing::TensorFlowSrcRoot(), kTestDataSharded);
151
+ TF_ASSERT_OK(LoadSavedModel(session_options, run_options, export_dir,
152
+ {kSavedModelTagServe}, &bundle));
153
+ MetaGraphDef actual_metagraph;
154
+ TF_ASSERT_OK(ReadMetaGraphDefFromSavedModel(export_dir, {kSavedModelTagServe},
155
+ &actual_metagraph));
156
+ EXPECT_EQ(actual_metagraph.DebugString(),
157
+ bundle.meta_graph_def.DebugString());
158
+ }
159
+
160
+ TEST_F(LoaderTest, RestoreSession) {
161
+ SavedModelBundle bundle;
162
+ SessionOptions session_options;
163
+ RunOptions run_options;
164
+
165
+ const string export_dir =
166
+ io::JoinPath(testing::TensorFlowSrcRoot(), kTestDataSharded);
167
+ TF_ASSERT_OK(LoadSavedModel(session_options, run_options, export_dir,
168
+ {kSavedModelTagServe}, &bundle));
169
+
170
+ SavedModelBundle actual_bundle;
171
+ const std::unordered_set<std::string> tags = {kSavedModelTagServe};
172
+ TF_ASSERT_OK(ReadMetaGraphDefFromSavedModel(export_dir, tags,
173
+ &actual_bundle.meta_graph_def));
174
+ TF_ASSERT_OK(LoadMetagraphIntoSession(
175
+ session_options, actual_bundle.meta_graph_def, &actual_bundle.session));
176
+ TF_ASSERT_OK(RestoreSession(run_options, actual_bundle.meta_graph_def,
177
+ export_dir, &actual_bundle.session));
178
+ CheckSavedModelBundle(export_dir, actual_bundle);
179
+ }
180
+
181
+ TEST_F(LoaderTest, NoTagMatch) {
182
+ SavedModelBundle bundle;
183
+ RunOptions run_options;
184
+ SessionOptions session_options;
185
+
186
+ const string export_dir =
187
+ io::JoinPath(testing::TensorFlowSrcRoot(), kTestDataSharded);
188
+ absl::Status st = LoadSavedModel(session_options, run_options, export_dir,
189
+ {"missing-tag"}, &bundle);
190
+ EXPECT_FALSE(st.ok());
191
+ EXPECT_TRUE(absl::StrContains(
192
+ st.message(),
193
+ "Could not find meta graph def matching supplied tags: { missing-tag }"))
194
+ << st.message();
195
+ }
196
+
197
+ TEST_F(LoaderTest, NoTagMatchMultiple) {
198
+ SavedModelBundle bundle;
199
+ RunOptions run_options;
200
+ SessionOptions session_options;
201
+
202
+ const string export_dir =
203
+ io::JoinPath(testing::TensorFlowSrcRoot(), kTestDataSharded);
204
+ absl::Status st =
205
+ LoadSavedModel(session_options, run_options, export_dir,
206
+ {kSavedModelTagServe, "missing-tag"}, &bundle);
207
+ EXPECT_FALSE(st.ok());
208
+ EXPECT_TRUE(absl::StrContains(
209
+ st.message(), "Could not find meta graph def matching supplied tags: "))
210
+ << st.message();
211
+ }
212
+
213
+ TEST_F(LoaderTest, SessionCreationFailure) {
214
+ SavedModelBundle bundle;
215
+ // Use invalid SessionOptions to cause session creation to fail. Default
216
+ // options work, so provide an invalid value for the target field.
217
+ SessionOptions session_options;
218
+ constexpr char kInvalidTarget[] = "invalid target";
219
+ session_options.target = kInvalidTarget;
220
+ RunOptions run_options;
221
+
222
+ const string export_dir =
223
+ io::JoinPath(testing::TensorFlowSrcRoot(), kTestDataSharded);
224
+ absl::Status st = LoadSavedModel(session_options, run_options, export_dir,
225
+ {kSavedModelTagServe}, &bundle);
226
+ EXPECT_FALSE(st.ok());
227
+ EXPECT_TRUE(absl::StrContains(st.message(), kInvalidTarget)) << st.message();
228
+ }
229
+
230
+ TEST_F(LoaderTest, PbtxtFormat) {
231
+ SavedModelBundle bundle;
232
+ SessionOptions session_options;
233
+ RunOptions run_options;
234
+
235
+ const string export_dir =
236
+ io::JoinPath(testing::TensorFlowSrcRoot(), kTestDataPbTxt);
237
+ TF_ASSERT_OK(LoadSavedModel(session_options, run_options, export_dir,
238
+ {kSavedModelTagServe}, &bundle));
239
+ CheckSavedModelBundle(export_dir, bundle);
240
+ }
241
+
242
+ TEST_F(LoaderTest, MainOpFormat) {
243
+ SavedModelBundle bundle;
244
+ SessionOptions session_options;
245
+ RunOptions run_options;
246
+
247
+ const string export_dir =
248
+ io::JoinPath(testing::TensorFlowSrcRoot(), kTestDataMainOp);
249
+ TF_ASSERT_OK(LoadSavedModel(session_options, run_options, export_dir,
250
+ {kSavedModelTagServe}, &bundle));
251
+ CheckSavedModelBundle(export_dir, bundle);
252
+ }
253
+
254
+ TEST_F(LoaderTest, InvalidExportPath) {
255
+ SavedModelBundle bundle;
256
+ RunOptions run_options;
257
+ SessionOptions session_options;
258
+
259
+ const string export_dir =
260
+ io::JoinPath(testing::TensorFlowSrcRoot(), "missing-path");
261
+ absl::Status st = LoadSavedModel(session_options, run_options, export_dir,
262
+ {kSavedModelTagServe}, &bundle);
263
+ EXPECT_FALSE(st.ok());
264
+ }
265
+
266
+ TEST_F(LoaderTest, MaybeSavedModelDirectory) {
267
+ // Valid SavedModel directory.
268
+ const string export_dir =
269
+ io::JoinPath(testing::TensorFlowSrcRoot(), kTestDataSharded);
270
+ EXPECT_TRUE(MaybeSavedModelDirectory(export_dir));
271
+
272
+ // Directory that does not exist.
273
+ const string missing_export_dir =
274
+ io::JoinPath(testing::TensorFlowSrcRoot(), "missing-path");
275
+ EXPECT_FALSE(MaybeSavedModelDirectory(missing_export_dir));
276
+
277
+ // Directory that exists but is an invalid SavedModel location.
278
+ const string invalid_export_dir =
279
+ io::JoinPath(testing::TensorFlowSrcRoot(), "cc/saved_model");
280
+ EXPECT_FALSE(MaybeSavedModelDirectory(invalid_export_dir));
281
+ }
282
+
283
+ TEST_F(LoaderTest, SavedModelInitOpV2Format) {
284
+ SavedModelBundle bundle;
285
+ SessionOptions session_options;
286
+ RunOptions run_options;
287
+
288
+ const string export_dir =
289
+ io::JoinPath(testing::TensorFlowSrcRoot(), kTestDataInitOpV2);
290
+ TF_ASSERT_OK(LoadSavedModel(session_options, run_options, export_dir,
291
+ {kSavedModelTagServe}, &bundle));
292
+ CheckSavedModelBundle(export_dir, bundle);
293
+ }
294
+
295
+ TEST_F(LoaderTest, SavedModelV2DebugInfo) {
296
+ SavedModelBundle bundle;
297
+ SessionOptions session_options;
298
+ RunOptions run_options;
299
+
300
+ const string export_dir =
301
+ io::JoinPath(testing::TensorFlowSrcRoot(), kTestDataV2DebugInfo);
302
+ TF_ASSERT_OK(LoadSavedModel(session_options, run_options, export_dir,
303
+ {kSavedModelTagServe}, &bundle));
304
+
305
+ // This SavedModel has debug info, so we should have loaded it.
306
+ EXPECT_NE(bundle.debug_info.get(), nullptr);
307
+ }
308
+
309
+ TEST_F(LoaderTest, NegativeShapeDimension) {
310
+ SavedModelBundle bundle;
311
+ RunOptions run_options;
312
+ SessionOptions session_options;
313
+
314
+ const string export_dir = io::JoinPath(testing::TensorFlowSrcRoot(),
315
+ kTestFuzzGeneratedNegativeShape);
316
+ absl::Status st = LoadSavedModel(session_options, run_options, export_dir,
317
+ {kSavedModelTagServe}, &bundle);
318
+ EXPECT_FALSE(st.ok());
319
+ EXPECT_NE(st.message().find("initializes from a tensor with -1 elements"),
320
+ std::string::npos);
321
+ }
322
+
323
+ TEST_F(LoaderTest, ConstNoValue) {
324
+ SavedModelBundle bundle;
325
+ RunOptions run_options;
326
+ SessionOptions session_options;
327
+
328
+ const string export_dir = io::JoinPath(testing::TensorFlowSrcRoot(),
329
+ kTestFuzzGeneratedConstWithNoValue);
330
+ absl::Status st = LoadSavedModel(session_options, run_options, export_dir,
331
+ {kSavedModelTagServe}, &bundle);
332
+ EXPECT_FALSE(st.ok());
333
+ EXPECT_NE(st.message().find("constant tensor but no value has been provided"),
334
+ std::string::npos);
335
+ }
336
+
337
+ TEST_F(LoaderTest, BadNodeAttr) {
338
+ SavedModelBundle bundle;
339
+ RunOptions run_options;
340
+ SessionOptions session_options;
341
+
342
+ const string export_dir =
343
+ io::JoinPath(testing::TensorFlowSrcRoot(), kTestFuzzGeneratedBadNodeAttr);
344
+ absl::Status st = LoadSavedModel(session_options, run_options, export_dir,
345
+ {kSavedModelTagServe}, &bundle);
346
+ EXPECT_FALSE(st.ok());
347
+ EXPECT_NE(st.message().find("constant tensor but no value has been provided"),
348
+ std::string::npos);
349
+ }
350
+
351
+ TEST_F(LoaderTest, UpdateMetricsV2) {
352
+ SavedModelBundle bundle;
353
+ SessionOptions session_options;
354
+ RunOptions run_options;
355
+ const string kCCLoadLabel = "cc_load";
356
+
357
+ const int read_count_v2 = metrics::SavedModelReadCount("2").value();
358
+ const int api_count = metrics::SavedModelReadApi(kCCLoadLabel).value();
359
+ const string export_dir =
360
+ io::JoinPath(testing::TensorFlowSrcRoot(), kTestCyclicModule);
361
+ TF_ASSERT_OK(LoadSavedModel(session_options, run_options, export_dir,
362
+ {kSavedModelTagServe}, &bundle));
363
+
364
+ EXPECT_EQ(metrics::SavedModelReadCount("2").value(), read_count_v2 + 1);
365
+ EXPECT_EQ(metrics::SavedModelReadApi(kCCLoadLabel).value(), api_count + 1);
366
+ }
367
+
368
+ TEST_F(LoaderTest, UpdateMetricsV1) {
369
+ SavedModelBundle bundle;
370
+ SessionOptions session_options;
371
+ RunOptions run_options;
372
+ const string kCCLoadLabel = "cc_load";
373
+
374
+ const int read_count_v1 = metrics::SavedModelReadCount("1").value();
375
+ const int read_count_v2 = metrics::SavedModelReadCount("2").value();
376
+
377
+ const int api_count = metrics::SavedModelReadApi(kCCLoadLabel).value();
378
+ const string export_dir =
379
+ io::JoinPath(testing::TensorFlowSrcRoot(), kTestSimpleV1Model);
380
+ TF_ASSERT_OK(LoadSavedModel(session_options, run_options, export_dir,
381
+ {kSavedModelTagServe}, &bundle));
382
+
383
+ EXPECT_EQ(metrics::SavedModelReadCount("1").value(), read_count_v1 + 1);
384
+ EXPECT_EQ(metrics::SavedModelReadCount("2").value(), read_count_v2);
385
+ EXPECT_EQ(metrics::SavedModelReadApi(kCCLoadLabel).value(), api_count + 1);
386
+ }
387
+
388
+ TEST_F(LoaderTest, UpdateFingerprintMetrics) {
389
+ SavedModelBundle bundle;
390
+ SessionOptions session_options;
391
+ RunOptions run_options;
392
+
393
+ const string export_dir =
394
+ io::JoinPath(testing::TensorFlowSrcRoot(), kVarsAndArithmeticObjectGraph);
395
+ TF_ASSERT_OK(LoadSavedModel(session_options, run_options, export_dir,
396
+ {kSavedModelTagServe}, &bundle));
397
+
398
+ EXPECT_EQ(metrics::SavedModelReadPath().value(), export_dir);
399
+
400
+ EXPECT_EQ(metrics::SavedModelReadFingerprint().value(),
401
+ kV2ModuleSavedModelChecksum);
402
+ }
403
+
404
+ } // namespace
405
+ } // namespace tensorflow
western_ai/usa/tensorflow/tensorflow/cc/saved_model/saved_model_fuzz.cc ADDED
@@ -0,0 +1,44 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /* Copyright 2023 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 <memory>
17
+
18
+ #include "fuzztest/fuzztest.h"
19
+ #include "tensorflow/cc/saved_model/constants.h"
20
+ #include "tensorflow/cc/saved_model/loader.h"
21
+ #include "tensorflow/cc/saved_model/tag_constants.h"
22
+ #include "tensorflow/core/protobuf/saved_model.pb.h"
23
+ #include "tensorflow/core/public/session_options.h"
24
+
25
+ namespace tensorflow::fuzzing {
26
+ namespace {
27
+
28
+ void FuzzLoadSavedModel(const SavedModel& model) {
29
+ SavedModelBundleLite bundle;
30
+ SessionOptions session_options;
31
+ RunOptions run_options;
32
+
33
+ string export_dir = "ram://";
34
+ TF_CHECK_OK(tsl::WriteBinaryProto(tensorflow::Env::Default(),
35
+ export_dir + kSavedModelFilenamePb, model));
36
+
37
+ LoadSavedModel(session_options, run_options, export_dir,
38
+ {kSavedModelTagServe}, &bundle)
39
+ .IgnoreError();
40
+ }
41
+ FUZZ_TEST(SavedModelFuzz, FuzzLoadSavedModel);
42
+
43
+ } // namespace
44
+ } // namespace tensorflow::fuzzing
western_ai/usa/tensorflow/tensorflow/cc/saved_model/signature_constants.h ADDED
@@ -0,0 +1,69 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /* Copyright 2016 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_SIGNATURE_CONSTANTS_H_
17
+ #define TENSORFLOW_CC_SAVED_MODEL_SIGNATURE_CONSTANTS_H_
18
+
19
+ namespace tensorflow {
20
+
21
+ /// Key in the signature def map for `default` serving signatures. The default
22
+ /// signature is used in inference requests where a specific signature was not
23
+ /// specified.
24
+ static constexpr char kDefaultServingSignatureDefKey[] = "serving_default";
25
+
26
+ ////////////////////////////////////////////////////////////////////////////////
27
+ /// Classification API constants.
28
+
29
+ /// Classification inputs.
30
+ static constexpr char kClassifyInputs[] = "inputs";
31
+
32
+ /// Classification method name used in a SignatureDef.
33
+ static constexpr char kClassifyMethodName[] = "tensorflow/serving/classify";
34
+
35
+ /// Classification classes output.
36
+ static constexpr char kClassifyOutputClasses[] = "classes";
37
+
38
+ /// Classification scores output.
39
+ static constexpr char kClassifyOutputScores[] = "scores";
40
+
41
+ ////////////////////////////////////////////////////////////////////////////////
42
+ /// Predict API constants.
43
+
44
+ /// Predict inputs.
45
+ static constexpr char kPredictInputs[] = "inputs";
46
+
47
+ /// Predict method name used in a SignatureDef.
48
+ static constexpr char kPredictMethodName[] = "tensorflow/serving/predict";
49
+
50
+ /// Predict outputs.
51
+ static constexpr char kPredictOutputs[] = "outputs";
52
+
53
+ ////////////////////////////////////////////////////////////////////////////////
54
+ /// Regression API constants.
55
+
56
+ /// Regression inputs.
57
+ static constexpr char kRegressInputs[] = "inputs";
58
+
59
+ /// Regression method name used in a SignatureDef.
60
+ static constexpr char kRegressMethodName[] = "tensorflow/serving/regress";
61
+
62
+ /// Regression outputs.
63
+ static constexpr char kRegressOutputs[] = "outputs";
64
+
65
+ ////////////////////////////////////////////////////////////////////////////////
66
+
67
+ } // namespace tensorflow
68
+
69
+ #endif // TENSORFLOW_CC_SAVED_MODEL_SIGNATURE_CONSTANTS_H_
western_ai/usa/tensorflow/tensorflow/cc/saved_model/tag_constants.h ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /* Copyright 2016 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_TAG_CONSTANTS_H_
17
+ #define TENSORFLOW_CC_SAVED_MODEL_TAG_CONSTANTS_H_
18
+
19
+ namespace tensorflow {
20
+
21
+ /// Tag for the `gpu` graph.
22
+ constexpr char kSavedModelTagGpu[] = "gpu";
23
+
24
+ /// Tag for the `tpu` graph.
25
+ constexpr char kSavedModelTagTpu[] = "tpu";
26
+
27
+ /// Tag for the `serving` graph.
28
+ constexpr char kSavedModelTagServe[] = "serve";
29
+
30
+ /// Tag for the `training` graph.
31
+ constexpr char kSavedModelTagTrain[] = "train";
32
+
33
+ } // namespace tensorflow
34
+
35
+ #endif // TENSORFLOW_CC_SAVED_MODEL_TAG_CONSTANTS_H_
western_ai/usa/tensorflow/tensorflow/cc/saved_model/test_utils.h ADDED
@@ -0,0 +1,53 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /* Copyright 2021 The TensorFlow Authors. All Rights Reserved.
2
+ Licensed under the Apache License, Version 2.0 (the "License");
3
+ you may not use this file except in compliance with the License.
4
+ You may obtain a copy of the License at
5
+ http://www.apache.org/licenses/LICENSE-2.0
6
+ Unless required by applicable law or agreed to in writing, software
7
+ distributed under the License is distributed on an "AS IS" BASIS,
8
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
9
+ See the License for the specific language governing permissions and
10
+ limitations under the License.
11
+ ==============================================================================*/
12
+ #ifndef TENSORFLOW_CC_SAVED_MODEL_TEST_UTILS_H_
13
+ #define TENSORFLOW_CC_SAVED_MODEL_TEST_UTILS_H_
14
+
15
+ #include <ostream>
16
+ #include <string>
17
+
18
+ #include "tensorflow/core/platform/protobuf.h"
19
+ #include "tensorflow/core/platform/test.h"
20
+
21
+ namespace tensorflow::saved_model {
22
+
23
+ // TODO(b/229726259) Switch to OSS version after it's available.
24
+ // Simple implementation of a proto matcher comparing string representations.
25
+ // Only works as ShapeProto's textual representation is deterministic.
26
+ class ProtoStringMatcher {
27
+ public:
28
+ explicit ProtoStringMatcher(const tensorflow::protobuf::Message& expected)
29
+ : expected_(expected.DebugString()) {}
30
+
31
+ template <typename Message>
32
+ bool MatchAndExplain(const Message& p,
33
+ ::testing::MatchResultListener*) const {
34
+ return p.DebugString() == expected_;
35
+ }
36
+
37
+ void DescribeTo(::std::ostream* os) const { *os << expected_; }
38
+ void DescribeNegationTo(::std::ostream* os) const {
39
+ *os << "not equal to expected message: " << expected_;
40
+ }
41
+
42
+ private:
43
+ const std::string expected_;
44
+ };
45
+
46
+ inline ::testing::PolymorphicMatcher<ProtoStringMatcher> EqualsProto(
47
+ const tensorflow::protobuf::Message& x) {
48
+ return ::testing::MakePolymorphicMatcher(ProtoStringMatcher(x));
49
+ }
50
+
51
+ } // namespace tensorflow::saved_model
52
+
53
+ #endif // TENSORFLOW_CC_SAVED_MODEL_TEST_UTILS_H_
western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/AssetModule/assets/test_asset.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ TEST ASSET FILE CONTENTS
western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/AssetModule/saved_model.pb ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:1fd1d22913556f2cab68ebf061c6f6ce2621acf431089882612e20502618d362
3
+ size 8248
western_ai/usa/tensorflow/tensorflow/cc/saved_model/testdata/AssetModule/variables/variables.data-00000-of-00001 ADDED
Binary file (38 Bytes). View file