Reduce boilerplate for custom tabular bq notebook (#1207)

* Reduced boilerplate

Ran linter and fixed install

Added pyarrow

Fixed pip install

Reverted unneeded changes

Fixed pip install

Linted code and added missing preprocessor call

Fixed preprocessor

Default to us-central1

* Ran linter

* Simplified service account section
This commit is contained in:
Ivan Cheung
2022-11-10 17:47:13 -05:00
committed by GitHub
parent 0ca86220f8
commit 6ccad69a18
5 changed files with 132 additions and 441 deletions
@@ -111,6 +111,7 @@ def _process_notebook(
) = remove_no_execute_cells_preprocessor.preprocess(nb)
(nb, resources) = update_variables_preprocessor.preprocess(nb, resources)
(nb, resources) = unique_strings_preprocessor.preprocess(nb, resources)
with open(notebook_path, mode="w", encoding="utf-8") as new_file:
nbformat.write(nb, new_file)
+9 -3
View File
@@ -73,13 +73,19 @@ def generate_uuid(length: int = 8) -> str:
class UniqueStringsPreprocessor(Preprocessor):
# A preprocessor that replaces strings that end with "-unique" with a uuid.
# A preprocessor that replaces strings that end with "-unique" or "_unique" with a uuid.
@staticmethod
def update_unique_strings(content: str):
# Replace strings that end with "-unique" with a uuid.
# Replace strings that end with "-unique" or "_unique" with a uuid.
return content.replace('-unique"', f'-{generate_uuid()}"')
unique_id = generate_uuid()
return (
content.replace('-unique"', f'-{unique_id}"')
.replace("-unique'", f'-{unique_id}"')
.replace('_unique"', f'_{unique_id}"')
.replace("_unique'", f'_{unique_id}"')
)
def preprocess(self, notebook, resources=None):
executable_cells = []
+4 -9
View File
@@ -61,9 +61,7 @@ def archive_code_and_upload(staging_bucket: str):
def download_blob_into_memory(
bucket_name: str,
blob_name: str,
download_as_text: Optional[bool]=False
bucket_name: str, blob_name: str, download_as_text: Optional[bool] = False
) -> Union[bytes, str]:
"""
Downloads a blob into memory as byte or as text if
@@ -79,13 +77,10 @@ def download_blob_into_memory(
# Download the blob content
if download_as_text:
contents = blob.download_as_text()
contents = blob.download_as_text()
else:
contents = blob.download_as_bytes()
contents = blob.download_as_bytes()
print(
f"Downloaded storage object {blob_name} from bucket {bucket_name}."
)
print(f"Downloaded storage object {blob_name} from bucket {bucket_name}.")
return contents
+2 -2
View File
@@ -255,7 +255,7 @@
},
"outputs": [],
"source": [
"REGION = \"[your-region]\" # @param {type: \"string\"}"
"REGION = \"us-central1\" # @param {type: \"string\"}"
]
},
{
@@ -327,7 +327,7 @@
},
"source": [
"**4. Service account or other**\n",
"* See all authentication options here: [Google Cloud Platform Jupyter Notebook Authentication Guide](https://github.com/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/notebook_authentication_guide.ipynb)"
"* See how to grant Cloud Storage permissions to your service account at https://cloud.google.com/storage/docs/gsutil/commands/iam#ch-examples."
]
},
{
@@ -123,55 +123,6 @@
"to generate a cost estimate based on your projected usage."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "384b53dfdb54"
},
"source": [
"### Set up your local development environment\n",
"\n",
"**If you are using Colab or Vertex AI Workbench Notebooks**, your environment already meets\n",
"all the requirements to run this notebook. You can skip this step."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "7e689ee0bc3c"
},
"source": [
"**Otherwise**, make sure your environment meets this notebook's requirements.\n",
"You need the following:\n",
"\n",
"* The Google Cloud SDK\n",
"* Git\n",
"* Python 3\n",
"* virtualenv\n",
"* Jupyter notebook running in a virtual environment with Python 3\n",
"\n",
"The Google Cloud guide to [Setting up a Python development\n",
"environment](https://cloud.google.com/python/setup) and the [Jupyter\n",
"installation guide](https://jupyter.org/install) provide detailed instructions\n",
"for meeting these requirements. The following steps provide a condensed set of\n",
"instructions:\n",
"\n",
"1. [Install and initialize the Cloud SDK.](https://cloud.google.com/sdk/docs/)\n",
"\n",
"1. [Install Python 3.](https://cloud.google.com/python/setup#installing_python)\n",
"\n",
"1. [Install\n",
" virtualenv](https://cloud.google.com/python/setup#installing_and_using_virtualenv)\n",
" and create a virtual environment that uses Python 3. Activate the virtual environment.\n",
"\n",
"1. To install Jupyter, run `pip3 install jupyter` on the\n",
"command-line in a terminal shell.\n",
"\n",
"1. To launch Jupyter, run `jupyter notebook` on the command-line in a terminal shell.\n",
"\n",
"1. Open this notebook in the Jupyter Notebook Dashboard."
]
},
{
"cell_type": "markdown",
"metadata": {
@@ -191,53 +142,35 @@
},
"outputs": [],
"source": [
"import os\n",
"\n",
"# The Vertex AI Workbench Notebook product has specific requirements\n",
"IS_WORKBENCH_NOTEBOOK = os.getenv(\"DL_ANACONDA_HOME\")\n",
"IS_USER_MANAGED_WORKBENCH_NOTEBOOK = os.path.exists(\n",
" \"/opt/deeplearning/metadata/env_version\"\n",
")\n",
"\n",
"# Vertex AI Notebook requires dependencies to be installed with '--user'\n",
"USER_FLAG = \"\"\n",
"if IS_WORKBENCH_NOTEBOOK:\n",
" USER_FLAG = \"--user\"\n",
"\n",
"# Install the packages\n",
"! pip3 install {USER_FLAG} --upgrade google-cloud-aiplatform \\\n",
" google-cloud-storage \\\n",
" google-cloud-bigquery \\\n",
" pyarrow -q"
"! pip3 install --upgrade google-cloud-aiplatform \\\n",
" google-cloud-storage \\\n",
" google-cloud-bigquery \\\n",
" pyarrow"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "restart"
"id": "blGlVGFYW9Pt"
},
"source": [
"### Restart the kernel\n",
"\n",
"Once you've installed everything, you need to restart the notebook kernel so it can find the packages."
"### Colab only: Uncomment the following cell to restart the kernel."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "bzPxhxS5lugp"
"id": "0JrvuK6LUYnQ"
},
"outputs": [],
"source": [
"import os\n",
"# Automatically restart kernel after installs so that your environment can access the new packages\n",
"# import IPython\n",
"\n",
"if not os.getenv(\"IS_TESTING\"):\n",
" # Automatically restart kernel after installs\n",
" import IPython\n",
"\n",
" app = IPython.Application.instance()\n",
" app.kernel.do_shutdown(True)"
"# app = IPython.Application.instance()\n",
"# app.kernel.do_shutdown(True)"
]
},
{
@@ -249,30 +182,6 @@
"## Before you begin"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "before_you_begin"
},
"source": [
"### 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",
"2. [Make sure that billing is enabled for your project](https://cloud.google.com/billing/docs/how-to/modify-project).\n",
"\n",
"3. [Enable the following APIs: Vertex AI API, Cloud Resource Manager API](https://console.cloud.google.com/flows/enableapi?apiid=aiplatform.googleapis.com,cloudresourcemanager.googleapis.com).\n",
"\n",
"4. If you are running this notebook locally, you need to install the [Cloud SDK](https://cloud.google.com/sdk).\n",
"\n",
"5. Enter your project ID in the cell below. Then run the cell to make sure the\n",
"Cloud SDK uses the right project for all the commands in this notebook.\n",
"\n",
"**Note**: Jupyter runs lines prefixed with `!` as shell commands, and it interpolates Python variables prefixed with `$` into these commands."
]
},
{
"cell_type": "markdown",
"metadata": {
@@ -281,7 +190,10 @@
"source": [
"#### Set your project ID\n",
"\n",
"**If you don't know your project ID**, you might be able to get your project ID using `gcloud`."
"**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)"
]
},
{
@@ -292,33 +204,10 @@
},
"outputs": [],
"source": [
"PROJECT_ID = \"[your-project-id]\" # @param {type:\"string\"}"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "a36c4b991a39"
},
"outputs": [],
"source": [
"if PROJECT_ID == \"\" or PROJECT_ID is None or PROJECT_ID == \"[your-project-id]\":\n",
" # Get your GCP project id from gcloud\n",
" shell_output = ! gcloud config list --format 'value(core.project)' 2>/dev/null\n",
" PROJECT_ID = shell_output[0]\n",
" print(\"Project ID:\", PROJECT_ID)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "f2e3c0f2cbfb"
},
"outputs": [],
"source": [
"! gcloud config set project $PROJECT_ID"
"PROJECT_ID = \"[your-project-id]\" # @param {type:\"string\"}\n",
"\n",
"# Set the project id\n",
"! gcloud config set project {PROJECT_ID}"
]
},
{
@@ -329,16 +218,7 @@
"source": [
"#### Region\n",
"\n",
"You can also change the `REGION` variable, which is used for operations\n",
"throughout the rest of this notebook. Below are regions supported for Vertex AI. We recommend that you choose the region closest to you.\n",
"\n",
"- Americas: `us-central1`\n",
"- Europe: `europe-west4`\n",
"- Asia Pacific: `asia-east1`\n",
"\n",
"You may not use a multi-regional bucket for training with Vertex AI. Not all regions provide support for all Vertex AI services.\n",
"\n",
"Learn more about [Vertex AI regions](https://cloud.google.com/vertex-ai/docs/general/locations)."
"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)."
]
},
{
@@ -349,41 +229,7 @@
},
"outputs": [],
"source": [
"REGION = \"[your-region]\" # @param {type: \"string\"}\n",
"\n",
"if REGION == \"[your-region]\":\n",
" REGION = \"us-central1\""
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "timestamp"
},
"source": [
"#### UUID\n",
"\n",
"If you are in a live tutorial session, you might be using a shared test account or project. To avoid name collisions between users on resources created, you create a uuid for each instance session, and append it onto the name of resources you create in this tutorial."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "c-pX32xalugs"
},
"outputs": [],
"source": [
"import random\n",
"import string\n",
"\n",
"\n",
"# Generate a uuid of length 8\n",
"def generate_uuid():\n",
" return \"\".join(random.choices(string.ascii_lowercase + string.digits, k=8))\n",
"\n",
"\n",
"UUID = generate_uuid()"
"REGION = \"us-central1\" # @param {type: \"string\"}"
]
},
{
@@ -394,71 +240,68 @@
"source": [
"### Authenticate your Google Cloud account\n",
"\n",
"**If you are using Vertex AI Workbench Notebooks**, your environment is already\n",
"authenticated. Skip this step."
"Depending on your Jupyter environment, you may have to manually authenticate. Follow the relevant instructions below."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "gcp_authenticate"
"id": "BaFKzJ_xXpvm"
},
"source": [
"**If you are using Colab**, run the cell below and follow the instructions\n",
"when prompted to authenticate your account via oAuth.\n",
"\n",
"**Otherwise**, follow these steps:\n",
"\n",
"1. In the Cloud Console, go to the [**Create service account key**\n",
" page](https://console.cloud.google.com/apis/credentials/serviceaccountkey).\n",
"\n",
"2. Click **Create service account**.\n",
"\n",
"3. In the **Service account name** field, enter a name, and\n",
" click **Create**.\n",
"\n",
"4. In the **Grant this service account access to project** section, click the **Role** drop-down list. Type \"Vertex AI\"\n",
"into the filter box, and select\n",
" **Vertex AI Administrator**. Type \"Storage Object Admin\" into the filter box, and select **Storage Object Admin**.\n",
"\n",
"5. Click *Create*. A JSON file that contains your key downloads to your\n",
"local environment.\n",
"\n",
"6. Enter the path to your service account key as the\n",
"`GOOGLE_APPLICATION_CREDENTIALS` variable in the cell below and run the cell."
"**1. Vertex AI Workbench**\n",
"* Do nothing as you are already authenticated."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "_fV-KyGAX4Xl"
},
"source": [
"**2. Local JupyterLab instance, uncomment and run:**\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "vF60K5v1lugs"
"id": "7uXB1HAPX6L_"
},
"outputs": [],
"source": [
"# If you are running this notebook in Colab, run this cell and follow the\n",
"# instructions to authenticate your GCP account. This provides access to your\n",
"# Cloud Storage bucket and lets you submit training jobs and prediction\n",
"# requests.\n",
"\n",
"import os\n",
"import sys\n",
"\n",
"# If on Vertex AI Workbench, then don't execute this code\n",
"IS_COLAB = \"google.colab\" in sys.modules\n",
"if not os.path.exists(\"/opt/deeplearning/metadata/env_version\") and not os.getenv(\n",
" \"DL_ANACONDA_HOME\"\n",
"):\n",
" if \"google.colab\" in sys.modules:\n",
" from google.colab import auth as google_auth\n",
"\n",
" google_auth.authenticate_user()\n",
"\n",
" # If you are running this notebook locally, replace the string below with the\n",
" # path to your service account key and run this cell to authenticate your GCP\n",
" # account.\n",
" elif not os.getenv(\"IS_TESTING\"):\n",
" %env GOOGLE_APPLICATION_CREDENTIALS ''"
"# ! gcloud auth login"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "Ab_TRMQIYCCX"
},
"source": [
"**3. Colab, uncomment and run:**"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "vx25htmYYExI"
},
"outputs": [],
"source": [
"# from google.colab import auth\n",
"# auth.authenticate_user()"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "uZdA0-jBYGqt"
},
"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."
]
},
{
@@ -469,17 +312,7 @@
"source": [
"### Create a Cloud Storage bucket\n",
"\n",
"**The following steps are required, regardless of your notebook environment.**\n",
"\n",
"When you submit a training job using the Cloud SDK, you upload a Python package\n",
"containing your training code to a Cloud Storage bucket. Vertex AI runs\n",
"the code from this package. In this tutorial, Vertex AI also saves the\n",
"trained model that results from your job in the same bucket. Using this model artifact, you can then\n",
"create Vertex AI model and endpoint resources in order to serve\n",
"online predictions.\n",
"\n",
"Set the name of your Cloud Storage bucket below. It must be unique across all\n",
"Cloud Storage buckets."
"Create a storage bucket to store intermediate artifacts such as datasets."
]
},
{
@@ -490,21 +323,7 @@
},
"outputs": [],
"source": [
"BUCKET_NAME = \"[your-bucket-name]\" # @param {type:\"string\"}\n",
"BUCKET_URI = f\"gs://{BUCKET_NAME}\""
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "autoset_bucket"
},
"outputs": [],
"source": [
"if BUCKET_NAME == \"\" or BUCKET_NAME is None or BUCKET_NAME == \"[your-bucket-name]\":\n",
" BUCKET_NAME = PROJECT_ID + \"aip-\" + UUID\n",
" BUCKET_URI = f\"gs://{BUCKET_NAME}\""
"BUCKET_URI = \"gs://your-bucket-name-unique\" # @param {type:\"string\"}"
]
},
{
@@ -527,26 +346,6 @@
"! gsutil mb -l $REGION -p $PROJECT_ID $BUCKET_URI"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "validate_bucket"
},
"source": [
"Finally, validate access to your Cloud Storage bucket by examining its contents:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "oadE10x2lugu"
},
"outputs": [],
"source": [
"! gsutil ls -al $BUCKET_URI"
]
},
{
"cell_type": "markdown",
"metadata": {
@@ -564,11 +363,6 @@
},
"outputs": [],
"source": [
"import json\n",
"import os\n",
"import sys\n",
"\n",
"import numpy as np\n",
"from google.cloud import aiplatform, bigquery"
]
},
@@ -618,40 +412,6 @@
"bqclient = bigquery.Client(project=PROJECT_ID)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "accelerators:training,prediction"
},
"source": [
"### Set hardware accelerators\n",
"\n",
"You can set hardware accelerators for both training and prediction.\n",
"\n",
"Set the variables `TRAIN_GPU/TRAIN_NGPU` and `DEPLOY_GPU/DEPLOY_NGPU` to use a container image supporting a GPU and the number of GPUs allocated to the virtual machine (VM) instance. For example, to use a GPU container image with 4 Nvidia Tesla K80 GPUs allocated to each VM, you would specify:\n",
"\n",
" (aip.AcceleratorType.NVIDIA_TESLA_K80, 4)\n",
"\n",
"See the [locations where accelerators are available](https://cloud.google.com/vertex-ai/docs/general/locations#accelerators).\n",
"\n",
"Otherwise specify `(None, None)` to use a container image to run on a CPU.\n",
"\n",
"Learn [which accelerators are available in your region.](https://cloud.google.com/vertex-ai/docs/general/locations#accelerators)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "xd5PLXDTlugv"
},
"outputs": [],
"source": [
"TRAIN_GPU, TRAIN_NGPU = (aiplatform.gapic.AcceleratorType.NVIDIA_TESLA_K80, 1)\n",
"\n",
"DEPLOY_GPU, DEPLOY_NGPU = (aiplatform.gapic.AcceleratorType.NVIDIA_TESLA_K80, 1)"
]
},
{
"cell_type": "markdown",
"metadata": {
@@ -673,62 +433,11 @@
},
"outputs": [],
"source": [
"TRAIN_VERSION = \"tf-gpu.2-8\"\n",
"DEPLOY_VERSION = \"tf2-gpu.2-8\"\n",
"TRAIN_VERSION = \"tf-cpu.2-8\"\n",
"DEPLOY_VERSION = \"tf2-cpu.2-8\"\n",
"\n",
"TRAIN_IMAGE = \"us-docker.pkg.dev/vertex-ai/training/{}:latest\".format(TRAIN_VERSION)\n",
"DEPLOY_IMAGE = \"us-docker.pkg.dev/vertex-ai/prediction/{}:latest\".format(DEPLOY_VERSION)\n",
"\n",
"print(\"Training:\", TRAIN_IMAGE, TRAIN_GPU, TRAIN_NGPU)\n",
"print(\"Deployment:\", DEPLOY_IMAGE, DEPLOY_GPU, DEPLOY_NGPU)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "machine:training,prediction"
},
"source": [
"### Set machine types\n",
"\n",
"Next, set the machine types to use for training and prediction.\n",
"\n",
"- Set the variables `TRAIN_COMPUTE` and `DEPLOY_COMPUTE` to configure your compute resources for training and prediction.\n",
" - `machine type`\n",
" - `n1-standard`: 3.75GB of memory per vCPU\n",
" - `n1-highmem`: 6.5GB of memory per vCPU\n",
" - `n1-highcpu`: 0.9 GB of memory per vCPU\n",
" - `vCPUs`: number of \\[2, 4, 8, 16, 32, 64, 96 \\]\n",
"\n",
"*Note: The following is not supported for training:*\n",
"\n",
" - `standard`: 2 vCPUs\n",
" - `highcpu`: 2, 4 and 8 vCPUs\n",
"\n",
"*Note: You may also use n2 and e2 machine types for training and deployment, but they do not support GPUs*.\n",
"\n",
"Learn [which machine types are available for training](https://cloud.google.com/vertex-ai/docs/training/configure-compute) and [which machine types are available for prediction](https://cloud.google.com/vertex-ai/docs/predictions/configure-compute)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "YAXwbqKKlugv"
},
"outputs": [],
"source": [
"MACHINE_TYPE = \"n1-standard\"\n",
"\n",
"VCPU = \"4\"\n",
"TRAIN_COMPUTE = MACHINE_TYPE + \"-\" + VCPU\n",
"print(\"Train machine type\", TRAIN_COMPUTE)\n",
"\n",
"MACHINE_TYPE = \"n1-standard\"\n",
"\n",
"VCPU = \"4\"\n",
"DEPLOY_COMPUTE = MACHINE_TYPE + \"-\" + VCPU\n",
"print(\"Deploy machine type\", DEPLOY_COMPUTE)"
"DEPLOY_IMAGE = \"us-docker.pkg.dev/vertex-ai/prediction/{}:latest\".format(DEPLOY_VERSION)"
]
},
{
@@ -752,6 +461,10 @@
},
"outputs": [],
"source": [
"import json\n",
"\n",
"import numpy as np\n",
"\n",
"# Calculate mean and std across all rows\n",
"\n",
"# Define the BigQuery source dataset\n",
@@ -878,12 +591,9 @@
},
"outputs": [],
"source": [
"JOB_NAME = \"custom_job_\" + UUID\n",
"JOB_NAME = \"custom_job_unique\"\n",
"\n",
"if not TRAIN_NGPU or TRAIN_NGPU < 2:\n",
" TRAIN_STRATEGY = \"single\"\n",
"else:\n",
" TRAIN_STRATEGY = \"mirror\"\n",
"TRAIN_STRATEGY = \"single\"\n",
"\n",
"EPOCHS = 20\n",
"BATCH_SIZE = 10\n",
@@ -1263,30 +973,15 @@
" model_serving_container_image_uri=DEPLOY_IMAGE,\n",
")\n",
"\n",
"MODEL_DISPLAY_NAME = \"penguins-\" + UUID\n",
"MODEL_DISPLAY_NAME = \"penguins_model_unique\"\n",
"\n",
"# Start the training\n",
"if TRAIN_GPU:\n",
" model = job.run(\n",
" dataset=dataset,\n",
" model_display_name=MODEL_DISPLAY_NAME,\n",
" bigquery_destination=f\"bq://{PROJECT_ID}\",\n",
" args=CMDARGS,\n",
" replica_count=1,\n",
" machine_type=TRAIN_COMPUTE,\n",
" accelerator_type=TRAIN_GPU.name,\n",
" accelerator_count=TRAIN_NGPU,\n",
" )\n",
"else:\n",
" model = job.run(\n",
" dataset=dataset,\n",
" model_display_name=MODEL_DISPLAY_NAME,\n",
" bigquery_destination=f\"bq://{PROJECT_ID}\",\n",
" args=CMDARGS,\n",
" replica_count=1,\n",
" machine_type=TRAIN_COMPUTE,\n",
" accelerator_count=0,\n",
" )"
"model = job.run(\n",
" dataset=dataset,\n",
" model_display_name=MODEL_DISPLAY_NAME,\n",
" bigquery_destination=f\"bq://{PROJECT_ID}\",\n",
" args=CMDARGS,\n",
")"
]
},
{
@@ -1340,33 +1035,9 @@
},
"outputs": [],
"source": [
"DEPLOYED_NAME = \"penguins_deployed-\" + UUID\n",
"DEPLOYED_NAME = \"penguins_deployed_unique\"\n",
"\n",
"TRAFFIC_SPLIT = {\"0\": 100}\n",
"\n",
"MIN_NODES = 1\n",
"MAX_NODES = 1\n",
"\n",
"if DEPLOY_GPU:\n",
" endpoint = model.deploy(\n",
" deployed_model_display_name=DEPLOYED_NAME,\n",
" traffic_split=TRAFFIC_SPLIT,\n",
" machine_type=DEPLOY_COMPUTE,\n",
" accelerator_type=DEPLOY_GPU.name,\n",
" accelerator_count=DEPLOY_NGPU,\n",
" min_replica_count=MIN_NODES,\n",
" max_replica_count=MAX_NODES,\n",
" )\n",
"else:\n",
" endpoint = model.deploy(\n",
" deployed_model_display_name=DEPLOYED_NAME,\n",
" traffic_split=TRAFFIC_SPLIT,\n",
" machine_type=DEPLOY_COMPUTE,\n",
" accelerator_type=DEPLOY_COMPUTE.name,\n",
" accelerator_count=0,\n",
" min_replica_count=MIN_NODES,\n",
" max_replica_count=MAX_NODES,\n",
" )"
"endpoint = model.deploy(deployed_model_display_name=DEPLOYED_NAME)"
]
},
{
@@ -1538,13 +1209,9 @@
"id": "undeploy_model"
},
"source": [
"## Undeploy the model\n",
"## Undeploy models\n",
"\n",
"To undeploy your `Model` resource from the serving `Endpoint` resource, use the endpoint's `undeploy` method with the following parameter:\n",
"\n",
"- `deployed_model_id`: The model deployment identifier returned by the endpoint service when the `Model` resource was deployed. You can retrieve the deployed models using the endpoint's `deployed_models` property.\n",
"\n",
"Since this is the only deployed model on the `Endpoint` resource, you can omit `traffic_split`."
"To undeploy all `Model` resources from the serving `Endpoint` resource, use the endpoint's `undeploy_all` method."
]
},
{
@@ -1555,8 +1222,7 @@
},
"outputs": [],
"source": [
"deployed_model_id = endpoint.list_models()[0].id\n",
"endpoint.undeploy(deployed_model_id=deployed_model_id)"
"endpoint.undeploy_all()"
]
},
{
@@ -1585,8 +1251,7 @@
},
"outputs": [],
"source": [
"# Warning: Setting this to true deletes everything in your bucket\n",
"delete_bucket = False\n",
"import os\n",
"\n",
"# Delete the training job\n",
"job.delete()\n",
@@ -1597,6 +1262,9 @@
"# Delete the endpoint\n",
"endpoint.delete()\n",
"\n",
"# Warning: Setting this to true deletes everything in your bucket\n",
"delete_bucket = False\n",
"\n",
"if delete_bucket or os.getenv(\"IS_TESTING\"):\n",
" ! gsutil rm -r $BUCKET_URI"
]
@@ -1604,7 +1272,28 @@
],
"metadata": {
"colab": {
"collapsed_sections": [],
"collapsed_sections": [
"overview:custom",
"objective:custom,training,online_prediction",
"dataset:custom,cifar10,icn",
"costs",
"7c163842eabd",
"accelerators:training,prediction",
"container:training,prediction",
"machine:training,prediction",
"59f24e7d2269",
"5c7732822757",
"train_custom_model",
"train_custom_job_args",
"taskpy_contents",
"train_custom_job",
"deploy_model:dedicated",
"make_prediction",
"get_test_item:test",
"send_prediction_request:image",
"undeploy_model",
"cleanup:custom"
],
"name": "custom-tabular-bq-managed-dataset.ipynb",
"toc_visible": true
},