aws / sagemaker-python-sdk

A library for training and deploying machine learning models on Amazon SageMaker
https://sagemaker.readthedocs.io/
Apache License 2.0
2.1k stars 1.14k forks source link

.predict_async(data) called on AsyncPredictor API fails if no name was provided to predictor #3210

Open joaopcm1996 opened 2 years ago

joaopcm1996 commented 2 years ago

Describe the bug In the SageMaker SDK, if no name is given to an AsyncPredictor, and you call .predict_async(data) with data and not an input_path, the SDK errors out.

To reproduce Deploy an async endpoint, wrap the resulting predictor with sagemaker.predictor_async.AsyncPredictor, and do not give it a name. Then call .predict_async on that AsyncPredictor, passing it actual data instead of an S3 input_path. As the SDK tries to use the async predictor's name to create a key to upload the input data, it fails because there is no name.

Error Logs

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
/tmp/ipykernel_6717/4064040838.py in <cell line: 1>()
----> 1 predictor_async_wrapper.predict_async(data=images.numpy())

~/anaconda3/envs/pytorch_p38/lib/python3.8/site-packages/sagemaker/predictor_async.py in predict_async(self, data, input_path, initial_args, inference_id)
    137             )
    138         if data is not None:
--> 139             input_path = self._upload_data_to_s3(data, input_path)
    140 
    141         self._input_path = input_path

~/anaconda3/envs/pytorch_p38/lib/python3.8/site-packages/sagemaker/predictor_async.py in _upload_data_to_s3(self, data, input_path)
    162             bucket = self.sagemaker_session.default_bucket()
    163             key = "async-endpoint-inputs/{}/{}-{}".format(
--> 164                 name_from_base(self.name, short=True),
    165                 timestamp,
    166                 my_uuid,

~/anaconda3/envs/pytorch_p38/lib/python3.8/site-packages/sagemaker/utils.py in name_from_base(base, max_length, short)
     77     """
     78     timestamp = sagemaker_short_timestamp() if short else sagemaker_timestamp()
---> 79     trimmed_base = base[: max_length - len(timestamp) - 1]
     80     return "{}-{}".format(trimmed_base, timestamp)
     81 

TypeError: 'NoneType' object is not subscriptable

System information

abdelaziz-jaddi commented 1 year ago

AsyncPredictoruses the predict_asyncmethod to make an asynchronous prediction. It starts by upload data to input_path, then calls invoke_endpoint_async with this input_path.

Solution: add input_path and async_predictor.predict(data =data, input_path= input_path)