Skip to content

"model_fn" being called several times in sagemaker endpoint deploy process

0

I am deploying a externally trained sklearn model

sklearn_model = SKLearnModel(name="tat-predict22",model_data="s3://sagemaker-tat-prediction/models/regression.tar.gz", role=role,entry_point="regression_model.inference", source_dir="s3://sagemaker-tat-prediction/models/regression_src3.tar.gz", #code_location="s3://sagemaker-tat-prediction/sources", framework_version="1.2-1" ,container_log_level=10)

sklearn_model.deploy(instance_type="ml.m5.xlarge", initial_instance_count=1)

model_fn in inference.py is called several times loading the model every time and leading to memory errors.. I was expected model_fn to be called only once when the model is deployed.

  • Hi, did you find a solution? I'm having the same issue.

asked 3 years ago410 views

1 Answer
0

(A little late here, but in case others are having the same issue)

In general, it should be expected that your model_fn may be called multiple times and it's best to write your model loader such that this is okay.

Although the different framework containers in SageMaker use different serving technologies, all the ones that I'm aware of attempt to provide a mechanism like this to support effectively utilizing large instances. In general, they'll have some kind of (multi-threaded) web server front end for handling incoming connections concurrently (you don't want to fail a /ping health check just because you're busy processing a request!)... And there'll typically be multiple back-end (Python) worker threads each with a loaded copy of your model, to actually serve the requests

For example, if you have 4 GPUs and a model that just-about-fits on one GPU, you'd probably want 4 worker threads to serve requests concurrently on each one. If you have 32 vCPUs and a model that runs fast-enough on 4 of them, maybe you'd want about 8 workers, etc.


The more tricky question is how can you configure how many model copies are loaded - for cases like this question where the default isn't working well (maybe you have lots of CPU resource but not enough RAM, etc).

This is not as straightforward to answer as I'd like and will probably require a little trial-and-error, because there are several different repositories defining the SageMaker framework serving stacks. As far as I'm currently aware, which may not be 100% correct:

For MMS-based containers like Scikit-Learn, you can try setting environment variables like MMS_DEFAULT_WORKERS_PER_MODEL to configure the count - as per their ConfigManager here and configuration docs. There should also (from SM Inference Toolkit) be a general SAGEMAKER_NUM_MODEL_WORKERS variable, but I'm not sure if that'll get picked up.

In SageMaker Python SDK, you can define environment variables like these in the env option when creating a Model or env_vars in ModelBuilder. If running repeated experiments, do double-check that the SDK isn't just re-using your previously-created Model in the API without updating it. Attached environment variables should be visible from your Model's detail page in the AWS Console for SageMaker AI

AWS
EXPERT

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.