Verta
Latest
Search
K

Deploying a PyTorch model

As mention in the deploying models guide, deploying models via Verta Inference is a two step process: (1) first create an endpoint, and (2) update the endpoint with a model.
This tutorial explains how Verta Inference can be used to deploy a PyTorch model.

1. Create an endpoint

Users can create an endpoint using Client.create_endpoint() as follows:
census_endpoint = client.create_endpoint(path="/census")

2. Updating the endpoint with a RMV

As discussed in the Catalog Overview, there are multiple of ways to create an RMV for a PyTorch model.
First, if we are provided with a PyTorch model object, users can use the PyTorch convenience functions to create a Verta Standard Model.
from verta.environment import Python
model_version = registered_model.create_standard_model_from_torch(
model,
environment=Python(requirements=["torch", "torchvision"]),
name="v1",
)
Alternatively, a serialized PyTorch saved model can be used as an artifact in a model that extends VertaModelBase.
torch.save(model.state_dict(), "model.pth")
from verta.registry import VertaModelBase
class FashionMNISTClassifier(VertaModelBase):
def __init__(self, artifacts):
self.model = NeuralNetwork()
model.load_state_dict(torch.load(artifacts["model.pth"]))
def predict(self, batch_input):
results = []
for one_input in batch_input:
with torch.no_grad():
pred = model(x)
results.append(pred)
return results
model_version = registered_model.create_standard_model(
model_cls=FashionMNISTClassifier,
environment=Python(requirements=["torch", "torchvision"]),
artifacts={"model.pth" : "model.pth"},
name="v2"
)
Note that the input and output of the predict function must be JSON serializable. For the full list of acceptable data types for model I/O, refer to the VertaModelBase documentation.
Prior to deploy, don't forget to test your model class locally as follows.
# test locally
mnist_model1 = FashionMNISTClassifier({"model.pth" : "model.pth"})
mnist_model1.predict([test_data[0][0]])
To ensure that the requirements specified in the model version are in fact adequate, you may build the model container locally or part of a continuous integration system. You may also deploy the model and make test predictions as show below.
Regardless of how a Registered Model Version has been created, the endpoint defined above can now be upated and we can make predictions against it.
fashion_mnist_endpoint = client.get_or_create_endpoint("fashion-mnist")
fashion_mnist_endpoint.update(model_version, wait=True)
deployed_model = fashion_mnist_endpoint.get_deployed_model()
deployed_model.predict([test_data[0][0]])
The full code for this tutorial can be found here.