Remove run_deprecated_v1 decorators from signature_def_utilts_test.

The tests that had that decorator all depended on build_tensor_info
and build_tensor_info_from_op, which in turn depend on tensor.name,
which is meaningless in eager mode. Migrating the tests to TF2 would
defeat their purpose completely, as they exercise V1 signature
building utilities. "with ops.Graph().as_default()"ed them.

PiperOrigin-RevId: 323807112
Change-Id: Iab1f20a4561acaa235d8ffd3fea436ac7cfddb91
This commit is contained in:
Cesar Crusius 2020-07-29 09:49:19 -07:00 committed by TensorFlower Gardener
parent b646fc7467
commit 66b1247f10
2 changed files with 121 additions and 83 deletions

View File

@ -22,7 +22,7 @@ from tensorflow.core.framework import types_pb2
from tensorflow.core.protobuf import meta_graph_pb2
from tensorflow.python.framework import constant_op
from tensorflow.python.framework import dtypes
from tensorflow.python.framework import test_util
from tensorflow.python.framework import ops
from tensorflow.python.ops import array_ops
from tensorflow.python.ops import math_ops
from tensorflow.python.platform import test
@ -60,17 +60,20 @@ def _make_signature(inputs, outputs, name=None):
class SignatureDefUtilsTest(test.TestCase):
@test_util.run_deprecated_v1
def testBuildSignatureDef(self):
x = array_ops.placeholder(dtypes.float32, 1, name="x")
x_tensor_info = utils.build_tensor_info(x)
inputs = {}
inputs["foo-input"] = x_tensor_info
# Force the test to run in graph mode.
# This tests a deprecated v1 API that uses functionality that does not work
# with eager tensors (namely build_tensor_info).
with ops.Graph().as_default():
x = array_ops.placeholder(dtypes.float32, 1, name="x")
x_tensor_info = utils.build_tensor_info(x)
inputs = {}
inputs["foo-input"] = x_tensor_info
y = array_ops.placeholder(dtypes.float32, name="y")
y_tensor_info = utils.build_tensor_info(y)
outputs = {}
outputs["foo-output"] = y_tensor_info
y = array_ops.placeholder(dtypes.float32, name="y")
y_tensor_info = utils.build_tensor_info(y)
outputs = {}
outputs["foo-output"] = y_tensor_info
signature_def = signature_def_utils_impl.build_signature_def(
inputs, outputs, "foo-method-name")
@ -91,12 +94,15 @@ class SignatureDefUtilsTest(test.TestCase):
self.assertEqual(types_pb2.DT_FLOAT, y_tensor_info_actual.dtype)
self.assertEqual(0, len(y_tensor_info_actual.tensor_shape.dim))
@test_util.run_deprecated_v1
def testRegressionSignatureDef(self):
input1 = constant_op.constant("a", name="input-1")
output1 = constant_op.constant(2.2, name="output-1")
signature_def = signature_def_utils_impl.regression_signature_def(
input1, output1)
# Force the test to run in graph mode.
# This tests a deprecated v1 API that uses functionality that does not work
# with eager tensors (namely build_tensor_info).
with ops.Graph().as_default():
input1 = constant_op.constant("a", name="input-1")
output1 = constant_op.constant(2.2, name="output-1")
signature_def = signature_def_utils_impl.regression_signature_def(
input1, output1)
self.assertEqual(signature_constants.REGRESS_METHOD_NAME,
signature_def.method_name)
@ -117,13 +123,16 @@ class SignatureDefUtilsTest(test.TestCase):
self.assertEqual(types_pb2.DT_FLOAT, y_tensor_info_actual.dtype)
self.assertEqual(0, len(y_tensor_info_actual.tensor_shape.dim))
@test_util.run_deprecated_v1
def testClassificationSignatureDef(self):
input1 = constant_op.constant("a", name="input-1")
output1 = constant_op.constant("b", name="output-1")
output2 = constant_op.constant(3.3, name="output-2")
signature_def = signature_def_utils_impl.classification_signature_def(
input1, output1, output2)
# Force the test to run in graph mode.
# This tests a deprecated v1 API that uses functionality that does not work
# with eager tensors (namely build_tensor_info).
with ops.Graph().as_default():
input1 = constant_op.constant("a", name="input-1")
output1 = constant_op.constant("b", name="output-1")
output2 = constant_op.constant(3.3, name="output-2")
signature_def = signature_def_utils_impl.classification_signature_def(
input1, output1, output2)
self.assertEqual(signature_constants.CLASSIFY_METHOD_NAME,
signature_def.method_name)
@ -149,17 +158,23 @@ class SignatureDefUtilsTest(test.TestCase):
self.assertEqual(types_pb2.DT_FLOAT, scores_tensor_info_actual.dtype)
self.assertEqual(0, len(scores_tensor_info_actual.tensor_shape.dim))
@test_util.run_deprecated_v1
def testPredictionSignatureDef(self):
input1 = constant_op.constant("a", name="input-1")
input2 = constant_op.constant("b", name="input-2")
output1 = constant_op.constant("c", name="output-1")
output2 = constant_op.constant("d", name="output-2")
signature_def = signature_def_utils_impl.predict_signature_def({
"input-1": input1,
"input-2": input2
}, {"output-1": output1,
"output-2": output2})
# Force the test to run in graph mode.
# This tests a deprecated v1 API that uses functionality that does not work
# with eager tensors (namely build_tensor_info).
with ops.Graph().as_default():
input1 = constant_op.constant("a", name="input-1")
input2 = constant_op.constant("b", name="input-2")
output1 = constant_op.constant("c", name="output-1")
output2 = constant_op.constant("d", name="output-2")
signature_def = signature_def_utils_impl.predict_signature_def(
{
"input-1": input1,
"input-2": input2
}, {
"output-1": output1,
"output-2": output2
})
self.assertEqual(signature_constants.PREDICT_METHOD_NAME,
signature_def.method_name)
@ -186,34 +201,38 @@ class SignatureDefUtilsTest(test.TestCase):
self.assertEqual(types_pb2.DT_STRING, output2_tensor_info_actual.dtype)
self.assertEqual(0, len(output2_tensor_info_actual.tensor_shape.dim))
@test_util.run_deprecated_v1
def testTrainSignatureDef(self):
self._testSupervisedSignatureDef(
signature_def_utils_impl.supervised_train_signature_def,
signature_constants.SUPERVISED_TRAIN_METHOD_NAME)
@test_util.run_deprecated_v1
def testEvalSignatureDef(self):
self._testSupervisedSignatureDef(
signature_def_utils_impl.supervised_eval_signature_def,
signature_constants.SUPERVISED_EVAL_METHOD_NAME)
def _testSupervisedSignatureDef(self, fn_to_test, method_name):
inputs = {
"input-1": constant_op.constant("a", name="input-1"),
"input-2": constant_op.constant("b", name="input-2"),
}
loss = {"loss-1": constant_op.constant(0.45, name="loss-1")}
predictions = {
"classes": constant_op.constant([100], name="classes"),
}
metrics_val = constant_op.constant(100.0, name="metrics_val")
metrics = {
"metrics/value": metrics_val,
"metrics/update_op": array_ops.identity(metrics_val, name="metrics_op"),
}
# Force the test to run in graph mode.
# This tests a deprecated v1 API that uses functionality that does not work
# with eager tensors (namely build_tensor_info).
with ops.Graph().as_default():
inputs = {
"input-1": constant_op.constant("a", name="input-1"),
"input-2": constant_op.constant("b", name="input-2"),
}
loss = {"loss-1": constant_op.constant(0.45, name="loss-1")}
predictions = {
"classes": constant_op.constant([100], name="classes"),
}
metrics_val = constant_op.constant(100.0, name="metrics_val")
metrics = {
"metrics/value":
metrics_val,
"metrics/update_op":
array_ops.identity(metrics_val, name="metrics_op"),
}
signature_def = fn_to_test(inputs, loss, predictions, metrics)
signature_def = fn_to_test(inputs, loss, predictions, metrics)
self.assertEqual(method_name, signature_def.method_name)
@ -246,44 +265,50 @@ class SignatureDefUtilsTest(test.TestCase):
self.assertEqual(
types_pb2.DT_FLOAT, signature_def.outputs["metrics/value"].dtype)
@test_util.run_deprecated_v1
def testTrainSignatureDefMissingInputs(self):
self._testSupervisedSignatureDefMissingInputs(
signature_def_utils_impl.supervised_train_signature_def,
signature_constants.SUPERVISED_TRAIN_METHOD_NAME)
@test_util.run_deprecated_v1
def testEvalSignatureDefMissingInputs(self):
self._testSupervisedSignatureDefMissingInputs(
signature_def_utils_impl.supervised_eval_signature_def,
signature_constants.SUPERVISED_EVAL_METHOD_NAME)
def _testSupervisedSignatureDefMissingInputs(self, fn_to_test, method_name):
inputs = {
"input-1": constant_op.constant("a", name="input-1"),
"input-2": constant_op.constant("b", name="input-2"),
}
loss = {"loss-1": constant_op.constant(0.45, name="loss-1")}
predictions = {
"classes": constant_op.constant([100], name="classes"),
}
metrics_val = constant_op.constant(100, name="metrics_val")
metrics = {
"metrics/value": metrics_val,
"metrics/update_op": array_ops.identity(metrics_val, name="metrics_op"),
}
# Force the test to run in graph mode.
# This tests a deprecated v1 API that uses functionality that does not work
# with eager tensors (namely build_tensor_info).
with ops.Graph().as_default():
inputs = {
"input-1": constant_op.constant("a", name="input-1"),
"input-2": constant_op.constant("b", name="input-2"),
}
loss = {"loss-1": constant_op.constant(0.45, name="loss-1")}
predictions = {
"classes": constant_op.constant([100], name="classes"),
}
metrics_val = constant_op.constant(100, name="metrics_val")
metrics = {
"metrics/value":
metrics_val,
"metrics/update_op":
array_ops.identity(metrics_val, name="metrics_op"),
}
with self.assertRaises(ValueError):
signature_def = fn_to_test(
{}, loss=loss, predictions=predictions, metrics=metrics)
with self.assertRaises(ValueError):
signature_def = fn_to_test({},
loss=loss,
predictions=predictions,
metrics=metrics)
signature_def = fn_to_test(inputs, loss=loss)
self.assertEqual(method_name, signature_def.method_name)
self.assertEqual(1, len(signature_def.outputs))
signature_def = fn_to_test(inputs, loss=loss)
self.assertEqual(method_name, signature_def.method_name)
self.assertEqual(1, len(signature_def.outputs))
signature_def = fn_to_test(inputs, metrics=metrics, loss=loss)
self.assertEqual(method_name, signature_def.method_name)
self.assertEqual(3, len(signature_def.outputs))
signature_def = fn_to_test(inputs, metrics=metrics, loss=loss)
self.assertEqual(method_name, signature_def.method_name)
self.assertEqual(3, len(signature_def.outputs))
def _assertValidSignature(self, inputs, outputs, method_name):
signature_def = signature_def_utils_impl.build_signature_def(
@ -423,23 +448,30 @@ class SignatureDefUtilsTest(test.TestCase):
{},
signature_constants.PREDICT_METHOD_NAME)
@test_util.run_v1_only("b/120545219")
def testOpSignatureDef(self):
key = "adding_1_and_2_key"
add_op = math_ops.add(1, 2, name="adding_1_and_2")
signature_def = signature_def_utils_impl.op_signature_def(add_op, key)
# Force the test to run in graph mode.
# This tests a deprecated v1 API that uses functionality that does not work
# with eager tensors (namely build_tensor_info_from_op).
with ops.Graph().as_default():
key = "adding_1_and_2_key"
add_op = math_ops.add(1, 2, name="adding_1_and_2")
signature_def = signature_def_utils_impl.op_signature_def(add_op, key)
self.assertIn(key, signature_def.outputs)
self.assertEqual(add_op.name, signature_def.outputs[key].name)
@test_util.run_v1_only("b/120545219")
def testLoadOpFromSignatureDef(self):
key = "adding_1_and_2_key"
add_op = math_ops.add(1, 2, name="adding_1_and_2")
signature_def = signature_def_utils_impl.op_signature_def(add_op, key)
self.assertEqual(
add_op,
signature_def_utils_impl.load_op_from_signature_def(signature_def, key))
# Force the test to run in graph mode.
# This tests a deprecated v1 API that uses functionality that does not work
# with eager tensors (namely build_tensor_info_from_op).
with ops.Graph().as_default():
key = "adding_1_and_2_key"
add_op = math_ops.add(1, 2, name="adding_1_and_2")
signature_def = signature_def_utils_impl.op_signature_def(add_op, key)
self.assertEqual(
add_op,
signature_def_utils_impl.load_op_from_signature_def(
signature_def, key))
if __name__ == "__main__":

View File

@ -126,7 +126,13 @@ def build_tensor_info_from_op(op):
Returns:
A TensorInfo protocol buffer constructed based on the supplied argument.
Raises:
RuntimeError: If eager execution is enabled.
"""
if context.executing_eagerly():
raise RuntimeError(
"build_tensor_info_from_op is not supported in Eager mode.")
return meta_graph_pb2.TensorInfo(
dtype=types_pb2.DT_INVALID,
tensor_shape=tensor_shape.unknown_shape().as_proto(),