Skip to content

Make exceptions less verbose by default #1330

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 20 additions & 25 deletions pytensor/compile/function/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -949,32 +949,27 @@ def __call__(self, *args, output_subset=None, **kwargs):

except Exception as e:
i = input_storage.index(arg_container)
function_name = "pytensor function"
argument_name = "argument"
if self.name:
function_name += ' with name "' + self.name + '"'
if hasattr(arg, "name") and arg.name:
argument_name += ' with name "' + arg.name + '"'
where = get_variable_trace_string(self.maker.inputs[i].variable)
if len(e.args) == 1:
e.args = (
"Bad input "
+ argument_name
+ " to "
+ function_name
+ f" at index {int(i)} (0-based). {where}"
+ e.args[0],
function_name = (
"pytensor function"
if self.name
else f"pytensor function '{self.name}'"
)
argument_name = (
f"argument {arg.name}"
if hasattr(arg, "name")
else "argument"
)
where = (
""
if config.exception_verbosity == "low"
else get_variable_trace_string(
self.maker.inputs[i].variable
)
else:
e.args = (
"Bad input "
+ argument_name
+ " to "
+ function_name
+ f" at index {int(i)} (0-based). {where}"
) + e.args
self._restore_defaults()
raise
)
e.add_note(
f"\nInvalid {argument_name} to {function_name} at index {i}.{where}"
)
raise e
arg_container.provided += 1

# Set keyword arguments
Expand Down
11 changes: 2 additions & 9 deletions pytensor/configdefaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -638,15 +638,8 @@ def add_error_and_warning_configvars():
# on all important apply nodes.
config.add(
"exception_verbosity",
"If 'low', the text of exceptions will generally refer "
"to apply nodes with short names such as "
"Elemwise{add_no_inplace}. If 'high', some exceptions "
"will also refer to apply nodes with long descriptions "
""" like:
A. Elemwise{add_no_inplace}
B. log_likelihood_v_given_h
C. log_likelihood_h""",
EnumStr("low", ["high"]),
"Verbosity of exceptions generated by PyTensor functions.",
EnumStr("low", ["medium", "high"]),
in_c_key=False,
)

Expand Down
6 changes: 6 additions & 0 deletions pytensor/link/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,12 @@ def raise_with_op(
# print a simple traceback from KeyboardInterrupt
raise exc_value.with_traceback(exc_trace)

if verbosity == "low":
exc_value.add_note(
"\nHINT: Set PyTensor `config.exception_verbosity` to `medium` or `high` for more information about the source of the error."
)
raise exc_value.with_traceback(exc_trace)

trace = getattr(node.outputs[0].tag, "trace", ())

exc_value.__thunk_trace__ = trace
Expand Down
Loading