Skip to content

handler

error.handler

ERROR_CODE_UNDEFINED module-attribute

ERROR_CODE_UNDEFINED: str = '999'

ERROR_MESSAGE_UNDEFINED module-attribute

ERROR_MESSAGE_UNDEFINED: str = 'Unknown error'

SUCCESS module-attribute

SUCCESS = 0

SUCCESS_MESSAGE module-attribute

SUCCESS_MESSAGE = 'Success'

P module-attribute

P = ParamSpec('P')

T module-attribute

T = TypeVar('T')

KNOWN_ERRORS module-attribute

KNOWN_ERRORS = {'torch.OutOfMemoryError': CudaOutOfMemoryError, 'torch.cuda.OutOfMemoryError': CudaOutOfMemoryError, 'numpy.core._exceptions._ArrayMemoryError': CpuOutOfMemoryError}

SaigeError

SaigeError(message=None)

Bases: Exception, ABC

Saige Research 6-digit error code

Info

notion.so/843e85df4fb7469c927170f66251da4c

Torch dataloader에서 작업 중 exception 발생 시 torch가 message를 넣어서 reraise하는 경우가 있음. 참고: https://teams.microsoft.com/l/message/19:874deea9bde44c2f9c2c43d4bc002a58@thread.tacv2/1688547469317?tenantId=4098be5f-d82a-474c-b4b6-a95727b671eb&groupId=3f51c33d-7ce9-4b2c-bf75-6d1032c4183b&parentMessageId=1688547469317&teamName=%EC%97%B0%EA%B5%AC%ED%8C%80&channelName=%5BPR%5D%20SaigeToolkit&createdTime=1688547469317&allowXTenantAccess=false

Source code in SaigeToolkit/error/base.py
def __init__(self, message=None) -> None:
    """
    Torch dataloader에서 작업 중 exception 발생 시 torch가 message를 넣어서 reraise하는 경우가 있음.
    참고: https://teams.microsoft.com/l/message/19:874deea9bde44c2f9c2c43d4bc002a58@thread.tacv2/1688547469317?tenantId=4098be5f-d82a-474c-b4b6-a95727b671eb&groupId=3f51c33d-7ce9-4b2c-bf75-6d1032c4183b&parentMessageId=1688547469317&teamName=%EC%97%B0%EA%B5%AC%ED%8C%80&channelName=%5BPR%5D%20SaigeToolkit&createdTime=1688547469317&allowXTenantAccess=false
    """
    super().__init__(message)
    if message is not None:
        # Add the message to the default message with new line and 2 spaces.
        self.message += f"\n  {message}"

head instance-attribute

head: str

repo instance-attribute

repo: str

code instance-attribute

code: str

message instance-attribute

message: str

__init_subclass__

__init_subclass__(**kwargs) -> None

Register a child class that inherits SaigeError with "repo", "code", and "message" attributes to ERROR_CODE_BOOK.

Source code in SaigeToolkit/error/base.py
def __init_subclass__(cls, **kwargs) -> None:
    """Register a child class that inherits SaigeError with "repo", "code", and "message" attributes to ERROR_CODE_BOOK."""

    super().__init_subclass__(**kwargs)

    if hasattr(cls, "repo") and not hasattr(cls, "code"):
        repo = cls.repo

        assert repo not in ERROR_REPO_BOOK or (
            ERROR_REPO_BOOK[repo].__name__ == cls.__name__
            and ERROR_REPO_BOOK[repo].head == cls.head
            and ERROR_REPO_BOOK[repo].__doc__ == cls.__doc__
        ), f"Error repo of {cls.__name__} & {ERROR_REPO_BOOK[repo].__name__} are duplicated!"

        ERROR_REPO_BOOK[repo] = cls

    if hasattr(cls, "repo") and hasattr(cls, "code") and hasattr(cls, "message"):
        code = cls.repo + cls.code

        assert code not in ERROR_CODE_BOOK or (
            ERROR_CODE_BOOK[code].__name__ == cls.__name__
            and ERROR_CODE_BOOK[code].code == cls.code
            and ERROR_CODE_BOOK[code].message == cls.message
        ), f"Error codes of {cls.__name__} & {ERROR_CODE_BOOK[code].__name__} are duplicated!"

        ERROR_CODE_BOOK[code] = cls

CpuOutOfMemoryError

CpuOutOfMemoryError(message=None)

Bases: SaigeToolkitError

Torch dataloader에서 작업 중 exception 발생 시 torch가 message를 넣어서 reraise하는 경우가 있음. 참고: https://teams.microsoft.com/l/message/19:874deea9bde44c2f9c2c43d4bc002a58@thread.tacv2/1688547469317?tenantId=4098be5f-d82a-474c-b4b6-a95727b671eb&groupId=3f51c33d-7ce9-4b2c-bf75-6d1032c4183b&parentMessageId=1688547469317&teamName=%EC%97%B0%EA%B5%AC%ED%8C%80&channelName=%5BPR%5D%20SaigeToolkit&createdTime=1688547469317&allowXTenantAccess=false

