From 44c868c4b44bd1ce2be29963b323702f38e5ccbc Mon Sep 17 00:00:00 2001 From: Andrew Ferlitsch Date: Tue, 27 Jun 2023 08:34:50 -0700 Subject: [PATCH] fix: boilerplate reduction (#2032) * fix: boulerplate reduction * fix: IS_COLAB * fix: IS_COLAB --- ...ertex_training_with_custom_container.ipynb | 300 +++++------------- 1 file changed, 74 insertions(+), 226 deletions(-) diff --git a/notebooks/official/training/multi_node_ddp_nccl_vertex_training_with_custom_container.ipynb b/notebooks/official/training/multi_node_ddp_nccl_vertex_training_with_custom_container.ipynb index 7d8630f66..f5922166c 100644 --- a/notebooks/official/training/multi_node_ddp_nccl_vertex_training_with_custom_container.ipynb +++ b/notebooks/official/training/multi_node_ddp_nccl_vertex_training_with_custom_container.ipynb @@ -122,48 +122,6 @@ " to generate a cost estimate based on your projected usage.\n" ] }, - { - "cell_type": "markdown", - "metadata": { - "id": "f9f2f9097170" - }, - "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.\n", - "\n", - "**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": { @@ -183,20 +141,7 @@ }, "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", - "! pip3 install {USER_FLAG} --upgrade google-cloud-aiplatform -q" + "! pip3 install --quiet --upgrade google-cloud-aiplatform" ] }, { @@ -205,9 +150,7 @@ "id": "35942e320683" }, "source": [ - "### Restart the kernel\n", - "\n", - "After you install the additional packages, you need to restart the notebook kernel so it can find the packages." + "### Colab only: Uncomment the following cell to restart the kernel." ] }, { @@ -218,15 +161,11 @@ }, "outputs": [], "source": [ - "# Automatically restart kernel after installs\n", - "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)" ] }, { @@ -235,31 +174,19 @@ "id": "0e3cab0cc491" }, "source": [ - "## Before you begin" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "c27795e4f4a1" - }, - "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", + "2. [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](https://console.cloud.google.com/flows/enableapi?apiid=aiplatform.googleapis.com). \n", + "3. [Enable the Vertex AI API](https://console.cloud.google.com/flows/enableapi?apiid=aiplatform.googleapis.com). {TODO: Update the APIs needed for your tutorial. Edit the API names, and update the link to append the API IDs, separating each one with a comma. For example, container.googleapis.com,cloudbuild.googleapis.com}\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. 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." + "4. If you are running this notebook locally, you need to install the [Cloud SDK](https://cloud.google.com/sdk)." ] }, { @@ -270,7 +197,10 @@ "source": [ "#### Set your project ID\n", "\n", - "**If you don't know your project ID**, you may 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)" ] }, { @@ -281,33 +211,10 @@ }, "outputs": [], "source": [ - "PROJECT_ID = \"[your-project-id]\" # @param {type:\"string\"}" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "5bf9979b96ff" - }, - "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": "09021c90b34c" - }, - "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}" ] }, { @@ -318,16 +225,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)." ] }, { @@ -338,10 +236,7 @@ }, "outputs": [], "source": [ - "REGION = \"[your-region]\" # @param {type: \"string\"}\n", - "\n", - "if REGION == \"[your-region]\":\n", - " REGION = \"us-central1\"" + "REGION = \"us-central1\" # @param {type: \"string\"}" ] }, { @@ -378,122 +273,90 @@ { "cell_type": "markdown", "metadata": { - "id": "870777863e09" + "id": "sBCra4QMA2wR" }, "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." - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "378e70541ba9" - }, - "source": [ - "**If you are using Colab**, run the cell below and follow the instructions\n", - "when prompted to authenticate your account via oAuth.\n", + "To authenticate your Google Cloud account, follow the instructions for your Jupyter environment:\n", "\n", - "**Otherwise**, follow these steps:\n", + "**1. Vertex AI Workbench**\n", + "
You are already authenticated.\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." + "**2. Local JupyterLab instance**\n", + "
Uncomment and run the following code:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { - "id": "611c28f7b861" + "id": "254614fa0c46" }, "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": "37aa2089e5a5" + "id": "ef21552ccea8" + }, + "source": [ + "**3. Colab**\n", + "
Uncomment and run the following code:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "603adbbf0532" + }, + "outputs": [], + "source": [ + "# from google.colab import auth\n", + "\n", + "# auth.authenticate_user()" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "c13224697bfb" + }, + "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": "markdown", + "metadata": { + "id": "zgPO1eR3CYjk" }, "source": [ "### Create a Cloud Storage bucket\n", "\n", - "**The following steps are required, regardless of your notebook environment.**\n", - "\n", - "When you initialize the Vertex AI SDK for Python, you specify a Cloud Storage staging bucket. The staging bucket is where all the data associated with your dataset and model resources are retained across sessions.\n", - "\n", - "Set the name of your Cloud Storage bucket below. Bucket names must be globally unique across all Google Cloud projects, including those outside of your organization." + "Create a storage bucket to store intermediate artifacts such as datasets." ] }, { "cell_type": "code", "execution_count": null, "metadata": { - "id": "ffd7beb19fd0" + "id": "MzGDU7TWdts_" }, "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": "6f9da502010b" - }, - "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 = f\"gs://your-bucket-name-{PROJECT_ID}-unique\" # @param {type:\"string\"}" ] }, { "cell_type": "markdown", "metadata": { - "id": "15bb44ff961c" + "id": "create_bucket" }, "source": [ "**Only if your bucket doesn't already exist**: Run the following cell to create your Cloud Storage bucket." @@ -503,31 +366,11 @@ "cell_type": "code", "execution_count": null, "metadata": { - "id": "fde15c57652b" + "id": "NIq7R4HZCfIc" }, "outputs": [], "source": [ - "! gsutil mb -l $REGION $BUCKET_URI" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "1bba6423a764" - }, - "source": [ - "Finally, validate access to your Cloud Storage bucket by examining its contents:" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "0b5ae674177e" - }, - "outputs": [], - "source": [ - "! gsutil ls -al $BUCKET_URI" + "! gsutil mb -l {REGION} -p {PROJECT_ID} {BUCKET_URI}" ] }, { @@ -560,6 +403,9 @@ }, "outputs": [], "source": [ + "import sys\n", + "\n", + "IS_COLAB = 'google.colab' in sys.modules\n", "if (\n", " SERVICE_ACCOUNT == \"\"\n", " or SERVICE_ACCOUNT is None\n", @@ -595,6 +441,8 @@ }, "outputs": [], "source": [ + "import os\n", + "\n", "from google.cloud import aiplatform\n", "\n", "content_name = \"pt-img-cls-multi-node-ddp-cust-cont\""