chore, refactor (egen): edits get_started_with_pytorch_rov notebook (#3376)

* chore, refactor: edits made according to the template

* chore: lint run

* fix: remove ray version

* fix: made dataset url to http to deal with job failure error

* chore: lint run

* chore: fixes markdown as per guide

* chore: lint

* chore, fix: adds testing code and also fix the error

* chore, fix: clear outputs adds retries and adds http dataset path in testing

* chore:  review comment addressed

* chore: lint run

* refactor: removes IS_TESTING flag

* chore, fix: Removes IS_TESTING, fixes packages installations, runs end to end

* chore: lint run
This commit is contained in:
Kaushik Koilada
2024-08-20 17:24:37 +00:00
committed by GitHub
parent 83c90b11dd
commit 41c9354efa
@@ -141,220 +141,123 @@
{
"cell_type": "markdown",
"metadata": {
"id": "i7EUnXsZhAGF"
"id": "d1ea81ac77f0"
},
"source": [
"## Installation\n",
"\n",
"Install the following packages required to execute this notebook."
"## Get started"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "e5d353aa47ac"
},
"source": [
"### Install Vertex AI SDK for Python and other required packages\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "2b4ef9b72d43"
"id": "462b9c16e32b"
},
"outputs": [],
"source": [
"# Install the packages\n",
"import os\n",
"\n",
"if not os.getenv(\"IS_TESTING\"):\n",
" USER = \"--user\"\n",
"else:\n",
" USER = \"\"\n",
"\n",
"! pip3 install {USER} google-cloud-aiplatform[ray]==1.56.0 ray[data]==2.9.3 ray[train]==2.9.3 ray[tune]==2.9.3 -q --no-warn-conflicts\n",
"! pip3 install {USER} torch==2.1.2 torchvision==0.16.2 torchmetrics==1.2.1 torchserve==0.9.0 torch-model-archiver==0.9.0 -q --no-warn-conflicts\n",
"! pip3 install {USER} google-auth==2.27.0 etils==1.5.2 -q --no-warn-conflicts"
"! pip3 install google-cloud-aiplatform[ray]==1.56.0 ray[data,train,tune,serve] google-cloud-bigquery-storage pyarrow gcsfs setuptools==69.5.1 \"numpy<2\" -q --no-warn-conflicts\n",
"! pip3 install torch==2.1.2 torchvision==0.16.2 torchmetrics==1.2.1 torchserve==0.9.0 torch-model-archiver==0.9.0 -q --no-warn-conflicts\n",
"! pip3 install google-auth==2.27.0 etils==1.5.2 -q --no-warn-conflicts"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "58707a750154"
"id": "16220914acc5"
},
"source": [
"### Colab only: Uncomment the following cell to restart the kernel."
"### Restart runtime (Colab only)\n",
"\n",
"To use the newly installed packages, you must restart the runtime on Google Colab."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "f200f10a1da3"
"id": "157953ab28f0"
},
"outputs": [],
"source": [
"# import IPython\n",
"import sys\n",
"\n",
"# app = IPython.Application.instance()\n",
"# app.kernel.do_shutdown(True)"
"if \"google.colab\" in sys.modules:\n",
"\n",
" import IPython\n",
"\n",
" app = IPython.Application.instance()\n",
" app.kernel.do_shutdown(True)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "BF1j6f9HApxa"
"id": "c87a2a5d7e35"
},
"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)."
"<div class=\"alert alert-block alert-warning\">\n",
"<b>⚠️ The kernel is going to restart. Wait until it's finished before continuing to the next step. ⚠️</b>\n",
"</div>\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "WReHDGG5g0XY"
"id": "5dccb1c8feb6"
},
"source": [
"#### Set your project ID\n",
"### Authenticate your notebook environment (Colab only)\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)"
"Authenticate your environment on Google Colab.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "oM1iC_MfAts1"
"id": "cc7251520a07"
},
"outputs": [],
"source": [
"PROJECT_ID = \"[your-project-id]\" # @param {type:\"string\"}"
"import sys\n",
"\n",
"if \"google.colab\" in sys.modules:\n",
"\n",
" from google.colab import auth\n",
"\n",
" auth.authenticate_user()"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "region"
"id": "c2fc3d7b6bfa"
},
"source": [
"#### Region\n",
"### Set Google Cloud project information and initialize Vertex AI SDK for Python\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)."
"To get started using Vertex AI, you must have an existing Google Cloud project and [enable the Vertex AI API](https://console.cloud.google.com/flows/enableapi?apiid=aiplatform.googleapis.com). Learn more about [setting up a project and a development environment](https://cloud.google.com/vertex-ai/docs/start/cloud-environment)."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "fPrDj6HE9_EU"
"id": "f02130bff721"
},
"outputs": [],
"source": [
"REGION = \"us-central1\" # @param {type: \"string\"}"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "timestamp"
},
"source": [
"#### Timestamp\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 timestamp for each instance session, and append the timestamp onto the name of resources you create in this tutorial."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "W6Le1schAziq"
},
"outputs": [],
"source": [
"from datetime import datetime\n",
"\n",
"TIMESTAMP = datetime.now().strftime(\"%Y%m%d%H%M%S\")"
]
},
{
"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."
"PROJECT_ID = \"[your-project-id]\" # @param {type:\"string\"}\n",
"LOCATION = \"us-central1\" # @param {type:\"string\"}"
]
},
{
@@ -396,7 +299,53 @@
},
"outputs": [],
"source": [
"! gsutil mb -l {REGION} -p {PROJECT_ID} {BUCKET_URI}"
"! gsutil mb -l {LOCATION} -p {PROJECT_ID} {BUCKET_URI}"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "3d5191a94246"
},
"source": [
"### Initialize Vertex AI SDK for Python"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "de483dc2a7ee"
},
"outputs": [],
"source": [
"from google.cloud import aiplatform as vertex_ai\n",
"\n",
"vertex_ai.init(project=PROJECT_ID, location=LOCATION, staging_bucket=BUCKET_URI)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "454cb4f7a9e9"
},
"source": [
"#### Timestamp\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, create a timestamp for each instance session, and append the timestamp onto the name of resources you create in this tutorial."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "a8c17026c384"
},
"outputs": [],
"source": [
"from datetime import datetime\n",
"\n",
"TIMESTAMP = datetime.now().strftime(\"%Y%m%d%H%M%S\")"
]
},
{
@@ -450,32 +399,9 @@
"outputs": [],
"source": [
"import vertex_ray\n",
"from google.cloud import aiplatform as vertex_ai\n",
"from vertex_ray import Resources"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "dorIZFvjnGKL"
},
"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": "VOOgvRJoQ6Xj"
},
"outputs": [],
"source": [
"vertex_ai.init(project=PROJECT_ID, location=REGION, staging_bucket=BUCKET_URI)"
]
},
{
"cell_type": "markdown",
"metadata": {
@@ -574,7 +500,7 @@
"id": "Ek1-iTbPjzdJ"
},
"source": [
"### Set tutorial folder\n",
"### Set tutorial folder\n",
"\n",
"Set up the folder you use in this tutorial."
]
@@ -795,7 +721,7 @@
"# ray libraries\n",
"import ray\n",
"from ray import train\n",
"from ray.train import ScalingConfig, RunConfig, CheckpointConfig, Checkpoint\n",
"from ray.train import ScalingConfig, RunConfig, CheckpointConfig, Checkpoint, FailureConfig\n",
"from ray.train.torch import TorchTrainer, TorchCheckpoint\n",
"\n",
"\n",
@@ -919,6 +845,8 @@
" normalize = transforms.Normalize(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5])\n",
" transform = transforms.Compose([transforms.ToTensor(), normalize])\n",
"\n",
" # Setting url as a fix as mentioned in github issue https://github.com/pytorch/vision/issues/5039#issuecomment-1309696669\n",
" datasets.CIFAR10.url=\"http://www.cs.toronto.edu/~kriz/cifar-10-python.tar.gz\"\n",
" train_dataset = datasets.CIFAR10(root=\"./train\",\n",
" transform=transform,\n",
" train=True, download=True)\n",
@@ -966,7 +894,8 @@
" scaling_config = ScalingConfig(num_workers=config['num_workers'], use_gpu=config['use_gpu'])\n",
" run_config = RunConfig(checkpoint_config=CheckpointConfig(num_to_keep=1),\n",
" storage_path=config['logging_dir'],\n",
" name=config['experiment_name'])\n",
" name=config['experiment_name'],\n",
" failure_config=FailureConfig(max_failures=5))\n",
"\n",
" trainer = TorchTrainer(\n",
" train_loop_per_worker=train_loop_per_worker,\n",
@@ -993,7 +922,7 @@
"id": "-AOOmNti23qc"
},
"source": [
"### Prepare the `requirements` file\n",
"### Prepare the requirements file\n",
"\n",
"The file `requirements.txt` includes the dependencies your Ray application needs to run."
]
@@ -1015,6 +944,8 @@
"torch==2.1.2\n",
"torchvision==0.16.2\n",
"torchmetrics==1.2.1\n",
"setuptools==69.5.1\n",
"numpy<2\n",
"\"\"\"\n",
"\n",
"with open(tutorial_path / \"requirements.txt\", \"w\") as f:\n",
@@ -1182,7 +1113,7 @@
"id": "VavpxaY1hlSN"
},
"source": [
"### Get the best training checkpoint\n",
"### Get the best training checkpoint\n",
"\n",
"Use the `ExperimentAnalysis` to retrive the best checkpoint according to relevant metrics and mode."
]
@@ -1208,7 +1139,7 @@
"id": "Bf2YPC5nlzjL"
},
"source": [
"### Get PyTorch Model from Ray TorchCheckpoint\n",
"### Get PyTorch Model from Ray TorchCheckpoint\n",
"\n",
"Convert a TorchCheckpoint to Pytorch Model."
]
@@ -1223,7 +1154,7 @@
"source": [
"class Cifar10Model(nn.Module):\n",
" def __init__(self):\n",
" super(Cifar10Model, self).__init__()\n",
" super().__init__()\n",
" self.conv1 = nn.Conv2d(3, 16, 3)\n",
" self.pool = nn.MaxPool2d(2, 2)\n",
" self.conv2 = nn.Conv2d(16, 16, 5)\n",
@@ -1266,7 +1197,7 @@
"id": "kvfiYnrZ63iN"
},
"source": [
"#### Save the model\n",
"#### Save the model\n",
"\n",
"The `model.pt` contains the model state_dict.\n"
]
@@ -1288,7 +1219,7 @@
"id": "vl-xT_zDmOvg"
},
"source": [
"#### Create the `model` module\n",
"#### Create the model module\n",
"\n",
"The `model.py` file should contain the model architecture.\n"
]
@@ -1338,7 +1269,7 @@
"id": "h5Jc76AmBP-C"
},
"source": [
"#### Create the `custom_handler` module\n",
"#### Create the custom_handler module\n",
"\n",
"The `custom_handler.py` file uses the TorchServe's inbuilt `image_classifier` handler name to handle custom TorchServe inference logic."
]
@@ -1444,7 +1375,7 @@
"id": "0zljbquGFtHP"
},
"source": [
"#### Upload `mar` to bucket\n",
"#### Upload mar to bucket\n",
"\n",
"Store the .mar file to Cloud bucket."
]
@@ -1521,7 +1452,7 @@
"source": [
"## Make online predictions\n",
"\n",
"Sample an image from the `CIFAR10` dataset for getting online predictions."
"Sample an image from the CIFAR10 dataset for getting online predictions."
]
},
{
@@ -1532,6 +1463,9 @@
},
"outputs": [],
"source": [
"# Setting url as a fix as mentioned in github issue https://github.com/pytorch/vision/issues/5039#issuecomment-1309696669\n",
"datasets.CIFAR10.url = \"http://www.cs.toronto.edu/~kriz/cifar-10-python.tar.gz\"\n",
"\n",
"test_dataset = datasets.CIFAR10(\n",
" root=tests_path / \"data\",\n",
" transform=transforms.ToTensor(),\n",
@@ -1605,32 +1539,30 @@
},
"outputs": [],
"source": [
"import os\n",
"\n",
"delete_endpoint = False\n",
"delete_model = False\n",
"delete_ray_cluster = False\n",
"delete_bucket = False\n",
"delete_tutorial = False\n",
"delete_bucket = True\n",
"delete_tutorial = True\n",
"\n",
"# Delete endpoint resource\n",
"if delete_endpoint or os.getenv(\"IS_TESTING\"):\n",
"if delete_endpoint:\n",
" endpoint.delete(force=True)\n",
"\n",
"# Delete model resource\n",
"if delete_model or os.getenv(\"IS_TESTING\"):\n",
"if delete_model:\n",
" registered_model.delete()\n",
"\n",
"# Delete ray on vertex cluster\n",
"if delete_ray_cluster or os.getenv(\"IS_TESTING\"):\n",
"if delete_ray_cluster:\n",
" vertex_ray.delete_ray_cluster(ray_cluster.cluster_resource_name)\n",
"\n",
"# Delete tutorial folder\n",
"if delete_tutorial or os.getenv(\"IS_TESTING\"):\n",
"if delete_tutorial:\n",
" shutil.rmtree(tutorial_path)\n",
"\n",
"# Delete Cloud Storage objects that were created\n",
"if delete_bucket or os.getenv(\"IS_TESTING\"):\n",
"if delete_bucket:\n",
" ! gsutil -q -m rm -r $BUCKET_URI"
]
}