<img height="1" width="1" style="display:none;" alt="" src="https://px.ads.linkedin.com/collect/?pid=2634489&amp;fmt=gif">

AI and Machine Learning, Vertex AI

Deploying your Generative AI model in only four steps with Vertex AI and PyTorch

By C2C Community Team | March 26, 2022

May 16, 2023

 

Erwin Huizenga

Developer Advocate Machine Learning

Eric Schmidt

Developer Relations Engineer

 

 

Foundational models are trained on extensive unlabeled data and used for downstream  generative AI tasks, such as text, images, and music generation. They are increasingly popular as businesses explore their potential to create new products and services. You can use foundational models for use cases like image generation. Diffusion models are generative models that have gained popularity over the past years because of the high-quality images they can generate. Stable Diffusion is a latent text-to-image diffusion model that researchers at CompVis, Stability AI, and LAION have developed. 

Deploying large models, like Stable Diffusion, can be challenging and time-consuming. In this blog, we will show how you can streamline the deployment of a PyTorch Stable Diffusion model by leveraging Vertex AI. PyTorch is the framework used by Stability AI on Stable Diffusion v1.5.  Vertex AI is a fully-managed machine learning platform with tools and infrastructure designed to help ML practitioners accelerate and scale ML in production with the benefit of open-source frameworks like PyTorch. In four steps you can deploy a PyTorch Stable Diffusion model (v1.5). 

 

 

Deploying a PyTorch Stable Diffusion model as a Vertex AI Endpoint

 

Deploying your Stable Diffusion model on a Vertex AI Endpoint can be done in four steps:

  1. Create a custom TorchServe handler.

  2. Upload model artifacts to Google Cloud Storage (GCS).

  3. Create a Vertex AI model with the model artifacts and a prebuilt PyTorch container image.

  4. Deploy the Vertex AI model onto an endpoint.

Let's have a look at each step in more detail. You can follow and implement the steps using the Notebook example

 

Step 1 - Create a custom TorchServe handler

TorchServe is an easy and flexible tool for serving PyTorch models. The model deployed to Vertex AI uses TorchServe to handle requests and return responses from the model. You must create a custom TorchServe handler to include in the model artifacts uploaded to Vertex AI. Include the handler file in the directory with the other model artifacts, like this: model_artifacts/handler.py.

After creating the handler file, you must package the handler as a model archiver (MAR) file. The output file must be named model.mar.

 

!torch-model-archiver \
-f \
--model-name <your_model_name> \
--version 1.0 \
--handler model_artifacts/handler.py \
--export-path model_artifacts

 

Step 2 - Upload the model artifacts to Google Cloud Storage

The next step is uploading model artifacts to GCS, like the model file or handler. The advantage of storing your artifacts on GCS is that you can track the artifacts in a central bucket.

 

BUCKET_NAME = "your-bucket-name-unique"  # @param {type:"string"}
BUCKET_URI = f"gs://{BUCKET_NAME}/"
-
# Will copy the artifacts into the bucket
!gsutil cp -r model_artifacts $BUCKET_URI

 

Step 3 - Create the Vertex AI model

Once you've uploaded the model artifacts into a GCS bucket, you can upload your PyTorch model to Vertex AI Model Registry. From the Vertex AI Model Registry, you have an overview of your models so you can better organize, track, and train new versions. For this you can use the Vertex AI SDK and our pre-built PyTorch container.

 

 

Click here to read more


Recent Articles

Google Cloud Strategy

AI Cheat Sheet

AI is no more and no less the drive to create robots with human minds so they can do everything we do and more. Use this cheat sheet to help decode the space.
By Leah Zitter
AI and Machine Learning

CarCast with Bruno Aziza: What Makes Us Better Than AI?!

Unlock the secrets of human cognitive superiority over AI in this compelling CarCast with Bruno Aziza and Kenneth Cukier.
By Bruno Aziza
AI and Machine Learning

CarCast with Bruno Aziza: The Metrics You CAN'T Afford To ...

Discover essential CEO metrics: Rule of 40, CAC Ratio, NRR/GRR, and more. Optimize your business for success now!
By Bruno Aziza