-
Notifications
You must be signed in to change notification settings - Fork 58
Expand file tree
/
Copy pathamazon_sagemaker_endpoint.py
More file actions
356 lines (298 loc) · 11.5 KB
/
amazon_sagemaker_endpoint.py
File metadata and controls
356 lines (298 loc) · 11.5 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
import logging
import re
from typing import NamedTuple
from uuid import UUID
import boto3
from botocore.exceptions import ClientError
from django.conf import settings
from lambda_tasks.logging import task_logger
from grandchallenge.components.backends.amazon_sagemaker_training import (
AmazonSageMakerTrainingExecutor,
)
from grandchallenge.components.backends.base import (
list_and_delete_objects_from_prefix,
)
from grandchallenge.components.backends.exceptions import ComponentException
from grandchallenge.components.backends.utils import UUID4_REGEX
from grandchallenge.core.error_messages import SystemErrorMessages
logger = logging.getLogger(__name__)
class ObjectParams(NamedTuple):
app_label: str
model_name: str
pk: UUID
class EndpointOrchestrator:
def __init__(
self,
endpoint_name,
job_id,
exec_image_repo_tag,
requires_gpu_type,
memory_limit,
api_method,
signing_key,
time_limit=settings.ALGORITHM_ENDPOINTS_MAXIMUM_INVOCATION_DURATION,
algorithm_model=None,
):
self._executor = AmazonSageMakerTrainingExecutor(
job_id=job_id,
exec_image_repo_tag=exec_image_repo_tag,
memory_limit=memory_limit,
time_limit=time_limit,
requires_gpu_type=requires_gpu_type,
use_warm_pool=False,
signing_key=signing_key,
api_method=api_method,
algorithm_model=None,
input_bucket_name=settings.ALGORITHM_ENDPOINTS_INPUT_BUCKET_NAME,
output_bucket_name=settings.ALGORITHM_ENDPOINTS_OUTPUT_BUCKET_NAME,
use_task_list=False,
)
self._endpoint_name = endpoint_name
self._exec_image_repo_tag = exec_image_repo_tag
self._algorithm_model = algorithm_model
self.__sagemaker_runtime_client = None
@property
def _s3_client(self):
return self._executor._s3_client
@property
def _sagemaker_client(self):
return self._executor._sagemaker_client
@property
def _sagemaker_runtime_client(self):
if self.__sagemaker_runtime_client is None:
self.__sagemaker_runtime_client = boto3.client(
"sagemaker-runtime",
region_name=settings.COMPONENTS_AMAZON_ECR_REGION,
)
return self.__sagemaker_runtime_client
@property
def _auxiliary_data_prefix(self):
return self._executor._auxiliary_data_prefix
@property
def _io_prefix(self):
return self._executor._io_prefix
@property
def _algorithm_model_key(self):
return self._executor._algorithm_model_key
@property
def _algorithm_model_s3_uri(self):
return f"s3://{settings.ALGORITHM_ENDPOINTS_INPUT_BUCKET_NAME}/{self._algorithm_model_key}"
@property
def _output_s3_uri(self):
return f"s3://{settings.ALGORITHM_ENDPOINTS_OUTPUT_BUCKET_NAME}/{self._io_prefix}/successes"
@property
def _failure_s3_uri(self):
return f"s3://{settings.ALGORITHM_ENDPOINTS_OUTPUT_BUCKET_NAME}/{self._io_prefix}/failures"
@property
def _invocation_key(self):
return self._executor._invocation_key
@property
def _invocation_s3_uri(self):
return f"s3://{settings.ALGORITHM_ENDPOINTS_INPUT_BUCKET_NAME}/{self._invocation_key}"
@property
def invocation_environment(self):
env = self._executor.invocation_environment
if self._algorithm_model:
env["GRAND_CHALLENGE_COMPONENT_MODEL"] = (
self._algorithm_model_s3_uri
)
return env
@property
def _instance_type(self):
return self._executor._instance_type
@property
def invoke_duration(self):
return self._executor.invoke_duration
@property
def usd_cents_per_hour(self):
return self._executor.usd_cents_per_hour
@property
def _required_volume_size_gb(self):
if self._instance_type.nvme_volume_size:
# This setting has no practical effect as the instances
# do not get an EBS volume
return self._instance_type.nvme_volume_size
else:
return 30
@property
def _time_limit(self):
return self._executor._time_limit
def provision_auxiliary_data(self):
if self._algorithm_model:
self._s3_client.copy(
CopySource={
"Bucket": settings.PROTECTED_S3_STORAGE_KWARGS[
"bucket_name"
],
"Key": str(self._algorithm_model),
},
Bucket=settings.ALGORITHM_ENDPOINTS_INPUT_BUCKET_NAME,
Key=self._algorithm_model_key,
)
def deprovision_auxiliary_data(self):
list_and_delete_objects_from_prefix(
s3_client=self._s3_client,
bucket=settings.ALGORITHM_ENDPOINTS_INPUT_BUCKET_NAME,
prefix=self._auxiliary_data_prefix,
)
def create_sagemaker_model(self):
self._sagemaker_client.create_model(
ModelName=self._endpoint_name,
ExecutionRoleArn=settings.ALGORITHM_ENDPOINTS_EXECUTION_ROLE_ARN,
PrimaryContainer={
"Image": self._exec_image_repo_tag,
"Environment": self.invocation_environment,
"Mode": "SingleModel",
},
VpcConfig={
"SecurityGroupIds": [
settings.ALGORITHM_ENDPOINTS_SECURITY_GROUP_ID
],
"Subnets": settings.ALGORITHM_ENDPOINTS_SUBNETS,
},
)
def delete_sagemaker_model(self):
self._sagemaker_client.delete_model(ModelName=self._endpoint_name)
def create_endpoint_config(self):
self._sagemaker_client.create_endpoint_config(
EndpointConfigName=self._endpoint_name,
AsyncInferenceConfig={
"ClientConfig": {
"MaxConcurrentInvocationsPerInstance": 1,
},
"OutputConfig": {
"S3FailurePath": self._failure_s3_uri,
"S3OutputPath": self._output_s3_uri,
"NotificationConfig": {
"SuccessTopic": settings.ALGORITHM_ENDPOINTS_SNS_TOPIC_ARN,
"ErrorTopic": settings.ALGORITHM_ENDPOINTS_SNS_TOPIC_ARN,
},
},
},
ProductionVariants=[
{
"VariantName": self._endpoint_name,
"ContainerStartupHealthCheckTimeoutInSeconds": 300,
"InitialInstanceCount": 1,
"InitialVariantWeight": 1,
"InstanceType": self._instance_type.name,
"ModelName": self._endpoint_name,
"VolumeSizeInGB": self._required_volume_size_gb,
}
],
)
def delete_endpoint_config(self):
self._sagemaker_client.delete_endpoint_config(
EndpointConfigName=self._endpoint_name
)
def create_endpoint(self):
self._sagemaker_client.create_endpoint(
EndpointName=self._endpoint_name,
EndpointConfigName=self._endpoint_name,
)
def delete_endpoint(self):
self._sagemaker_client.delete_endpoint(
EndpointName=self._endpoint_name
)
def deprovision(self):
def attempt(method):
try:
method()
except ClientError as error:
if (
error.response["Error"]["Code"] == "ValidationException"
and "Could not find" in error.response["Error"]["Message"]
):
pass # Nothing to clean up
else:
raise
attempt(self.delete_endpoint)
attempt(self.delete_endpoint_config)
attempt(self.delete_sagemaker_model)
self.deprovision_auxiliary_data()
@staticmethod
def get_endpoint_name(*, event):
return event["EndpointName"]
@staticmethod
def _get_endpoint_status(*, event):
return event["EndpointStatus"]
@staticmethod
def get_endpoint_params(*, endpoint_name):
prefix_regex = re.escape(settings.COMPONENTS_REGISTRY_PREFIX)
pattern = rf"^{prefix_regex}\-AE\-(?P<pk>{UUID4_REGEX})$"
result = re.match(pattern, endpoint_name)
if result is None:
raise ValueError("Invalid endpoint name")
else:
pk = result.group("pk")
return ObjectParams(
app_label="algorithms",
model_name="endpoint",
pk=pk,
)
def handle_status_event(self, *, event):
endpoint_status = self._get_endpoint_status(event=event)
if endpoint_status == "IN_SERVICE":
return
elif endpoint_status == "FAILED":
# Requires investigation
task_logger.info(event)
task_logger.error("Starting endpoint failed")
raise ComponentException(SystemErrorMessages.UNEXPECTED_ERROR)
else:
raise ValueError("Invalid endpoint status")
def provision_invocation_input_data(self, *, input_civs):
self._executor.provision(input_civs=input_civs, input_prefixes={})
def invoke_endpoint(self, *, inference_id):
self._sagemaker_runtime_client.invoke_endpoint_async(
EndpointName=self._endpoint_name,
ContentType="application/json",
InputLocation=self._invocation_s3_uri,
InferenceId=inference_id,
InvocationTimeoutSeconds=int(self._time_limit.total_seconds()),
)
@staticmethod
def get_inference_id(*, event):
return event["inferenceId"]
@staticmethod
def _get_invocation_status(*, event):
return event["invocationStatus"]
@staticmethod
def get_invocation_params(*, inference_id):
prefix_regex = re.escape(settings.COMPONENTS_REGISTRY_PREFIX)
pattern = rf"^{prefix_regex}\-AEI\-(?P<pk>{UUID4_REGEX})$"
result = re.match(pattern, inference_id)
if result is None:
raise ValueError("Invalid inference id")
else:
pk = result.group("pk")
return ObjectParams(
app_label="algorithms",
model_name="invocation",
pk=pk,
)
def handle_event(self, *, event):
invocation_status = self._get_invocation_status(event=event)
# TODO: set task_logs and runtime metrics
if invocation_status == "Completed":
self._handle_completed_invocation()
elif invocation_status == "Expired":
self._handle_expired_invocation(event=event)
elif invocation_status == "Failed":
self._handle_failed_invocation(event=event)
else:
raise ValueError("Invalid invocation status")
def _handle_completed_invocation(self):
self._executor._handle_completed_job()
def _handle_expired_invocation(self, *, event):
# Requires investigation
task_logger.info(event)
task_logger.error("Endpoint invocation expired")
raise ComponentException(SystemErrorMessages.UNEXPECTED_ERROR)
def _handle_failed_invocation(self, *, event):
# Requires investigation
task_logger.info(event)
task_logger.error("Endpoint invocation failed")
raise ComponentException(SystemErrorMessages.UNEXPECTED_ERROR)
def get_outputs(self, *, output_interfaces):
return self._executor.get_outputs(output_interfaces=output_interfaces)