Skip to content

Commit 0ee1636

Browse files
committed
Tutorial on using TorchServe on Vertex AI
This new tutorial is based on the Vertex AI documentation. The PR provides a similar doc as a tutorial hosted locally. Fixes #2346 Signed-off-by: Sahdev Zala <spzala@us.ibm.com>
1 parent 16e4f2a commit 0ee1636

File tree

2 files changed

+149
-0
lines changed

2 files changed

+149
-0
lines changed

index.rst

+8
Original file line numberDiff line numberDiff line change
@@ -637,6 +637,13 @@ What's new in PyTorch tutorials?
637637
:link: beginner/knowledge_distillation_tutorial.html
638638
:tags: Model-Optimization,Image/Video
639639

640+
.. customcarditem::
641+
:header: Deploying a PyTorch Stable Diffusion model as a Vertex AI Endpoint
642+
:card_description: Learn how to deploy model in Vertex AI with TorchServe
643+
:image: _static/img/thumbnails/cropped/generic-pytorch-logo.png
644+
:link: intermediate/torchserve_vertexai_tutorial.rst
645+
:tags: Model-Optimization,Production
646+
640647
.. Parallel-and-Distributed-Training
641648
642649
@@ -1042,6 +1049,7 @@ Additional Resources
10421049
intermediate/inductor_debug_cpu
10431050
intermediate/scaled_dot_product_attention_tutorial
10441051
beginner/knowledge_distillation_tutorial
1052+
intermediate/torchserve_vertexai_tutorial.rst
10451053

