Compare commits

...
11 Commits
Author SHA1 Message Date
yinghsienwu fb89ec2c07 fix: links 2024-01-23 17:26:22 +00:00
yinghsienwu 569264a0c4 fix: docs 2024-01-23 17:20:00 +00:00
yinghsienwu b6b2e74cd7 fix: docs 2024-01-22 22:08:03 +00:00
yinghsienwu 4cb9b7fe38 fix: update notebook 2024-01-22 19:21:47 +00:00
yinghsienwu 4466795a7f fix: clean up 2024-01-22 18:01:09 +00:00
yinghsienwu 5bcb683e85 fix: lint 2024-01-20 04:00:47 +00:00
yinghsienwu 8a41b1d7bf fix: scale up 2024-01-20 03:53:21 +00:00
yinghsienwu c5458742ff fix: python version 2024-01-19 23:44:56 +00:00
yinghsienwu b61562c95b fix: vpc 2024-01-19 23:35:10 +00:00
yinghsienwu 204c214a7e fix: lint 2024-01-19 23:12:50 +00:00
yinghsienwu 70b8d8c877 feat: Add Ray on Vertex cluster management sample 2024-01-19 23:02:35 +00:00
@@ -0,0 +1,637 @@
{
"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": "JAPoU8Sm5E6e"
},
"source": [
"# Ray on Vertex AI cluster management\n",
"\n",
"\n",
"<table align=\"left\">\n",
"\n",
" <td>\n",
" <a href=\"https://colab.research.google.com/github/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/official/ray_on_vertex_ai/ray_cluster_management.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",
" <td>\n",
" <a href=\"https://github.com/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/official/ray_on_vertex_ai/ray_cluster_management.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>\n",
" <a href=\"https://console.cloud.google.com/vertex-ai/workbench/deploy-notebook?download_url=https://raw.githubusercontent.com/GoogleCloudPlatform/vertex-ai-samples/main/notebooks/official/ray_on_vertex_ai/ray_cluster_management.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": "tvgnzT1CKxrO"
},
"source": [
"## Overview\n",
"\n",
"This tutorial demonstrates how to use Ray on Vertex AI SDK for cluster management.\n",
"\n",
"Learn more about [Ray on Vertex AI overview](https://cloud.google.com/vertex-ai/docs/open-source/ray-on-vertex-ai/overview)."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "d975e698c9a4"
},
"source": [
"### Objective\n",
"\n",
"In this tutorial, you learn how to create a cluster, list existing clusters, get a cluster, update (manually scaling) a cluster, and delete a cluster.\n",
"\n",
"This tutorial uses the following Google Cloud ML services and resources:\n",
"\n",
"- [`Ray on Vertex AI`](https://cloud.google.com/vertex-ai/docs/open-source/ray-on-vertex-ai/overview)\n",
"\n",
"\n",
"The steps performed include:\n",
"\n",
"- Create a cluster.\n",
"- List existing clusters.\n",
"- Get a cluster.\n",
"- Manually scale up the cluster, then scale down the cluster.\n",
"- Delete existing clusters."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "aed92deeb4a0"
},
"source": [
"### Costs \n",
"\n",
"This tutorial uses billable components of Google Cloud:\n",
"\n",
"* Vertex AI\n",
"\n",
"Learn about [Ray on Vertex AI pricing](https://cloud.google.com/vertex-ai/docs/open-source/ray-on-vertex-ai/overview#pricing),\n",
"and use the [Pricing Calculator](https://cloud.google.com/products/calculator/)\n",
"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. \n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "2b4ef9b72d43"
},
"outputs": [],
"source": [
"! pip3 install --upgrade --quiet google-cloud-aiplatform[ray]"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "58707a750154"
},
"source": [
"### Colab only: Uncomment the following cell to restart the kernel."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "f200f10a1da3"
},
"outputs": [],
"source": [
"# 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",
"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 Vertex AI API](https://console.cloud.google.com/flows/enableapi?apiid=aiplatform.googleapis.com).\n",
"\n",
"4. If you are running this notebook locally, you need to install the [Cloud SDK](https://cloud.google.com/sdk)."
]
},
{
"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": "region"
},
"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": "markdown",
"metadata": {
"id": "zgPO1eR3CYjk"
},
"source": [
"### Create a Cloud Storage bucket\n",
"\n",
"Create a storage bucket to store intermediate artifacts such as datasets.\n",
"\n",
"- *{Note to notebook author: For any user-provided strings that need to be unique (like bucket names or model ID's), append \"-unique\" to the end so proper testing can occur}*"
]
},
{
"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 vertex_ray\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.\n",
"\n",
"[Set up a VPC peering network](https://cloud.google.com/vertex-ai/docs/general/vpc-peering) and private services connection to access Vertex AI."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "3927074343e3"
},
"outputs": [],
"source": [
"# Retrieve the project number\n",
"PROJECT_NUMBER = !gcloud projects list --filter=\"PROJECT_ID:'{PROJECT_ID}'\" --format='value(PROJECT_NUMBER)'\n",
"PROJECT_NUMBER = PROJECT_NUMBER[0]\n",
"\n",
"VPC_NETWORK = \"[your-network-name]\"\n",
"VPC_NETWORK_FULL = \"projects/{}/global/networks/{}\".format(PROJECT_NUMBER, VPC_NETWORK)\n",
"VPC_NETWORK_FULL"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "init_aip:mbsdk,all"
},
"outputs": [],
"source": [
"aiplatform.init(project=PROJECT_ID, location=REGION, staging_bucket=BUCKET_URI)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "4481de089cc2"
},
"source": [
"## Create a cluster\n",
"\n",
"Note that within the same VPC network, numbers of clusters and nodes you can create are restricted by IP ranges."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "6031842560d2"
},
"outputs": [],
"source": [
"head_node_type = vertex_ray.Resources(\n",
" machine_type=\"n1-standard-4\",\n",
" node_count=1,\n",
")\n",
"\n",
"worker_node_types = [\n",
" vertex_ray.Resources(\n",
" machine_type=\"n1-standard-8\",\n",
" node_count=2, # Can be > 1\n",
" accelerator_type=\"NVIDIA_TESLA_K80\",\n",
" accelerator_count=1,\n",
" )\n",
"]\n",
"\n",
"cluster_resource_name = vertex_ray.create_ray_cluster(\n",
" head_node_type=head_node_type,\n",
" network=VPC_NETWORK_FULL,\n",
" worker_node_types=worker_node_types,\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "95da29c3e54b"
},
"source": [
"## List existing clusters"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "f35e48bf1b35"
},
"outputs": [],
"source": [
"clusters = vertex_ray.list_ray_clusters()\n",
"clusters"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "be2e79a1af08"
},
"source": [
"## Update an existing cluster (manually scaling)\n",
"\n",
"\n",
"Note that the maximum number of worker nodes you can scale up depends on the initial node counts (more details are in these [formulas](https://cloud.google.com/vertex-ai/docs/open-source/ray-on-vertex-ai/set-up)) and is restricted by IP ranges within the same VPC network.\n",
"\n",
"Get the cluster you want to scale."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "7b0d96c88191"
},
"outputs": [],
"source": [
"cluster = vertex_ray.get_ray_cluster(cluster_resource_name)\n",
"cluster"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "f1a20a92c80b"
},
"source": [
"Scale down workers from 2 nodes to 1 node."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "03d3780f8b5f"
},
"outputs": [],
"source": [
"new_worker_node_types = []\n",
"for worker_node_type in cluster.worker_node_types:\n",
" worker_node_type.node_count = 1\n",
" new_worker_node_types.append(worker_node_type)\n",
"\n",
"cluster_resource_name = vertex_ray.update_ray_cluster(\n",
" cluster_resource_name=cluster_resource_name,\n",
" worker_node_types=new_worker_node_types,\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "b025fdd97348"
},
"source": [
"Verify if the cluster is successfully scaled down."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "604a124c425f"
},
"outputs": [],
"source": [
"cluster = vertex_ray.get_ray_cluster(cluster_resource_name)\n",
"cluster"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "72dc8445c9cd"
},
"source": [
"Scale up to 2 worker nodes."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "750293506627"
},
"outputs": [],
"source": [
"new_worker_node_types = []\n",
"for worker_node_type in cluster.worker_node_types:\n",
" worker_node_type.node_count = 2\n",
" new_worker_node_types.append(worker_node_type)\n",
"\n",
"cluster_resource_name = vertex_ray.update_ray_cluster(\n",
" cluster_resource_name=cluster_resource_name,\n",
" worker_node_types=new_worker_node_types,\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "e62b56ac6429"
},
"source": [
"Verify if the cluster is successfully scaled up."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "be90cef02cf2"
},
"outputs": [],
"source": [
"cluster = vertex_ray.get_ray_cluster(cluster_resource_name)\n",
"cluster"
]
},
{
"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 cluster you created in this tutorial."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "sx_vKniMq9ZX"
},
"outputs": [],
"source": [
"import os\n",
"\n",
"# Delete the cluster\n",
"vertex_ray.delete_ray_cluster(cluster.cluster_resource_name)\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": {
"collapsed_sections": [],
"name": "ray_cluster_management.ipynb",
"toc_visible": true
},
"kernelspec": {
"display_name": "Python 3",
"name": "python3"
}
},
"nbformat": 4,
"nbformat_minor": 0
}