Source code in SaigeToolkit/error/base.py
def __init__(self, message=None) -> None:
    """
    Torch dataloader에서 작업 중 exception 발생 시 torch가 message를 넣어서 reraise하는 경우가 있음.
    참고: https://teams.microsoft.com/l/message/19:874deea9bde44c2f9c2c43d4bc002a58@thread.tacv2/1688547469317?tenantId=4098be5f-d82a-474c-b4b6-a95727b671eb&groupId=3f51c33d-7ce9-4b2c-bf75-6d1032c4183b&parentMessageId=1688547469317&teamName=%EC%97%B0%EA%B5%AC%ED%8C%80&channelName=%5BPR%5D%20SaigeToolkit&createdTime=1688547469317&allowXTenantAccess=false
    """
    super().__init__(message)
    if message is not None:
        # Add the message to the default message with new line and 2 spaces.
        self.message += f"\n  {message}"

code class-attribute instance-attribute

code = '202'

message class-attribute instance-attribute

message = 'CPU out of memory'

CudaOutOfMemoryError

CudaOutOfMemoryError(message=None)

Bases: SaigeToolkitError

Torch dataloader에서 작업 중 exception 발생 시 torch가 message를 넣어서 reraise하는 경우가 있음. 참고: https://teams.microsoft.com/l/message/19:874deea9bde44c2f9c2c43d4bc002a58@thread.tacv2/1688547469317?tenantId=4098be5f-d82a-474c-b4b6-a95727b671eb&groupId=3f51c33d-7ce9-4b2c-bf75-6d1032c4183b&parentMessageId=1688547469317&teamName=%EC%97%B0%EA%B5%AC%ED%8C%80&channelName=%5BPR%5D%20SaigeToolkit&createdTime=1688547469317&allowXTenantAccess=false

Source code in SaigeToolkit/error/base.py
def __init__(self, message=None) -> None:
    """
    Torch dataloader에서 작업 중 exception 발생 시 torch가 message를 넣어서 reraise하는 경우가 있음.
    참고: https://teams.microsoft.com/l/message/19:874deea9bde44c2f9c2c43d4bc002a58@thread.tacv2/1688547469317?tenantId=4098be5f-d82a-474c-b4b6-a95727b671eb&groupId=3f51c33d-7ce9-4b2c-bf75-6d1032c4183b&parentMessageId=1688547469317&teamName=%EC%97%B0%EA%B5%AC%ED%8C%80&channelName=%5BPR%5D%20SaigeToolkit&createdTime=1688547469317&allowXTenantAccess=false
    """
    super().__init__(message)
    if message is not None:
        # Add the message to the default message with new line and 2 spaces.
        self.message += f"\n  {message}"

code class-attribute instance-attribute

code = '200'

message class-attribute instance-attribute

message = 'CUDA out of memory'

get_error_handler

get_error_handler(head_error: Type[SaigeError])

Returns error handling decorator The handler wraps the original return value into (error_code, return_value) tuple.

Parameters:

Source code in SaigeToolkit/error/handler.py
def get_error_handler(head_error: Type[SaigeError]):
    """Returns error handling decorator
    The handler wraps the original return value into (error_code, return_value) tuple.

    Args:
        head_error (Type[SaigeError]): head error
    """

    def error_handler(function: Callable[P, T]) -> Callable[P, Tuple[int, str, Optional[T]]]:
        @wraps(function)
        def decorator(*args: P.args, **kwargs: P.kwargs) -> Tuple[int, Optional[T]]:
            try:
                return (SUCCESS, SUCCESS_MESSAGE, function(*args, **kwargs))

            except SaigeError as e:
                error_code = -int(head_error.head + e.repo + e.code)
                message = e.message
                # use `from e` to print the cause of the error.
                if e.__cause__ is not None:
                    # Add the message to the default message with new line and 2 spaces.
                    message += f"\n  [from] {e.__cause__.__class__.__name__}: {e.__cause__}"
                return (error_code, message, None)

            except Exception as e:
                error = KNOWN_ERRORS.get(f"{e.__class__.__module__}.{e.__class__.__qualname__}")

                if error is None:
                    # XXX: `traceback` has potential concern about divulgence of intellectual property,
                    # although not protected at the moment due to customer service issue.
                    print(traceback.format_exc(), file=sys.stderr)
                    repo, code, message = head_error.repo, ERROR_CODE_UNDEFINED, ERROR_MESSAGE_UNDEFINED
                else:
                    repo, code, message = error.repo, error.code, error.message

                error_code = -int(head_error.head + repo + code)
                return (error_code, message, None)

        return decorator

    return error_handler