payonear
payonear

Reputation: 51

Sagemaker deployment with source_dir as S3 URI

I'm deploying Pytorch model saved as .pth to AWS Sagemaker. I specify custom inference.py file and my catalog looks like this:

|   my_model
|           |--model.pth
|
|           code
|               |--inference.py
|               |--requirements.txt
|

That's completely in line with SDK doc. So I put the archive to S3 bucket, after that I define my model as following:

pytorch_model = PyTorchModel(model_data='s3://{bucket_name}/my_path/my-model.tar.gz',
                             framework_version="1.8.1", py_version="py3",
                             role=role, entry_point='inference.py')

Again, seems for me to be consistent with requirements. Still it returns error, claiming, that inference.py doesn't exist. I've tried to specify source_dir to be code. Still doesn't find. Then I tried to give it as path to my-model.tar.gz, as suggested in the codehere. Deployment succeeds, but then in logs I see it's asking me to define model_fn func, which is defined inside inference.py file and works perfectly if the code stored with the notebook, which deploys the model.

What am I doing wrong? How should I store model artifacts in S3 bucket and how should I define entry_point and source_dir to deploy it successfully?

Upvotes: 2

Views: 1097

Answers (2)

Mohamed Elzarei
Mohamed Elzarei

Reputation: 546

If you aren't working in a git repository there can be issues with source_dir that is why I just define the source_dir as an absolute path instead e.g for your example assuming your notebook is inside code directory

base_dir = os.path.dirname(os.path.realpath(__file__))
source_dir = os.path.join(base_dir, '..', 'code')

Upvotes: 1

Mia Chang
Mia Chang

Reputation: 151

You will need to specify the source_dir.

Here is the sample notebook which shows "bring your own Pytorch model" deployment setup. Since the sagemaker.pytorch.model.PyTorchModel is based on sagemaker.model.FrameworkModel, you may use the source_dir when initializing the PyTorchModel.

Reference

Upvotes: 0

Related Questions