forked from flyteorg/flytekit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconnector.py
More file actions
157 lines (125 loc) · 5.54 KB
/
Copy pathconnector.py
File metadata and controls
157 lines (125 loc) · 5.54 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
"""SageMaker batch-transform connector.
Mirrors the training-job connector. Targets ``CreateTransformJob`` /
``DescribeTransformJob`` / ``StopTransformJob``. Surfaces the predictions
``S3OutputPath`` so downstream Flyte tasks can read scores written by SageMaker
without any extra plumbing.
Note: ``TransformJobStatus`` has no ``Deleting`` state and there is no
``SecondaryStatus`` — running phase has no live progress signal beyond the job
being in flight.
"""
from dataclasses import dataclass
from datetime import datetime
from typing import Any, Dict, Optional
import cloudpickle
from flyteidl.core.execution_pb2 import TaskExecution
from flytekitplugins.awssagemaker_inference.boto3_mixin import (
Boto3ConnectorMixin,
CustomException,
)
from flytekit.extend.backend.base_connector import (
AsyncConnectorBase,
ConnectorRegistry,
Resource,
ResourceMeta,
)
from flytekit.models.literals import LiteralMap
from flytekit.models.task import TaskTemplate
@dataclass
class SageMakerTransformJobMetadata(ResourceMeta):
config: Dict[str, Any]
region: Optional[str] = None
inputs: Optional[LiteralMap] = None
def encode(self) -> bytes:
return cloudpickle.dumps(self)
@classmethod
def decode(cls, data: bytes) -> "SageMakerTransformJobMetadata":
return cloudpickle.loads(data)
_STATE_MAP = {
"InProgress": TaskExecution.RUNNING,
"Stopping": TaskExecution.RUNNING,
"Completed": TaskExecution.SUCCEEDED,
"Failed": TaskExecution.FAILED,
"Stopped": TaskExecution.FAILED,
}
def _isoformat(value: Any) -> Any:
if isinstance(value, datetime):
return value.isoformat()
return value
def _build_outputs(describe_response: Dict[str, Any]) -> Dict[str, Any]:
"""Project describe_transform_job down to a stable, downstream-friendly dict."""
transform_output = describe_response.get("TransformOutput") or {}
return {
"TransformJobArn": describe_response.get("TransformJobArn"),
"TransformJobName": describe_response.get("TransformJobName"),
"ModelName": describe_response.get("ModelName"),
"TransformOutput": {"S3OutputPath": transform_output.get("S3OutputPath")},
"TransformStartTime": _isoformat(describe_response.get("TransformStartTime")),
"TransformEndTime": _isoformat(describe_response.get("TransformEndTime")),
}
class SageMakerTransformJobConnector(Boto3ConnectorMixin, AsyncConnectorBase):
"""Long-running connector for SageMaker batch-transform jobs."""
name = "SageMaker Transform Job Connector"
def __init__(self):
super().__init__(
service="sagemaker",
task_type_name="sagemaker-transform-job",
metadata_type=SageMakerTransformJobMetadata,
)
async def create(
self, task_template: TaskTemplate, inputs: Optional[LiteralMap] = None, **kwargs
) -> SageMakerTransformJobMetadata:
custom = task_template.custom
config = custom.get("config")
region = custom.get("region")
try:
await self._call(
method="create_transform_job",
config=config,
inputs=inputs,
region=region,
)
except CustomException as e:
original_exception = e.original_exception
error_code = original_exception.response["Error"]["Code"]
error_message = original_exception.response["Error"]["Message"]
if e.idempotence_token and (
error_code == "ResourceInUse"
or (error_code == "ValidationException" and "Cannot create already existing" in error_message)
):
return SageMakerTransformJobMetadata(config=config, region=region, inputs=inputs)
raise e
return SageMakerTransformJobMetadata(config=config, region=region, inputs=inputs)
async def get(self, resource_meta: SageMakerTransformJobMetadata, **kwargs) -> Resource:
describe_response, _ = await self._call(
method="describe_transform_job",
config={"TransformJobName": resource_meta.config.get("TransformJobName")},
inputs=resource_meta.inputs,
region=resource_meta.region,
)
current_state = describe_response.get("TransformJobStatus")
flyte_phase = _STATE_MAP.get(current_state, TaskExecution.RUNNING)
message: Optional[str] = None
if current_state in ("Failed", "Stopped"):
message = describe_response.get("FailureReason")
outputs: Optional[Dict[str, Any]] = None
if current_state == "Completed":
outputs = {"result": _build_outputs(describe_response)}
return Resource(phase=flyte_phase, outputs=outputs, message=message)
async def delete(self, resource_meta: SageMakerTransformJobMetadata, **kwargs):
try:
await self._call(
method="stop_transform_job",
config={"TransformJobName": resource_meta.config.get("TransformJobName")},
region=resource_meta.region,
inputs=resource_meta.inputs,
)
except CustomException as e:
original_exception = e.original_exception
error_code = original_exception.response["Error"]["Code"]
error_message = original_exception.response["Error"]["Message"]
if error_code == "ResourceNotFound" or (
error_code == "ValidationException" and "non-running" in error_message
):
return
raise e
ConnectorRegistry.register(SageMakerTransformJobConnector())