Skip to content
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

[ONNX] Diagnostic option 'warnings_as_errors' #105886

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 24 additions & 0 deletions test/onnx/internal/test_diagnostics.py
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,30 @@ def test_diagnostic_context_raises_if_diagnostic_is_error(self):
)
)

def test_diagnostic_context_raises_original_exception_from_diagnostic_created_from_it(
self,
):
with self.assertRaises(ValueError):
try:
raise ValueError("original exception")
except ValueError as e:
diagnostic = infra.Diagnostic(
self.rules.rule_without_message_args, infra.Level.ERROR
)
diagnostic = diagnostic.with_source_exception(e)
self.context.log_and_raise_if_error(diagnostic)

def test_diagnostic_context_raises_if_diagnostic_is_warning_and_warnings_as_errors_is_true(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice clear test naming 🎉

self,
):
with self.assertRaises(infra.RuntimeErrorWithDiagnostic):
self.context.options.warnings_as_errors = True
self.context.log_and_raise_if_error(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to include this part to enable it? How do we use log_and_raise_if_error and option: warnings_as_errors?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

log_and_raise_if_error is used by diagnose_call internally, so for inflight diagnostics if you set its level as warning, and warnings_as_errors is turned on, the inflight diagnostics becomes an error and raises. E.g, if warnings_as_errors is turned on, failed op_level_debug w/ warning diagnostic will automatically become error and raise.

For regular diagnostics, one always need to log them into diagnostic_context. log_and_raise_if_error is the api for that.

warnings_as_errors is a flag under DiagnosticOptions. This is not exposed in export api though, that is left as a follow up to discuss.

infra.Diagnostic(
self.rules.rule_without_message_args, infra.Level.WARNING
)
)


if __name__ == "__main__":
common_utils.run_tests()
2 changes: 2 additions & 0 deletions torch/onnx/_internal/diagnostics/infra/_infra.py
Original file line number Diff line number Diff line change
Expand Up @@ -323,3 +323,5 @@ class DiagnosticOptions:

log_verbose: bool = dataclasses.field(default=False)
log_level: Level = dataclasses.field(default=Level.ERROR)
warnings_as_errors: bool = dataclasses.field(default=False)
"""If True, warnings are treated as errors."""
8 changes: 5 additions & 3 deletions torch/onnx/_internal/diagnostics/infra/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,16 +255,18 @@ def log(self, diagnostic: Diagnostic) -> None:
raise TypeError(
f"Expected diagnostic of type {Diagnostic}, got {type(diagnostic)}"
)
if self.options.warnings_as_errors and diagnostic.level == infra.Level.WARNING:
diagnostic.level = infra.Level.ERROR
self.diagnostics.append(diagnostic)
self.logger.log(diagnostic.level, diagnostic.message)
self.logger.log(diagnostic.level, diagnostic.additional_message)

def log_and_raise_if_error(self, diagnostic: Diagnostic) -> None:
self.log(diagnostic)
if diagnostic.level == infra.Level.ERROR:
raise RuntimeErrorWithDiagnostic(
diagnostic
) from diagnostic.source_exception
if diagnostic.source_exception is not None:
raise diagnostic.source_exception
raise RuntimeErrorWithDiagnostic(diagnostic)

@contextlib.contextmanager
def add_inflight_diagnostic(
Expand Down