From 0f379ec01fbc74e5d3154c144c878bce59eb4992 Mon Sep 17 00:00:00 2001 From: Allen Lavoie Date: Mon, 13 May 2019 09:22:15 -0700 Subject: [PATCH] Prettier printing for no-matching-signature errors in loaded SavedModels Example (from the unit test): ValueError: Could not find matching function to call loaded from the SavedModel. Got: Positional arguments (2 total): * [7, ({'a': }, )] * True Keyword arguments: {} Expected these arguments to match one of the following 1 option(s): Option 1: Positional arguments (2 total): * [6, ({u'a': TensorSpec(shape=(), dtype=tf.int32, name=u'x/1/0/a')}, TensorSpec(shape=(), dtype=tf.int32, name=u'x/1/1'))] * True Keyword arguments: {} PiperOrigin-RevId: 247948754 --- .../saved_model/function_deserialization.py | 26 ++++++++++++++----- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/tensorflow/python/saved_model/function_deserialization.py b/tensorflow/python/saved_model/function_deserialization.py index 3c8a58793ba..94618989e4f 100644 --- a/tensorflow/python/saved_model/function_deserialization.py +++ b/tensorflow/python/saved_model/function_deserialization.py @@ -234,14 +234,26 @@ def recreate_function(saved_function, concrete_functions): if _concrete_function_callable_with(function, inputs, allow_conversion): return _call_concrete_function(function, inputs) - available_signatures = [ - concrete_functions[function_name].graph.structured_input_signature - for function_name in saved_function.concrete_functions - ] + signature_descriptions = [] + + def _pretty_format_positional(positional): + return "Positional arguments ({} total):\n * {}".format( + len(positional), + "\n * ".join([str(a) for a in positional])) + + for index, function_name in enumerate(saved_function.concrete_functions): + concrete_function = concrete_functions[function_name] + positional, keyword = concrete_function.structured_input_signature + signature_descriptions.append( + "Option {}:\n {}\n Keyword arguments: {}" + .format(index + 1, _pretty_format_positional(positional), keyword)) raise ValueError( - "Could not find matching function to call for inputs %r. " - "Only existing signatures are %r." - % (inputs, available_signatures)) + "Could not find matching function to call loaded from the SavedModel. " + "Got:\n {}\n Keyword arguments: {}\n\nExpected " + "these arguments to match one of the following {} option(s):\n\n{}" + .format(_pretty_format_positional(args), kwargs, + len(saved_function.concrete_functions), + "\n\n".join(signature_descriptions))) concrete_function_objects = [] for concrete_function_name in saved_function.concrete_functions: