How does sagemaker creates a model?

1

I am creating sagemaker resources such as model, endpoint configuration and real time endpoint via cloudformation ( sample below) . in the template below, we provide the s3 bucket URI for the model artifact in the ModelDataUrl argument. if we update the model artifact , or delete a older version and upload a new model.tar file in the same bucket. will that work, instead of creating a new model resource everytime there is a new version of model.tar file ? when making a inference, I understand , sagemaker downloads the model.tar file in the container specified , unpack the model.tar file and call the binary file for inference ,so it doesn't matter if we update the model.tar file , right? sagemaker will simply download whatever tar file is present in the s3 URI and works with that.

SageMakerModel:
    Type: AWS::SageMaker::Model
    Properties: 
      Containers: 
        -
          Image: !Ref ImageURI
          ModelDataUrl: 's3://some-bucket/model.tar'
          Mode: SingleModel
      ExecutionRoleArn: !Ref RoleArn
asked 2 years ago224 views
2 Answers
1

If you trained your model in SageMaker, the model artifacts are saved as a single compressed tar file in Amazon S3. If you trained your model outside SageMaker, you need to create this single compressed tar file and save it in a S3 location. SageMaker decompresses this tar file into /opt/ml/model directory before your container starts.

Reference : https://docs.aws.amazon.com/sagemaker/latest/dg/your-algorithms-inference-code.html

profile pictureAWS
EXPERT
answered 2 years ago
1

Just to add a bit more detail to Nitin's answer since you raised a couple of interesting secondary questions:

First, to be picky, your artifact does need to be a compressed .tar.gz file as mentioned here. Plain .tar files (which you mentioned a couple of times) likely won't have this compression and won't work. From my recent tests the filename extension itself in S3 doesn't matter though - so if your object is in the correct format but just happens to be called .tar, this should be fine.

Second, yes today you can simply update the saved model artifact in S3 and continue referencing the same SageMaker model - but when can you do it and should you?

One important caveat to bear in mind is that if you're deploying your models to online inference endpoints, there's no API available to force running instances to re-fetch an updated model tarball. If you update your artifact while an endpoint is live (especially if auto-scaling is enabled or some endpoint update is in progress) you could end up with endpoint instances running a mixture of old vs new model.

The second is that, even if you only run defined batch jobs and can guarantee the timing of artifact updates won't overlap with job start-up, keeping track of your model versions is a pretty important best practice to help keep your operations in order.

So even though it's possible to just update the artifact in S3, I'd usually suggest users track their historical models: For example by treating the S3 prefix as immutable, re-creating Models for new artifact versions, and using SageMaker Model Registry to track the history of versions. It's worth mentioning that storing your history of versions ("ModelPackages") in Model Registry doesn't necessarily have to balloon the number of "Models" in SageMaker: As discussed here, a ModelPackage isn't necessarily tied to a Model, and you need to create a Model from it to be able to use in deployment/inference.

AWS
EXPERT
Alex_T
answered 2 years ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions