Remove v1 decorators from saved_model:saved_model_test.

Saved model tests care about building the right graphs, so they were
forced into graph mode (instead of being ported to tf.function
infrastructure). There are differences in the graphs produced between
v1 and v2, but those can be handled easily - for testing purposes -
by carefully choosing operation names based on the mode.

There were unneeded collection operations in a few tests, which were
removed. Other small changes were made but mostly tests were forced to
run in graph mode.

PiperOrigin-RevId: 324289722
Change-Id: Iad60aac85cc3954755a9c6a35cf5a4ddb42640ed
This commit is contained in:
Cesar Crusius 2020-07-31 14:52:21 -07:00 committed by TensorFlower Gardener
parent e3ac0822d8
commit 0bf6bb64ce

View File

@ -19,6 +19,7 @@ from __future__ import division
from __future__ import print_function
import os
import six
from tensorflow.core.framework import types_pb2
from tensorflow.core.protobuf import config_pb2
@ -34,6 +35,7 @@ from tensorflow.python.lib.io import file_io
from tensorflow.python.ops import control_flow_ops
from tensorflow.python.ops import math_ops
from tensorflow.python.ops import state_ops
from tensorflow.python.ops import variable_scope
from tensorflow.python.ops import variables
from tensorflow.python.ops.ragged import ragged_factory_ops
from tensorflow.python.platform import test
@ -80,6 +82,26 @@ class SavedModelTestBase(test.TestCase):
asset_collection = ops.get_collection(ops.GraphKeys.ASSET_FILEPATHS)
return asset_collection
def _eval(self, tensor):
"""Evaluate a tensor.
Takes care of the variations between graphs produced with and without
resource variables when determining the name of the operation to run.
Args:
tensor: The tensor to evaluate, or a string with the tensor name.
Returns:
The evaluated tensor as a numpy array.
"""
name = tensor if isinstance(tensor, six.string_types) else tensor.name
index = "0"
if ":" in name:
name, index = name.split(":")
if variable_scope.resource_variables_enabled():
name = name + "/Read/ReadVariableOp"
return self.evaluate(name + ":" + index)
class SavedModelTest(SavedModelTestBase):
@ -119,12 +141,10 @@ class SavedModelTest(SavedModelTestBase):
with self.session(graph=ops.Graph()) as sess:
self._init_and_validate_variable(sess, "v", 42)
foo_signature = signature_def_utils.build_signature_def({
"foo_inputs": tensor_info
}, dict(), "foo")
foo_signature = signature_def_utils.build_signature_def(
{"foo_inputs": tensor_info}, dict(), "foo")
builder.add_meta_graph_and_variables(
sess, ["foo"],
signature_def_map={"foo_key": foo_signature})
sess, ["foo"], signature_def_map={"foo_key": foo_signature})
def _validate_outputs_tensor_info_fail(self, builder, tensor_info):
with self.session(graph=ops.Graph()) as sess:
@ -145,8 +165,7 @@ class SavedModelTest(SavedModelTestBase):
foo_signature = signature_def_utils.build_signature_def(
dict(), {"foo_outputs": tensor_info}, "foo")
builder.add_meta_graph_and_variables(
sess, ["foo"],
signature_def_map={"foo_key": foo_signature})
sess, ["foo"], signature_def_map={"foo_key": foo_signature})
def _validate_sig_def_keys(self, builder, valid_tensor_info, invalid_key):
with self.session(graph=ops.Graph()) as sess:
@ -201,11 +220,11 @@ class SavedModelTest(SavedModelTestBase):
"Cannot parse file.*%s" % constants.SAVED_MODEL_FILENAME_PBTXT):
loader.load(sess, ["foo"], export_dir)
@test_util.run_deprecated_v1
def testVerifySessionGraphUsage(self):
export_dir = self._get_export_dir("test_verify_session_graph_usage")
builder = saved_model_builder._SavedModelBuilder(export_dir)
with ops.Graph().as_default():
with self.session(graph=ops.Graph()) as sess:
self._init_and_validate_variable(sess, "v", 42)
builder.add_meta_graph_and_variables(sess, [tag_constants.TRAINING])
@ -220,31 +239,33 @@ class SavedModelTest(SavedModelTestBase):
# Check the variable within the scope of the session and its graph.
with sess:
self.assertEqual(
42, ops.get_collection(ops.GraphKeys.GLOBAL_VARIABLES)[0].eval())
42,
self._eval(ops.get_collection(ops.GraphKeys.GLOBAL_VARIABLES)[0]))
@test_util.run_deprecated_v1
def testSequence(self):
export_dir = self._get_export_dir("test_sequence")
builder = saved_model_builder._SavedModelBuilder(export_dir)
with ops.Graph().as_default():
# Expect an assertion error since add_meta_graph_and_variables() should be
# invoked before any add_meta_graph() calls.
with self.session(graph=ops.Graph()) as sess:
self.assertRaises(AssertionError, builder.add_meta_graph, ["foo"])
# Expect an assertion error for multiple calls of
# add_meta_graph_and_variables() since weights should be saved exactly once.
# add_meta_graph_and_variables() since weights should be saved exactly
# once.
with self.session(graph=ops.Graph()) as sess:
self._init_and_validate_variable(sess, "v", 42)
builder.add_meta_graph_and_variables(sess, ["bar"])
self.assertRaises(AssertionError, builder.add_meta_graph_and_variables,
sess, ["baz"])
@test_util.run_deprecated_v1
def testTags(self):
export_dir = self._get_export_dir("test_tags")
builder = saved_model_builder._SavedModelBuilder(export_dir)
with ops.Graph().as_default():
# Graph with a single variable. SavedModel invoked to:
# - add with weights.
# - a single tag (from predefined constants).
@ -283,58 +304,66 @@ class SavedModelTest(SavedModelTestBase):
# Save the SavedModel to disk.
builder.save()
# Restore the graph with a single predefined tag whose variables were saved.
# Restore the graph with a single predefined tag whose variables were
# saved.
with self.session(graph=ops.Graph()) as sess:
loader.load(sess, [tag_constants.TRAINING], export_dir)
self.assertEqual(
42, ops.get_collection(ops.GraphKeys.GLOBAL_VARIABLES)[0].eval())
42,
self._eval(ops.get_collection(ops.GraphKeys.GLOBAL_VARIABLES)[0]))
# Restore the graph with a single predefined tag whose variables were not
# saved.
with self.session(graph=ops.Graph()) as sess:
loader.load(sess, [tag_constants.SERVING], export_dir)
self.assertEqual(
42, ops.get_collection(ops.GraphKeys.GLOBAL_VARIABLES)[0].eval())
42,
self._eval(ops.get_collection(ops.GraphKeys.GLOBAL_VARIABLES)[0]))
# Restore the graph with multiple predefined tags whose variables were not
# saved.
with self.session(graph=ops.Graph()) as sess:
loader.load(sess, [tag_constants.SERVING, tag_constants.GPU], export_dir)
loader.load(sess, [tag_constants.SERVING, tag_constants.GPU],
export_dir)
self.assertEqual(
42, ops.get_collection(ops.GraphKeys.GLOBAL_VARIABLES)[0].eval())
42,
self._eval(ops.get_collection(ops.GraphKeys.GLOBAL_VARIABLES)[0]))
# Restore the graph with multiple predefined tags (for serving on TPU)
# whose variables were not saved.
with self.session(graph=ops.Graph()) as sess:
loader.load(sess, [tag_constants.SERVING, tag_constants.TPU], export_dir)
loader.load(sess, [tag_constants.SERVING, tag_constants.TPU],
export_dir)
self.assertEqual(
42, ops.get_collection(ops.GraphKeys.GLOBAL_VARIABLES)[0].eval())
42,
self._eval(ops.get_collection(ops.GraphKeys.GLOBAL_VARIABLES)[0]))
# Restore the graph with multiple tags. Provide duplicate tags to test set
# semantics.
with self.session(graph=ops.Graph()) as sess:
loader.load(sess, ["foo", "bar", "foo"], export_dir)
self.assertEqual(
42, ops.get_collection(ops.GraphKeys.GLOBAL_VARIABLES)[0].eval())
42,
self._eval(ops.get_collection(ops.GraphKeys.GLOBAL_VARIABLES)[0]))
# Try restoring a graph with a non-existent tag. This should yield a runtime
# error.
# Try restoring a graph with a non-existent tag. This should yield a
# runtime error.
with self.session(graph=ops.Graph()) as sess:
self.assertRaises(RuntimeError, loader.load, sess, ["INVALID"],
export_dir)
# Try restoring a graph where a subset of the tags match. Since tag matching
# for meta graph defs follows "all" semantics, this should yield a runtime
# error.
# Try restoring a graph where a subset of the tags match. Since tag
# matching for meta graph defs follows "all" semantics, this should yield
# a runtime error.
with self.session(graph=ops.Graph()) as sess:
self.assertRaises(RuntimeError, loader.load, sess, ["foo", "baz"],
export_dir)
@test_util.run_v1_only("b/120545219")
def testVariables(self):
export_dir = self._get_export_dir("test_variables")
builder = saved_model_builder._SavedModelBuilder(export_dir)
with ops.Graph().as_default():
# Graph with two variables. SavedModel invoked to:
# - add with weights.
with self.session(graph=ops.Graph()) as sess:
@ -349,8 +378,8 @@ class SavedModelTest(SavedModelTestBase):
self._init_and_validate_variable(sess, "v2", 3)
builder.add_meta_graph(["bar"])
# Graph with a single variable (disjoint set of variables from the previous
# graph whose weights were saved). SavedModel invoked to:
# Graph with a single variable (disjoint set of variables from the
# previous graph whose weights were saved). SavedModel invoked to:
# - simply add the model (weights are not updated).
with self.session(graph=ops.Graph()) as sess:
self._init_and_validate_variable(sess, "v3", 4)
@ -364,17 +393,17 @@ class SavedModelTest(SavedModelTestBase):
loader.load(sess, ["foo"], export_dir)
collection_vars = ops.get_collection(ops.GraphKeys.GLOBAL_VARIABLES)
self.assertEqual(len(collection_vars), 2)
self.assertEqual(1, collection_vars[0].eval())
self.assertEqual(2, collection_vars[1].eval())
self.assertEqual(1, self._eval(collection_vars[0]))
self.assertEqual(2, self._eval(collection_vars[1]))
# Restore the graph with tag "bar", whose variables were not saved. Only the
# subset of the variables added to the graph will be restored with the
# Restore the graph with tag "bar", whose variables were not saved. Only
# the subset of the variables added to the graph will be restored with the
# checkpointed value.
with self.session(graph=ops.Graph()) as sess:
loader.load(sess, ["bar"], export_dir)
collection_vars = ops.get_collection(ops.GraphKeys.GLOBAL_VARIABLES)
self.assertEqual(len(collection_vars), 1)
self.assertEqual(2, collection_vars[0].eval())
self.assertEqual(2, self._eval(collection_vars[0]))
# Try restoring the graph with tag "baz", whose variables were not saved.
# Since this graph has a disjoint set of variables from the set that was
@ -383,11 +412,11 @@ class SavedModelTest(SavedModelTestBase):
self.assertRaises(errors.NotFoundError, loader.load, sess, ["baz"],
export_dir)
@test_util.run_deprecated_v1
def testGraphWithoutVariables(self):
export_dir = self._get_export_dir("test_graph_has_variables")
builder = saved_model_builder._SavedModelBuilder(export_dir)
with ops.Graph().as_default():
# Graph with no variables.
with self.session(graph=ops.Graph()) as sess:
constant_5_name = constant_op.constant(5.0).name
@ -419,11 +448,11 @@ class SavedModelTest(SavedModelTestBase):
c = a * b
self.assertEqual(30.0, self.evaluate(c))
@test_util.run_deprecated_v1
def testNoOverwrite(self):
export_dir = self._get_export_dir("test_no_overwrite")
builder = saved_model_builder._SavedModelBuilder(export_dir)
with ops.Graph().as_default():
# Graph with a single variable. SavedModel invoked to:
# - add with weights.
with self.session(graph=ops.Graph()) as sess:
@ -436,19 +465,18 @@ class SavedModelTest(SavedModelTestBase):
# Restore the graph with tag "foo", whose variables were saved.
with self.session(graph=ops.Graph()) as sess:
loader.load(sess, ["foo"], export_dir)
self.assertEqual(
42, ops.get_collection(ops.GraphKeys.GLOBAL_VARIABLES)[0].eval())
self.assertEqual(42, self._eval("v"))
# An attempt to create another builder with the same export directory should
# result in an assertion error.
# An attempt to create another builder with the same export directory
# should result in an assertion error.
self.assertRaises(AssertionError, saved_model_builder._SavedModelBuilder,
export_dir)
@test_util.run_deprecated_v1
def testSaveAsText(self):
export_dir = self._get_export_dir("test_astext")
builder = saved_model_builder._SavedModelBuilder(export_dir)
with ops.Graph().as_default():
# Graph with a single variable. SavedModel invoked to:
# - add with weights.
with self.session(graph=ops.Graph()) as sess:
@ -468,20 +496,23 @@ class SavedModelTest(SavedModelTestBase):
with self.session(graph=ops.Graph()) as sess:
loader.load(sess, ["foo"], export_dir)
self.assertEqual(
42, ops.get_collection(ops.GraphKeys.GLOBAL_VARIABLES)[0].eval())
42,
self._eval(ops.get_collection(ops.GraphKeys.GLOBAL_VARIABLES)[0]))
# Restore the graph with tag "bar", whose variables were not saved.
with self.session(graph=ops.Graph()) as sess:
loader.load(sess, ["bar"], export_dir)
self.assertEqual(
42, ops.get_collection(ops.GraphKeys.GLOBAL_VARIABLES)[0].eval())
42,
self._eval(ops.get_collection(ops.GraphKeys.GLOBAL_VARIABLES)[0]))
@test_util.run_v1_only("b/120545219")
def testCollections(self):
export_dir = self._get_export_dir("test_collections")
builder = saved_model_builder._SavedModelBuilder(export_dir)
# Graph with a single variable added to a collection. SavedModel invoked to:
with ops.Graph().as_default():
# Graph with a single variable added to a collection. SavedModel invoked
# to:
# - add with weights.
with self.session(graph=ops.Graph()) as sess:
v = variables.VariableV1(42, name="v")
@ -510,7 +541,7 @@ class SavedModelTest(SavedModelTestBase):
loader.load(sess, ["foo"], export_dir)
collection_foo_vars = ops.get_collection("foo_vars")
self.assertEqual(len(collection_foo_vars), 1)
self.assertEqual(42, collection_foo_vars[0].eval())
self.assertEqual(42, self._eval(collection_foo_vars[0]))
self.assertEqual(len(ops.get_collection("bar_vars")), 0)
@ -523,39 +554,37 @@ class SavedModelTest(SavedModelTestBase):
loader.load(sess, ["bar"], export_dir)
collection_bar_vars = ops.get_collection("bar_vars")
self.assertEqual(len(collection_bar_vars), 1)
self.assertEqual(42, collection_bar_vars[0].eval())
self.assertEqual(42, self._eval(collection_bar_vars[0]))
self.assertEqual(len(ops.get_collection("foo_vars")), 0)
@test_util.run_deprecated_v1
def testSignatureDefs(self):
export_dir = self._get_export_dir("test_signature_defs")
builder = saved_model_builder._SavedModelBuilder(export_dir)
# Graph with a single variable and a single entry in the signature def map.
# SavedModel is invoked to add with weights.
with ops.Graph().as_default():
# Graph with a single variable and a single entry in the signature def
# map. SavedModel is invoked to add with weights.
with self.session(graph=ops.Graph()) as sess:
self._init_and_validate_variable(sess, "v", 42)
# Build and populate an empty SignatureDef for testing.
foo_signature = signature_def_utils.build_signature_def(dict(),
dict(), "foo")
foo_signature = signature_def_utils.build_signature_def(
dict(), dict(), "foo")
builder.add_meta_graph_and_variables(
sess, ["foo"], signature_def_map={"foo_key": foo_signature})
# Graph with the same single variable and multiple entries in the signature
# def map. No weights are saved by SavedModel.
# Graph with the same single variable and multiple entries in the
# signature def map. No weights are saved by SavedModel.
with self.session(graph=ops.Graph()) as sess:
self._init_and_validate_variable(sess, "v", 43)
# Build and populate a different SignatureDef for testing.
bar_signature = signature_def_utils.build_signature_def(dict(),
dict(), "bar")
# Also, build a different SignatureDef corresponding to "foo_key" defined
# in the previous graph.
foo_new_signature = signature_def_utils.build_signature_def(dict(),
dict(),
"foo_new")
builder.add_meta_graph(
["bar"],
bar_signature = signature_def_utils.build_signature_def(
dict(), dict(), "bar")
# Also, build a different SignatureDef corresponding to "foo_key"
# defined in the previous graph.
foo_new_signature = signature_def_utils.build_signature_def(
dict(), dict(), "foo_new")
builder.add_meta_graph(["bar"],
signature_def_map={
"bar_key": bar_signature,
"foo_key": foo_new_signature
@ -564,12 +593,13 @@ class SavedModelTest(SavedModelTestBase):
# Save the SavedModel to disk.
builder.save()
# Restore the graph with tag "foo". The single entry in the SignatureDef map
# corresponding to "foo_key" should exist.
# Restore the graph with tag "foo". The single entry in the SignatureDef
# map corresponding to "foo_key" should exist.
with self.session(graph=ops.Graph()) as sess:
foo_graph = loader.load(sess, ["foo"], export_dir)
self.assertEqual(
42, ops.get_collection(ops.GraphKeys.GLOBAL_VARIABLES)[0].eval())
42,
self._eval(ops.get_collection(ops.GraphKeys.GLOBAL_VARIABLES)[0]))
foo_signature = foo_graph.signature_def
self.assertEqual(len(foo_signature), 1)
@ -581,7 +611,8 @@ class SavedModelTest(SavedModelTestBase):
with self.session(graph=ops.Graph()) as sess:
bar_graph = loader.load(sess, ["bar"], export_dir)
self.assertEqual(
42, ops.get_collection(ops.GraphKeys.GLOBAL_VARIABLES)[0].eval())
42,
self._eval(ops.get_collection(ops.GraphKeys.GLOBAL_VARIABLES)[0]))
bar_signature = bar_graph.signature_def
self.assertEqual(len(bar_signature), 2)
@ -615,12 +646,12 @@ class SavedModelTest(SavedModelTestBase):
self._validate_sig_def_keys(builder, valid_tensor_info,
constants.TRAIN_OP_SIGNATURE_KEY)
@test_util.run_deprecated_v1
def testSignatureDefValidationSucceedsWithName(self):
tensor_with_name = meta_graph_pb2.TensorInfo()
tensor_with_name.name = "foo"
tensor_with_name.dtype = types_pb2.DT_FLOAT
with ops.Graph().as_default():
export_dir = self._get_export_dir("test_signature_def_validation_name_1")
builder = saved_model_builder._SavedModelBuilder(export_dir)
self._validate_inputs_tensor_info_accept(builder, tensor_with_name)
@ -629,8 +660,8 @@ class SavedModelTest(SavedModelTestBase):
builder = saved_model_builder._SavedModelBuilder(export_dir)
self._validate_outputs_tensor_info_accept(builder, tensor_with_name)
@test_util.run_deprecated_v1
def testSignatureDefValidationSucceedsWithCoo(self):
with ops.Graph().as_default():
tensor_with_coo = meta_graph_pb2.TensorInfo()
# TODO(soergel) test validation of each of the fields of coo_sparse
tensor_with_coo.coo_sparse.values_tensor_name = "foo"
@ -644,30 +675,33 @@ class SavedModelTest(SavedModelTestBase):
builder = saved_model_builder._SavedModelBuilder(export_dir)
self._validate_outputs_tensor_info_accept(builder, tensor_with_coo)
@test_util.run_deprecated_v1
def testSignatureDefValidationSucceedsWithRagged(self):
with ops.Graph().as_default():
ragged_tensor = ragged_factory_ops.constant([[1, 2], [3]])
tensor_with_ragged = utils.build_tensor_info(ragged_tensor)
export_dir = self._get_export_dir("test_signature_def_validation_ragged_1")
export_dir = self._get_export_dir(
"test_signature_def_validation_ragged_1")
builder = saved_model_builder._SavedModelBuilder(export_dir)
self._validate_inputs_tensor_info_accept(builder, tensor_with_ragged)
export_dir = self._get_export_dir("test_signature_def_validation_ragged_2")
export_dir = self._get_export_dir(
"test_signature_def_validation_ragged_2")
builder = saved_model_builder._SavedModelBuilder(export_dir)
self._validate_outputs_tensor_info_accept(builder, tensor_with_ragged)
@test_util.run_deprecated_v1
def testAssets(self):
export_dir = self._get_export_dir("test_assets")
builder = saved_model_builder._SavedModelBuilder(export_dir)
with ops.Graph().as_default():
with self.session(graph=ops.Graph()) as sess:
self._init_and_validate_variable(sess, "v", 42)
# Build an asset collection.
ignored_filepath = os.path.join(
compat.as_bytes(test.get_temp_dir()), compat.as_bytes("ignored.txt"))
compat.as_bytes(test.get_temp_dir()),
compat.as_bytes("ignored.txt"))
file_io.write_string_to_file(ignored_filepath, "will be ignored")
asset_list = self._build_asset_collection("hello42.txt", "foo bar baz",
@ -681,19 +715,20 @@ class SavedModelTest(SavedModelTestBase):
with self.session(graph=ops.Graph()) as sess:
foo_graph = loader.load(sess, ["foo"], export_dir)
self._validate_assets(export_dir, foo_graph.asset_file_def, "hello42.txt",
"foo bar baz", "asset_file_tensor:0")
self._validate_assets(export_dir, foo_graph.asset_file_def,
"hello42.txt", "foo bar baz",
"asset_file_tensor:0")
ignored_asset_path = os.path.join(
compat.as_bytes(export_dir),
compat.as_bytes(constants.ASSETS_DIRECTORY),
compat.as_bytes("ignored.txt"))
self.assertFalse(file_io.file_exists(ignored_asset_path))
@test_util.run_deprecated_v1
def testAssetsNameCollisionDiffFile(self):
export_dir = self._get_export_dir("test_assets_name_collision_diff_file")
builder = saved_model_builder._SavedModelBuilder(export_dir)
with ops.Graph().as_default():
with self.session(graph=ops.Graph()) as sess:
self._init_and_validate_variable(sess, "v", 42)
@ -701,7 +736,10 @@ class SavedModelTest(SavedModelTestBase):
"hello42.txt", "foo bar bak", "asset_file_tensor", asset_subdir="1")
asset_list = self._build_asset_collection(
"hello42.txt", "foo bar baz", "asset_file_tensor_1", asset_subdir="2")
"hello42.txt",
"foo bar baz",
"asset_file_tensor_1",
asset_subdir="2")
builder.add_meta_graph_and_variables(
sess, ["foo"], assets_list=asset_list)
@ -711,8 +749,9 @@ class SavedModelTest(SavedModelTestBase):
with self.session(graph=ops.Graph()) as sess:
foo_graph = loader.load(sess, ["foo"], export_dir)
self._validate_assets(export_dir, foo_graph.asset_file_def, "hello42.txt",
"foo bar bak", "asset_file_tensor:0")
self._validate_assets(export_dir, foo_graph.asset_file_def,
"hello42.txt", "foo bar bak",
"asset_file_tensor:0")
self._validate_assets(
export_dir,
foo_graph.asset_file_def,
@ -721,11 +760,11 @@ class SavedModelTest(SavedModelTestBase):
"asset_file_tensor_1:0",
asset_id=1)
@test_util.run_deprecated_v1
def testAssetsNameCollisionSameFilepath(self):
export_dir = self._get_export_dir("test_assets_name_collision_same_path")
builder = saved_model_builder._SavedModelBuilder(export_dir)
with ops.Graph().as_default():
with self.session(graph=ops.Graph()) as sess:
self._init_and_validate_variable(sess, "v", 42)
@ -743,8 +782,9 @@ class SavedModelTest(SavedModelTestBase):
with self.session(graph=ops.Graph()) as sess:
foo_graph = loader.load(sess, ["foo"], export_dir)
self._validate_assets(export_dir, foo_graph.asset_file_def, "hello42.txt",
"foo bar baz", "asset_file_tensor:0")
self._validate_assets(export_dir, foo_graph.asset_file_def,
"hello42.txt", "foo bar baz",
"asset_file_tensor:0")
# The second tensor should be recorded, but the same.
self._validate_assets(
export_dir,
@ -759,11 +799,11 @@ class SavedModelTest(SavedModelTestBase):
compat.as_bytes("hello42.txt_1"))
self.assertFalse(file_io.file_exists(ignored_asset_path))
@test_util.run_deprecated_v1
def testAssetsNameCollisionSameFile(self):
export_dir = self._get_export_dir("test_assets_name_collision_same_file")
builder = saved_model_builder._SavedModelBuilder(export_dir)
with ops.Graph().as_default():
with self.session(graph=ops.Graph()) as sess:
self._init_and_validate_variable(sess, "v", 42)
@ -771,7 +811,10 @@ class SavedModelTest(SavedModelTestBase):
"hello42.txt", "foo bar baz", "asset_file_tensor", asset_subdir="1")
asset_list = self._build_asset_collection(
"hello42.txt", "foo bar baz", "asset_file_tensor_1", asset_subdir="2")
"hello42.txt",
"foo bar baz",
"asset_file_tensor_1",
asset_subdir="2")
builder.add_meta_graph_and_variables(
sess, ["foo"], assets_list=asset_list)
@ -781,8 +824,9 @@ class SavedModelTest(SavedModelTestBase):
with self.session(graph=ops.Graph()) as sess:
foo_graph = loader.load(sess, ["foo"], export_dir)
self._validate_assets(export_dir, foo_graph.asset_file_def, "hello42.txt",
"foo bar baz", "asset_file_tensor:0")
self._validate_assets(export_dir, foo_graph.asset_file_def,
"hello42.txt", "foo bar baz",
"asset_file_tensor:0")
# The second tensor should be recorded, but the same.
self._validate_assets(
export_dir,
@ -797,11 +841,11 @@ class SavedModelTest(SavedModelTestBase):
compat.as_bytes("hello42.txt_1"))
self.assertFalse(file_io.file_exists(ignored_asset_path))
@test_util.run_deprecated_v1
def testAssetsNameCollisionManyFiles(self):
export_dir = self._get_export_dir("test_assets_name_collision_many_files")
builder = saved_model_builder._SavedModelBuilder(export_dir)
with ops.Graph().as_default():
with self.session(graph=ops.Graph()) as sess:
self._init_and_validate_variable(sess, "v", 42)
@ -831,30 +875,30 @@ class SavedModelTest(SavedModelTestBase):
"asset_file_tensor_{}:0".format(idx),
asset_id=i)
self._validate_assets(export_dir, foo_graph.asset_file_def, "hello42.txt",
"foo bar baz 0", "asset_file_tensor_0:0")
self._validate_assets(export_dir, foo_graph.asset_file_def,
"hello42.txt", "foo bar baz 0",
"asset_file_tensor_0:0")
@test_util.run_v1_only("b/120545219")
def testCustomInitOp(self):
export_dir = self._get_export_dir("test_main_op")
builder = saved_model_builder._SavedModelBuilder(export_dir)
with ops.Graph().as_default():
with self.session(graph=ops.Graph()) as sess:
# Add `v1` and `v2` variables to the graph.
v1 = variables.VariableV1(1, name="v1")
ops.add_to_collection("v", v1)
v2 = variables.VariableV1(2, name="v2")
ops.add_to_collection("v", v2)
# Initialize another variable `v3` to 42.
v3 = variables.VariableV1(42, name="v3")
ops.add_to_collection("v", v3)
# Set up an assignment op to be run as part of the main_op.
with ops.control_dependencies([main_op.main_op()]):
add_v1_v2 = math_ops.add(v1._ref(), v2._ref())
custom_init_op = control_flow_ops.group(state_ops.assign(v3, add_v1_v2))
add_v1_v2 = math_ops.add(v1, v2)
custom_init_op = control_flow_ops.group(
state_ops.assign(v3, add_v1_v2))
self.evaluate(variables.global_variables_initializer())
self.evaluate(custom_init_op)
builder.add_meta_graph_and_variables(
sess, ["foo"], init_op=custom_init_op)
@ -864,23 +908,21 @@ class SavedModelTest(SavedModelTestBase):
with self.session(graph=ops.Graph()) as sess:
loader.load(sess, ["foo"], export_dir)
self.assertEqual(1, ops.get_collection("v")[0].eval())
self.assertEqual(2, ops.get_collection("v")[1].eval())
# Evaluates to the sum of the first two variables and assigned as part of
# the main_op, following a restore.
self.assertEqual(3, ops.get_collection("v")[2].eval())
self.assertEqual(1, self._eval("v1"))
self.assertEqual(2, self._eval("v2"))
# Evaluates to the sum of the first two variables and assigned as part
# of the main_op, following a restore.
self.assertEqual(3, self._eval("v3"))
@test_util.run_v1_only("b/120545219")
def testTrainOp(self):
export_dir = self._get_export_dir("test_train_op")
builder = saved_model_builder._SavedModelBuilder(export_dir)
with ops.Graph().as_default():
with self.session(graph=ops.Graph()) as sess:
# Add `v1` and `v2` variables to the graph.
v1 = variables.VariableV1(1, name="v1")
ops.add_to_collection("v", v1)
v2 = variables.VariableV1(2, name="v2")
ops.add_to_collection("v", v2)
self.evaluate(variables.global_variables_initializer())
train_op = state_ops.assign_add(v1, v2)
@ -893,22 +935,25 @@ class SavedModelTest(SavedModelTestBase):
with self.session(graph=ops.Graph()) as sess:
meta_graph_def = loader.load(sess, ["foo"], export_dir)
self.assertEqual(3, ops.get_collection("v")[0].eval())
self.assertEqual(2, ops.get_collection("v")[1].eval())
self.assertEqual(3, self._eval("v1"))
self.assertEqual(2, self._eval("v2"))
if variable_scope.resource_variables_enabled():
self.assertEqual(
loader_impl.get_train_op(meta_graph_def).type,
"AssignAddVariableOp")
else:
self.assertIsInstance(
loader_impl.get_train_op(meta_graph_def), ops.Tensor)
@test_util.run_v1_only("b/120545219")
def testTrainOpGroup(self):
export_dir = self._get_export_dir("test_train_op_group")
builder = saved_model_builder._SavedModelBuilder(export_dir)
with ops.Graph().as_default():
with self.session(graph=ops.Graph()) as sess:
# Add `v1` and `v2` variables to the graph.
v1 = variables.VariableV1(1, name="v1")
ops.add_to_collection("v", v1)
v2 = variables.VariableV1(2, name="v2")
ops.add_to_collection("v", v2)
variables.VariableV1(1, name="v1")
variables.VariableV1(2, name="v2")
self.evaluate(variables.global_variables_initializer())
train_op = control_flow_ops.group()
@ -921,22 +966,20 @@ class SavedModelTest(SavedModelTestBase):
with self.session(graph=ops.Graph()) as sess:
meta_graph_def = loader.load(sess, ["foo"], export_dir)
self.assertEqual(1, ops.get_collection("v")[0].eval())
self.assertEqual(2, ops.get_collection("v")[1].eval())
self.assertEqual(1, self._eval("v1"))
self.assertEqual(2, self._eval("v2"))
self.assertIsInstance(
loader_impl.get_train_op(meta_graph_def), ops.Operation)
@test_util.run_v1_only("b/120545219")
def testTrainOpAfterVariables(self):
export_dir = self._get_export_dir("test_train_op_after_variables")
builder = saved_model_builder._SavedModelBuilder(export_dir)
with ops.Graph().as_default():
with self.session(graph=ops.Graph()) as sess:
# Add `v1` and `v2` variables to the graph.
v1 = variables.VariableV1(1, name="v1")
ops.add_to_collection("v", v1)
v2 = variables.VariableV1(2, name="v2")
ops.add_to_collection("v", v2)
self.evaluate(variables.global_variables_initializer())
builder.add_meta_graph_and_variables(sess, ["pre_foo"])
@ -950,6 +993,11 @@ class SavedModelTest(SavedModelTestBase):
with self.session(graph=ops.Graph()) as sess:
meta_graph_def = loader.load(sess, ["foo"], export_dir)
if variable_scope.resource_variables_enabled():
self.assertEqual(
loader_impl.get_train_op(meta_graph_def).type,
"AssignAddVariableOp")
else:
self.assertIsInstance(
loader_impl.get_train_op(meta_graph_def), ops.Tensor)
@ -957,11 +1005,11 @@ class SavedModelTest(SavedModelTestBase):
loader.load(sess, ["pre_foo"], export_dir)
self.assertFalse(ops.get_collection(constants.TRAIN_OP_KEY))
@test_util.run_deprecated_v1
def testMultipleAssets(self):
export_dir = self._get_export_dir("test_multiple_assets")
builder = saved_model_builder._SavedModelBuilder(export_dir)
with ops.Graph().as_default():
with self.session(graph=ops.Graph()) as sess:
self._init_and_validate_variable(sess, "v", 42)
@ -998,11 +1046,11 @@ class SavedModelTest(SavedModelTestBase):
self._validate_assets(export_dir, bar_graph.asset_file_def, "bar.txt",
"content_bar", "asset_file_tensor:0")
@test_util.run_deprecated_v1
def testDuplicateAssets(self):
export_dir = self._get_export_dir("test_duplicate_assets")
builder = saved_model_builder._SavedModelBuilder(export_dir)
with ops.Graph().as_default():
with self.session(graph=ops.Graph()) as sess:
self._init_and_validate_variable(sess, "v", 42)
@ -1045,11 +1093,11 @@ class SavedModelTest(SavedModelTestBase):
self._validate_assets(export_dir, bar_graph.asset_file_def, "foo.txt",
"content_foo", "asset_file_tensor:0")
@test_util.run_v1_only("b/120545219")
def testOp(self):
export_dir = self._get_export_dir("test_op")
builder = saved_model_builder._SavedModelBuilder(export_dir)
with ops.Graph().as_default():
with session.Session(
graph=ops.Graph(),
config=config_pb2.ConfigProto(device_count={"CPU": 2})) as sess:
@ -1062,16 +1110,11 @@ class SavedModelTest(SavedModelTestBase):
# exercise the ability to run an init op when restoring a graph.
v3 = variables.VariableV1(1, name="v3", trainable=False, collections=[])
assign_v3 = state_ops.assign(v3, math_ops.add(v1, v2))
init_op = control_flow_ops.group(assign_v3, name="init_op")
ops.add_to_collection("v", v1)
ops.add_to_collection("v", v2)
ops.add_to_collection("v", v3)
ops.add_to_collection("init_op", init_op)
control_flow_ops.group(assign_v3, name="init_op")
self.evaluate(variables.global_variables_initializer())
self.assertEqual(1, ops.get_collection("v")[0].eval())
self.assertEqual(2, ops.get_collection("v")[1].eval())
self.assertEqual(1, self._eval("v1"))
self.assertEqual(2, self._eval("v2"))
builder.add_meta_graph_and_variables(sess, ["foo"])
@ -1084,10 +1127,10 @@ class SavedModelTest(SavedModelTestBase):
loader.load(sess, ["foo"], export_dir)
# Validate variables, run the init op and verify result.
self.assertEqual(1, ops.get_collection("v")[0].eval())
self.assertEqual(2, ops.get_collection("v")[1].eval())
ops.get_collection("init_op")[0].run()
self.assertEqual(3, ops.get_collection("v")[2].eval())
self.assertEqual(1, self._eval("v1"))
self.assertEqual(2, self._eval("v2"))
sess.run("init_op")
self.assertEqual(3, self._eval("v3"))
def testCustomSaveable(self):
export_dir = self._get_export_dir("custom_saveable")
@ -1118,11 +1161,11 @@ class SavedModelTest(SavedModelTestBase):
self.assertEqual(b"k1", v1.keys().eval())
self.assertEqual(3.0, v1.values().eval())
@test_util.run_deprecated_v1
def testCustomSaver(self):
export_dir = self._get_export_dir("test_custom_saver")
builder = saved_model_builder._SavedModelBuilder(export_dir)
with ops.Graph().as_default() as graph:
with self.session(graph=ops.Graph()) as sess:
variables.VariableV1(1, name="v1")
self.evaluate(variables.global_variables_initializer())
@ -1132,7 +1175,6 @@ class SavedModelTest(SavedModelTestBase):
# Save the SavedModel to disk.
builder.save()
with ops.Graph().as_default() as graph:
with self.session(graph=graph) as sess:
saved_graph = loader.load(sess, ["tag"], export_dir)
graph_ops = [x.name for x in graph.get_operations()]
@ -1141,11 +1183,11 @@ class SavedModelTest(SavedModelTestBase):
self.assertEqual(
saved_graph.saver_def.restore_op_name, "my_saver/restore_all")
@test_util.run_deprecated_v1
def testNoCustomSaver(self):
export_dir = self._get_export_dir("test_no_custom_saver")
builder = saved_model_builder._SavedModelBuilder(export_dir)
with ops.Graph().as_default() as graph:
with self.session(graph=ops.Graph()) as sess:
variables.VariableV1(1, name="v1")
self.evaluate(variables.global_variables_initializer())
@ -1155,7 +1197,6 @@ class SavedModelTest(SavedModelTestBase):
# Save the SavedModel to disk.
builder.save()
with ops.Graph().as_default() as graph:
with self.session(graph=graph) as sess:
saved_graph = loader.load(sess, ["tag"], export_dir)
graph_ops = [x.name for x in graph.get_operations()]
@ -1164,11 +1205,11 @@ class SavedModelTest(SavedModelTestBase):
self.assertEqual(
saved_graph.saver_def.restore_op_name, "save/restore_all")
@test_util.run_deprecated_v1
def testMultipleCustomSavers(self):
export_dir = self._get_export_dir("test_multiple_custom_savers")
builder = saved_model_builder._SavedModelBuilder(export_dir)
with ops.Graph().as_default():
with self.session(graph=ops.Graph()) as sess:
variables.VariableV1(1, name="v1")
self.evaluate(variables.global_variables_initializer())
@ -1195,11 +1236,11 @@ class SavedModelTest(SavedModelTestBase):
_validate_custom_saver("tag_1", "save_1/restore_all")
_validate_custom_saver("tag_2", "save_2/restore_all")
@test_util.run_deprecated_v1
def testImportScope(self):
export_dir = self._get_export_dir("test_scoped_assets")
builder = saved_model_builder._SavedModelBuilder(export_dir)
with ops.Graph().as_default():
# Build a SavedModel with a variable, an asset, and a constant tensor.
with self.session(graph=ops.Graph()) as sess:
self._init_and_validate_variable(sess, "v", 42)
@ -1220,14 +1261,12 @@ class SavedModelTest(SavedModelTestBase):
graph_proto = loader.load(
sess, ["tag_name"], export_dir, import_scope="scope_name")
# The loaded variable tensor should be scoped, but its contents should be
# unchanged.
# The loaded variable tensor should be scoped, but its contents should
# be unchanged.
self.assertEqual(
"scope_name/v:0",
ops.get_collection(ops.GraphKeys.GLOBAL_VARIABLES)[0].name)
self.assertEqual(
42,
ops.get_collection(ops.GraphKeys.GLOBAL_VARIABLES)[0].eval())
self.assertEqual(42, self._eval("scope_name/v"))
# The loaded asset tensor should be scoped, but the asset file path and
# contents should be unchanged.
@ -1247,13 +1286,12 @@ class SavedModelTest(SavedModelTestBase):
ops.get_default_graph().get_tensor_by_name(
"scope_name/constant_tensor_name:0").eval())
@test_util.run_deprecated_v1
def testClearDevices(self):
export_dir = self._get_export_dir("test_clear_devices")
builder = saved_model_builder._SavedModelBuilder(export_dir)
with ops.Graph().as_default():
# Specify a device and save a variable.
ops.reset_default_graph()
with session.Session(
target="",
config=config_pb2.ConfigProto(device_count={"CPU": 2})) as sess:
@ -1265,12 +1303,11 @@ class SavedModelTest(SavedModelTestBase):
# Save the SavedModel to disk.
builder.save()
# Restore the graph with a single predefined tag whose variables were saved
# without any device information.
# Restore the graph with a single predefined tag whose variables were
# saved without any device information.
with self.session(graph=ops.Graph()) as sess:
loader.load(sess, [tag_constants.TRAINING], export_dir)
self.assertEqual(
42, ops.get_collection(ops.GraphKeys.GLOBAL_VARIABLES)[0].eval())
self.assertEqual(42, self._eval("v"))
# Tests the behavior of loading SavedModels that having missing attrs or attrs
# with incorrect types.
@ -1361,21 +1398,23 @@ class SavedModelV1Test(SavedModelTestBase):
self.assertEqual(expected_asset_file_name, asset.filename)
self.assertEqual(expected_asset_tensor_name, asset.tensor_info.name)
@test_util.run_deprecated_v1
def testWritingAssetsToCollection(self):
export_dir = self._get_export_dir("test_writing_assets_to_collection")
builder = saved_model_builder.SavedModelBuilder(export_dir)
with ops.Graph().as_default():
with self.session(graph=ops.Graph()) as sess:
self._init_and_validate_variable(sess, "v", 42)
# Build an asset list.
ignored_filepath = os.path.join(
compat.as_bytes(test.get_temp_dir()), compat.as_bytes("ignored.txt"))
compat.as_bytes(test.get_temp_dir()),
compat.as_bytes("ignored.txt"))
file_io.write_string_to_file(ignored_filepath, "will be ignored")
asset_collection = self._build_asset_collection(
"hello42.txt", "foo bar baz", "asset_file_tensor")
asset_collection = self._build_asset_collection("hello42.txt",
"foo bar baz",
"asset_file_tensor")
builder.add_meta_graph_and_variables(
sess, ["foo"], assets_collection=asset_collection)
@ -1394,14 +1433,12 @@ class SavedModelV1Test(SavedModelTestBase):
compat.as_bytes("ignored.txt"))
self.assertFalse(file_io.file_exists(ignored_asset_path))
@test_util.run_deprecated_v1
def testLegacyInitOpWithNonEmptyCollection(self):
export_dir = self._get_export_dir(
"test_legacy_init_op_with_non_empty_collection")
self._testInitOpsWithNonEmptyCollection(export_dir,
constants.LEGACY_INIT_OP_KEY)
@test_util.run_deprecated_v1
def testMainOpWithNonEmptyCollection(self):
export_dir = self._get_export_dir("test_main_op_with_non_empty_collection")
self._testInitOpsWithNonEmptyCollection(export_dir, constants.MAIN_OP_KEY)
@ -1409,14 +1446,15 @@ class SavedModelV1Test(SavedModelTestBase):
def _testInitOpsWithNonEmptyCollection(self, export_dir, key):
builder = saved_model_builder.SavedModelBuilder(export_dir)
g = ops.Graph()
with self.session(graph=g) as sess:
with ops.Graph().as_default():
with self.session() as sess:
# Initialize variable `v1` to 1.
v1 = variables.VariableV1(1, name="v1")
ops.add_to_collection("v", v1)
# Initialize another variable `v2` to 42.
v2 = variables.VariableV1(42, name="v2", trainable=False, collections=[])
v2 = variables.VariableV1(
42, name="v2", trainable=False, collections=[])
ops.add_to_collection("v", v2)
# Set up an assignment op to be run as part of the init op.
@ -1503,25 +1541,23 @@ class SavedModelV1Test(SavedModelTestBase):
self.assertIn("T", node_def.attr)
self.assertIn("Tout", node_def.attr)
@test_util.run_v1_only("b/120545219")
def testLegacyInitOp(self):
export_dir = self._get_export_dir("test_legacy_init_op")
builder = saved_model_builder.SavedModelBuilder(export_dir)
with ops.Graph().as_default():
with self.session(graph=ops.Graph()) as sess:
# Add `v1` and `v2` variables to the graph.
v1 = variables.VariableV1(1, name="v1")
ops.add_to_collection("v", v1)
v2 = variables.VariableV1(2, name="v2")
ops.add_to_collection("v", v2)
# Initialize another variable `v3` to 42.
v3 = variables.VariableV1(42, name="v3", trainable=False, collections=[])
ops.add_to_collection("v", v3)
v3 = variables.VariableV1(42, name="v3", trainable=False)
# Set up an assignment op to be run as part of the init_op.
assign_v3 = state_ops.assign(v3, math_ops.add(v1, v2))
legacy_init_op = control_flow_ops.group(assign_v3, name="legacy_init_op")
legacy_init_op = control_flow_ops.group(
assign_v3, name="legacy_init_op")
self.evaluate(variables.global_variables_initializer())
builder.add_meta_graph_and_variables(
@ -1532,11 +1568,11 @@ class SavedModelV1Test(SavedModelTestBase):
with self.session(graph=ops.Graph()) as sess:
loader.load(sess, ["foo"], export_dir)
self.assertEqual(1, ops.get_collection("v")[0].eval())
self.assertEqual(2, ops.get_collection("v")[1].eval())
# Evaluates to the sum of the first two variables and assigned as part of
# the legacy_init_op, following a restore.
self.assertEqual(3, ops.get_collection("v")[2].eval())
self.assertEqual(1, self._eval("v1"))
self.assertEqual(2, self._eval("v2"))
# Evaluates to the sum of the first two variables and assigned as part
# of the legacy_init_op, following a restore.
self.assertEqual(3, self._eval("v3"))
if __name__ == "__main__":