Add AutoGluon notebook. (#2649)

This commit is contained in:
Lav Rai
2024-01-25 17:38:17 +00:00
committed by GitHub
parent 7d7db7d2d7
commit ad947ea952
2 changed files with 692 additions and 0 deletions
+1
View File
@@ -74,6 +74,7 @@
/notebooks/community/model_garden/model_garden_pytorch_detectron2.ipynb @lavraicse
/notebooks/community/model_garden/model_garden_pytorch_dolly_v2.ipynb @lavraicse
/notebooks/community/model_garden/model_garden_pytorch_bart_large_cnn.ipynb @lavraicse
/notebooks/community/model_garden/model_garden_pytorch_autogluon.ipynb @lavraicse
/notebooks/community/model_garden/model_garden_pytorch_starcoder.ipynb @xcchen1
/notebooks/community/model_garden/model_garden_jax_vision_transformer.ipynb @lavraicse
/notebooks/community/model_garden/model_garden_jax_fvlm.ipynb @lavraicse
@@ -0,0 +1,691 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "ur8xi4C7S06n"
},
"outputs": [],
"source": [
"# Copyright 2024 Google LLC\n",
"#\n",
"# Licensed under the Apache License, Version 2.0 (the \"License\");\n",
"# you may not use this file except in compliance with the License.\n",
"# You may obtain a copy of the License at\n",
"#\n",
"# https://www.apache.org/licenses/LICENSE-2.0\n",
"#\n",
"# Unless required by applicable law or agreed to in writing, software\n",
"# distributed under the License is distributed on an \"AS IS\" BASIS,\n",
"# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
"# See the License for the specific language governing permissions and\n",
"# limitations under the License."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "99c1c3fc2ca5"
},
"source": [
"# Vertex AI Model Garden - AutoGluon\n",
"\n",
"<table align=\"left\">\n",
" <td>\n",
" <a href=\"https://colab.research.google.com/github/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/community/model_garden/model_garden_pytorch_autogluon.ipynb\">\n",
" <img src=\"https://cloud.google.com/ml-engine/images/colab-logo-32px.png\" alt=\"Colab logo\"> Run in Colab\n",
" </a>\n",
" </td>\n",
"\n",
" <td>\n",
" <a href=\"https://github.com/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/community/model_garden/model_garden_pytorch_autogluon.ipynb\">\n",
" <img src=\"https://cloud.google.com/ml-engine/images/github-logo-32px.png\" alt=\"GitHub logo\">\n",
" View on GitHub\n",
" </a>\n",
" </td>\n",
" <td> <td>\n",
" <a href=\"https://console.cloud.google.com/vertex-ai/notebooks/deploy-notebook?download_url=https://raw.githubusercontent.com/GoogleCloudPlatform/vertex-ai-samples/main/notebooks/community/model_garden/model_garden_pytorch_autogluon.ipynb\">\n",
" <img src=\"https://lh3.googleusercontent.com/UiNooY4LUgW_oTvpsNhPpQzsstV5W8F7rYgxgGBD85cWJoLmrOzhVs_ksK_vgx40SHs7jCqkTkCk=e14-rj-sc0xffffff-h130-w32\" alt=\"Vertex AI logo\">\n",
"Open in Vertex AI Workbench\n",
" </a>\n",
" </td>\n",
"</table>"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "24743cf4a1e1"
},
"source": [
"**_NOTE_**: This notebook has been tested in the following environment:\n",
"\n",
"* Python version = 3.10"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "tvgnzT1CKxrO"
},
"source": [
"## Overview\n",
"\n",
"This notebook demonstrates finetuning a PyTorch based [Autogluon model for tabular data](https://auto.gluon.ai/stable/tutorials/tabular/index.html) on CPU and deploying it on Vertex AI for online prediction."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "d975e698c9a4"
},
"source": [
"### Objective\n",
"\n",
"In this tutorial, you learn how to:\n",
"\n",
"- Finetune a PyTorch AutoGluon tabular model.\n",
"- Upload the model to [Model Registry](https://cloud.google.com/vertex-ai/docs/model-registry/introduction).\n",
"- Deploy the model on [Endpoint](https://cloud.google.com/vertex-ai/docs/predictions/using-private-endpoints).\n",
"- Run online predictions for tabular data.\n",
"\n",
"This tutorial uses the following Google Cloud ML services and resources:\n",
"\n",
"- Vertex AI Training\n",
"- Vertex AI Model Registry\n",
"- Vertex AI Online Prediction"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "08d289fa873f"
},
"source": [
"### Dataset\n",
"\n",
"You can find the details for the [example dataset here](https://auto.gluon.ai/stable/tutorials/tabular/tabular-quick-start.html#example-data)."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "aed92deeb4a0"
},
"source": [
"### Costs\n",
"\n",
"This tutorial uses billable components of Google Cloud:\n",
"\n",
"* Vertex AI\n",
"* Cloud Storage\n",
"\n",
"Learn about [Vertex AI pricing](https://cloud.google.com/vertex-ai/pricing) and [Cloud Storage pricing](https://cloud.google.com/storage/pricing), and use the [Pricing Calculator](https://cloud.google.com/products/calculator/) to generate a cost estimate based on your projected usage."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "i7EUnXsZhAGF"
},
"source": [
"## Installation\n",
"\n",
"Install the following packages required to execute this notebook."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "2b4ef9b72d43"
},
"outputs": [],
"source": [
"# Install the packages.\n",
"! pip3 install --upgrade google-cloud-aiplatform"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "58707a750154"
},
"source": [
"### Colab only"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "f200f10a1da3"
},
"outputs": [],
"source": [
"# Automatically restart kernel after installs so that your environment can access the new packages.\n",
"import IPython\n",
"\n",
"app = IPython.Application.instance()\n",
"app.kernel.do_shutdown(True)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "BF1j6f9HApxa"
},
"source": [
"## Before you begin\n",
"\n",
"### Set up your Google Cloud project\n",
"\n",
"**The following steps are required, regardless of your notebook environment.**\n",
"\n",
"1. [Select or create a Google Cloud project](https://console.cloud.google.com/cloud-resource-manager). When you first create an account, you get a $300 free credit towards your compute/storage costs.\n",
"\n",
"1. [Make sure that billing is enabled for your project](https://cloud.google.com/billing/docs/how-to/modify-project).\n",
"\n",
"1. [Enable the Vertex AI API and Compute Engine API](https://console.cloud.google.com/flows/enableapi?apiid=aiplatform.googleapis.com,compute_component).\n",
"\n",
"1. If you are running this notebook locally, you need to install the [Cloud SDK](https://cloud.google.com/sdk).\n",
"\n",
"1. [Create a service account](https://cloud.google.com/iam/docs/service-accounts-create#iam-service-accounts-create-console) with `Vertex AI User` and `Storage Object Admin` roles for deploying fine tuned model to Vertex AI endpoint.\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "WReHDGG5g0XY"
},
"source": [
"#### Set your project ID\n",
"\n",
"**If you don't know your project ID**, try the following:\n",
"* Run `gcloud config list`.\n",
"* Run `gcloud projects list`.\n",
"* See the support page: [Locate the project ID](https://support.google.com/googleapi/answer/7014113)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "oM1iC_MfAts1"
},
"outputs": [],
"source": [
"PROJECT_ID = \"your-project-id\" # @param {type:\"string\"}\n",
"\n",
"# Set the project id\n",
"! gcloud config set project {PROJECT_ID}"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "region"
},
"source": [
"#### Region\n",
"\n",
"You can also change the `REGION` variable used by Vertex AI. Learn more about [Vertex AI regions](https://cloud.google.com/vertex-ai/docs/general/locations)."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "twgKk-LsLmX3"
},
"outputs": [],
"source": [
"REGION = \"us-central1\" # @param {type: \"string\"}"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "sBCra4QMA2wR"
},
"source": [
"### Authenticate your Google Cloud account\n",
"\n",
"Depending on your Jupyter environment, you may have to manually authenticate. Follow the relevant instructions below."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "74ccc9e52986"
},
"source": [
"**1. Vertex AI Workbench**\n",
"* Do nothing as you are already authenticated."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "de775a3773ba"
},
"source": [
"**2. Local JupyterLab instance, uncomment and run:**"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "254614fa0c46"
},
"outputs": [],
"source": [
"# ! gcloud auth login"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "ef21552ccea8"
},
"source": [
"**3. Colab, uncomment and run:**"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "603adbbf0532"
},
"outputs": [],
"source": [
"# from google.colab import auth\n",
"# auth.authenticate_user()"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "f6b2ccc891ed"
},
"source": [
"**4. Service account or other**\n",
"* See how to grant Cloud Storage permissions to your service account at https://cloud.google.com/storage/docs/gsutil/commands/iam#ch-examples."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "Z36ywjGtRey3"
},
"outputs": [],
"source": [
"# The service account for deploying fine tuned model.\n",
"# The service account looks like:\n",
"# '<account_name>@<project>.iam.gserviceaccount.com'\n",
"SERVICE_ACCOUNT = \"your-service-account\" # @param {type:\"string\"}"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "zgPO1eR3CYjk"
},
"source": [
"### Create a Cloud Storage bucket\n",
"\n",
"Create a storage bucket to store intermediate artifacts such as datasets."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "MzGDU7TWdts_"
},
"outputs": [],
"source": [
"BUCKET_URI = f\"gs://your-bucket-name-{PROJECT_ID}-unique\" # @param {type:\"string\"}"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "-EcIXiGsCePi"
},
"source": [
"**Only if your bucket doesn't already exist**: Run the following cell to create your Cloud Storage bucket."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "NIq7R4HZCfIc"
},
"outputs": [],
"source": [
"! gsutil mb -l {REGION} -p {PROJECT_ID} {BUCKET_URI}"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "960505627ddf"
},
"source": [
"### Import libraries"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "PyQmSRbKA8r-"
},
"outputs": [],
"source": [
"import os\n",
"from datetime import datetime\n",
"\n",
"from google.cloud import aiplatform"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "init_aip:mbsdk,all"
},
"source": [
"### Initialize Vertex AI SDK for Python\n",
"\n",
"Initialize the Vertex AI SDK for Python for your project."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "vS1hQiGuLmX4"
},
"outputs": [],
"source": [
"staging_bucket = os.path.join(BUCKET_URI, \"autogluon_staging\")\n",
"aiplatform.init(project=PROJECT_ID, location=REGION, staging_bucket=staging_bucket)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "2cc825514deb"
},
"source": [
"### Define constants"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "b42bd4fa2b2d"
},
"outputs": [],
"source": [
"# The pre-built training docker image.\n",
"TRAIN_DOCKER_URI = \"us-docker.pkg.dev/vertex-ai/vertex-vision-model-garden-dockers/pytorch-autogluon-train:20240124_0927_RC00\"\n",
"# The pre-built serving docker image.\n",
"SERVE_DOCKER_URI = \"us-docker.pkg.dev/vertex-ai/vertex-vision-model-garden-dockers/pytorch-autogluon-serve:20240124_0938_RC00\"\n",
"# Serving port.\n",
"PORT = 8501"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "0c250872074f"
},
"source": [
"### Define common functions\n",
"\n",
"This section defines functions for:\n",
"\n",
"- Converting a Cloud Storage path such as `gs://bucket-name` to GCSFuse path format such as `/gcsfuse/bucket-name`.\n",
"- Deploy the trained model to Vertex AI Endpoint for prediction."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "XcYUGwr-AJGY"
},
"outputs": [],
"source": [
"def gcs_fuse_path(path: str) -> str:\n",
" \"\"\"Try to convert path to gcsfuse path if it starts with gs:// else do not modify it.\"\"\"\n",
" path = path.strip()\n",
" if path.startswith(\"gs://\"):\n",
" return \"/gcs/\" + path[5:]\n",
" return path\n",
"\n",
"\n",
"def deploy_model(model_path):\n",
" \"\"\"Deploy the model to Vertex AI Endpoint for prediction.\"\"\"\n",
" model_name = \"autogluon\"\n",
" endpoint = aiplatform.Endpoint.create(display_name=f\"{model_name}-endpoint\")\n",
" serving_env = {\n",
" \"model_path\": model_path,\n",
" }\n",
" # Since the model_id is a GCS path, use artifact_uri to pass it\n",
" # to the serving docker.\n",
" artifact_uri = model_path\n",
" model = aiplatform.Model.upload(\n",
" display_name=model_name,\n",
" serving_container_image_uri=SERVE_DOCKER_URI,\n",
" serving_container_ports=[PORT],\n",
" serving_container_predict_route=\"/predict\",\n",
" serving_container_health_route=\"/ping\",\n",
" serving_container_environment_variables=serving_env,\n",
" artifact_uri=artifact_uri,\n",
" )\n",
" model.deploy(\n",
" endpoint=endpoint,\n",
" machine_type=\"n1-highmem-16\",\n",
" deploy_request_timeout=1800,\n",
" service_account=SERVICE_ACCOUNT,\n",
" )\n",
" return model, endpoint"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "aCpLmWPMpJQ8"
},
"source": [
"## Finetune with AutoGluon\n",
"\n",
"Create and run the training job with the model-garden PyTorch AutoGluon training docker using the Vertex AI SDK. The training uses one CPU and runs for around 3 mins once the training job begins."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "aec22792ee84"
},
"outputs": [],
"source": [
"# Set up training docker arguments.\n",
"\n",
"TIMESTAMP = datetime.now().strftime(\"%Y%m%d_%H%M%S\")\n",
"JOB_NAME = \"pytorch_autogluon\" + TIMESTAMP\n",
"\n",
"finetuning_workdir = os.path.join(BUCKET_URI, JOB_NAME)\n",
"train_data_path = (\n",
" \"https://raw.githubusercontent.com/mli/ag-docs/main/knot_theory/train.csv\"\n",
")\n",
"# The column id to predict.\n",
"label = \"signature\"\n",
"\n",
"# We are using the\n",
"docker_args_list = [\n",
" \"--train_data_path\",\n",
" train_data_path,\n",
" \"--label\",\n",
" label,\n",
" \"--model_save_path\",\n",
" f\"{gcs_fuse_path(finetuning_workdir)}\",\n",
"]\n",
"print(docker_args_list)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "2ELphfgj1f3Q"
},
"outputs": [],
"source": [
"# Create and run the training job.\n",
"# Click on the generated link in the output under \"View backing custom job:\" to see your run in the Cloud Console.\n",
"container_uri = TRAIN_DOCKER_URI\n",
"job = aiplatform.CustomContainerTrainingJob(\n",
" display_name=JOB_NAME,\n",
" container_uri=container_uri,\n",
")\n",
"model = job.run(\n",
" args=docker_args_list,\n",
" base_output_dir=f\"{finetuning_workdir}\",\n",
" replica_count=1,\n",
" machine_type=\"n1-highmem-16\",\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "iILhhP3TfO8B"
},
"source": [
"## Run online prediction\n",
"\n",
"Run online prediction with the trained model."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "XswgX6JqRwFK"
},
"source": [
"Upload the trained model and deploy it to an endpoint for prediction. This step takes around 20 minutes to finish."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "74yqis5ufO8B"
},
"outputs": [],
"source": [
"model, endpoint = deploy_model(model_path=finetuning_workdir)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "iiozz1aVR7Pe"
},
"source": [
"Send the prediction request for the query data. The expected `signature` label for this example query data is `-2`. You can also send comma separated multiple queries."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "qxj4Xv_DhHXj"
},
"outputs": [],
"source": [
"instances = [\n",
" {\n",
" \"Unnamed: 0\": 70746,\n",
" \"chern_simons\": 0.0905302166938781,\n",
" \"cusp_volume\": 12.226321765565215,\n",
" \"hyperbolic_adjoint_torsion_degree\": 0,\n",
" \"hyperbolic_torsion_degree\": 10,\n",
" \"injectivity_radius\": 0.5077560544013977,\n",
" \"longitudinal_translation\": 10.685555458068848,\n",
" \"meridinal_translation_imag\": 1.1441915035247805,\n",
" \"meridinal_translation_real\": -0.5191566348075867,\n",
" \"short_geodesic_imag_part\": -2.7606005668640137,\n",
" \"short_geodesic_real_part\": 1.0155121088027954,\n",
" \"Symmetry_0\": 0.0,\n",
" \"Symmetry_D3\": 0.0,\n",
" \"Symmetry_D4\": 0.0,\n",
" \"Symmetry_D6\": 0.0,\n",
" \"Symmetry_D8\": 0.0,\n",
" \"Symmetry_Z/2 + Z/2\": 1.0,\n",
" \"volume\": 11.393224716186523,\n",
" },\n",
"]\n",
"predictions = endpoint.predict(instances=instances).predictions\n",
"print(predictions)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "TpV-iwP9qw9c"
},
"source": [
"## Cleaning up\n",
"\n",
"To clean up all Google Cloud resources used in this project, you can [delete the Google Cloud\n",
"project](https://cloud.google.com/resource-manager/docs/creating-managing-projects#shutting_down_projects) you used for the tutorial.\n",
"\n",
"Otherwise, you can delete the individual resources you created in this tutorial:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "sx_vKniMq9ZX"
},
"outputs": [],
"source": [
"# Delete endpoint resource.\n",
"endpoint.delete(force=True)\n",
"\n",
"# Delete model resource.\n",
"model.delete()\n",
"\n",
"# Delete Cloud Storage objects that were created.\n",
"delete_bucket = False\n",
"if delete_bucket or os.getenv(\"IS_TESTING\"):\n",
" ! gsutil -m rm -r $BUCKET_URI"
]
}
],
"metadata": {
"colab": {
"name": "model_garden_pytorch_autogluon.ipynb",
"toc_visible": true
},
"kernelspec": {
"display_name": "Python 3",
"name": "python3"
}
},
"nbformat": 4,
"nbformat_minor": 0
}