xref: /aosp_15_r20/external/executorch/exir/error.py (revision 523fa7a60841cd1ecfb9cc4201f1ca8b03ed023a)
1# Copyright (c) Meta Platforms, Inc. and affiliates.
2# All rights reserved.
3#
4# This source code is licensed under the BSD-style license found in the
5# LICENSE file in the root directory of this source tree.
6
7# pyre-strict
8from enum import Enum
9
10
11class ExportErrorType(Enum):
12    INVALID_INPUT_TYPE = (
13        1  # User providing invalid inputs to either tracer, or other public facing APIs
14    )
15    INVALID_OUTPUT_TYPE = (
16        2  # User returning values from their models that we don’t support.
17    )
18    VIOLATION_OF_SPEC = 3  # User doing something against our assumptions.
19    NOT_SUPPORTED = 4  # User’s code contains types and functionalities we don’t support
20    MISSING_PROPERTY = 5  # User's code didn't provide necessary details for us to successfully trace and export. For example, we use a lot of decorators and ask users to annotate their model.
21    UNINITIALIZED = 6  # User is usingan API without proper initialization step.
22
23
24def internal_assert(pred: bool, assert_msg: str) -> None:
25    """
26    This is exir's custom assert method. It internally just throws InternalError.
27    Note that the sole purpose is to throw our own error while maintaining similar syntax
28    as python assert.
29    """
30
31    if not pred:
32        raise InternalError(assert_msg)
33
34
35class InternalError(Exception):
36    """
37    Raised when an internal invariance is violated in EXIR stack.
38    Should hint users to report a bug to dev and expose the original
39    error message.
40    """
41
42    def __init__(self, message: str) -> None:
43        super().__init__(message)
44
45
46class ExportError(Exception):
47    """
48    This type of exception is raised for errors that are directly caused by the user
49    code. In general, user errors happen during model authoring, tracing, using our public
50    facing APIs, and writing graph passes.
51    """
52
53    def __init__(self, error_code: ExportErrorType, message: str) -> None:
54        prefix = f"[{error_code}]: "
55        super().__init__(prefix + message)
56