How to serve a model in sagemaker?

Viewed 331
1 Answers

Interesting question. I'm assuming you're trying to use the PyTorch container from SageMaker in what we call "script mode" - where you just provide the .py entrypoint. Have you tried to define a model_fn() function, where you specify how to load your model? The documentation talks about this here.

More details:

Before a model can be served, it must be loaded. The SageMaker PyTorch model server loads your model by invoking a model_fn function that you must provide in your script when you are not using Elastic Inference.

import torch
import os
import YOUR_MODEL_DEFINITION

def model_fn(model_dir):
    model = YOUR_MODEL_DEFINITION()
    with open(os.path.join(model_dir, 'YOUR-MODEL-FILE-HERE'), 'rb') as f:
        model.load_state_dict(torch.load(f))
    return model

Let me know if this works!

Related