-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathpredictor_async.py
463 lines (397 loc) · 19.4 KB
/
predictor_async.py
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"). You
# may not use this file except in compliance with the License. A copy of
# the License is located at
#
# http://aws.amazon.com/apache2.0/
#
# or in the "license" file accompanying this file. This file is
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
# ANY KIND, either express or implied. See the License for the specific
# language governing permissions and limitations under the License.
"""Placeholder docstring"""
from __future__ import absolute_import
import threading
import time
import uuid
from botocore.exceptions import WaiterError
from sagemaker import s3
from sagemaker.exceptions import PollingTimeoutError, AsyncInferenceModelError
from sagemaker.async_inference import WaiterConfig, AsyncInferenceResponse
from sagemaker.s3 import parse_s3_url
from sagemaker.session import Session
from sagemaker.utils import name_from_base, sagemaker_timestamp, format_tags
class AsyncPredictor:
"""Make async prediction requests to an Amazon SageMaker endpoint."""
def __init__(
self,
predictor,
name=None,
):
"""Initialize an ``AsyncPredictor``.
Args:
predictor (sagemaker.predictor.Predictor): General ``Predictor``
object has useful methods and variables. ``AsyncPredictor``
stands on top of it with capability for async inference.
"""
self.predictor = predictor
self.endpoint_name = predictor.endpoint_name
self.sagemaker_session = predictor.sagemaker_session or Session()
if self.sagemaker_session.s3_client is None:
self.s3_client = self.sagemaker_session.boto_session.client(
"s3",
region_name=self.sagemaker_session.boto_region_name,
)
else:
self.s3_client = self.sagemaker_session.s3_client
self.serializer = predictor.serializer
self.deserializer = predictor.deserializer
self.name = name
self._endpoint_config_name = None
self._model_names = None
self._context = None
self._input_path = None
def predict(
self,
data=None,
input_path=None,
initial_args=None,
inference_id=None,
waiter_config=WaiterConfig(),
):
"""Wait and return the Async Inference result from the specified endpoint.
Args:
data (object): Input data for which you want the model to provide
inference. If a serializer was specified in the encapsulated
Predictor object, the result of the serializer is sent as input
data. Otherwise the data must be sequence of bytes, and the
predict method then sends the bytes in the request body as is.
input_path (str): Amazon S3 URI contains input data for which you want
the model to provide async inference. (Default: None)
initial_args (dict[str,str]): Optional. Default arguments for boto3
``invoke_endpoint_async`` call. (Default: None).
inference_id (str): If you provide a value, it is added to the captured data
when you enable data capture on the endpoint (Default: None).
waiter_config (sagemaker.async_inference.waiter_config.WaiterConfig): Configuration
for the waiter. (Default: {"Delay": 15 seconds, "MaxAttempts": 60}
Raises:
ValueError: If both input data and input Amazon S3 path are not provided
Returns:
object: Inference for the given input. If a deserializer was specified when creating
the Predictor, the result of the deserializer is
returned. Otherwise the response returns the sequence of bytes
as is.
"""
if data is None and input_path is None:
raise ValueError(
"Please provide input data or input Amazon S3 location to use async prediction"
)
if data is not None:
input_path = self._upload_data_to_s3(data, input_path)
self._input_path = input_path
response = self._submit_async_request(input_path, initial_args, inference_id)
output_location = response["OutputLocation"]
failure_location = response.get("FailureLocation")
result = self._wait_for_output(
output_path=output_location, failure_path=failure_location, waiter_config=waiter_config
)
return result
def predict_async(
self,
data=None,
input_path=None,
initial_args=None,
inference_id=None,
):
"""Return the Async Inference ouput Amazon S3 path from the specified endpoint.
Args:
data (object): Input data for which you want the model to provide
inference. If a serializer was specified in the encapsulated
Predictor object, the result of the serializer is sent as input
data. Otherwise the data must be sequence of bytes, and the
predict method then upload the data to the ``input_s3_path``. If
``input_s3_path`` is None, upload the data to
input_path (str): Amazon S3 URI contains input data for which you want
the model to provide async inference. (Default: None)
initial_args (dict[str,str]): Optional. Default arguments for boto3
``invoke_endpoint_async`` call. (Default: None).
inference_id (str): If you provide a value, it is added to the captured data
when you enable data capture on the endpoint (Default: None).
Raises:
ValueError: If both input data and input Amazon S3 path are not provided
Returns:
AsyncInferenceResponse:
Inference response for the given input. It provides method to check
the result in the Amazon S3 output path.
"""
if data is None and input_path is None:
raise ValueError(
"Please provide input data or input Amazon S3 location to use async prediction"
)
if data is not None:
input_path = self._upload_data_to_s3(data, input_path)
self._input_path = input_path
response = self._submit_async_request(input_path, initial_args, inference_id)
output_location = response["OutputLocation"]
failure_location = response.get("FailureLocation")
response_async = AsyncInferenceResponse(
predictor_async=self,
output_path=output_location,
failure_path=failure_location,
)
return response_async
def _upload_data_to_s3(
self,
data,
input_path=None,
):
"""Upload request data to Amazon S3 for users"""
if input_path:
bucket, key = parse_s3_url(input_path)
else:
my_uuid = str(uuid.uuid4())
timestamp = sagemaker_timestamp()
bucket = self.sagemaker_session.default_bucket()
key = s3.s3_path_join(
self.sagemaker_session.default_bucket_prefix,
"async-endpoint-inputs",
name_from_base(self.name, short=True),
"{}-{}".format(timestamp, my_uuid),
)
data = self.serializer.serialize(data)
self.s3_client.put_object(
Body=data, Bucket=bucket, Key=key, ContentType=self.serializer.CONTENT_TYPE
)
input_path = input_path or "s3://{}/{}".format(bucket, key)
return input_path
def _create_request_args(
self,
input_path,
initial_args=None,
inference_id=None,
):
"""Create request args for ``invoke_endpoint_async``"""
args = dict(initial_args) if initial_args else {}
args["InputLocation"] = input_path
if "EndpointName" not in args:
args["EndpointName"] = self.predictor.endpoint_name
if "Accept" not in args:
args["Accept"] = ", ".join(self.predictor.accept)
if inference_id:
args["InferenceId"] = inference_id
return args
def _submit_async_request(
self,
input_path,
initial_args,
inference_id,
):
"""Create request and invoke async endpoint with the request"""
request_args = self._create_request_args(input_path, initial_args, inference_id)
response = self.sagemaker_session.sagemaker_runtime_client.invoke_endpoint_async(
**request_args
)
return response
def _wait_for_output(self, output_path, failure_path, waiter_config):
"""Retrieve output based on the presense of failure_path."""
if failure_path is not None:
return self._check_output_and_failure_paths(output_path, failure_path, waiter_config)
return self._check_output_path(output_path, waiter_config)
def _check_output_path(self, output_path, waiter_config):
"""Check the Amazon S3 output path for the output.
Periodically check Amazon S3 output path for async inference result.
Timeout automatically after max attempts reached
"""
bucket, key = parse_s3_url(output_path)
s3_waiter = self.s3_client.get_waiter("object_exists")
try:
s3_waiter.wait(Bucket=bucket, Key=key, WaiterConfig=waiter_config._to_request_dict())
except WaiterError:
raise PollingTimeoutError(
message="Inference could still be running",
output_path=output_path,
seconds=waiter_config.delay * waiter_config.max_attempts,
)
s3_object = self.s3_client.get_object(Bucket=bucket, Key=key)
result = self.predictor._handle_response(response=s3_object)
return result
def _check_output_and_failure_paths(self, output_path, failure_path, waiter_config):
"""Check the Amazon S3 output path for the output.
This method waits for either the output file or the failure file to be found on the
specified S3 output path. Whichever file is found first, its corresponding event is
triggered, and the method executes the appropriate action based on the event.
Args:
output_path (str): The S3 path where the output file is expected to be found.
failure_path (str): The S3 path where the failure file is expected to be found.
waiter_config (boto3.waiter.WaiterConfig): The configuration for the S3 waiter.
Returns:
Any: The deserialized result from the output file, if the output file is found first.
Otherwise, raises an exception.
Raises:
AsyncInferenceModelError: If the failure file is found before the output file.
PollingTimeoutError: If both files are not found and the S3 waiter
has thrown a WaiterError.
"""
output_bucket, output_key = parse_s3_url(output_path)
failure_bucket, failure_key = parse_s3_url(failure_path)
output_file_found = threading.Event()
failure_file_found = threading.Event()
waiter_error_catched = threading.Event()
def check_output_file():
try:
output_file_waiter = self.s3_client.get_waiter("object_exists")
output_file_waiter.wait(
Bucket=output_bucket,
Key=output_key,
WaiterConfig=waiter_config._to_request_dict(),
)
output_file_found.set()
except WaiterError:
waiter_error_catched.set()
def check_failure_file():
try:
failure_file_waiter = self.s3_client.get_waiter("object_exists")
failure_file_waiter.wait(
Bucket=failure_bucket,
Key=failure_key,
WaiterConfig=waiter_config._to_request_dict(),
)
failure_file_found.set()
except WaiterError:
waiter_error_catched.set()
output_thread = threading.Thread(target=check_output_file)
failure_thread = threading.Thread(target=check_failure_file)
output_thread.start()
failure_thread.start()
while (
not output_file_found.is_set()
and not failure_file_found.is_set()
and not waiter_error_catched.is_set()
):
time.sleep(1)
if output_file_found.is_set():
s3_object = self.s3_client.get_object(Bucket=output_bucket, Key=output_key)
result = self.predictor._handle_response(response=s3_object)
return result
if failure_file_found.is_set():
failure_object = self.s3_client.get_object(Bucket=failure_bucket, Key=failure_key)
failure_response = self.predictor._handle_response(response=failure_object)
raise AsyncInferenceModelError(message=failure_response)
raise PollingTimeoutError(
message="Inference could still be running",
output_path=output_path,
seconds=waiter_config.delay * waiter_config.max_attempts,
)
def update_endpoint(
self,
initial_instance_count=None,
instance_type=None,
accelerator_type=None,
model_name=None,
tags=None,
kms_key=None,
data_capture_config_dict=None,
wait=True,
):
"""Update the existing endpoint with the provided attributes.
This creates a new EndpointConfig in the process. If ``initial_instance_count``,
``instance_type``, ``accelerator_type``, or ``model_name`` is specified, then a new
ProductionVariant configuration is created; values from the existing configuration
are not preserved if any of those parameters are specified.
Args:
initial_instance_count (int): The initial number of instances to run in the endpoint.
This is required if ``instance_type``, ``accelerator_type``, or ``model_name`` is
specified. Otherwise, the values from the existing endpoint configuration's
ProductionVariants are used.
instance_type (str): The EC2 instance type to deploy the endpoint to.
This is required if ``initial_instance_count`` or ``accelerator_type`` is specified.
Otherwise, the values from the existing endpoint configuration's
``ProductionVariants`` are used.
accelerator_type (str): The type of Elastic Inference accelerator to attach to
the endpoint, e.g. "ml.eia1.medium". If not specified, and
``initial_instance_count``, ``instance_type``, and ``model_name`` are also ``None``,
the values from the existing endpoint configuration's ``ProductionVariants`` are
used. Otherwise, no Elastic Inference accelerator is attached to the endpoint.
model_name (str): The name of the model to be associated with the endpoint.
This is required if ``initial_instance_count``, ``instance_type``, or
``accelerator_type`` is specified and if there is more than one model associated
with the endpoint. Otherwise, the existing model for the endpoint is used.
tags (list[dict[str, str]]): The list of tags to add to the endpoint
config. If not specified, the tags of the existing endpoint configuration are used.
If any of the existing tags are reserved AWS ones (i.e. begin with "aws"),
they are not carried over to the new endpoint configuration.
kms_key (str): The KMS key that is used to encrypt the data on the storage volume
attached to the instance hosting the endpoint If not specified,
the KMS key of the existing endpoint configuration is used.
data_capture_config_dict (dict): The endpoint data capture configuration
for use with Amazon SageMaker Model Monitoring. If not specified,
the data capture configuration of the existing endpoint configuration is used.
wait (bool): Wait for updating to finish
"""
self.predictor.update_endpoint(
initial_instance_count=initial_instance_count,
instance_type=instance_type,
accelerator_type=accelerator_type,
model_name=model_name,
tags=format_tags(tags),
kms_key=kms_key,
data_capture_config_dict=data_capture_config_dict,
wait=wait,
)
def delete_endpoint(self, delete_endpoint_config=True):
"""Delete the Amazon SageMaker endpoint backing this async predictor.
This also delete the endpoint configuration attached to it if
delete_endpoint_config is True.
Args:
delete_endpoint_config (bool, optional): Flag to indicate whether to
delete endpoint configuration together with endpoint. Defaults
to True. If True, both endpoint and endpoint configuration will
be deleted. If False, only endpoint will be deleted.
"""
self.predictor.delete_endpoint(delete_endpoint_config)
def delete_model(self):
"""Deletes the Amazon SageMaker models backing this predictor."""
self.predictor.delete_model()
def enable_data_capture(self):
"""Enables data capture by updating DataCaptureConfig.
This function updates the DataCaptureConfig for the Predictor's associated Amazon SageMaker
Endpoint to enable data capture. For a more customized experience, refer to
update_data_capture_config, instead.
"""
self.predictor.enable_data_capture()
def disable_data_capture(self):
"""Disables data capture by updating DataCaptureConfig.
This function updates the DataCaptureConfig for the Predictor's associated Amazon SageMaker
Endpoint to disable data capture. For a more customized experience, refer to
update_data_capture_config, instead.
"""
self.predictor.disable_data_capture()
def update_data_capture_config(self, data_capture_config):
"""Updates the DataCaptureConfig for the Predictor's associated Amazon SageMaker Endpoint.
Update is done using the provided DataCaptureConfig.
Args:
data_capture_config (sagemaker.model_monitor.DataCaptureConfig): The
DataCaptureConfig to update the predictor's endpoint to use.
"""
self.predictor.update_data_capture_config(data_capture_config)
def list_monitors(self):
"""Generates ModelMonitor objects (or DefaultModelMonitors).
Objects are generated based on the schedule(s) associated with the endpoint
that this predictor refers to.
Returns:
[sagemaker.model_monitor.model_monitoring.ModelMonitor]: A list of
ModelMonitor (or DefaultModelMonitor) objects.
"""
return self.predictor.list_monitors()
def endpoint_context(self):
"""Retrieves the lineage context object representing the endpoint.
Examples:
.. code-block:: python
predictor = Predictor()
context = predictor.endpoint_context()
models = context.models()
Returns:
ContextEndpoint: The context for the endpoint.
"""
return self.predictor.endpoint_context()