File size: 1,584 Bytes
36333c5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
"""Domain errors converted into stable API responses."""


class GatewayError(Exception):
    """An expected error that is safe to return to an API consumer."""

    def __init__(
        self, message: str, *, status_code: int = 400, code: str = "gateway_error"
    ) -> None:
        super().__init__(message)
        self.message = message
        self.status_code = status_code
        self.code = code


class QueueCapacityError(GatewayError):
    """The bounded inference queue cannot accept another job."""

    def __init__(self) -> None:
        super().__init__(
            "The inference queue is full. Retry later.", status_code=503, code="queue_full"
        )


class ModelLoadError(GatewayError):
    """A model could not be initialized."""

    def __init__(self, model_name: str, reason: str) -> None:
        super().__init__(
            f"Could not load {model_name}: {reason}",
            status_code=503,
            code="model_load_failed",
        )


class InferenceError(GatewayError):
    """A model failed while generating an output."""

    def __init__(self, model_name: str, reason: str) -> None:
        super().__init__(
            f"{model_name} inference failed: {reason}",
            status_code=500,
            code="inference_failed",
        )


class OutOfMemoryError(GatewayError):
    """The selected execution device ran out of memory."""

    def __init__(self, model_name: str) -> None:
        super().__init__(
            f"{model_name} ran out of memory",
            status_code=507,
            code="out_of_memory",
        )