xref: /aosp_15_r20/external/pytorch/torch/_export/db/logging.py (revision da0073e96a02ea20f0ac840b70461e3646d07c45)
1# mypy: allow-untyped-defs
2
3
4def exportdb_error_message(case_name: str):
5    from .examples import all_examples
6    from torch._utils_internal import log_export_usage
7
8    ALL_EXAMPLES = all_examples()
9    # Detect whether case_name is really registered in exportdb.
10    if case_name in ALL_EXAMPLES:
11        url_case_name = case_name.replace("_", "-")
12        return f"See {case_name} in exportdb for unsupported case. \
13                https://pytorch.org/docs/main/generated/exportdb/index.html#{url_case_name}"
14    else:
15        log_export_usage(
16            event="export.error.casenotregistered",
17            message=case_name,
18        )
19        return f"{case_name} is unsupported."
20
21
22def get_class_if_classified_error(e):
23    """
24    Returns a string case name if the export error e is classified.
25    Returns None otherwise.
26    """
27
28    from torch._dynamo.exc import TorchRuntimeError, Unsupported, UserError
29
30    ALWAYS_CLASSIFIED = "always_classified"
31    DEFAULT_CLASS_SIGIL = "case_name"
32
33    # add error types that should be classified, along with any attribute name
34    # whose presence acts like a sigil to further distinguish which errors of
35    # that type should be classified. If the attribute name is None, then the
36    # error type is always classified.
37    _ALLOW_LIST = {
38        Unsupported: DEFAULT_CLASS_SIGIL,
39        UserError: DEFAULT_CLASS_SIGIL,
40        TorchRuntimeError: None,
41    }
42    if type(e) in _ALLOW_LIST:
43        attr_name = _ALLOW_LIST[type(e)]
44        if attr_name is None:
45            return ALWAYS_CLASSIFIED
46        return getattr(e, attr_name, None)
47    return None
48