10461054
.. toctree::
10471055
:maxdepth: 2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
Deploying a PyTorch Stable Diffusion model as a Vertex AI Endpoint
2+
==================================================================
3+
4+
Deploying large models, like Stable Diffusion, can be challenging and time-consuming.
5+
In this tutorial, we will show how you can streamline the deployment of a PyTorch Stable Diffusion
6+
model by leveraging Vertex AI. PyTorch is the framework used by Stability AI on Stable
7+
Diffusion v1.5. Vertex AI is a fully-managed machine learning platform with tools and
8+
infrastructure designed to help ML practitioners accelerate and scale ML in production with
9+
the benefit of open-source frameworks like PyTorch. In four steps you can deploy a PyTorch
10+
Stable Diffusion model (v1.5).
11+
12+
Deploying your Stable Diffusion model on a Vertex AI Endpoint can be done in four steps:
13+
14+
* Create a custom TorchServe handler.
15+
16+
* Upload model artifacts to Google Cloud Storage (GCS).
17+
18+
* Create a Vertex AI model with the model artifacts and a prebuilt PyTorch container image.
19+
20+
* Deploy the Vertex AI model onto an endpoint.
21+
22+
Let’s have a look at each step in more detail. You can follow and implement the steps using the
23+
`Notebook example <https://github.com/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/community/vertex_endpoints/torchserve/dreambooth_stablediffusion.ipynb>`__.
24+
25+
NOTE: please keep in mind that this tutorial requires a billable Vertex AI as explained in more details in the notebook example.
26+
27+
Create a custom TorchServe handler
28+
----------------------------------
29+
30+
TorchServe is an easy and flexible tool for serving PyTorch models. The model deployed to Vertex AI
31+
uses TorchServe to handle requests and return responses from the model. You must create a custom
32+
TorchServe handler to include in the model artifacts uploaded to Vertex AI. Include the handler file in the
33+
directory with the other model artifacts, like this: `model_artifacts/handler.py`.
34+
35+
After creating the handler file, you must package the handler as a model archiver (MAR) file.
36+
The output file must be named `model.mar`.
37+
38+
39+
.. code:: shell
40+
41+
!torch-model-archiver \
42+
-f \
43+
--model-name <your_model_name> \
44+
--version 1.0 \
45+
--handler model_artifacts/handler.py \
46+
--export-path model_artifacts
47+
48+
Upload model artifacts to Google Cloud Storage (GCS)
49+
----------------------------------------------------
50+
51+
In this step we are uploading
52+
`model artifacts <https://github.com/pytorch/serve/tree/master/model-archiver#artifact-details>`__
53+
to GCS, like the model file or handler. The advantage of storing your artifacts on GCS is that you can
54+
track the artifacts in a central bucket.
55+
56+
57+
.. code:: shell
58+
59+
BUCKET_NAME = "your-bucket-name-unique" # @param {type:"string"}
60+
BUCKET_URI = f"gs://{BUCKET_NAME}/"
61+
62+
# Will copy the artifacts into the bucket
63+
!gsutil cp -r model_artifacts $BUCKET_URI
64+
65+
Create a Vertex AI model with the model artifacts and a prebuilt PyTorch container image
66+
----------------------------------------------------------------------------------------
67+
68+
Once you've uploaded the model artifacts into a GCS bucket, you can upload your PyTorch model to
69+
`Vertex AI Model Registry <https://cloud.google.com/vertex-ai/docs/model-registry/introduction>`__.
70+
From the Vertex AI Model Registry, you have an overview of your models
71+
so you can better organize, track, and train new versions. For this you can use the
72+
`Vertex AI SDK <https://cloud.google.com/vertex-ai/docs/python-sdk/use-vertex-ai-python-sdk>`__
73+
and this
74+
`pre-built PyTorch container <https://cloud.google.com/blog/products/ai-machine-learning/prebuilt-containers-with-pytorch-and-vertex-ai>`__.
75+
76+
77+
.. code:: shell
78+
79+
from google.cloud import aiplatform as vertexai
80+
PYTORCH_PREDICTION_IMAGE_URI = (
81+
"us-docker.pkg.dev/vertex-ai/prediction/pytorch-gpu.1-12:latest"
82+
)
83+
MODEL_DISPLAY_NAME = "stable_diffusion_1_5-unique"
84+
MODEL_DESCRIPTION = "stable_diffusion_1_5 container"
85+
86+
vertexai.init(project='your_project', location='us-central1', staging_bucket=BUCKET_NAME)
87+
88+
model = aiplatform.Model.upload(
89+
display_name=MODEL_DISPLAY_NAME,
90+
description=MODEL_DESCRIPTION,
91+
serving_container_image_uri=PYTORCH_PREDICTION_IMAGE_URI,
92+
artifact_uri=BUCKET_URI,
93+
)
94+
95+
Deploy the Vertex AI model onto an endpoint
96+
-------------------------------------------
97+
98+
Once the model has been uploaded to Vertex AI Model Registry you can then take it and deploy
99+
it to an Vertex AI Endpoint. For this you can use the Console or the Vertex AI SDK. In this
100+
example you will deploy the model on a NVIDIA Tesla P100 GPU and n1-standard-8 machine. You can
101+
specify your machine type.
102+
103+
104+
.. code:: shell
105+
106+
endpoint = aiplatform.Endpoint.create(display_name=ENDPOINT_DISPLAY_NAME)
107+
108+
model.deploy(
109+
endpoint=endpoint,
110+
deployed_model_display_name=MODEL_DISPLAY_NAME,
111+
machine_type="n1-standard-8",
112+
accelerator_type="NVIDIA_TESLA_P100",
113+
accelerator_count=1,
114+
traffic_percentage=100,
115+
deploy_request_timeout=1200,
116+
sync=True,
117+
)
118+
119+
If you follow the
120+
`notebook <https://github.com/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/community/vertex_endpoints/torchserve/dreambooth_stablediffusion.ipynb>`__
121+
you can also get online predictions using the Vertex AI SDK as shown in the following snippet.
122+
123+
124+
.. code:: shell
125+
126+
instances = [{"prompt": "An examplePup dog with a baseball jersey."}]
127+
response = endpoint.predict(instances=instances)
128+
129+
with open("img.jpg", "wb") as g:
130+
g.write(base64.b64decode(response.predictions[0]))
131+
132+
display.Image("img.jpg")
133+
134+
Create a Vertex AI model with the model artifacts and a prebuilt PyTorch container image
135+
136+
More resources
137+
--------------
138+
139+
This tutorial was created using the vendor documentation. To refer to the original documentation on the vendor site, please see
140+
`torchserve example <https://cloud.google.com/blog/products/ai-machine-learning/get-your-genai-model-going-in-four-easy-steps>`__.
141+

0 commit comments

Comments
 (0)