mirror of
https://github.com/GoogleCloudPlatform/vertex-ai-samples.git
synced 2026-09-26 22:51:56 +00:00
Compare commits
45
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7307cbc04c | ||
|
|
efd93073d0 | ||
|
|
cc2011e354 | ||
|
|
772881903b | ||
|
|
04f86e647d | ||
|
|
de120ccd62 | ||
|
|
0e5c1cebdf | ||
|
|
cb0caee3ea | ||
|
|
8eedc45652 | ||
|
|
255b520c01 | ||
|
|
5b079af33d | ||
|
|
bd89533c50 | ||
|
|
6555569156 | ||
|
|
a6aa61ae1d | ||
|
|
1580c63617 | ||
|
|
e05e777d1d | ||
|
|
91efb27aea | ||
|
|
b08b7a21f1 | ||
|
|
3b2adb2e1d | ||
|
|
472fe95430 | ||
|
|
cb8ec73e0a | ||
|
|
510e3d08ce | ||
|
|
9e3edfc8e4 | ||
|
|
72c3ad3e26 | ||
|
|
7626cd7025 | ||
|
|
c1f54a2de9 | ||
|
|
795f182de3 | ||
|
|
86670246d9 | ||
|
|
c5443d656c | ||
|
|
ba526e4ac4 | ||
|
|
e6360bb1e6 | ||
|
|
e61b249410 | ||
|
|
00f722def7 | ||
|
|
68269b07b0 | ||
|
|
e83ca6e8e9 | ||
|
|
8a679b5e41 | ||
|
|
d30089754b | ||
|
|
df7df07acf | ||
|
|
96fa6122cf | ||
|
|
7995377baf | ||
|
|
1b51866384 | ||
|
|
2edd90b3b5 | ||
|
|
44ffa09022 | ||
|
|
7ea4669471 | ||
|
|
d24204a2f8 |
@@ -3,7 +3,7 @@ ipython
|
||||
jupyter
|
||||
nbconvert
|
||||
black==24.4.2
|
||||
pyupgrade==3.16.0
|
||||
pyupgrade==3.17.0
|
||||
isort==5.13.2
|
||||
flake8==7.1.0
|
||||
nbqa==1.8.5
|
||||
|
||||
@@ -58,7 +58,7 @@ done
|
||||
# Only check notebooks in test folders modified in this pull request.
|
||||
# Note: Use process substitution to persist the data in the array
|
||||
if [ ${#notebooks[@]} -eq 0 ]; then
|
||||
echo "Checking for changed notebooked using git"
|
||||
echo "Checking for changed notebooks using git"
|
||||
while read -r file || [ -n "$line" ]; do
|
||||
notebooks+=("$file")
|
||||
done < <(git diff --name-only main... | grep '\.ipynb$')
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
google-cloud-pubsub==2.5.0
|
||||
pillow==10.3.0
|
||||
tf-agents==0.8.0
|
||||
tensorflow==2.7.2
|
||||
tensorflow==2.12.1
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
import numpy as np
|
||||
import os
|
||||
import pickle
|
||||
|
||||
from google.cloud.aiplatform.constants import prediction
|
||||
from google.cloud.aiplatform.utils import prediction_utils
|
||||
from google.cloud.aiplatform.prediction.predictor import Predictor
|
||||
from sklearn.datasets import make_blobs
|
||||
from sklearn.linear_model import LinearRegression
|
||||
|
||||
class LinearRegressionPredictor(Predictor):
|
||||
|
||||
def __init__(self):
|
||||
return
|
||||
|
||||
def load(self, artifacts_uri: str) -> None:
|
||||
prediction_utils.download_model_artifacts(artifacts_uri)
|
||||
if os.path.exists(prediction.MODEL_FILENAME_PKL):
|
||||
self._model = pickle.load(open(prediction.MODEL_FILENAME_PKL, "rb"))
|
||||
else:
|
||||
self._model = LogisticRegression()
|
||||
X, y = make_blobs(n_samples=100, centers=2, n_features=2, random_state=1)
|
||||
self._model.fit(X, y)
|
||||
|
||||
def preprocess(self, prediction_input: dict) -> np.ndarray:
|
||||
instances = prediction_input["instances"]
|
||||
return np.asarray(instances)
|
||||
|
||||
def predict(self, instances: np.ndarray) -> np.ndarray:
|
||||
return self._model.predict_proba(instances)
|
||||
|
||||
def postprocess(self, prediction_results: np.ndarray) -> dict:
|
||||
return {"predictions": prediction_results.tolist()}
|
||||
@@ -1,6 +1,6 @@
|
||||
--find-links https://download.pytorch.org/whl/torch_stable.html
|
||||
|
||||
torch==2.0.1+cu118
|
||||
torch==2.2.0
|
||||
numpy==1.26.1
|
||||
absl_py==2.0.0
|
||||
accelerate==0.24.0
|
||||
|
||||
@@ -231,22 +231,6 @@ def download_image(url: str) -> str:
|
||||
return Image.open(io.BytesIO(response.content))
|
||||
|
||||
|
||||
def resize_image(image: Any, new_width: int = 1000) -> Any:
|
||||
"""Resizes an image to a certain width.
|
||||
|
||||
Args:
|
||||
image: The image which has to be resized.
|
||||
new_width: New width of the image.
|
||||
|
||||
Returns:
|
||||
New resized image.
|
||||
"""
|
||||
width, height = image.size
|
||||
new_height = int(height * new_width / width)
|
||||
new_img = image.resize((new_width, new_height))
|
||||
return new_img
|
||||
|
||||
|
||||
def load_img(path: str) -> Any:
|
||||
"""Reads image from path and return PIL.Image instance.
|
||||
|
||||
@@ -389,6 +373,7 @@ def get_resource_id(accelerator_type: str, is_for_training: bool) -> str:
|
||||
"NVIDIA_L4": "custom_model_training_nvidia_l4_gpus",
|
||||
"NVIDIA_TESLA_A100": "custom_model_training_nvidia_a100_gpus",
|
||||
"NVIDIA_A100_80GB": "custom_model_training_nvidia_a100_80gb_gpus",
|
||||
"NVIDIA_H100_80GB": "custom_model_training_nvidia_h100_gpus",
|
||||
"NVIDIA_TESLA_T4": "custom_model_training_nvidia_t4_gpus",
|
||||
"TPU_V5e": "custom_model_training_tpu_v5e",
|
||||
"TPU_V3": "custom_model_training_tpu_v3",
|
||||
@@ -398,6 +383,7 @@ def get_resource_id(accelerator_type: str, is_for_training: bool) -> str:
|
||||
"NVIDIA_L4": "custom_model_serving_nvidia_l4_gpus",
|
||||
"NVIDIA_TESLA_A100": "custom_model_serving_nvidia_a100_gpus",
|
||||
"NVIDIA_A100_80GB": "custom_model_serving_nvidia_a100_80gb_gpus",
|
||||
"NVIDIA_H100_80GB": "custom_model_serving_nvidia_h100_gpus",
|
||||
"NVIDIA_TESLA_T4": "custom_model_serving_nvidia_t4_gpus",
|
||||
"TPU_V5e": "custom_model_serving_tpu_v5e",
|
||||
}
|
||||
|
||||
@@ -71,6 +71,7 @@
|
||||
/notebooks/community/model_garden/model_garden_pytorch_stable_diffusion_inpainting.ipynb @xiangxu-google
|
||||
/notebooks/community/model_garden/model_garden_pytorch_stable_diffusion_xl_1_0.ipynb @bingatgoogle
|
||||
/notebooks/community/model_garden/model_garden_pytorch_stable_diffusion_xl_lcm.ipynb @weigary
|
||||
/notebooks/community/model_garden/model_garden_pytorch_qwen2_deployment.ipynb @rayandasoriya
|
||||
/notebooks/community/model_garden/model_garden_pytorch_stable_diffusion_xl_lightning.ipynb @xcchen1
|
||||
/notebooks/community/model_garden/model_garden_pytorch_stable_diffusion_xl_lora.ipynb @weigary
|
||||
/notebooks/community/model_garden/model_garden_pytorch_stable_diffusion_xl_turbo.ipynb @weigary
|
||||
@@ -152,3 +153,4 @@
|
||||
/notebooks/community/model_garden/synthetic_data_generation_using_llama3_1.ipynb @xiangxu-google
|
||||
/notebooks/community/model_garden/model_garden_autosxs_evaluation_llama3_1.ipynb @inardini
|
||||
/notebooks/community/model_garden/model_garden_openai_api_llama3_1.ipynb @inardini
|
||||
/notebooks/community/model_garden/model_garden_phi3_deployment.ipynb @yashikagandhi
|
||||
|
||||
@@ -109,33 +109,49 @@
|
||||
"\n",
|
||||
"# @markdown 1. [Make sure that billing is enabled for your project](https://cloud.google.com/billing/docs/how-to/modify-project).\n",
|
||||
"\n",
|
||||
"# @markdown **[Optional]** Set the GCS BUCKET_URI to store the experiment artifacts, if you want to use your own bucket. **If not set, a unique GCS bucket will be created automatically on your behalf**.\n",
|
||||
"# @markdown 2. [Optional] [Create a Cloud Storage bucket](https://cloud.google.com/storage/docs/creating-buckets) for storing experiment outputs. Set the BUCKET_URI for the experiment environment. The specified Cloud Storage bucket (`BUCKET_URI`) should be located in the same region as where the notebook was launched. Note that a multi-region bucket (eg. \"us\") is not considered a match for a single region covered by the multi-region range (eg. \"us-central1\"). If not set, a unique GCS bucket will be created instead.\n",
|
||||
"\n",
|
||||
"import json\n",
|
||||
"# Import the necessary packages\n",
|
||||
"\n",
|
||||
"! git clone https://github.com/GoogleCloudPlatform/vertex-ai-samples.git\n",
|
||||
"\n",
|
||||
"import importlib\n",
|
||||
"import os\n",
|
||||
"import uuid\n",
|
||||
"from datetime import datetime\n",
|
||||
"from typing import Tuple\n",
|
||||
"\n",
|
||||
"from google.cloud import aiplatform\n",
|
||||
"\n",
|
||||
"common_util = importlib.import_module(\n",
|
||||
" \"vertex-ai-samples.community-content.vertex_model_garden.model_oss.notebook_util.common_util\"\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"models, endpoints = {}, {}\n",
|
||||
"\n",
|
||||
"# Get the default cloud project id.\n",
|
||||
"PROJECT_ID = os.environ[\"GOOGLE_CLOUD_PROJECT\"]\n",
|
||||
"\n",
|
||||
"# Get the default region for launching jobs.\n",
|
||||
"REGION = os.environ[\"GOOGLE_CLOUD_REGION\"]\n",
|
||||
"\n",
|
||||
"# Enable the Vertex AI API and Compute Engine API, if not already.\n",
|
||||
"print(\"Enabling Vertex AI API and Compute Engine API.\")\n",
|
||||
"! gcloud services enable aiplatform.googleapis.com compute.googleapis.com\n",
|
||||
"\n",
|
||||
"# Cloud Storage bucket for storing the experiment artifacts.\n",
|
||||
"# A unique GCS bucket will be created for the purpose of this notebook. If you\n",
|
||||
"# prefer using your own GCS bucket, please change the value yourself below.\n",
|
||||
"# prefer using your own GCS bucket, change the value yourself below.\n",
|
||||
"now = datetime.now().strftime(\"%Y%m%d%H%M%S\")\n",
|
||||
"BUCKET_URI = \"gs://\" # @param {type:\"string\"}\n",
|
||||
"BUCKET_NAME = \"/\".join(BUCKET_URI.split(\"/\")[:3])\n",
|
||||
"\n",
|
||||
"if BUCKET_URI is None or BUCKET_URI.strip() == \"\" or BUCKET_URI == \"gs://\":\n",
|
||||
" # Create a unique GCS bucket for this notebook if not specified\n",
|
||||
" BUCKET_URI = f\"gs://{PROJECT_ID}-tmp-{now}\"\n",
|
||||
" BUCKET_URI = f\"gs://{PROJECT_ID}-tmp-{now}-{str(uuid.uuid4())[:4]}\"\n",
|
||||
" BUCKET_NAME = \"/\".join(BUCKET_URI.split(\"/\")[:3])\n",
|
||||
" ! gsutil mb -l {REGION} {BUCKET_URI}\n",
|
||||
"else:\n",
|
||||
" assert BUCKET_URI.startswith(\"gs://\"), \"BUCKET_URI must start with `gs://`.\"\n",
|
||||
" BUCKET_NAME = \"/\".join(BUCKET_URI.split(\"/\")[:3])\n",
|
||||
" shell_output = ! gsutil ls -Lb {BUCKET_NAME} | grep \"Location constraint:\" | sed \"s/Location constraint://\"\n",
|
||||
" bucket_region = shell_output[0].strip().lower()\n",
|
||||
" if bucket_region != REGION:\n",
|
||||
@@ -147,6 +163,10 @@
|
||||
"\n",
|
||||
"STAGING_BUCKET = os.path.join(BUCKET_URI, \"temporal\")\n",
|
||||
"MODEL_BUCKET = os.path.join(BUCKET_URI, \"gemma2\")\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"# Initialize Vertex AI API.\n",
|
||||
"print(\"Initializing Vertex AI API.\")\n",
|
||||
"aiplatform.init(project=PROJECT_ID, location=REGION, staging_bucket=STAGING_BUCKET)\n",
|
||||
"\n",
|
||||
"# Gets the default SERVICE_ACCOUNT.\n",
|
||||
@@ -155,13 +175,11 @@
|
||||
"SERVICE_ACCOUNT = f\"{project_number}-compute@developer.gserviceaccount.com\"\n",
|
||||
"print(\"Using this default Service Account:\", SERVICE_ACCOUNT)\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"# Provision permissions to the SERVICE_ACCOUNT with the GCS bucket\n",
|
||||
"BUCKET_NAME = \"/\".join(BUCKET_URI.split(\"/\")[:3])\n",
|
||||
"! gsutil iam ch serviceAccount:{SERVICE_ACCOUNT}:roles/storage.admin $BUCKET_NAME\n",
|
||||
"\n",
|
||||
"# Enable Vertex AI and Cloud Compute APIs.\n",
|
||||
"! gcloud config set project $PROJECT_ID\n",
|
||||
"! gcloud services enable aiplatform.googleapis.com compute.googleapis.com\n",
|
||||
"\n",
|
||||
"# @markdown ## Access Gemma 2 Models\n",
|
||||
"\n",
|
||||
@@ -170,7 +188,7 @@
|
||||
"HF_TOKEN = \"\" # @param {type:\"string\", isTemplate:true}\n",
|
||||
"assert (\n",
|
||||
" HF_TOKEN\n",
|
||||
"), \"Please provide a read HF_TOKEN to load models from Hugging Face, or select a different model source.\"\n",
|
||||
"), \"Provide a read HF_TOKEN to load models from Hugging Face, or select a different model source.\"\n",
|
||||
"\n",
|
||||
"model_path_prefix = \"google/\"\n",
|
||||
"\n",
|
||||
@@ -178,20 +196,14 @@
|
||||
"HEXLLM_DOCKER_URI = \"us-docker.pkg.dev/vertex-ai-restricted/vertex-vision-model-garden-dockers/hex-llm-serve:gemma2\"\n",
|
||||
"TGI_DOCKER_URI = \"us-docker.pkg.dev/deeplearning-platform-release/gcr.io/huggingface-text-generation-inference-cu121.2-1.ubuntu2204.py310\"\n",
|
||||
"\n",
|
||||
"SERVICE_ENDPOINT = \"aiplatform.googleapis.com\"\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"def get_job_name_with_datetime(prefix: str) -> str:\n",
|
||||
" \"\"\"Gets the job name with date time when triggering deployment jobs.\"\"\"\n",
|
||||
" return prefix + datetime.now().strftime(\"_%Y%m%d_%H%M%S\")\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"def deploy_model_hexllm(\n",
|
||||
" model_name: str,\n",
|
||||
" model_id: str,\n",
|
||||
" service_account: str,\n",
|
||||
" machine_type: str = \"ct5lp-hightpu-1t\",\n",
|
||||
" base_model_id: str = None,\n",
|
||||
" tensor_parallel_size: int = 1,\n",
|
||||
" machine_type: str = \"ct5lp-hightpu-1t\",\n",
|
||||
" hbm_utilization_factor: float = 0.6,\n",
|
||||
" max_running_seqs: int = 256,\n",
|
||||
" endpoint_id: str = \"\",\n",
|
||||
@@ -205,28 +217,42 @@
|
||||
" )\n",
|
||||
" endpoint = aiplatform.Endpoint(aip_endpoint_name)\n",
|
||||
" else:\n",
|
||||
" endpoint = aiplatform.Endpoint.create(display_name=f\"{model_name}-endpoint\")\n",
|
||||
" endpoint = aiplatform.Endpoint.create(\n",
|
||||
" display_name=f\"{model_name}-endpoint\",\n",
|
||||
" location=TPU_DEPLOYMENT_REGION,\n",
|
||||
" )\n",
|
||||
"\n",
|
||||
" if not base_model_id:\n",
|
||||
" base_model_id = model_id\n",
|
||||
"\n",
|
||||
" if not tensor_parallel_size:\n",
|
||||
" tensor_parallel_size = int(machine_type[-2])\n",
|
||||
"\n",
|
||||
" hexllm_args = [\n",
|
||||
" \"--host=0.0.0.0\",\n",
|
||||
" \"--port=7080\",\n",
|
||||
" \"--log_level=INFO\",\n",
|
||||
" \"--enable_jit\",\n",
|
||||
" f\"--model={model_id}\",\n",
|
||||
" \"--load_format=auto\",\n",
|
||||
" f\"--tensor_parallel_size={tensor_parallel_size}\",\n",
|
||||
" \"--enable_jit\",\n",
|
||||
" \"--load_format=auto\",\n",
|
||||
" f\"--hbm_utilization_factor={hbm_utilization_factor}\",\n",
|
||||
" f\"--max_running_seqs={max_running_seqs}\",\n",
|
||||
" ]\n",
|
||||
" hexllm_envs = {\n",
|
||||
"\n",
|
||||
" env_vars = {\n",
|
||||
" \"MODEL_ID\": base_model_id,\n",
|
||||
" \"PJRT_DEVICE\": \"TPU\",\n",
|
||||
" \"RAY_DEDUP_LOGS\": \"0\",\n",
|
||||
" \"RAY_USAGE_STATS_ENABLED\": \"0\",\n",
|
||||
" \"MODEL_ID\": model_id,\n",
|
||||
" \"DEPLOY_SOURCE\": \"notebook\",\n",
|
||||
" }\n",
|
||||
" if HF_TOKEN:\n",
|
||||
" hexllm_envs.update({\"HF_TOKEN\": HF_TOKEN})\n",
|
||||
"\n",
|
||||
" try:\n",
|
||||
" if HF_TOKEN:\n",
|
||||
" env_vars.update({\"HF_TOKEN\": HF_TOKEN})\n",
|
||||
" except:\n",
|
||||
" pass\n",
|
||||
"\n",
|
||||
" model = aiplatform.Model.upload(\n",
|
||||
" display_name=model_name,\n",
|
||||
@@ -236,9 +262,10 @@
|
||||
" serving_container_ports=[7080],\n",
|
||||
" serving_container_predict_route=\"/generate\",\n",
|
||||
" serving_container_health_route=\"/ping\",\n",
|
||||
" serving_container_environment_variables=hexllm_envs,\n",
|
||||
" serving_container_environment_variables=env_vars,\n",
|
||||
" serving_container_shared_memory_size_mb=(16 * 1024), # 16 GB\n",
|
||||
" serving_container_deployment_timeout=7200,\n",
|
||||
" location=TPU_DEPLOYMENT_REGION,\n",
|
||||
" )\n",
|
||||
"\n",
|
||||
" model.deploy(\n",
|
||||
@@ -256,10 +283,10 @@
|
||||
" model_name: str,\n",
|
||||
" model_id: str,\n",
|
||||
" service_account: str,\n",
|
||||
" machine_type: str = \"g2-standard-24\",\n",
|
||||
" machine_type: str = \"g2-standard-8\",\n",
|
||||
" accelerator_type: str = \"NVIDIA_L4\",\n",
|
||||
" accelerator_count: int = 2,\n",
|
||||
" max_input_length: int = 1562,\n",
|
||||
" accelerator_count: int = 1,\n",
|
||||
" max_input_length: int = 512,\n",
|
||||
" max_total_tokens: int = 2048,\n",
|
||||
" max_batch_prefill_tokens: int = 2048,\n",
|
||||
") -> Tuple[aiplatform.Model, aiplatform.Endpoint]:\n",
|
||||
@@ -267,23 +294,21 @@
|
||||
" endpoint = aiplatform.Endpoint.create(display_name=f\"{model_name}-endpoint\")\n",
|
||||
"\n",
|
||||
" env_vars = {\n",
|
||||
" \"AIP_HTTP_PORT\": 7080,\n",
|
||||
" \"MODEL_ID\": model_id,\n",
|
||||
" \"NUM_SHARD\": f\"{accelerator_count}\",\n",
|
||||
" \"MAX_INPUT_LENGTH\": f\"{max_input_length}\",\n",
|
||||
" \"MAX_TOTAL_TOKENS\": f\"{max_total_tokens}\",\n",
|
||||
" \"MAX_BATCH_PREFILL_TOKENS\": f\"{max_batch_prefill_tokens}\",\n",
|
||||
" \"CUDA_MEMORY_FRACTION\": 0.93,\n",
|
||||
" \"DEPLOY_SOURCE\": \"notebook\",\n",
|
||||
" }\n",
|
||||
"\n",
|
||||
" if HF_TOKEN:\n",
|
||||
" env_vars[\"HF_TOKEN\"] = HF_TOKEN\n",
|
||||
" env_vars[\"HUGGING_FACE_HUB_TOKEN\"] = HF_TOKEN\n",
|
||||
"\n",
|
||||
" model = aiplatform.Model.upload(\n",
|
||||
" display_name=model_name,\n",
|
||||
" serving_container_image_uri=TGI_DOCKER_URI,\n",
|
||||
" serving_container_ports=[7080],\n",
|
||||
" serving_container_ports=[80],\n",
|
||||
" serving_container_environment_variables=env_vars,\n",
|
||||
" serving_container_shared_memory_size_mb=(16 * 1024), # 16 GB\n",
|
||||
" )\n",
|
||||
@@ -296,103 +321,7 @@
|
||||
" deploy_request_timeout=1800,\n",
|
||||
" service_account=service_account,\n",
|
||||
" )\n",
|
||||
" return model, endpoint\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"def get_quota(project_id: str, region: str, resource_id: str) -> int:\n",
|
||||
" \"\"\"Returns the quota for a resource in a region. Returns -1 if can not figure out the quota.\"\"\"\n",
|
||||
" quota_list_output = !gcloud alpha services quota list --service=$SERVICE_ENDPOINT --consumer=projects/$project_id --filter=\"$SERVICE_ENDPOINT/$resource_id\" --format=json\n",
|
||||
" # Use '.s' on the command output because it is an SList type.\n",
|
||||
" quota_data = json.loads(quota_list_output.s)\n",
|
||||
" if len(quota_data) == 0 or \"consumerQuotaLimits\" not in quota_data[0]:\n",
|
||||
" return -1\n",
|
||||
" if (\n",
|
||||
" len(quota_data[0][\"consumerQuotaLimits\"]) == 0\n",
|
||||
" or \"quotaBuckets\" not in quota_data[0][\"consumerQuotaLimits\"][0]\n",
|
||||
" ):\n",
|
||||
" return -1\n",
|
||||
" all_regions_data = quota_data[0][\"consumerQuotaLimits\"][0][\"quotaBuckets\"]\n",
|
||||
" for region_data in all_regions_data:\n",
|
||||
" if (\n",
|
||||
" region_data.get(\"dimensions\")\n",
|
||||
" and region_data[\"dimensions\"][\"region\"] == region\n",
|
||||
" ):\n",
|
||||
" if \"effectiveLimit\" in region_data:\n",
|
||||
" return int(region_data[\"effectiveLimit\"])\n",
|
||||
" else:\n",
|
||||
" return 0\n",
|
||||
" return -1\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"def get_resource_id(accelerator_type: str, is_for_training: bool) -> str:\n",
|
||||
" \"\"\"Returns the resource id for a given accelerator type and the use case.\n",
|
||||
" Args:\n",
|
||||
" accelerator_type: The accelerator type.\n",
|
||||
" is_for_training: Whether the resource is used for training. Set false\n",
|
||||
" for serving use case.\n",
|
||||
" Returns:\n",
|
||||
" The resource id.\n",
|
||||
" \"\"\"\n",
|
||||
" training_accelerator_map = {\n",
|
||||
" \"NVIDIA_TESLA_V100\": \"custom_model_training_nvidia_v100_gpus\",\n",
|
||||
" \"NVIDIA_L4\": \"custom_model_training_nvidia_l4_gpus\",\n",
|
||||
" \"NVIDIA_TESLA_A100\": \"custom_model_training_nvidia_a100_gpus\",\n",
|
||||
" \"NVIDIA_TESLA_T4\": \"custom_model_training_nvidia_t4_gpus\",\n",
|
||||
" \"TPU_V5e\": \"custom_model_training_tpu_v5e\",\n",
|
||||
" \"TPU_V3\": \"custom_model_training_tpu_v3\",\n",
|
||||
" }\n",
|
||||
" serving_accelerator_map = {\n",
|
||||
" \"NVIDIA_TESLA_V100\": \"custom_model_serving_nvidia_v100_gpus\",\n",
|
||||
" \"NVIDIA_L4\": \"custom_model_serving_nvidia_l4_gpus\",\n",
|
||||
" \"NVIDIA_TESLA_A100\": \"custom_model_serving_nvidia_a100_gpus\",\n",
|
||||
" \"NVIDIA_TESLA_T4\": \"custom_model_serving_nvidia_t4_gpus\",\n",
|
||||
" \"TPU_V5e\": \"custom_model_serving_tpu_v5e\",\n",
|
||||
" }\n",
|
||||
" if is_for_training:\n",
|
||||
" if accelerator_type in training_accelerator_map:\n",
|
||||
" return training_accelerator_map[accelerator_type]\n",
|
||||
" else:\n",
|
||||
" raise ValueError(\n",
|
||||
" f\"Could not find accelerator type: {accelerator_type} for training.\"\n",
|
||||
" )\n",
|
||||
" else:\n",
|
||||
" if accelerator_type in serving_accelerator_map:\n",
|
||||
" return serving_accelerator_map[accelerator_type]\n",
|
||||
" else:\n",
|
||||
" raise ValueError(\n",
|
||||
" f\"Could not find accelerator type: {accelerator_type} for serving.\"\n",
|
||||
" )\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"def check_quota(\n",
|
||||
" project_id: str,\n",
|
||||
" region: str,\n",
|
||||
" accelerator_type: str,\n",
|
||||
" accelerator_count: int,\n",
|
||||
" is_for_training: bool,\n",
|
||||
"):\n",
|
||||
" \"\"\"Checks if the project and the region has the required quota.\"\"\"\n",
|
||||
" resource_id = get_resource_id(accelerator_type, is_for_training)\n",
|
||||
" quota = get_quota(project_id, region, resource_id)\n",
|
||||
" quota_request_instruction = (\n",
|
||||
" \"Either use \"\n",
|
||||
" \"a different region or request additional quota. Follow \"\n",
|
||||
" \"instructions here \"\n",
|
||||
" \"https://cloud.google.com/docs/quotas/view-manage#requesting_higher_quota\"\n",
|
||||
" \" to check quota in a region or request additional quota for \"\n",
|
||||
" \"your project.\"\n",
|
||||
" )\n",
|
||||
" if quota == -1:\n",
|
||||
" raise ValueError(\n",
|
||||
" f\"\"\"Quota not found for: {resource_id} in {region}.\n",
|
||||
" {quota_request_instruction}\"\"\"\n",
|
||||
" )\n",
|
||||
" if quota < accelerator_count:\n",
|
||||
" raise ValueError(\n",
|
||||
" f\"\"\"Quota not enough for {resource_id} in {region}:\n",
|
||||
" {quota} < {accelerator_count}.\n",
|
||||
" {quota_request_instruction}\"\"\"\n",
|
||||
" )"
|
||||
" return model, endpoint"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -421,12 +350,19 @@
|
||||
"# @markdown Set the model ID. Model weights can be loaded from HuggingFace or from a GCS bucket.\n",
|
||||
"\n",
|
||||
"# @markdown Select one of the four model variations.\n",
|
||||
"MODEL_ID = \"gemma-2-9b\" # @param [\"gemma-2-9b\", \"gemma-2-9b-it\", \"gemma-2-27b\", \"gemma-2-27b-it\"] {allow-input: true, isTemplate: true}\n",
|
||||
"MODEL_ID = \"gemma-2-2b-it\" # @param [\"gemma-2-2b\", \"gemma-2-2b-it\", \"gemma-2-9b\", \"gemma-2-9b-it\", \"gemma-2-27b\", \"gemma-2-27b-it\"] {allow-input: true, isTemplate: true}\n",
|
||||
"TPU_DEPLOYMENT_REGION = \"us-west1\" # @param [\"us-west1\"] {isTemplate:true}\n",
|
||||
"model_id = os.path.join(model_path_prefix, MODEL_ID)\n",
|
||||
"\n",
|
||||
"# @markdown Find Vertex AI prediction TPUv5e machine types in\n",
|
||||
"# @markdown https://cloud.google.com/vertex-ai/docs/predictions/use-tpu#deploy_a_model.\n",
|
||||
"if \"9b\" in model_id:\n",
|
||||
"if \"2b\" in model_id:\n",
|
||||
" # Sets ct5lp-hightpu-1t (1 TPU chip) to deploy Gemma 2 2B models.\n",
|
||||
" machine_type = \"ct5lp-hightpu-1t\"\n",
|
||||
" accelerator_type = \"TPU_V5e\"\n",
|
||||
" # Note: 1 TPU V5 chip has only one core.\n",
|
||||
" accelerator_count = 1\n",
|
||||
"elif \"9b\" in model_id:\n",
|
||||
" # Sets ct5lp-hightpu-4t (4 TPU chips) to deploy Gemma 2 9B models.\n",
|
||||
" machine_type = \"ct5lp-hightpu-4t\"\n",
|
||||
" accelerator_type = \"TPU_V5e\"\n",
|
||||
@@ -439,9 +375,9 @@
|
||||
" # Note: 1 TPU V5 chip has only one core.\n",
|
||||
" accelerator_count = 8\n",
|
||||
"\n",
|
||||
"check_quota(\n",
|
||||
"common_util.check_quota(\n",
|
||||
" project_id=PROJECT_ID,\n",
|
||||
" region=REGION,\n",
|
||||
" region=TPU_DEPLOYMENT_REGION,\n",
|
||||
" accelerator_type=accelerator_type,\n",
|
||||
" accelerator_count=accelerator_count,\n",
|
||||
" is_for_training=False,\n",
|
||||
@@ -456,8 +392,8 @@
|
||||
"min_replica_count = 1\n",
|
||||
"max_replica_count = 1\n",
|
||||
"\n",
|
||||
"model_hexllm, endpoint_hexllm = deploy_model_hexllm(\n",
|
||||
" model_name=get_job_name_with_datetime(prefix=MODEL_ID),\n",
|
||||
"models[\"hexllm_tpu\"], endpoints[\"hexllm_tpu\"] = deploy_model_hexllm(\n",
|
||||
" model_name=common_util.get_job_name_with_datetime(prefix=MODEL_ID),\n",
|
||||
" model_id=model_id,\n",
|
||||
" service_account=SERVICE_ACCOUNT,\n",
|
||||
" machine_type=machine_type,\n",
|
||||
@@ -480,7 +416,9 @@
|
||||
"source": [
|
||||
"# @title Predict\n",
|
||||
"\n",
|
||||
"# @markdown Once deployment succeeds, you can send requests to the endpoint with text prompts. The first few requests may have high latency. This is because the server needs to warm up with the initial requests. The following requests should not have the same delay.\n",
|
||||
"# @markdown Once deployment succeeds, you can send requests to the endpoint with text prompts based on your `template`. Note that the first few prompts will take longer to execute.\n",
|
||||
"\n",
|
||||
"# @markdown Additionally, you can moderate the generated text with Vertex AI. See [Moderate text documentation](https://cloud.google.com/natural-language/docs/moderating-text) for more details.\n",
|
||||
"\n",
|
||||
"# @markdown Example:\n",
|
||||
"\n",
|
||||
@@ -492,8 +430,8 @@
|
||||
"# @markdown Additionally, you can moderate the generated text with Vertex AI. See [Moderate text documentation](https://cloud.google.com/natural-language/docs/moderating-text) for more details.\n",
|
||||
"\n",
|
||||
"# Loads an existing endpoint instance using the endpoint name:\n",
|
||||
"# - Using `endpoint_name = endpoint_hexllm.name` allows us to get the endpoint\n",
|
||||
"# name of the endpoint `endpoint_hexllm` created in the cell above.\n",
|
||||
"# - Using `endpoint_name = endpoint.name` allows us to get the endpoint\n",
|
||||
"# name of the endpoint `endpoint` created in the cell above.\n",
|
||||
"# - Alternatively, you can set `endpoint_name = \"1234567890123456789\"` to load\n",
|
||||
"# an existing endpoint with the ID 1234567890123456789.\n",
|
||||
"# You may uncomment the code below to load an existing endpoint:\n",
|
||||
@@ -502,7 +440,7 @@
|
||||
"# aip_endpoint_name = (\n",
|
||||
"# f\"projects/{PROJECT_ID}/locations/{REGION}/endpoints/{endpoint_name}\"\n",
|
||||
"# )\n",
|
||||
"# endpoint_hexllm = aiplatform.Endpoint(aip_endpoint_name)\n",
|
||||
"# endpoint = aiplatform.Endpoint(aip_endpoint_name)\n",
|
||||
"\n",
|
||||
"prompt = \"What is a car?\" # @param {type: \"string\"}\n",
|
||||
"max_tokens = 50 # @param {type: \"integer\"}\n",
|
||||
@@ -518,10 +456,10 @@
|
||||
" \"top_k\": top_k,\n",
|
||||
" },\n",
|
||||
"]\n",
|
||||
"response = endpoint_hexllm.predict(instances=instances)\n",
|
||||
"response = endpoints[\"hexllm_tpu\"].predict(instances=instances)\n",
|
||||
"\n",
|
||||
"prediction = response.predictions[0]\n",
|
||||
"print(prediction)"
|
||||
"for prediction in response.predictions:\n",
|
||||
" print(prediction)"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -536,7 +474,7 @@
|
||||
"\n",
|
||||
"Currently, only L4 GPUs are demonstrated in this notebook. Functionality on other GPU types will be confirmed and added in the future.\n",
|
||||
"\n",
|
||||
"Gemma2 9B models require at least 2 L4 GPUs for deployment. Gemma2 27B models require at least 4 L4 GPUs for deployment."
|
||||
"Gemma2 2B, 9B and 27B models require at least 1, 2, and 4 L4 GPUs respectively for deployment."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -549,7 +487,7 @@
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# @title Deploy\n",
|
||||
"MODEL_ID = \"gemma-2-9b\" # @param [\"gemma-2-9b\", \"gemma-2-9b-it\", \"gemma-2-27b\", \"gemma-2-27b-it\"] {allow-input: true, isTemplate: true}\n",
|
||||
"MODEL_ID = \"gemma-2-2b\" # @param [\"gemma-2-2b\", \"gemma-2-2b-it\", \"gemma-2-9b\", \"gemma-2-9b-it\", \"gemma-2-27b\", \"gemma-2-27b-it\"] {allow-input: true, isTemplate: true}\n",
|
||||
"model_id = os.path.join(model_path_prefix, MODEL_ID)\n",
|
||||
"\n",
|
||||
"# @markdown Finds Vertex AI prediction supported accelerators and regions in\n",
|
||||
@@ -557,9 +495,19 @@
|
||||
"\n",
|
||||
"accelerator_type = \"NVIDIA_L4\" # @param [\"NVIDIA_L4\"] {isTemplate: true}\n",
|
||||
"\n",
|
||||
"if \"9b\" in MODEL_ID:\n",
|
||||
"if \"2b\" in MODEL_ID:\n",
|
||||
" if accelerator_type == \"NVIDIA_L4\":\n",
|
||||
" # Sets 2 L4 (24G) to deploy Gemma 9B models.\n",
|
||||
" # Sets 1 L4 (24G) to deploy Gemma 2 2B models.\n",
|
||||
" machine_type = \"g2-standard-12\"\n",
|
||||
" accelerator_count = 1\n",
|
||||
" else:\n",
|
||||
" raise ValueError(\n",
|
||||
" \"Recommended machine settings not found for accelerator type: %s\"\n",
|
||||
" % accelerator_type\n",
|
||||
" )\n",
|
||||
"elif \"9b\" in MODEL_ID:\n",
|
||||
" if accelerator_type == \"NVIDIA_L4\":\n",
|
||||
" # Sets 2 L4 (24G) to deploy Gemma 2 9B models.\n",
|
||||
" machine_type = \"g2-standard-24\"\n",
|
||||
" accelerator_count = 2\n",
|
||||
" else:\n",
|
||||
@@ -569,7 +517,7 @@
|
||||
" )\n",
|
||||
"elif \"27b\" in MODEL_ID:\n",
|
||||
" if accelerator_type == \"NVIDIA_L4\":\n",
|
||||
" # Sets 4 L4 (24G) to deploy Gemma 27B models.\n",
|
||||
" # Sets 4 L4 (24G) to deploy Gemma 2 27B models.\n",
|
||||
" machine_type = \"g2-standard-48\"\n",
|
||||
" accelerator_count = 4\n",
|
||||
" else:\n",
|
||||
@@ -580,7 +528,7 @@
|
||||
"else:\n",
|
||||
" raise ValueError(\"Recommended machine settings not found for model: %s\" % MODEL_ID)\n",
|
||||
"\n",
|
||||
"check_quota(\n",
|
||||
"common_util.check_quota(\n",
|
||||
" project_id=PROJECT_ID,\n",
|
||||
" region=REGION,\n",
|
||||
" accelerator_type=accelerator_type,\n",
|
||||
@@ -590,13 +538,13 @@
|
||||
"\n",
|
||||
"# Note that larger token counts will require more GPU memory. For example, if you'd\n",
|
||||
"# like to increase the `max_total_tokens` and `max_batch_prefill_tokens` to 8192,\n",
|
||||
"# you may need 4 L4s for the 9b model, and 8 L4s for the 27b model.\n",
|
||||
"# you may need 1 L4 for 2b model, 4 L4s for the 9b model, and 8 L4s for the 27b model.\n",
|
||||
"max_input_length = 1562\n",
|
||||
"max_total_tokens = 2048\n",
|
||||
"max_batch_prefill_tokens = 2048\n",
|
||||
"\n",
|
||||
"model_tgi, endpoint_tgi = deploy_model_tgi(\n",
|
||||
" model_name=get_job_name_with_datetime(prefix=MODEL_ID),\n",
|
||||
"models[\"tgi\"], endpoints[\"tgi\"] = deploy_model_tgi(\n",
|
||||
" model_name=common_util.get_job_name_with_datetime(prefix=MODEL_ID),\n",
|
||||
" model_id=model_id,\n",
|
||||
" service_account=SERVICE_ACCOUNT,\n",
|
||||
" machine_type=machine_type,\n",
|
||||
@@ -618,23 +566,20 @@
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# @title Predict\n",
|
||||
"\n",
|
||||
"# @markdown Once deployment succeeds, you can send requests to the endpoint with text prompts.\n",
|
||||
"\n",
|
||||
"# @markdown Example:\n",
|
||||
"# @markdown Here we use an example from the [timdettmers/openassistant-guanaco](https://huggingface.co/datasets/timdettmers/openassistant-guanaco) to show the finetuning outcome:\n",
|
||||
"\n",
|
||||
"# @markdown ```\n",
|
||||
"# @markdown > What is a car?\n",
|
||||
"# @markdown > A car is a four-wheeled vehicle designed for the transportation of passengers and their belongings.\n",
|
||||
"# @markdown ### Human: How would the Future of AI in 10 Years look?### Assistant: Predicting the future is always a challenging task, but here are some possible ways that AI could evolve over the next 10 years: Continued advancements in deep learning: Deep learning has been one of the main drivers of recent AI breakthroughs, and we can expect continued advancements in this area. This may include improvements to existing algorithms, as well as the development of new architectures that are better suited to specific types of data and tasks. Increased use of AI in healthcare: AI has the potential to revolutionize healthcare, by improving the accuracy of diagnoses, developing new treatments, and personalizing patient care. We can expect to see continued investment in this area, with more healthcare providers and researchers using AI to improve patient outcomes. Greater automation in the workplace: Automation is already transforming many industries, and AI is likely to play an increasingly important role in this process. We can expect to see more jobs being automated, as well as the development of new types of jobs that require a combination of human and machine skills. More natural and intuitive interactions with technology: As AI becomes more advanced, we can expect to see more natural and intuitive ways of interacting with technology. This may include voice and gesture recognition, as well as more sophisticated chatbots and virtual assistants. Increased focus on ethical considerations: As AI becomes more powerful, there will be a growing need to consider its ethical implications. This may include issues such as bias in AI algorithms, the impact of automation on employment, and the use of AI in surveillance and policing. Overall, the future of AI in 10 years is likely to be shaped by a combination of technological advancements, societal changes, and ethical considerations. While there are many exciting possibilities for AI in the future, it will be important to carefully consider its potential impact on society and to work towards ensuring that its benefits are shared fairly and equitably.\n",
|
||||
"# @markdown ```\n",
|
||||
"\n",
|
||||
"# @markdown Additionally, you can moderate the generated text with Vertex AI. See [Moderate text documentation](https://cloud.google.com/natural-language/docs/moderating-text) for more details.\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"# @markdown Please click \"Show Code\" to see more details.\n",
|
||||
"# @markdown Click \"Show Code\" to see more details.\n",
|
||||
"\n",
|
||||
"# Loads an existing endpoint instance using the endpoint name:\n",
|
||||
"# - Using `endpoint_name = endpoint_tgi.name` allows us to get the\n",
|
||||
"# endpoint name of the endpoint `endpoint_tgi` created in the cell\n",
|
||||
"# - Using `endpoint_name = endpoint.name` allows us to get the\n",
|
||||
"# endpoint name of the endpoint `endpoint` created in the cell\n",
|
||||
"# above.\n",
|
||||
"# - Alternatively, you can set `endpoint_name = \"1234567890123456789\"` to load\n",
|
||||
"# an existing endpoint with the ID 1234567890123456789.\n",
|
||||
@@ -644,30 +589,29 @@
|
||||
"# aip_endpoint_name = (\n",
|
||||
"# f\"projects/{PROJECT_ID}/locations/{REGION}/endpoints/{endpoint_name}\"\n",
|
||||
"# )\n",
|
||||
"# endpoint_tgi = aiplatform.Endpoint(aip_endpoint_name)\n",
|
||||
"# endpoint = aiplatform.Endpoint(aip_endpoint_name)\n",
|
||||
"\n",
|
||||
"prompt = \"What is a car?\" # @param {type: \"string\"}\n",
|
||||
"max_new_tokens = 128 # @param {type:\"integer\"}\n",
|
||||
"prompt = \"How would the Future of AI in 10 Years look?\" # @param {type: \"string\"}\n",
|
||||
"max_tokens = 128 # @param {type:\"integer\"}\n",
|
||||
"temperature = 1.0 # @param {type:\"number\"}\n",
|
||||
"top_p = 0.9 # @param {type:\"number\"}\n",
|
||||
"top_k = 1 # @param {type:\"integer\"}\n",
|
||||
"\n",
|
||||
"# Overides max_new_tokens and top_k parameters during inferences.\n",
|
||||
"# Overrides max_tokens and top_k parameters during inferences.\n",
|
||||
"# If you encounter the issue like `ServiceUnavailable: 503 Took too long to respond when processing`,\n",
|
||||
"# you can reduce the max length, such as set max_new_tokens as 20.\n",
|
||||
"# you can reduce the max length, such as set max_tokens as 20.\n",
|
||||
"instances = [\n",
|
||||
" {\n",
|
||||
" \"inputs\": f\"### Human: {prompt}### Assistant: \",\n",
|
||||
" \"parameters\": {\n",
|
||||
" \"max_new_tokens\": max_new_tokens,\n",
|
||||
" \"max_new_tokens\": max_tokens,\n",
|
||||
" \"temperature\": temperature,\n",
|
||||
" \"top_p\": top_p,\n",
|
||||
" \"top_k\": top_k,\n",
|
||||
" },\n",
|
||||
" },\n",
|
||||
"]\n",
|
||||
"\n",
|
||||
"response = endpoint_tgi.predict(instances=instances)\n",
|
||||
"response = endpoints[\"tgi\"].predict(instances=instances)\n",
|
||||
"\n",
|
||||
"for prediction in response.predictions:\n",
|
||||
" print(prediction)"
|
||||
@@ -691,22 +635,20 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# @title Delete the models and endpoints\n",
|
||||
"\n",
|
||||
"# @markdown Delete the experiment models and endpoints to recycle the resources\n",
|
||||
"# @markdown and avoid unnecessary continouous charges that may incur.\n",
|
||||
"# Undeploy models and delete endpoints.\n",
|
||||
"endpoint_hexllm.delete(force=True)\n",
|
||||
"endpoint_tgi.delete(force=True)\n",
|
||||
"\n",
|
||||
"# Undeploy model and delete endpoint.\n",
|
||||
"for endpoint in endpoints.values():\n",
|
||||
" endpoint.delete(force=True)\n",
|
||||
"\n",
|
||||
"# Delete models.\n",
|
||||
"model_hexllm.delete()\n",
|
||||
"model_tgi.delete()\n",
|
||||
"for model in models.values():\n",
|
||||
" model.delete()\n",
|
||||
"\n",
|
||||
"# Delete Cloud Storage objects.\n",
|
||||
"delete_bucket = False # @param {type:\"boolean\", isTemplate: true}\n",
|
||||
"delete_bucket = False # @param {type:\"boolean\"}\n",
|
||||
"if delete_bucket:\n",
|
||||
" ! gsutil -m rm -r $BUCKET_URI"
|
||||
" ! gsutil -m rm -r $BUCKET_NAME"
|
||||
]
|
||||
}
|
||||
],
|
||||
|
||||
@@ -212,7 +212,7 @@
|
||||
"\n",
|
||||
" ! mkdir -p ./gemma\n",
|
||||
" ! curl -X GET \"{signed_url}\" | tar -xzvf - -C ./gemma/\n",
|
||||
" ! gsutil -m cp -R ./gemma/* {MODEL_BUCKET}\n",
|
||||
" ! gsutil -m rsync -R ./gemma/* {MODEL_BUCKET}\n",
|
||||
"\n",
|
||||
" base_model_path_prefix = MODEL_BUCKET\n",
|
||||
" HF_TOKEN = \"\"\n",
|
||||
|
||||
@@ -0,0 +1,486 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "20qcPG1PmFUM"
|
||||
},
|
||||
"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": "QXYOa1odnikj"
|
||||
},
|
||||
"source": [
|
||||
"# Vertex AI Model Garden - Phi-3 (Deployment)\n",
|
||||
"\n",
|
||||
"<table><tbody><tr>\n",
|
||||
" <td style=\"text-align: center\">\n",
|
||||
" <a href=\"https://console.cloud.google.com/vertex-ai/colab/import/https:%2F%2Fraw.githubusercontent.com%2FGoogleCloudPlatform%2Fvertex-ai-samples%2Fmain%2Fnotebooks%2Fcommunity%2Fmodel_garden%2Fmodel_garden_phi3_deployment.ipynb\">\n",
|
||||
" <img alt=\"Google Cloud Colab Enterprise logo\" src=\"https://lh3.googleusercontent.com/JmcxdQi-qOpctIvWKgPtrzZdJJK-J3sWE1RsfjZNwshCFgE_9fULcNpuXYTilIR2hjwN\" width=\"32px\"><br> Run in Colab Enterprise\n",
|
||||
" </a>\n",
|
||||
" </td>\n",
|
||||
" <td style=\"text-align: center\">\n",
|
||||
" <a href=\"https://github.com/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/community/model_garden/model_garden_phi3_deployment.ipynb\">\n",
|
||||
" <img alt=\"GitHub logo\" src=\"https://cloud.google.com/ml-engine/images/github-logo-32px.png\" width=\"32px\"><br> View on GitHub\n",
|
||||
" </a>\n",
|
||||
" </td>\n",
|
||||
"</tr></tbody></table>"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "cbDI9ag4oR4C"
|
||||
},
|
||||
"source": [
|
||||
"## Overview\n",
|
||||
"\n",
|
||||
"This notebook demonstrates deploying prebuilt [Phi-3 models](https://huggingface.co/collections/microsoft/phi-3-6626e15e9585a200d2d761e3) with [vLLM](https://github.com/vllm-project/vllm) to improve serving throughput.\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"### Objective\n",
|
||||
"\n",
|
||||
"- Download and deploy prebuilt Phi-3 models\n",
|
||||
"- Deploy Phi-3 with [vLLM](https://github.com/vllm-project/vllm) to improve serving throughput\n",
|
||||
"\n",
|
||||
"### 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), [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": "hQJWRopioSKT"
|
||||
},
|
||||
"source": [
|
||||
"## Before you begin"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"cellView": "form",
|
||||
"id": "J_jmxcIZoSxU"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# @title Setup Google Cloud project\n",
|
||||
"\n",
|
||||
"# @markdown 1. [Make sure that billing is enabled for your project](https://cloud.google.com/billing/docs/how-to/modify-project).\n",
|
||||
"\n",
|
||||
"# @markdown 2. [Optional] [Create a Cloud Storage bucket](https://cloud.google.com/storage/docs/creating-buckets) for storing experiment outputs. Set the BUCKET_URI for the experiment environment. The specified Cloud Storage bucket (`BUCKET_URI`) should be located in the same region as where the notebook was launched. Note that a multi-region bucket (eg. \"us\") is not considered a match for a single region covered by the multi-region range (eg. \"us-central1\"). If not set, a unique GCS bucket will be created instead.\n",
|
||||
"\n",
|
||||
"# Import the necessary packages\n",
|
||||
"import importlib\n",
|
||||
"import os\n",
|
||||
"import uuid\n",
|
||||
"from datetime import datetime\n",
|
||||
"from typing import Tuple\n",
|
||||
"\n",
|
||||
"from google.cloud import aiplatform\n",
|
||||
"\n",
|
||||
"! git clone https://github.com/GoogleCloudPlatform/vertex-ai-samples.git\n",
|
||||
"\n",
|
||||
"models, endpoints = {}, {}\n",
|
||||
"\n",
|
||||
"common_util = importlib.import_module(\n",
|
||||
" \"vertex-ai-samples.community-content.vertex_model_garden.model_oss.notebook_util.common_util\"\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"# Get the default cloud project id.\n",
|
||||
"PROJECT_ID = os.environ[\"GOOGLE_CLOUD_PROJECT\"]\n",
|
||||
"\n",
|
||||
"# Get the default region for launching jobs.\n",
|
||||
"REGION = os.environ[\"GOOGLE_CLOUD_REGION\"]\n",
|
||||
"\n",
|
||||
"# Enable the Vertex AI API and Compute Engine API, if not already.\n",
|
||||
"print(\"Enabling Vertex AI API and Compute Engine API.\")\n",
|
||||
"! gcloud services enable aiplatform.googleapis.com compute.googleapis.com\n",
|
||||
"\n",
|
||||
"# Cloud Storage bucket for storing the experiment artifacts.\n",
|
||||
"# A unique GCS bucket will be created for the purpose of this notebook. If you\n",
|
||||
"# prefer using your own GCS bucket, change the value yourself below.\n",
|
||||
"now = datetime.now().strftime(\"%Y%m%d%H%M%S\")\n",
|
||||
"BUCKET_URI = \"gs://\" # @param {type:\"string\"}\n",
|
||||
"BUCKET_NAME = \"/\".join(BUCKET_URI.split(\"/\")[:3])\n",
|
||||
"\n",
|
||||
"if BUCKET_URI is None or BUCKET_URI.strip() == \"\" or BUCKET_URI == \"gs://\":\n",
|
||||
" BUCKET_URI = f\"gs://{PROJECT_ID}-tmp-{now}-{str(uuid.uuid4())[:4]}\"\n",
|
||||
" BUCKET_NAME = \"/\".join(BUCKET_URI.split(\"/\")[:3])\n",
|
||||
" ! gsutil mb -l {REGION} {BUCKET_URI}\n",
|
||||
"else:\n",
|
||||
" assert BUCKET_URI.startswith(\"gs://\"), \"BUCKET_URI must start with `gs://`.\"\n",
|
||||
" shell_output = ! gsutil ls -Lb {BUCKET_NAME} | grep \"Location constraint:\" | sed \"s/Location constraint://\"\n",
|
||||
" bucket_region = shell_output[0].strip().lower()\n",
|
||||
" if bucket_region != REGION:\n",
|
||||
" raise ValueError(\n",
|
||||
" \"Bucket region %s is different from notebook region %s\"\n",
|
||||
" % (bucket_region, REGION)\n",
|
||||
" )\n",
|
||||
"print(f\"Using this GCS Bucket: {BUCKET_URI}\")\n",
|
||||
"\n",
|
||||
"STAGING_BUCKET = os.path.join(BUCKET_URI, \"temporal\")\n",
|
||||
"MODEL_BUCKET = os.path.join(BUCKET_URI, \"phi3\")\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"# Initialize Vertex AI API.\n",
|
||||
"print(\"Initializing Vertex AI API.\")\n",
|
||||
"aiplatform.init(project=PROJECT_ID, location=REGION, staging_bucket=STAGING_BUCKET)\n",
|
||||
"\n",
|
||||
"# Gets the default SERVICE_ACCOUNT.\n",
|
||||
"shell_output = ! gcloud projects describe $PROJECT_ID\n",
|
||||
"project_number = shell_output[-1].split(\":\")[1].strip().replace(\"'\", \"\")\n",
|
||||
"SERVICE_ACCOUNT = f\"{project_number}-compute@developer.gserviceaccount.com\"\n",
|
||||
"print(\"Using this default Service Account:\", SERVICE_ACCOUNT)\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"# Provision permissions to the SERVICE_ACCOUNT with the GCS bucket\n",
|
||||
"! gsutil iam ch serviceAccount:{SERVICE_ACCOUNT}:roles/storage.admin $BUCKET_NAME\n",
|
||||
"\n",
|
||||
"! gcloud config set project $PROJECT_ID\n",
|
||||
"\n",
|
||||
"# The pre-built serving docker images.\n",
|
||||
"VLLM_DOCKER_URI = \"us-docker.pkg.dev/vertex-ai/vertex-vision-model-garden-dockers/pytorch-vllm-serve:20240620_1616_RC00\"\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"def deploy_model_vllm(\n",
|
||||
" model_name: str,\n",
|
||||
" model_id: str,\n",
|
||||
" service_account: str,\n",
|
||||
" base_model_id: str = None,\n",
|
||||
" machine_type: str = \"g2-standard-8\",\n",
|
||||
" accelerator_type: str = \"NVIDIA_L4\",\n",
|
||||
" accelerator_count: int = 1,\n",
|
||||
" gpu_memory_utilization: float = 0.9,\n",
|
||||
" max_model_len: int = 4096,\n",
|
||||
" dtype: str = \"auto\",\n",
|
||||
") -> Tuple[aiplatform.Model, aiplatform.Endpoint]:\n",
|
||||
" \"\"\"Deploys trained models with vLLM into Vertex AI.\"\"\"\n",
|
||||
" endpoint = aiplatform.Endpoint.create(display_name=f\"{model_name}-endpoint\")\n",
|
||||
"\n",
|
||||
" if not base_model_id:\n",
|
||||
" base_model_id = model_id\n",
|
||||
"\n",
|
||||
" vllm_args = [\n",
|
||||
" \"--host=0.0.0.0\",\n",
|
||||
" \"--port=7080\",\n",
|
||||
" f\"--model={model_id}\",\n",
|
||||
" f\"--tensor-parallel-size={accelerator_count}\",\n",
|
||||
" \"--swap-space=16\",\n",
|
||||
" f\"--gpu-memory-utilization={gpu_memory_utilization}\",\n",
|
||||
" f\"--max-model-len={max_model_len}\",\n",
|
||||
" f\"--dtype={dtype}\",\n",
|
||||
" \"--disable-log-stats\",\n",
|
||||
" \"--trust-remote-code\",\n",
|
||||
" ]\n",
|
||||
"\n",
|
||||
" env_vars = {\n",
|
||||
" \"MODEL_ID\": base_model_id,\n",
|
||||
" \"DEPLOY_SOURCE\": \"notebook\",\n",
|
||||
" }\n",
|
||||
"\n",
|
||||
" try:\n",
|
||||
" if HF_TOKEN:\n",
|
||||
" env_vars[\"HF_TOKEN\"] = HF_TOKEN\n",
|
||||
" except:\n",
|
||||
" pass\n",
|
||||
"\n",
|
||||
" model = aiplatform.Model.upload(\n",
|
||||
" display_name=model_name,\n",
|
||||
" serving_container_image_uri=VLLM_DOCKER_URI,\n",
|
||||
" serving_container_command=[\"python\", \"-m\", \"vllm.entrypoints.api_server\"],\n",
|
||||
" serving_container_args=vllm_args,\n",
|
||||
" serving_container_ports=[7080],\n",
|
||||
" serving_container_predict_route=\"/generate\",\n",
|
||||
" serving_container_health_route=\"/ping\",\n",
|
||||
" serving_container_environment_variables=env_vars,\n",
|
||||
" serving_container_shared_memory_size_mb=(16 * 1024), # 16 GB\n",
|
||||
" serving_container_deployment_timeout=7200,\n",
|
||||
" )\n",
|
||||
" print(\n",
|
||||
" f\"Deploying {model_name} on {machine_type} with {accelerator_count} {accelerator_type} GPU(s).\"\n",
|
||||
" )\n",
|
||||
" model.deploy(\n",
|
||||
" endpoint=endpoint,\n",
|
||||
" machine_type=machine_type,\n",
|
||||
" accelerator_type=accelerator_type,\n",
|
||||
" accelerator_count=accelerator_count,\n",
|
||||
" deploy_request_timeout=1800,\n",
|
||||
" service_account=service_account,\n",
|
||||
" )\n",
|
||||
" print(\"endpoint_name:\", endpoint.name)\n",
|
||||
"\n",
|
||||
" return model, endpoint"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "z9UuiysLu_gB"
|
||||
},
|
||||
"source": [
|
||||
"## Deploy prebuilt Phi-3 models on vLLM"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"cellView": "form",
|
||||
"id": "USB7dvYqvNdu"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# @title Deploy\n",
|
||||
"\n",
|
||||
"# @markdown This section uploads prebuilt Phi3 models to Model Registry and deploys it to a Vertex AI Endpoint.\n",
|
||||
"\n",
|
||||
"# @markdown Increasing the max model length of the model configurations will require more memory and GPU resources. The table below shows the default configurations to deploy each Phi-3 variant.\n",
|
||||
"\n",
|
||||
"# @markdown The 'Phi-3-medium-128k-instruct' variant has been configured with a max model length of 20000.\n",
|
||||
"\n",
|
||||
"# @markdown The Phi-3 model variants may take 15-30 minutes to deploy.\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"# @markdown | Model Version | Default Max Model Length | Default GPU configuration |\n",
|
||||
"# @markdown |----------------------------|------------------|-----------------------------|\n",
|
||||
"# @markdown | Phi-3-mini-4k-instruct | 4096 | 1 NVIDIA_L4 g2-standard-12 |\n",
|
||||
"# @markdown | Phi-3-small-8k-instruct | 8192 | 1 NVIDIA_L4 g2-standard-12 |\n",
|
||||
"# @markdown | Phi-3-small-128k-instruct | 131072 | 4 NVIDIA_L4 g2-standard-48 |\n",
|
||||
"# @markdown | Phi-3-medium-4k-instruct | 4096 | 2 NVIDIA_L4 g2-standard-24 |\n",
|
||||
"# @markdown | Phi-3-medium-128k-instruct | 20000 | 2 NVIDIA_L4 g2-standard-24 |\n",
|
||||
"\n",
|
||||
"MODEL_ID = \"Phi-3-mini-4k-instruct\" # @param [\"Phi-3-mini-4k-instruct\", \"Phi-3-small-8k-instruct\", \"Phi-3-small-128k-instruct\", \"Phi-3-medium-4k-instruct\", \"Phi-3-medium-128k-instruct\"] {isTemplate: true}\n",
|
||||
"model_path_prefix = \"microsoft\"\n",
|
||||
"model_id = os.path.join(model_path_prefix, MODEL_ID)\n",
|
||||
"\n",
|
||||
"accelerator_type = \"NVIDIA_L4\" # @param [\"NVIDIA_L4\"] {isTemplate: true}\n",
|
||||
"machine_type = \"g2-standard-12\"\n",
|
||||
"vllm_dtype = \"bfloat16\"\n",
|
||||
"accelerator_count = 1\n",
|
||||
"max_model_len = None\n",
|
||||
"gpu_memory_utilization = 0.85\n",
|
||||
"\n",
|
||||
"if \"mini\" in MODEL_ID:\n",
|
||||
" if \"4k\" in MODEL_ID:\n",
|
||||
" max_model_len = 4096\n",
|
||||
" else:\n",
|
||||
" raise ValueError(\"Invalid model id: %s\" % MODEL_ID)\n",
|
||||
"\n",
|
||||
" if accelerator_type == \"NVIDIA_L4\":\n",
|
||||
" accelerator_count = 1\n",
|
||||
" machine_type = \"g2-standard-12\"\n",
|
||||
" else:\n",
|
||||
" raise ValueError(\n",
|
||||
" \"Recommended machine settings not found for accelerator type: %s\"\n",
|
||||
" % accelerator_type\n",
|
||||
" )\n",
|
||||
"elif \"small\" in MODEL_ID:\n",
|
||||
" if \"128k\" in MODEL_ID:\n",
|
||||
" max_model_len = 131072\n",
|
||||
" if accelerator_type == \"NVIDIA_L4\":\n",
|
||||
" accelerator_count = 4\n",
|
||||
" machine_type = \"g2-standard-48\"\n",
|
||||
" else:\n",
|
||||
" raise ValueError(\n",
|
||||
" \"Recommended machine settings not found for accelerator type: %s\"\n",
|
||||
" % accelerator_type\n",
|
||||
" )\n",
|
||||
" elif \"8k\" in MODEL_ID:\n",
|
||||
" max_model_len = 8192\n",
|
||||
" if accelerator_type == \"NVIDIA_L4\":\n",
|
||||
" accelerator_count = 1\n",
|
||||
" machine_type = \"g2-standard-12\"\n",
|
||||
" else:\n",
|
||||
" raise ValueError(\n",
|
||||
" \"Recommended machine settings not found for accelerator type: %s\"\n",
|
||||
" % accelerator_type\n",
|
||||
" )\n",
|
||||
" else:\n",
|
||||
" raise ValueError(\"Invalid model id: %s\" % MODEL_ID)\n",
|
||||
"elif \"medium\" in MODEL_ID:\n",
|
||||
" if \"4k\" in MODEL_ID:\n",
|
||||
" max_model_len = 4096\n",
|
||||
" if accelerator_type == \"NVIDIA_L4\":\n",
|
||||
" accelerator_count = 2\n",
|
||||
" machine_type = \"g2-standard-24\"\n",
|
||||
" else:\n",
|
||||
" raise ValueError(\n",
|
||||
" \"Recommended machine settings not found for accelerator type: %s\"\n",
|
||||
" % accelerator_type\n",
|
||||
" )\n",
|
||||
" elif \"128k\" in MODEL_ID:\n",
|
||||
" max_model_len = 20000\n",
|
||||
" if accelerator_type == \"NVIDIA_L4\":\n",
|
||||
" accelerator_count = 2\n",
|
||||
" machine_type = \"g2-standard-24\"\n",
|
||||
" else:\n",
|
||||
" raise ValueError(\n",
|
||||
" \"Recommended machine settings not found for accelerator type: %s\"\n",
|
||||
" % accelerator_type\n",
|
||||
" )\n",
|
||||
" else:\n",
|
||||
" raise ValueError(\"Invalid model id: %s\" % MODEL_ID)\n",
|
||||
"else:\n",
|
||||
" raise ValueError(\"Invalid model id: %s\" % MODEL_ID)\n",
|
||||
"\n",
|
||||
"common_util.check_quota(\n",
|
||||
" project_id=PROJECT_ID,\n",
|
||||
" region=REGION,\n",
|
||||
" accelerator_type=accelerator_type,\n",
|
||||
" accelerator_count=accelerator_count,\n",
|
||||
" is_for_training=False,\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"models[\"vllm_gpu\"], endpoints[\"vllm_gpu\"] = deploy_model_vllm(\n",
|
||||
" model_name=common_util.get_job_name_with_datetime(prefix=MODEL_ID),\n",
|
||||
" model_id=model_id,\n",
|
||||
" service_account=SERVICE_ACCOUNT,\n",
|
||||
" machine_type=machine_type,\n",
|
||||
" accelerator_type=accelerator_type,\n",
|
||||
" accelerator_count=accelerator_count,\n",
|
||||
" max_model_len=max_model_len,\n",
|
||||
" gpu_memory_utilization=gpu_memory_utilization,\n",
|
||||
" dtype=vllm_dtype,\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"# @markdown Click \"Show Code\" to see more details."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"cellView": "form",
|
||||
"id": "Aa4e1-6FvRAP"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# @title Predict\n",
|
||||
"\n",
|
||||
"# @markdown Once deployment succeeds, you can send requests to the endpoint with text prompts. Sampling parameters supported by vLLM can be found [here](https://docs.vllm.ai/en/latest/dev/sampling_params.html).\n",
|
||||
"\n",
|
||||
"# @markdown Example:\n",
|
||||
"\n",
|
||||
"# @markdown ```\n",
|
||||
"# @markdown Human: What is a car?\n",
|
||||
"# @markdown Assistant: A car, or a motor car, is a road-connected human-transportation system used to move people or goods from one place to another. The term also encompasses a wide range of vehicles, including motorboats, trains, and aircrafts. Cars typically have four wheels, a cabin for passengers, and an engine or motor. They have been around since the early 19th century and are now one of the most popular forms of transportation, used for daily commuting, shopping, and other purposes.\n",
|
||||
"# @markdown ```\n",
|
||||
"# @markdown Additionally, you can moderate the generated text with Vertex AI. See [Moderate text documentation](https://cloud.google.com/natural-language/docs/moderating-text) for more details.\n",
|
||||
"\n",
|
||||
"# Loads an existing endpoint instance using the endpoint name:\n",
|
||||
"# - Using `endpoint_name = endpoint.name` allows us to get the\n",
|
||||
"# endpoint name of the endpoint `endpoint` created in the cell\n",
|
||||
"# above.\n",
|
||||
"# - Alternatively, you can set `endpoint_name = \"1234567890123456789\"` to load\n",
|
||||
"# an existing endpoint with the ID 1234567890123456789.\n",
|
||||
"# You may uncomment the code below to load an existing endpoint.\n",
|
||||
"\n",
|
||||
"# endpoint_name = \"\" # @param {type:\"string\"}\n",
|
||||
"# aip_endpoint_name = (\n",
|
||||
"# f\"projects/{PROJECT_ID}/locations/{REGION}/endpoints/{endpoint_name}\"\n",
|
||||
"# )\n",
|
||||
"# endpoint = aiplatform.Endpoint(aip_endpoint_name)\n",
|
||||
"\n",
|
||||
"prompt = \"What is a car?\" # @param {type: \"string\"}\n",
|
||||
"max_tokens = 50 # @param {type:\"integer\"}\n",
|
||||
"temperature = 1.0 # @param {type:\"number\"}\n",
|
||||
"top_p = 1.0 # @param {type:\"number\"}\n",
|
||||
"top_k = 1 # @param {type:\"integer\"}\n",
|
||||
"raw_response = False # @param {type:\"boolean\"}\n",
|
||||
"\n",
|
||||
"# Overrides parameters for inferences.\n",
|
||||
"# If you encounter the issue like `ServiceUnavailable: 503 Took too long to respond when processing`,\n",
|
||||
"# you can reduce the maximum number of output tokens, such as set max_tokens as 20.\n",
|
||||
"instances = [\n",
|
||||
" {\n",
|
||||
" \"prompt\": prompt,\n",
|
||||
" \"max_tokens\": max_tokens,\n",
|
||||
" \"temperature\": temperature,\n",
|
||||
" \"top_p\": top_p,\n",
|
||||
" \"top_k\": top_k,\n",
|
||||
" \"raw_response\": raw_response,\n",
|
||||
" },\n",
|
||||
"]\n",
|
||||
"response = endpoints[\"vllm_gpu\"].predict(instances=instances)\n",
|
||||
"\n",
|
||||
"for prediction in response.predictions:\n",
|
||||
" print(prediction)\n",
|
||||
"\n",
|
||||
"# @markdown Click \"Show Code\" to see more details."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "tAelDidov5AW"
|
||||
},
|
||||
"source": [
|
||||
"## Clean up resources"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"cellView": "form",
|
||||
"id": "8SeZCFo5v7z-"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# @title Delete the models and endpoints\n",
|
||||
"# @markdown Delete the experiment models and endpoints to recycle the resources\n",
|
||||
"# @markdown and avoid unnecessary continouous charges that may incur.\n",
|
||||
"\n",
|
||||
"# Undeploy model and delete endpoint.\n",
|
||||
"for endpoint in endpoints.values():\n",
|
||||
" endpoint.delete(force=True)\n",
|
||||
"\n",
|
||||
"# Delete models.\n",
|
||||
"for model in models.values():\n",
|
||||
" model.delete()\n",
|
||||
"\n",
|
||||
"delete_bucket = False # @param {type:\"boolean\"}\n",
|
||||
"if delete_bucket:\n",
|
||||
" ! gsutil -m rm -r $BUCKET_NAME"
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"colab": {
|
||||
"name": "model_garden_phi3_deployment.ipynb",
|
||||
"toc_visible": true
|
||||
},
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3",
|
||||
"name": "python3"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 0
|
||||
}
|
||||
@@ -512,7 +512,7 @@
|
||||
"\n",
|
||||
"if \"70\" in base_model_name:\n",
|
||||
" accelerator_type = \"NVIDIA_L4\"\n",
|
||||
" machine_type = \"g2-standard-8\"\n",
|
||||
" machine_type = \"g2-standard-96\"\n",
|
||||
" accelerator_count = 8\n",
|
||||
"elif \"405\" in base_model_name:\n",
|
||||
" accelerator_type = \"NVIDIA_H100_80GB\"\n",
|
||||
|
||||
@@ -57,7 +57,7 @@
|
||||
"\n",
|
||||
"### Objective\n",
|
||||
"\n",
|
||||
"* Finetune and merge Mistral model with PEFT training docker image.\n",
|
||||
"* Finetune and merge Mistral model using PEFT training docker image.\n",
|
||||
"* Deploy the finetuned model with vLLM docker image on a Vertex AI Endpoint.\n",
|
||||
"* Run inference on the deployed Vertex AI Endpoint.\n",
|
||||
"\n",
|
||||
@@ -68,7 +68,7 @@
|
||||
"* 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."
|
||||
"Learn about [Vertex AI pricing](https://cloud.google.com/vertex-ai/pricing), [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."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -95,32 +95,48 @@
|
||||
"\n",
|
||||
"# @markdown 2. [Optional] [Create a Cloud Storage bucket](https://cloud.google.com/storage/docs/creating-buckets) for storing experiment outputs. Set the BUCKET_URI for the experiment environment. The specified Cloud Storage bucket (`BUCKET_URI`) should be located in the same region as where the notebook was launched. Note that a multi-region bucket (eg. \"us\") is not considered a match for a single region covered by the multi-region range (eg. \"us-central1\"). If not set, a unique GCS bucket will be created instead.\n",
|
||||
"\n",
|
||||
"# @markdown 3. [Make sure that you have GPU quota for Vertex Training (finetuning) and Vertex Prediction (serving)](https://cloud.google.com/docs/quotas/view-manage). The quota name for Vertex Training is \"Custom model training your-gpu-type per region\" and the quota name for Vertex Prediction is \"Custom model serving your-gpu-type per region\" such as `Custom model training Nvidia L4 GPUs per region` and `Custom model serving Nvidia L4 GPUs per region` for L4 GPUs. [Submit a quota increase request](https://cloud.google.com/docs/quotas/view-manage#requesting_higher_quota) if additional quota is needed. At minimum, running this notebook requires 4 L4s for finetuning and 1 L4 for serving. More GPUs may be needed for larger models and different finetuning configurations. To secure GPUs for larger models, ask your customer engineer to get you allowlisted for a Shared Reservation or a Dynamic Workload Scheduler.\n",
|
||||
"\n",
|
||||
"# Import the necessary packages\n",
|
||||
"! git clone https://github.com/GoogleCloudPlatform/vertex-ai-samples.git\n",
|
||||
"\n",
|
||||
"import importlib\n",
|
||||
"import os\n",
|
||||
"import uuid\n",
|
||||
"from datetime import datetime\n",
|
||||
"from typing import Tuple\n",
|
||||
"\n",
|
||||
"from google.cloud import aiplatform\n",
|
||||
"\n",
|
||||
"common_util = importlib.import_module(\n",
|
||||
" \"vertex-ai-samples.community-content.vertex_model_garden.model_oss.notebook_util.common_util\"\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"models, endpoints = {}, {}\n",
|
||||
"\n",
|
||||
"# Get the default cloud project id.\n",
|
||||
"PROJECT_ID = os.environ[\"GOOGLE_CLOUD_PROJECT\"]\n",
|
||||
"\n",
|
||||
"# Get the default region for launching jobs.\n",
|
||||
"REGION = os.environ[\"GOOGLE_CLOUD_REGION\"]\n",
|
||||
"\n",
|
||||
"# Enable the Vertex AI API and Compute Engine API, if not already.\n",
|
||||
"print(\"Enabling Vertex AI API and Compute Engine API.\")\n",
|
||||
"! gcloud services enable aiplatform.googleapis.com compute.googleapis.com\n",
|
||||
"\n",
|
||||
"# Cloud Storage bucket for storing the experiment artifacts.\n",
|
||||
"# A unique GCS bucket will be created for the purpose of this notebook. If you\n",
|
||||
"# prefer using your own GCS bucket, change the value yourself below.\n",
|
||||
"now = datetime.now().strftime(\"%Y%m%d%H%M%S\")\n",
|
||||
"BUCKET_URI = \"gs://\" # @param {type: \"string\"}\n",
|
||||
"assert BUCKET_URI.startswith(\"gs://\"), \"BUCKET_URI must start with `gs://`.\"\n",
|
||||
"BUCKET_URI = \"gs://\" # @param {type:\"string\"}\n",
|
||||
"BUCKET_NAME = \"/\".join(BUCKET_URI.split(\"/\")[:3])\n",
|
||||
"\n",
|
||||
"# Create a unique GCS bucket for this notebook, if not specified by the user.\n",
|
||||
"assert BUCKET_URI.startswith(\"gs://\"), \"BUCKET_URI must start with `gs://`.\"\n",
|
||||
"if BUCKET_URI is None or BUCKET_URI.strip() == \"\" or BUCKET_URI == \"gs://\":\n",
|
||||
" BUCKET_URI = f\"gs://{PROJECT_ID}-tmp-{now}\"\n",
|
||||
" BUCKET_URI = f\"gs://{PROJECT_ID}-tmp-{now}-{str(uuid.uuid4())[:4]}\"\n",
|
||||
" BUCKET_NAME = \"/\".join(BUCKET_URI.split(\"/\")[:3])\n",
|
||||
" ! gsutil mb -l {REGION} {BUCKET_URI}\n",
|
||||
"else:\n",
|
||||
" BUCKET_NAME = \"/\".join(BUCKET_URI.split(\"/\")[:3])\n",
|
||||
" assert BUCKET_URI.startswith(\"gs://\"), \"BUCKET_URI must start with `gs://`.\"\n",
|
||||
" shell_output = ! gsutil ls -Lb {BUCKET_NAME} | grep \"Location constraint:\" | sed \"s/Location constraint://\"\n",
|
||||
" bucket_region = shell_output[0].strip().lower()\n",
|
||||
" if bucket_region != REGION:\n",
|
||||
@@ -128,78 +144,92 @@
|
||||
" \"Bucket region %s is different from notebook region %s\"\n",
|
||||
" % (bucket_region, REGION)\n",
|
||||
" )\n",
|
||||
"\n",
|
||||
"print(f\"Using this GCS Bucket: {BUCKET_URI}\")\n",
|
||||
"\n",
|
||||
"# @markdown Click \"Show code\" to see more details.\n",
|
||||
"\n",
|
||||
"! gcloud config set project $PROJECT_ID\n",
|
||||
"! gcloud services enable language.googleapis.com\n",
|
||||
"\n",
|
||||
"STAGING_BUCKET = os.path.join(BUCKET_URI, \"temporal\")\n",
|
||||
"EXPERIMENT_BUCKET = os.path.join(BUCKET_URI, \"peft\")\n",
|
||||
"MODEL_BUCKET = os.path.join(EXPERIMENT_BUCKET, \"model\")\n",
|
||||
"MODEL_BUCKET = os.path.join(BUCKET_URI, \"mistral\")\n",
|
||||
"\n",
|
||||
"# Gets the default BUCKET_URI and SERVICE_ACCOUNT if they were not specified by the user.\n",
|
||||
"\n",
|
||||
"SERVICE_ACCOUNT = None\n",
|
||||
"# Initialize Vertex AI API.\n",
|
||||
"print(\"Initializing Vertex AI API.\")\n",
|
||||
"aiplatform.init(project=PROJECT_ID, location=REGION, staging_bucket=STAGING_BUCKET)\n",
|
||||
"\n",
|
||||
"# Gets the default SERVICE_ACCOUNT.\n",
|
||||
"shell_output = ! gcloud projects describe $PROJECT_ID\n",
|
||||
"project_number = shell_output[-1].split(\":\")[1].strip().replace(\"'\", \"\")\n",
|
||||
"SERVICE_ACCOUNT = f\"{project_number}-compute@developer.gserviceaccount.com\"\n",
|
||||
"\n",
|
||||
"print(\"Using this default Service Account:\", SERVICE_ACCOUNT)\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"# Provision permissions to the SERVICE_ACCOUNT with the GCS bucket\n",
|
||||
"! gsutil iam ch serviceAccount:{SERVICE_ACCOUNT}:roles/storage.admin $BUCKET_NAME\n",
|
||||
"\n",
|
||||
"aiplatform.init(project=PROJECT_ID, location=REGION, staging_bucket=STAGING_BUCKET)\n",
|
||||
"! gcloud config set project $PROJECT_ID\n",
|
||||
"\n",
|
||||
"# The pre-built training and serving docker images.\n",
|
||||
"VLLM_DOCKER_URI = \"us-docker.pkg.dev/vertex-ai/vertex-vision-model-garden-dockers/pytorch-vllm-serve:20240313_0916_RC00\"\n",
|
||||
"TRAIN_DOCKER_URI = \"us-docker.pkg.dev/vertex-ai/vertex-vision-model-garden-dockers/pytorch-peft-train:20240220_0936_RC01\"\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"def create_name_with_datetime(prefix: str) -> str:\n",
|
||||
" \"\"\"Creates a name with date time when triggering training or deployment\n",
|
||||
" jobs in Vertex AI.\n",
|
||||
" \"\"\"\n",
|
||||
" return prefix + datetime.now().strftime(\"_%Y%m%d_%H%M%S\")\n",
|
||||
"VLLM_DOCKER_URI = \"us-docker.pkg.dev/vertex-ai/vertex-vision-model-garden-dockers/pytorch-vllm-serve:20240721_0916_RC00\"\n",
|
||||
"TRAIN_DOCKER_URI = \"us-docker.pkg.dev/vertex-ai/vertex-vision-model-garden-dockers/pytorch-peft-train:20240724_0936_RC00\"\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"def deploy_model_vllm(\n",
|
||||
" model_name: str,\n",
|
||||
" model_id: str,\n",
|
||||
" service_account: str,\n",
|
||||
" base_model_id: str = None,\n",
|
||||
" machine_type: str = \"g2-standard-8\",\n",
|
||||
" accelerator_type: str = \"NVIDIA_L4\",\n",
|
||||
" accelerator_count: int = 1,\n",
|
||||
" quantization_method: str = \"\",\n",
|
||||
" gpu_memory_utilization: float = 0.9,\n",
|
||||
" max_model_len: int = 4096,\n",
|
||||
" dtype: str = \"auto\",\n",
|
||||
") -> Tuple[aiplatform.Model, aiplatform.Endpoint]:\n",
|
||||
" \"\"\"Deploys trained models with vLLM into Vertex AI.\"\"\"\n",
|
||||
" endpoint = aiplatform.Endpoint.create(display_name=f\"{model_name}-endpoint\")\n",
|
||||
"\n",
|
||||
" if not base_model_id:\n",
|
||||
" base_model_id = model_id\n",
|
||||
"\n",
|
||||
" vllm_args = [\n",
|
||||
" \"python\",\n",
|
||||
" \"-m\",\n",
|
||||
" \"vllm.entrypoints.api_server\",\n",
|
||||
" \"--host=0.0.0.0\",\n",
|
||||
" \"--port=7080\",\n",
|
||||
" f\"--model={model_id}\",\n",
|
||||
" f\"--tensor-parallel-size={accelerator_count}\",\n",
|
||||
" \"--swap-space=16\",\n",
|
||||
" \"--gpu-memory-utilization=0.9\",\n",
|
||||
" \"--max-num-batched-tokens=4096\",\n",
|
||||
" f\"--gpu-memory-utilization={gpu_memory_utilization}\",\n",
|
||||
" f\"--max-model-len={max_model_len}\",\n",
|
||||
" f\"--dtype={dtype}\",\n",
|
||||
" \"--disable-log-stats\",\n",
|
||||
" ]\n",
|
||||
"\n",
|
||||
" env_vars = {\n",
|
||||
" \"MODEL_ID\": base_model_id,\n",
|
||||
" \"DEPLOY_SOURCE\": \"notebook\",\n",
|
||||
" }\n",
|
||||
"\n",
|
||||
" # HF_TOKEN is not a compulsory field and may not be defined.\n",
|
||||
" try:\n",
|
||||
" if HF_TOKEN:\n",
|
||||
" env_vars[\"HF_TOKEN\"] = HF_TOKEN\n",
|
||||
" except NameError:\n",
|
||||
" pass\n",
|
||||
"\n",
|
||||
" model = aiplatform.Model.upload(\n",
|
||||
" display_name=model_name,\n",
|
||||
" serving_container_image_uri=VLLM_DOCKER_URI,\n",
|
||||
" serving_container_command=[\"python\", \"-m\", \"vllm.entrypoints.api_server\"],\n",
|
||||
" serving_container_args=vllm_args,\n",
|
||||
" serving_container_ports=[7080],\n",
|
||||
" serving_container_predict_route=\"/generate\",\n",
|
||||
" serving_container_health_route=\"/ping\",\n",
|
||||
" serving_container_environment_variables=env_vars,\n",
|
||||
" serving_container_shared_memory_size_mb=(16 * 1024), # 16 GB\n",
|
||||
" serving_container_deployment_timeout=7200,\n",
|
||||
" )\n",
|
||||
" print(\n",
|
||||
" f\"Deploying {model_name} on {machine_type} with {accelerator_count} {accelerator_type} GPU(s).\"\n",
|
||||
" )\n",
|
||||
"\n",
|
||||
" model.deploy(\n",
|
||||
" endpoint=endpoint,\n",
|
||||
" machine_type=machine_type,\n",
|
||||
@@ -208,9 +238,86 @@
|
||||
" deploy_request_timeout=1800,\n",
|
||||
" service_account=service_account,\n",
|
||||
" )\n",
|
||||
" print(\"endpoint_name:\", endpoint.name)\n",
|
||||
"\n",
|
||||
" return model, endpoint"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"cellView": "form",
|
||||
"id": "5K169qf_udor"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# @title Set dataset\n",
|
||||
"\n",
|
||||
"# @markdown Use the Vertex AI SDK to create and run the custom training jobs.\n",
|
||||
"\n",
|
||||
"# @markdown This notebook uses [timdettmers/openassistant-guanaco](https://huggingface.co/datasets/timdettmers/openassistant-guanaco) dataset as an example.\n",
|
||||
"# @markdown You can set `dataset_name` to any existing [Hugging Face dataset](https://huggingface.co/datasets) name, and set `instruct_column_in_dataset` to the name of the dataset column containing training data. The [timdettmers/openassistant-guanaco](https://huggingface.co/datasets/timdettmers/openassistant-guanaco) has only one column `text`, and therefore we set `instruct_column_in_dataset` to `text` in this notebook.\n",
|
||||
"\n",
|
||||
"# @markdown ### (Optional) Prepare a custom JSONL dataset for finetuning\n",
|
||||
"\n",
|
||||
"# @markdown You can prepare a JSONL file where each line is a valid JSON string as your custom training dataset. For example, here is one line from the [timdettmers/openassistant-guanaco](https://huggingface.co/datasets/timdettmers/openassistant-guanaco) dataset:\n",
|
||||
"# @markdown ```\n",
|
||||
"# @markdown {\"text\": \"### Human: Hola### Assistant: \\u00a1Hola! \\u00bfEn qu\\u00e9 puedo ayudarte hoy?\"}\n",
|
||||
"# @markdown ```\n",
|
||||
"\n",
|
||||
"# @markdown The JSON object has a key `text`, which should match `instruct_column_in_dataset`; The value should be one training data point, i.e. a string. After you prepared your JSONL file, you can either upload it to [Hugging Face datasets](https://huggingface.co/datasets) or [Google Cloud Storage](https://cloud.google.com/storage).\n",
|
||||
"\n",
|
||||
"# @markdown - To upload a JSONL dataset to [Hugging Face datasets](https://huggingface.co/datasets), follow the instructions on [Uploading Datasets](https://huggingface.co/docs/hub/en/datasets-adding). Then, set `dataset_name` to the name of your newly created dataset on Hugging Face.\n",
|
||||
"\n",
|
||||
"# @markdown - To upload a JSONL dataset to [Google Cloud Storage](https://cloud.google.com/storage), follow the instructions on [Upload objects from a filesystem](https://cloud.google.com/storage/docs/uploading-objects). Then, set `dataset_name` to the `gs://` URI to your JSONL file. For example: `gs://cloud-samples-data/vertex-ai/model-evaluation/peft_train_sample.jsonl`.\n",
|
||||
"\n",
|
||||
"# @markdown Optionally update the `instruct_column_in_dataset` field below if your JSON objects use a key other than the default `text`.\n",
|
||||
"\n",
|
||||
"# @markdown ### (Optional) Format your data with custom JSON template\n",
|
||||
"\n",
|
||||
"# @markdown Sometimes, your dataset might have multiple text columns and you want to construct the training data with a template. You can prepare a JSON template in the following format:\n",
|
||||
"\n",
|
||||
"# @markdown ```\n",
|
||||
"# @markdown {\n",
|
||||
"# @markdown \"description\": \"Template that accepts text-bison format.\",\n",
|
||||
"# @markdown \"source\": \"https://cloud.google.com/vertex-ai/generative-ai/docs/models/tune-text-models-supervised#dataset-format\",\n",
|
||||
"# @markdown \"prompt_input\": \"\\n\\n<|start_header_id|>user<|end_header_id|>\\n\\n{input_text}<|eot_id|>\\n\\n<|start_header_id|>assistant<|end_header_id|>\\n\\n{output_text}<|eot_id|>\",\n",
|
||||
"# @markdown \"instruction_separator\": \"<|start_header_id|>user<|end_header_id|>\\n\\n\",\n",
|
||||
"# @markdown \"response_separator\": \"<|start_header_id|>assistant<|end_header_id|>\\n\\n\"\n",
|
||||
"# @markdown }\n",
|
||||
"# @markdown ```\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"# @markdown As an example, the template above can be used to format the following training data (this line comes from `gs://cloud-samples-data/vertex-ai/model-evaluation/peft_train_sample.jsonl`):\n",
|
||||
"\n",
|
||||
"# @markdown ```\n",
|
||||
"# @markdown {\"input_text\":\"TRANSCRIPT: \\nREASON FOR EVALUATION:,\\n\\n LABEL:\",\"output_text\":\"Chiropractic\"}\n",
|
||||
"# @markdown ```\n",
|
||||
"\n",
|
||||
"# @markdown This example template simply concatenates `input_text` with `output_text` with some special tokens in between.\n",
|
||||
"# @markdown\n",
|
||||
"# @markdown To try such custom dataset, you can make the following changes:\n",
|
||||
"# @markdown 1. Set `template` to `llama3-text-bison`\n",
|
||||
"# @markdown 1. Set `train_dataset_name` to `gs://cloud-samples-data/vertex-ai/model-evaluation/peft_train_sample.jsonl`\n",
|
||||
"# @markdown 1. Set `train_split_name` to `train`\n",
|
||||
"# @markdown 1. Set `eval_dataset_name` to `gs://cloud-samples-data/vertex-ai/model-evaluation/peft_eval_sample.jsonl`\n",
|
||||
"# @markdown 1. Set `eval_split_name` to `train` (**NOT** `test`)\n",
|
||||
"# @markdown 1. Set `instruct_column_in_dataset` as `input_text`.\n",
|
||||
"\n",
|
||||
"# Template name or gs:// URI to a custom template.\n",
|
||||
"template = \"openassistant-guanaco\" # @param {type:\"string\"}\n",
|
||||
"\n",
|
||||
"# Hugging Face dataset name or gs:// URI to a custom JSONL dataset.\n",
|
||||
"train_dataset_name = \"timdettmers/openassistant-guanaco\" # @param {type:\"string\"}\n",
|
||||
"train_split_name = \"train\" # @param {type:\"string\"}\n",
|
||||
"eval_dataset_name = \"timdettmers/openassistant-guanaco\" # @param {type:\"string\"}\n",
|
||||
"eval_split_name = \"test\" # @param {type:\"string\"}\n",
|
||||
"\n",
|
||||
"# Name of the dataset column containing training text input.\n",
|
||||
"instruct_column_in_dataset = \"text\" # @param {type:\"string\"}"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
@@ -222,134 +329,163 @@
|
||||
"source": [
|
||||
"# @title Finetune\n",
|
||||
"\n",
|
||||
"# @markdown Use the Vertex AI SDK to create and run the custom training jobs.\n",
|
||||
"\n",
|
||||
"# @markdown **Note**:\n",
|
||||
"# @markdown 1. We recommend setting `finetuning_precision_mode` to `4bit` because it enables using fewer hardware resources for finetuning.\n",
|
||||
"# @markdown 1. If `max_steps > 0`, it will precedence over `epochs`. One can set a small `max_steps` value to quickly check the pipeline.\n",
|
||||
"# @markdown 1. With the default setting, training takes between 1.5 ~ 2 hours.\n",
|
||||
"\n",
|
||||
"# @markdown This section demonstrates how to finetune the Mistral-7B model and merge the finetuned LoRA adapter with the base model on Vertex AI.\n",
|
||||
"\n",
|
||||
"# @markdown This example uses the dataset [fredmo/vertexai-qna-500](https://huggingface.co/datasets/fredmo/vertexai-qna-500). You can set `dataset_name` to any existing [Hugging Face dataset](https://huggingface.co/datasets) name.\n",
|
||||
"\n",
|
||||
"# @markdown ### (Optional) Prepare a custom JSONL dataset for finetuning\n",
|
||||
"\n",
|
||||
"# @markdown You can prepare a JSONL file where each line is a valid JSON string as your custom training dataset. For example, here is one line from the [fredmo/vertexai-qna-500](https://huggingface.co/datasets/fredmo/vertexai-qna-500) dataset:\n",
|
||||
"# @markdown ```\n",
|
||||
"# @markdown {\"input_text\": \"question: What is the first step in setting up a project for Vertex AI?\", \"output_text\": \"Select or create a Google Cloud project.\"}\n",
|
||||
"# @markdown ```\n",
|
||||
"\n",
|
||||
"# @markdown - To upload a JSONL dataset to [Hugging Face datasets](https://huggingface.co/datasets), follow the instructions on [Uploading Datasets](https://huggingface.co/docs/hub/en/datasets-adding). Then, set `dataset_name` to the name of your newly created dataset on Hugging Face.\n",
|
||||
"\n",
|
||||
"# @markdown - To upload a JSONL dataset to [Google Cloud Storage](https://cloud.google.com/storage), follow the instructions on [Upload objects from a filesystem](https://cloud.google.com/storage/docs/uploading-objects). Then, set `dataset_name` to the `gs://` URI to your JSONL file. For example: `gs://cloud-samples-data/vertex-ai/model-evaluation/peft_train_sample.jsonl`.\n",
|
||||
"\n",
|
||||
"# @markdown ### (Optional) Format your data with custom JSON template\n",
|
||||
"\n",
|
||||
"# @markdown Sometimes, your dataset might have multiple text columns and you want to construct the training data with a template. You can prepare a JSON template in the following format:\n",
|
||||
"\n",
|
||||
"# @markdown ```\n",
|
||||
"# @markdown {\n",
|
||||
"# @markdown \"description\": \"A short template for vertex sample dataset.\",\n",
|
||||
"# @markdown \"prompt_input\": \"{input_text}{output_text}\",\n",
|
||||
"# @markdown \"prompt_no_input\": \"{input_text}{output_text}\"\n",
|
||||
"# @markdown }\n",
|
||||
"# @markdown ```\n",
|
||||
"\n",
|
||||
"# @markdown As an example, the template above can be used to format the following training data (this line comes from `gs://cloud-samples-data/vertex-ai/model-evaluation/peft_train_sample.jsonl`):\n",
|
||||
"\n",
|
||||
"# @markdown ```\n",
|
||||
"# @markdown {\"input_text\":\"TRANSCRIPT: \\nREASON FOR EVALUATION:,\\n\\n LABEL:\",\"output_text\":\"Chiropractic\"}\n",
|
||||
"# @markdown ```\n",
|
||||
"\n",
|
||||
"# @markdown This example template simply concatenates `input_text` with `output_text`. You can set `template` to `vertex_sample` to try out this built-in template with the dataset `gs://cloud-samples-data/vertex-ai/model-evaluation/peft_train_sample.jsonl`, or build more complicated JSON templates such as [the alpaca example](https://github.com/tloen/alpaca-lora/blob/main/templates/alpaca.json). To use your own JSON template, please [upload it to Google Cloud Storage](https://cloud.google.com/storage/docs/uploading-objects) and put the `gs://` URI in the `template` field below.\n",
|
||||
"\n",
|
||||
"# Huggingface dataset name or gs:// URI to a custom JSONL dataset.\n",
|
||||
"base_model_id = \"mistralai/Mistral-7B-v0.1\"\n",
|
||||
"gcs_model_id = f\"gs://vertex-model-garden-public-us/{base_model_id}\"\n",
|
||||
"dataset_name = \"fredmo/vertexai-qna-500\" # @param {type:\"string\"}\n",
|
||||
"pretrained_model_id = f\"gs://vertex-model-garden-public-us/{base_model_id}\"\n",
|
||||
"\n",
|
||||
"# Optional. Template name or gs:// URI to a custom template.\n",
|
||||
"template = \"vertex_sample\" # @param {type:\"string\"}\n",
|
||||
"\n",
|
||||
"# Number of training steps.\n",
|
||||
"max_steps = 10 # @param {type:\"integer\"}\n",
|
||||
"\n",
|
||||
"# LoRA parameters.\n",
|
||||
"# @markdown Batch size for finetuning.\n",
|
||||
"per_device_train_batch_size = 1 # @param{type:\"integer\"}\n",
|
||||
"# @markdown Number of updates steps to accumulate the gradients for, before performing a backward/update pass.\n",
|
||||
"gradient_accumulation_steps = 8 # @param{type:\"integer\"}\n",
|
||||
"# @markdown Maximum sequence length.\n",
|
||||
"max_seq_length = 4096 # @param{type:\"integer\"}\n",
|
||||
"# @markdown Setting a positive `max_steps` here will override `num_epochs`.\n",
|
||||
"max_steps = -1 # @param{type:\"integer\"}\n",
|
||||
"num_epochs = 1.0 # @param{type:\"number\"}\n",
|
||||
"# @markdown Precision mode for finetuning.\n",
|
||||
"finetuning_precision_mode = \"4bit\" # @param [\"4bit\", \"8bit\", \"float16\"]\n",
|
||||
"# @markdown Learning rate.\n",
|
||||
"learning_rate = 5e-5 # @param{type:\"number\"}\n",
|
||||
"# @markdown The scheduler type to use.\n",
|
||||
"lr_scheduler_type = \"cosine\" # @param{type:\"string\"}\n",
|
||||
"# @markdown LoRA parameters.\n",
|
||||
"lora_rank = 16 # @param{type:\"integer\"}\n",
|
||||
"lora_alpha = 64 # @param{type:\"integer\"}\n",
|
||||
"lora_dropout = 0.1 # @param{type:\"number\"}\n",
|
||||
"\n",
|
||||
"# Learning rate.\n",
|
||||
"learning_rate = 0.0001 # @param{type:\"number\"}\n",
|
||||
"\n",
|
||||
"# Precision mode for finetuning.\n",
|
||||
"finetuning_precision_mode = \"float16\"\n",
|
||||
"lora_alpha = 32 # @param{type:\"integer\"}\n",
|
||||
"lora_dropout = 0.05 # @param{type:\"number\"}\n",
|
||||
"# Activates gradient checkpointing for the current model (may be referred to as activation checkpointing or checkpoint activations in other frameworks).\n",
|
||||
"enable_gradient_checkpointing = True\n",
|
||||
"# Attention implementation to use in the model.\n",
|
||||
"attn_implementation = \"flash_attention_2\"\n",
|
||||
"# The optimizer for which to schedule the learning rate.\n",
|
||||
"optimizer = \"paged_adamw_32bit\"\n",
|
||||
"# Define the proportion of training to be dedicated to a linear warmup where learning rate gradually increases.\n",
|
||||
"warmup_ratio = \"0.01\"\n",
|
||||
"# The list or string of integrations to report the results and logs to.\n",
|
||||
"report_to = \"tensorboard\"\n",
|
||||
"# Number of updates steps before two checkpoint saves.\n",
|
||||
"save_steps = 10\n",
|
||||
"# Number of update steps between two logs.\n",
|
||||
"logging_steps = save_steps\n",
|
||||
"# Train precision of the model.\n",
|
||||
"train_precision = \"float16\"\n",
|
||||
"\n",
|
||||
"# Worker pool spec for 4bit finetuning.\n",
|
||||
"accelerator_type = \"NVIDIA_L4\" # @param[\"NVIDIA_TESLA_V100\", \"NVIDIA_L4\", \"NVIDIA_TESLA_A100\"]\n",
|
||||
"accelerator_type = \"NVIDIA_L4\" # @param[\"NVIDIA_A100_80GB\", \"NVIDIA_L4\"]\n",
|
||||
"\n",
|
||||
"if accelerator_type == \"NVIDIA_TESLA_V100\":\n",
|
||||
" machine_type = \"n1-highmem-16\"\n",
|
||||
" accelerator_count = 2\n",
|
||||
"elif accelerator_type == \"NVIDIA_L4\":\n",
|
||||
" machine_type = \"g2-standard-8\"\n",
|
||||
" accelerator_count = 1\n",
|
||||
"elif accelerator_type == \"NVIDIA_TESLA_A100\":\n",
|
||||
" machine_type = \"a2-highgpu-1g\"\n",
|
||||
" accelerator_count = 1\n",
|
||||
"if accelerator_type == \"NVIDIA_L4\":\n",
|
||||
" accelerator_count = 4\n",
|
||||
" machine_type = \"g2-standard-48\"\n",
|
||||
"elif accelerator_type == \"NVIDIA_A100_80GB\":\n",
|
||||
" accelerator_count = 4\n",
|
||||
" machine_type = \"a2-ultragpu-4g\"\n",
|
||||
"else:\n",
|
||||
" raise ValueError(f\"Unsupported accelerator type: {accelerator_type}\")\n",
|
||||
"\n",
|
||||
"replica_count = 1\n",
|
||||
"\n",
|
||||
"# @markdown Click \"Show code\" to see more details.\n",
|
||||
"common_util.check_quota(\n",
|
||||
" project_id=PROJECT_ID,\n",
|
||||
" region=REGION,\n",
|
||||
" accelerator_type=accelerator_type,\n",
|
||||
" accelerator_count=accelerator_count,\n",
|
||||
" is_for_training=True,\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"# Setup training job.\n",
|
||||
"job_name = create_name_with_datetime(\"mistral-lora-train\")\n",
|
||||
"job_name = common_util.get_job_name_with_datetime(\"mistral-lora-train\").replace(\n",
|
||||
" \"_\", \"-\"\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"base_output_dir = os.path.join(STAGING_BUCKET, job_name)\n",
|
||||
"# Create a GCS folder to store the LORA adapter.\n",
|
||||
"lora_output_dir = os.path.join(base_output_dir, \"adapter\")\n",
|
||||
"# Create a GCS folder to store the merged model with the base model and the\n",
|
||||
"# finetuned LORA adapter.\n",
|
||||
"merged_model_output_dir = os.path.join(base_output_dir, \"merged-model\")\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"eval_args = [\n",
|
||||
" f\"--eval_dataset_path={eval_dataset_name}\",\n",
|
||||
" f\"--eval_column={instruct_column_in_dataset}\",\n",
|
||||
" f\"--eval_template={template}\",\n",
|
||||
" f\"--eval_split={eval_split_name}\",\n",
|
||||
" f\"--eval_steps={save_steps}\",\n",
|
||||
" \"--eval_tasks=builtin_eval\",\n",
|
||||
" \"--eval_metric_name=loss\",\n",
|
||||
"]\n",
|
||||
"\n",
|
||||
"train_job_args = [\n",
|
||||
" \"--config_file=vertex_vision_model_garden_peft/deepspeed_zero2_4gpu.yaml\",\n",
|
||||
" \"--task=instruct-lora\",\n",
|
||||
" \"--completion_only=False\",\n",
|
||||
" f\"--pretrained_model_id={pretrained_model_id}\",\n",
|
||||
" f\"--dataset_name={train_dataset_name}\",\n",
|
||||
" f\"--train_split_name={train_split_name}\",\n",
|
||||
" f\"--instruct_column_in_dataset={instruct_column_in_dataset}\",\n",
|
||||
" f\"--output_dir={lora_output_dir}\",\n",
|
||||
" f\"--merge_base_and_lora_output_dir={merged_model_output_dir}\",\n",
|
||||
" f\"--per_device_train_batch_size={per_device_train_batch_size}\",\n",
|
||||
" f\"--gradient_accumulation_steps={gradient_accumulation_steps}\",\n",
|
||||
" f\"--lora_rank={lora_rank}\",\n",
|
||||
" f\"--lora_alpha={lora_alpha}\",\n",
|
||||
" f\"--lora_dropout={lora_dropout}\",\n",
|
||||
" f\"--max_steps={max_steps}\",\n",
|
||||
" f\"--max_seq_length={max_seq_length}\",\n",
|
||||
" f\"--learning_rate={learning_rate}\",\n",
|
||||
" f\"--lr_scheduler_type={lr_scheduler_type}\",\n",
|
||||
" f\"--precision_mode={finetuning_precision_mode}\",\n",
|
||||
" f\"--train_precision={train_precision}\",\n",
|
||||
" f\"--enable_gradient_checkpointing={enable_gradient_checkpointing}\",\n",
|
||||
" f\"--num_epochs={num_epochs}\",\n",
|
||||
" f\"--attn_implementation={attn_implementation}\",\n",
|
||||
" f\"--optimizer={optimizer}\",\n",
|
||||
" f\"--warmup_ratio={warmup_ratio}\",\n",
|
||||
" f\"--report_to={report_to}\",\n",
|
||||
" f\"--logging_output_dir={base_output_dir}\",\n",
|
||||
" f\"--save_steps={save_steps}\",\n",
|
||||
" f\"--logging_steps={logging_steps}\",\n",
|
||||
" f\"--template={template}\",\n",
|
||||
"] + eval_args\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"# Create TensorBoard\n",
|
||||
"tensorboard = aiplatform.Tensorboard.create(job_name)\n",
|
||||
"exp = aiplatform.TensorboardExperiment.create(\n",
|
||||
" tensorboard_experiment_id=job_name, tensorboard_name=tensorboard.name\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"train_job = aiplatform.CustomContainerTrainingJob(\n",
|
||||
" display_name=job_name,\n",
|
||||
" container_uri=TRAIN_DOCKER_URI,\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"\"\"\"\n",
|
||||
"In the below code, the finetuned LoRA adapter will be saved to a GCS bucket\n",
|
||||
"specified by the variable lora_output_dir below; and you merge the\n",
|
||||
"LoRa adapter with the base model, and save it to a separate GCS bucket\n",
|
||||
"specified by merged_model_output_dir below.\n",
|
||||
"\"\"\"\n",
|
||||
"\n",
|
||||
"# Create a GCS folder to store the LORA adapter.\n",
|
||||
"lora_adapter_dir = create_name_with_datetime(\"mistral-lora-adapter\")\n",
|
||||
"lora_output_dir = os.path.join(MODEL_BUCKET, lora_adapter_dir)\n",
|
||||
"\n",
|
||||
"# Create a GCS folder to store the merged model with the base model and the\n",
|
||||
"# finetuned LORA adapter.\n",
|
||||
"merged_model_dir = create_name_with_datetime(\"mistral-merged-model\")\n",
|
||||
"merged_model_output_dir = os.path.join(MODEL_BUCKET, merged_model_dir)\n",
|
||||
"\n",
|
||||
"# Pass training arguments and launch job.\n",
|
||||
"train_job.run(\n",
|
||||
" args=[\n",
|
||||
" \"--task=causal-language-modeling-lora\",\n",
|
||||
" f\"--pretrained_model_id={gcs_model_id}\",\n",
|
||||
" f\"--dataset_name={dataset_name}\",\n",
|
||||
" f\"--output_dir={lora_output_dir}\",\n",
|
||||
" f\"--merge_base_and_lora_output_dir={merged_model_output_dir}\",\n",
|
||||
" f\"--lora_rank={lora_rank}\",\n",
|
||||
" f\"--lora_alpha={lora_alpha}\",\n",
|
||||
" f\"--lora_dropout={lora_dropout}\",\n",
|
||||
" \"--warmup_steps=10\",\n",
|
||||
" f\"--max_steps={max_steps}\",\n",
|
||||
" f\"--learning_rate={learning_rate}\",\n",
|
||||
" f\"--precision_mode={finetuning_precision_mode}\",\n",
|
||||
" f\"--template={template}\",\n",
|
||||
" ],\n",
|
||||
" args=train_job_args,\n",
|
||||
" environment_variables={\"WANDB_DISABLED\": True},\n",
|
||||
" replica_count=replica_count,\n",
|
||||
" machine_type=machine_type,\n",
|
||||
" accelerator_type=accelerator_type,\n",
|
||||
" accelerator_count=accelerator_count,\n",
|
||||
" boot_disk_size_gb=500,\n",
|
||||
" service_account=SERVICE_ACCOUNT,\n",
|
||||
" tensorboard=tensorboard.resource_name,\n",
|
||||
" base_output_dir=base_output_dir,\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"print(\"The finetuned Lora adapter can be found at: \", lora_output_dir)\n",
|
||||
"print(\n",
|
||||
" \"The finetuned Lora adapter merged with the base model can be found at: \",\n",
|
||||
" merged_model_output_dir,\n",
|
||||
")"
|
||||
"print(\"LoRA adapter was saved in: \", lora_output_dir)\n",
|
||||
"print(\"Trained and merged models were saved in: \", merged_model_output_dir)\n",
|
||||
"\n",
|
||||
"# @markdown Click \"Show Code\" to see more details."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -362,25 +498,39 @@
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# @title Deploy\n",
|
||||
"# @markdown This section uploads the model to Model Registry and deploys it on the Endpoint. The model deployment step will take ~15 minutes to complete.\n",
|
||||
"# @markdown This section uploads the model to Model Registry and deploys it on the Endpoint. It takes 15 minutes to 1 hour to finish depending on the size of model.\n",
|
||||
"\n",
|
||||
"# @markdown Click \"Show code\" to see more details.\n",
|
||||
"print(\"Deploying models in: \", merged_model_output_dir)\n",
|
||||
"\n",
|
||||
"# Finds Vertex AI prediction supported accelerators and regions in\n",
|
||||
"# https://cloud.google.com/vertex-ai/docs/predictions/configure-compute.\n",
|
||||
"# Find Vertex AI prediction supported accelerators and regions [here](https://cloud.google.com/vertex-ai/docs/predictions/configure-compute).\n",
|
||||
"\n",
|
||||
"model, endpoint = deploy_model_vllm(\n",
|
||||
" model_name=create_name_with_datetime(prefix=\"mistral-peft-serve-vllm\"),\n",
|
||||
"common_util.check_quota(\n",
|
||||
" project_id=PROJECT_ID,\n",
|
||||
" region=REGION,\n",
|
||||
" accelerator_type=accelerator_type,\n",
|
||||
" accelerator_count=accelerator_count,\n",
|
||||
" is_for_training=False,\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"gpu_memory_utilization = 0.85\n",
|
||||
"max_model_len = 8192 # Maximum context length.\n",
|
||||
"\n",
|
||||
"# Ensure max_model_len does not exceed the limit\n",
|
||||
"if max_model_len > 8192:\n",
|
||||
" raise ValueError(\"max_model_len cannot exceed 8192\")\n",
|
||||
"\n",
|
||||
"models[\"vllm_gpu\"], endpoints[\"vllm_gpu\"] = deploy_model_vllm(\n",
|
||||
" model_name=common_util.get_job_name_with_datetime(prefix=\"mistral-vllm-serve\"),\n",
|
||||
" model_id=merged_model_output_dir,\n",
|
||||
" service_account=SERVICE_ACCOUNT,\n",
|
||||
" machine_type=machine_type,\n",
|
||||
" accelerator_type=accelerator_type,\n",
|
||||
" accelerator_count=accelerator_count,\n",
|
||||
" gpu_memory_utilization=gpu_memory_utilization,\n",
|
||||
" max_model_len=max_model_len,\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"print(\"endpoint_name:\", endpoint.name)\n",
|
||||
"print(\"model_name:\", model.display_name)\n",
|
||||
"print(\"model_id:\", model.resource_name)"
|
||||
"# @markdown Click \"Show code\" to see more details."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -393,8 +543,8 @@
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# @title Predict\n",
|
||||
"# @markdown Once deployment succeeds, you can send requests to the endpoint with text prompts.\n",
|
||||
"# @markdown Additionally, you can moderate the generated text with Vertex AI. See [Moderate text documentation](https://cloud.google.com/natural-language/docs/moderating-text) for more details.\n",
|
||||
"\n",
|
||||
"# @markdown Once deployment succeeds, you can send requests to the endpoint with text prompts. Sampling parameters supported by vLLM can be found [here](https://docs.vllm.ai/en/latest/dev/sampling_params.html).\n",
|
||||
"\n",
|
||||
"# @markdown Example:\n",
|
||||
"\n",
|
||||
@@ -402,6 +552,7 @@
|
||||
"# @markdown Human: What is a car?\n",
|
||||
"# @markdown Assistant: A car, or a motor car, is a road-connected human-transportation system used to move people or goods from one place to another. The term also encompasses a wide range of vehicles, including motorboats, trains, and aircrafts. Cars typically have four wheels, a cabin for passengers, and an engine or motor. They have been around since the early 19th century and are now one of the most popular forms of transportation, used for daily commuting, shopping, and other purposes.\n",
|
||||
"# @markdown ```\n",
|
||||
"# @markdown Additionally, you can moderate the generated text with Vertex AI. See [Moderate text documentation](https://cloud.google.com/natural-language/docs/moderating-text) for more details.\n",
|
||||
"\n",
|
||||
"# Loads an existing endpoint instance using the endpoint name:\n",
|
||||
"# - Using `endpoint_name = endpoint.name` allows us to get the\n",
|
||||
@@ -417,24 +568,31 @@
|
||||
"# )\n",
|
||||
"# endpoint = aiplatform.Endpoint(aip_endpoint_name)\n",
|
||||
"\n",
|
||||
"prompt = \"What is Vertex AI?\" # @param {type: \"string\"}\n",
|
||||
"max_tokens = 100 # @param {type:\"integer\"}\n",
|
||||
"prompt = \"What is a car?\" # @param {type: \"string\"}\n",
|
||||
"# @markdown If you encounter the issue like `ServiceUnavailable: 503 Took too long to respond when processing`, you can reduce the maximum number of output tokens, such as set `max_tokens` as 20.\n",
|
||||
"max_tokens = 50 # @param {type:\"integer\"}\n",
|
||||
"temperature = 1.0 # @param {type:\"number\"}\n",
|
||||
"top_p = 1.0 # @param {type:\"number\"}\n",
|
||||
"top_k = 1 # @param {type:\"integer\"}\n",
|
||||
"raw_response = False # @param {type:\"boolean\"}\n",
|
||||
"\n",
|
||||
"instance = {\n",
|
||||
" \"prompt\": prompt,\n",
|
||||
" \"max_tokens\": max_tokens,\n",
|
||||
" \"temperature\": temperature,\n",
|
||||
" \"top_p\": top_p,\n",
|
||||
" \"top_k\": top_k,\n",
|
||||
"}\n",
|
||||
"\n",
|
||||
"response = endpoint.predict(instances=[instance])\n",
|
||||
"# Overrides parameters for inferences.\n",
|
||||
"instances = [\n",
|
||||
" {\n",
|
||||
" \"prompt\": prompt,\n",
|
||||
" \"max_tokens\": max_tokens,\n",
|
||||
" \"temperature\": temperature,\n",
|
||||
" \"top_p\": top_p,\n",
|
||||
" \"top_k\": top_k,\n",
|
||||
" \"raw_response\": raw_response,\n",
|
||||
" },\n",
|
||||
"]\n",
|
||||
"response = endpoints[\"vllm_gpu\"].predict(instances=instances)\n",
|
||||
"\n",
|
||||
"for prediction in response.predictions:\n",
|
||||
" print(prediction)"
|
||||
" print(prediction)\n",
|
||||
"\n",
|
||||
"# @markdown Click \"Show Code\" to see more details."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -446,16 +604,22 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# @title Clean up resources\n",
|
||||
"# @markdown Delete the experiment models and endpoints to recycle the resources\n",
|
||||
"# @markdown and avoid unnecessary continouous charges that may incur.\n",
|
||||
"# @title Delete the model and endpoint\n",
|
||||
"\n",
|
||||
"endpoint.delete(force=True)\n",
|
||||
"model.delete()\n",
|
||||
"# @markdown Delete the experiment models and endpoints to recycle the resources\n",
|
||||
"# @markdown and avoid unnecessary continuous charges that may incur.\n",
|
||||
"\n",
|
||||
"# Undeploy model and delete endpoint.\n",
|
||||
"for endpoint in endpoints.values():\n",
|
||||
" endpoint.delete(force=True)\n",
|
||||
"\n",
|
||||
"# Delete models.\n",
|
||||
"for model in models.values():\n",
|
||||
" model.delete()\n",
|
||||
"\n",
|
||||
"delete_bucket = False # @param {type:\"boolean\"}\n",
|
||||
"if delete_bucket:\n",
|
||||
" ! gsutil -m rm -r $BUCKET_URI"
|
||||
" ! gsutil -m rm -r $BUCKET_NAME"
|
||||
]
|
||||
}
|
||||
],
|
||||
|
||||
@@ -0,0 +1,436 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "20qcPG1PmFUM"
|
||||
},
|
||||
"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": "QXYOa1odnikj"
|
||||
},
|
||||
"source": [
|
||||
"# Vertex AI Model Garden - Qwen2 (Deployment)\n",
|
||||
"\n",
|
||||
"<table><tbody><tr>\n",
|
||||
" <td style=\"text-align: center\">\n",
|
||||
" <a href=\"https://console.cloud.google.com/vertex-ai/colab/import/https:%2F%2Fraw.githubusercontent.com%2FGoogleCloudPlatform%2Fvertex-ai-samples%2Fmain%2Fnotebooks%2Fcommunity%2Fmodel_garden%2Fmodel_garden_pytorch_qwen2_deployment.ipynb\">\n",
|
||||
" <img alt=\"Google Cloud Colab Enterprise logo\" src=\"https://lh3.googleusercontent.com/JmcxdQi-qOpctIvWKgPtrzZdJJK-J3sWE1RsfjZNwshCFgE_9fULcNpuXYTilIR2hjwN\" width=\"32px\"><br> Run in Colab Enterprise\n",
|
||||
" </a>\n",
|
||||
" </td>\n",
|
||||
" <td style=\"text-align: center\">\n",
|
||||
" <a href=\"https://github.com/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/community/model_garden/model_garden_pytorch_qwen2_deployment.ipynb\">\n",
|
||||
" <img alt=\"GitHub logo\" src=\"https://cloud.google.com/ml-engine/images/github-logo-32px.png\" width=\"32px\"><br> View on GitHub\n",
|
||||
" </a>\n",
|
||||
" </td>\n",
|
||||
"</tr></tbody></table>"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "cbDI9ag4oR4C"
|
||||
},
|
||||
"source": [
|
||||
"## Overview\n",
|
||||
"\n",
|
||||
"This notebook demonstrates deploying prebuilt [Qwen2 models](https://huggingface.co/collections/Qwen/qwen2-6659360b33528ced941e557f) with [vLLM](https://github.com/vllm-project/vllm) to improve serving throughput.\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"### Objective\n",
|
||||
"\n",
|
||||
"- Download and deploy prebuilt Qwen2 models\n",
|
||||
"- Deploy Qwen2 with [vLLM](https://github.com/vllm-project/vllm) to improve serving throughput\n",
|
||||
"\n",
|
||||
"### 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), [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": "hQJWRopioSKT"
|
||||
},
|
||||
"source": [
|
||||
"## Before you begin"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"cellView": "form",
|
||||
"id": "J_jmxcIZoSxU"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# @title Setup Google Cloud project\n",
|
||||
"\n",
|
||||
"# @markdown 1. [Make sure that billing is enabled for your project](https://cloud.google.com/billing/docs/how-to/modify-project).\n",
|
||||
"\n",
|
||||
"# @markdown 2. [Optional] [Create a Cloud Storage bucket](https://cloud.google.com/storage/docs/creating-buckets) for storing experiment outputs. Set the BUCKET_URI for the experiment environment. The specified Cloud Storage bucket (`BUCKET_URI`) should be located in the same region as where the notebook was launched. Note that a multi-region bucket (eg. \"us\") is not considered a match for a single region covered by the multi-region range (eg. \"us-central1\"). If not set, a unique GCS bucket will be created instead.\n",
|
||||
"\n",
|
||||
"# Import the necessary packages\n",
|
||||
"import importlib\n",
|
||||
"import os\n",
|
||||
"import uuid\n",
|
||||
"from datetime import datetime\n",
|
||||
"from typing import Tuple\n",
|
||||
"\n",
|
||||
"from google.cloud import aiplatform\n",
|
||||
"\n",
|
||||
"! git clone https://github.com/GoogleCloudPlatform/vertex-ai-samples.git\n",
|
||||
"\n",
|
||||
"models, endpoints = {}, {}\n",
|
||||
"\n",
|
||||
"common_util = importlib.import_module(\n",
|
||||
" \"vertex-ai-samples.community-content.vertex_model_garden.model_oss.notebook_util.common_util\"\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"# Get the default cloud project id.\n",
|
||||
"PROJECT_ID = os.environ[\"GOOGLE_CLOUD_PROJECT\"]\n",
|
||||
"\n",
|
||||
"# Get the default region for launching jobs.\n",
|
||||
"REGION = os.environ[\"GOOGLE_CLOUD_REGION\"]\n",
|
||||
"\n",
|
||||
"# Enable the Vertex AI API and Compute Engine API, if not already.\n",
|
||||
"print(\"Enabling Vertex AI API and Compute Engine API.\")\n",
|
||||
"! gcloud services enable aiplatform.googleapis.com compute.googleapis.com\n",
|
||||
"\n",
|
||||
"# Cloud Storage bucket for storing the experiment artifacts.\n",
|
||||
"# A unique GCS bucket will be created for the purpose of this notebook. If you\n",
|
||||
"# prefer using your own GCS bucket, change the value yourself below.\n",
|
||||
"now = datetime.now().strftime(\"%Y%m%d%H%M%S\")\n",
|
||||
"BUCKET_URI = \"gs://\" # @param {type:\"string\"}\n",
|
||||
"BUCKET_NAME = \"/\".join(BUCKET_URI.split(\"/\")[:3])\n",
|
||||
"\n",
|
||||
"if BUCKET_URI is None or BUCKET_URI.strip() == \"\" or BUCKET_URI == \"gs://\":\n",
|
||||
" BUCKET_URI = f\"gs://{PROJECT_ID}-tmp-{now}-{str(uuid.uuid4())[:4]}\"\n",
|
||||
" BUCKET_NAME = \"/\".join(BUCKET_URI.split(\"/\")[:3])\n",
|
||||
" ! gsutil mb -l {REGION} {BUCKET_URI}\n",
|
||||
"else:\n",
|
||||
" assert BUCKET_URI.startswith(\"gs://\"), \"BUCKET_URI must start with `gs://`.\"\n",
|
||||
" shell_output = ! gsutil ls -Lb {BUCKET_NAME} | grep \"Location constraint:\" | sed \"s/Location constraint://\"\n",
|
||||
" bucket_region = shell_output[0].strip().lower()\n",
|
||||
" if bucket_region != REGION:\n",
|
||||
" raise ValueError(\n",
|
||||
" \"Bucket region %s is different from notebook region %s\"\n",
|
||||
" % (bucket_region, REGION)\n",
|
||||
" )\n",
|
||||
"print(f\"Using this GCS Bucket: {BUCKET_URI}\")\n",
|
||||
"\n",
|
||||
"STAGING_BUCKET = os.path.join(BUCKET_URI, \"temporal\")\n",
|
||||
"MODEL_BUCKET = os.path.join(BUCKET_URI, \"qwen2\")\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"# Initialize Vertex AI API.\n",
|
||||
"print(\"Initializing Vertex AI API.\")\n",
|
||||
"aiplatform.init(project=PROJECT_ID, location=REGION, staging_bucket=STAGING_BUCKET)\n",
|
||||
"\n",
|
||||
"# Gets the default SERVICE_ACCOUNT.\n",
|
||||
"shell_output = ! gcloud projects describe $PROJECT_ID\n",
|
||||
"project_number = shell_output[-1].split(\":\")[1].strip().replace(\"'\", \"\")\n",
|
||||
"SERVICE_ACCOUNT = f\"{project_number}-compute@developer.gserviceaccount.com\"\n",
|
||||
"print(\"Using this default Service Account:\", SERVICE_ACCOUNT)\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"# Provision permissions to the SERVICE_ACCOUNT with the GCS bucket\n",
|
||||
"! gsutil iam ch serviceAccount:{SERVICE_ACCOUNT}:roles/storage.admin $BUCKET_NAME\n",
|
||||
"\n",
|
||||
"! gcloud config set project $PROJECT_ID\n",
|
||||
"\n",
|
||||
"# The pre-built serving docker images.\n",
|
||||
"VLLM_DOCKER_URI = \"us-docker.pkg.dev/vertex-ai/vertex-vision-model-garden-dockers/pytorch-vllm-serve:20240721_0916_RC00\"\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"def deploy_model_vllm(\n",
|
||||
" model_name: str,\n",
|
||||
" model_id: str,\n",
|
||||
" service_account: str,\n",
|
||||
" base_model_id: str = None,\n",
|
||||
" machine_type: str = \"g2-standard-8\",\n",
|
||||
" accelerator_type: str = \"NVIDIA_L4\",\n",
|
||||
" accelerator_count: int = 1,\n",
|
||||
" gpu_memory_utilization: float = 0.9,\n",
|
||||
" max_model_len: int = 4096,\n",
|
||||
" dtype: str = \"auto\",\n",
|
||||
") -> Tuple[aiplatform.Model, aiplatform.Endpoint]:\n",
|
||||
" \"\"\"Deploys trained models with vLLM into Vertex AI.\"\"\"\n",
|
||||
" endpoint = aiplatform.Endpoint.create(display_name=f\"{model_name}-endpoint\")\n",
|
||||
"\n",
|
||||
" if not base_model_id:\n",
|
||||
" base_model_id = model_id\n",
|
||||
"\n",
|
||||
" vllm_args = [\n",
|
||||
" \"--host=0.0.0.0\",\n",
|
||||
" \"--port=7080\",\n",
|
||||
" f\"--model={model_id}\",\n",
|
||||
" f\"--tensor-parallel-size={accelerator_count}\",\n",
|
||||
" \"--swap-space=16\",\n",
|
||||
" f\"--gpu-memory-utilization={gpu_memory_utilization}\",\n",
|
||||
" f\"--max-model-len={max_model_len}\",\n",
|
||||
" f\"--dtype={dtype}\",\n",
|
||||
" \"--disable-log-stats\",\n",
|
||||
" ]\n",
|
||||
"\n",
|
||||
" env_vars = {\n",
|
||||
" \"MODEL_ID\": base_model_id,\n",
|
||||
" \"DEPLOY_SOURCE\": \"notebook\",\n",
|
||||
" }\n",
|
||||
"\n",
|
||||
" try:\n",
|
||||
" if HF_TOKEN:\n",
|
||||
" env_vars[\"HF_TOKEN\"] = HF_TOKEN\n",
|
||||
" except:\n",
|
||||
" pass\n",
|
||||
"\n",
|
||||
" model = aiplatform.Model.upload(\n",
|
||||
" display_name=model_name,\n",
|
||||
" serving_container_image_uri=VLLM_DOCKER_URI,\n",
|
||||
" serving_container_command=[\"python\", \"-m\", \"vllm.entrypoints.api_server\"],\n",
|
||||
" serving_container_args=vllm_args,\n",
|
||||
" serving_container_ports=[7080],\n",
|
||||
" serving_container_predict_route=\"/generate\",\n",
|
||||
" serving_container_health_route=\"/ping\",\n",
|
||||
" serving_container_environment_variables=env_vars,\n",
|
||||
" serving_container_shared_memory_size_mb=(16 * 1024), # 16 GB\n",
|
||||
" serving_container_deployment_timeout=7200,\n",
|
||||
" )\n",
|
||||
" print(\n",
|
||||
" f\"Deploying {model_name} on {machine_type} with {accelerator_count} {accelerator_type} GPU(s).\"\n",
|
||||
" )\n",
|
||||
" model.deploy(\n",
|
||||
" endpoint=endpoint,\n",
|
||||
" machine_type=machine_type,\n",
|
||||
" accelerator_type=accelerator_type,\n",
|
||||
" accelerator_count=accelerator_count,\n",
|
||||
" deploy_request_timeout=1800,\n",
|
||||
" service_account=service_account,\n",
|
||||
" )\n",
|
||||
" print(\"endpoint_name:\", endpoint.name)\n",
|
||||
"\n",
|
||||
" return model, endpoint"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "z9UuiysLu_gB"
|
||||
},
|
||||
"source": [
|
||||
"## Deploy prebuilt Qwen2 models on vLLM"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"cellView": "form",
|
||||
"id": "USB7dvYqvNdu"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# @title Deploy\n",
|
||||
"\n",
|
||||
"# @markdown This section uploads prebuilt Qwen2 models to Model Registry and deploys it to a Vertex AI Endpoint. It takes 15 to 30 minutes to finish depending on the size of the model.\n",
|
||||
"\n",
|
||||
"MODEL_ID = \"Qwen2-0.5B-Instruct\" # @param [\"Qwen2-0.5B-Instruct\", \"Qwen2-1.5B-Instruct\", \"Qwen2-7B-Instruct\"] {isTemplate: true}\n",
|
||||
"model_path_prefix = \"Qwen\"\n",
|
||||
"model_id = os.path.join(model_path_prefix, MODEL_ID)\n",
|
||||
"\n",
|
||||
"accelerator_type = \"NVIDIA_L4\" # @param [\"NVIDIA_L4\"] {isTemplate: true}\n",
|
||||
"vllm_dtype = \"bfloat16\"\n",
|
||||
"gpu_memory_utilization = 0.85\n",
|
||||
"\n",
|
||||
"if \"0.5B\" in MODEL_ID or \"1.5B\" in MODEL_ID:\n",
|
||||
" if accelerator_type == \"NVIDIA_L4\":\n",
|
||||
" # Sets 1 L4 (24G) to deploy Qwen2-0.5B and Qwen2-1.5B models.\n",
|
||||
" accelerator_count = 1\n",
|
||||
" # Sets machine type to g2-standard-12 for 1 L4\n",
|
||||
" machine_type = \"g2-standard-12\"\n",
|
||||
" else:\n",
|
||||
" raise ValueError(\n",
|
||||
" \"Recommended machine settings not found for accelerator type: %s\"\n",
|
||||
" % accelerator_type\n",
|
||||
" )\n",
|
||||
"elif \"7B\" in MODEL_ID:\n",
|
||||
" if accelerator_type == \"NVIDIA_L4\":\n",
|
||||
" # Sets 2 L4 (24G) to deploy Qwen2-7B model.\n",
|
||||
" accelerator_count = 2\n",
|
||||
" # Sets machine type to g2-standard-24 for 2 L4's\n",
|
||||
" machine_type = \"g2-standard-24\"\n",
|
||||
" else:\n",
|
||||
" raise ValueError(\n",
|
||||
" \"Recommended machine settings not found for accelerator type: %s\"\n",
|
||||
" % accelerator_type\n",
|
||||
" )\n",
|
||||
"else:\n",
|
||||
" raise ValueError(\"Invalid model id: %s\" % MODEL_ID)\n",
|
||||
"\n",
|
||||
"# Sets max model length dependent on context length in model ID\n",
|
||||
"if \"0.5B\" in MODEL_ID or \"1.5B\" in MODEL_ID:\n",
|
||||
" max_model_len = 32768\n",
|
||||
"elif \"7B\" in MODEL_ID:\n",
|
||||
" max_model_len = 131072\n",
|
||||
"else:\n",
|
||||
" raise ValueError(\"Invalid model id: %s\" % MODEL_ID)\n",
|
||||
"\n",
|
||||
"common_util.check_quota(\n",
|
||||
" project_id=PROJECT_ID,\n",
|
||||
" region=REGION,\n",
|
||||
" accelerator_type=accelerator_type,\n",
|
||||
" accelerator_count=accelerator_count,\n",
|
||||
" is_for_training=False,\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"models[\"vllm_gpu\"], endpoints[\"vllm_gpu\"] = deploy_model_vllm(\n",
|
||||
" model_name=common_util.get_job_name_with_datetime(prefix=MODEL_ID),\n",
|
||||
" model_id=model_id,\n",
|
||||
" service_account=SERVICE_ACCOUNT,\n",
|
||||
" machine_type=machine_type,\n",
|
||||
" accelerator_type=accelerator_type,\n",
|
||||
" accelerator_count=accelerator_count,\n",
|
||||
" max_model_len=max_model_len,\n",
|
||||
" gpu_memory_utilization=gpu_memory_utilization,\n",
|
||||
" dtype=vllm_dtype,\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"# @markdown Click \"Show Code\" to see more details."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"cellView": "form",
|
||||
"id": "Aa4e1-6FvRAP"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# @title Predict\n",
|
||||
"\n",
|
||||
"# @markdown Once deployment succeeds, you can send requests to the endpoint with text prompts. Sampling parameters supported by vLLM can be found [here](https://docs.vllm.ai/en/latest/dev/sampling_params.html).\n",
|
||||
"\n",
|
||||
"# @markdown Example:\n",
|
||||
"\n",
|
||||
"# @markdown ```\n",
|
||||
"# @markdown Human: What is a car?\n",
|
||||
"# @markdown Assistant: A car, or a motor car, is a road-connected human-transportation system used to move people or goods from one place to another. The term also encompasses a wide range of vehicles, including motorboats, trains, and aircrafts. Cars typically have four wheels, a cabin for passengers, and an engine or motor. They have been around since the early 19th century and are now one of the most popular forms of transportation, used for daily commuting, shopping, and other purposes.\n",
|
||||
"# @markdown ```\n",
|
||||
"# @markdown Additionally, you can moderate the generated text with Vertex AI. See [Moderate text documentation](https://cloud.google.com/natural-language/docs/moderating-text) for more details.\n",
|
||||
"\n",
|
||||
"# Loads an existing endpoint instance using the endpoint name:\n",
|
||||
"# - Using `endpoint_name = endpoint.name` allows us to get the\n",
|
||||
"# endpoint name of the endpoint `endpoint` created in the cell\n",
|
||||
"# above.\n",
|
||||
"# - Alternatively, you can set `endpoint_name = \"1234567890123456789\"` to load\n",
|
||||
"# an existing endpoint with the ID 1234567890123456789.\n",
|
||||
"# You may uncomment the code below to load an existing endpoint.\n",
|
||||
"\n",
|
||||
"# endpoint_name = \"\" # @param {type:\"string\"}\n",
|
||||
"# aip_endpoint_name = (\n",
|
||||
"# f\"projects/{PROJECT_ID}/locations/{REGION}/endpoints/{endpoint_name}\"\n",
|
||||
"# )\n",
|
||||
"# endpoint = aiplatform.Endpoint(aip_endpoint_name)\n",
|
||||
"\n",
|
||||
"prompt = \"What is a car?\" # @param {type: \"string\"}\n",
|
||||
"# @markdown If you encounter the issue like `ServiceUnavailable: 503 Took too long to respond when processing`, you can reduce the maximum number of output tokens, such as set `max_tokens` as 20.\n",
|
||||
"max_tokens = 50 # @param {type:\"integer\"}\n",
|
||||
"temperature = 1.0 # @param {type:\"number\"}\n",
|
||||
"top_p = 1.0 # @param {type:\"number\"}\n",
|
||||
"top_k = 1 # @param {type:\"integer\"}\n",
|
||||
"raw_response = False # @param {type:\"boolean\"}\n",
|
||||
"\n",
|
||||
"# Overrides parameters for inferences.\n",
|
||||
"instances = [\n",
|
||||
" {\n",
|
||||
" \"prompt\": prompt,\n",
|
||||
" \"max_tokens\": max_tokens,\n",
|
||||
" \"temperature\": temperature,\n",
|
||||
" \"top_p\": top_p,\n",
|
||||
" \"top_k\": top_k,\n",
|
||||
" \"raw_response\": raw_response,\n",
|
||||
" },\n",
|
||||
"]\n",
|
||||
"response = endpoints[\"vllm_gpu\"].predict(instances=instances)\n",
|
||||
"\n",
|
||||
"for prediction in response.predictions:\n",
|
||||
" print(prediction)\n",
|
||||
"\n",
|
||||
"# @markdown Click \"Show Code\" to see more details."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "tAelDidov5AW"
|
||||
},
|
||||
"source": [
|
||||
"## Clean up resources"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"cellView": "form",
|
||||
"id": "8SeZCFo5v7z-"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# @title Delete the models and endpoints\n",
|
||||
"# @markdown Delete the experiment models and endpoints to recycle the resources\n",
|
||||
"# @markdown and avoid unnecessary continuous charges that may incur.\n",
|
||||
"\n",
|
||||
"# Undeploy model and delete endpoint.\n",
|
||||
"for endpoint in endpoints.values():\n",
|
||||
" endpoint.delete(force=True)\n",
|
||||
"\n",
|
||||
"# Delete models.\n",
|
||||
"for model in models.values():\n",
|
||||
" model.delete()\n",
|
||||
"\n",
|
||||
"delete_bucket = False # @param {type:\"boolean\"}\n",
|
||||
"if delete_bucket:\n",
|
||||
" ! gsutil -m rm -r $BUCKET_NAME"
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"colab": {
|
||||
"name": "model_garden_pytorch_qwen2_deployment.ipynb",
|
||||
"toc_visible": true
|
||||
},
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3",
|
||||
"name": "python3"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 0
|
||||
}
|
||||
@@ -1,291 +0,0 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "Ug_ZXeBdbFI4"
|
||||
},
|
||||
"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."
|
||||
]
|
||||
},
|
||||
{
|
||||
"attachments": {},
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "Jr2jRuqabG1m"
|
||||
},
|
||||
"source": [
|
||||
"# Vertex AI Model Garden - Text To Video\n",
|
||||
"\n",
|
||||
"<table align=\"left\"><tbody><tr>\n",
|
||||
" <td>\n",
|
||||
" <a href=\"https://console.cloud.google.com/vertex-ai/colab/import/https:%2F%2Fraw.githubusercontent.com%2FGoogleCloudPlatform%2Fvertex-ai-samples%2Fmain%2Fnotebooks%2Fcommunity%2Fmodel_garden%2Fmodel_garden_pytorch_text_to_video.ipynb\">\n",
|
||||
" <img alt=\"Google Cloud Colab Enterprise logo\" src=\"https://lh3.googleusercontent.com/JmcxdQi-qOpctIvWKgPtrzZdJJK-J3sWE1RsfjZNwshCFgE_9fULcNpuXYTilIR2hjwN\" width=\"32px\"><br> Run in Colab Enterprise\n",
|
||||
" </a>\n",
|
||||
" </td>\n",
|
||||
" <td>\n",
|
||||
" <a href=\"https://github.com/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/community/model_garden/model_garden_pytorch_text_to_video.ipynb\">\n",
|
||||
" <img src=\"https://cloud.google.com/ml-engine/images/github-logo-32px.png\" alt=\"GitHub logo\"><br>\n",
|
||||
" View on GitHub\n",
|
||||
" </a>\n",
|
||||
" </td>\n",
|
||||
"</tr></tbody></table>"
|
||||
]
|
||||
},
|
||||
{
|
||||
"attachments": {},
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "wLLfRT_6bTZO"
|
||||
},
|
||||
"source": [
|
||||
"## Overview\n",
|
||||
"\n",
|
||||
"This notebook demonstrates deploying the pre-trained [Text To Video](https://huggingface.co/docs/diffusers/main/en/api/pipelines/text_to_video) model on Vertex AI for online prediction.\n",
|
||||
"\n",
|
||||
"### Objective\n",
|
||||
"\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 text-to-video.\n",
|
||||
"\n",
|
||||
"### 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": "c8b29e68bb87"
|
||||
},
|
||||
"source": [
|
||||
"## Run the notebook"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"cellView": "form",
|
||||
"id": "dbf16ae5574d"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# @title Setup Google Cloud project\n",
|
||||
"\n",
|
||||
"# @markdown 1. [Make sure that billing is enabled for your project](https://cloud.google.com/billing/docs/how-to/modify-project).\n",
|
||||
"\n",
|
||||
"# @markdown 2. [Optional] [Create a Cloud Storage bucket](https://cloud.google.com/storage/docs/creating-buckets) for storing experiment outputs. Set the BUCKET_URI for the experiment environment. The specified Cloud Storage bucket (`BUCKET_URI`) should be located in the same region as where the notebook was launched. Note that a multi-region bucket (eg. \"us\") is not considered a match for a single region covered by the multi-region range (eg. \"us-central1\"). If not set, a unique GCS bucket will be created instead.\n",
|
||||
"\n",
|
||||
"import os\n",
|
||||
"import sys\n",
|
||||
"import uuid\n",
|
||||
"from datetime import datetime\n",
|
||||
"\n",
|
||||
"from google.cloud import aiplatform\n",
|
||||
"from IPython.display import HTML\n",
|
||||
"\n",
|
||||
"# Get the default cloud project id.\n",
|
||||
"PROJECT_ID = os.environ[\"GOOGLE_CLOUD_PROJECT\"]\n",
|
||||
"\n",
|
||||
"# Get the default region for launching jobs.\n",
|
||||
"REGION = os.environ[\"GOOGLE_CLOUD_REGION\"]\n",
|
||||
"\n",
|
||||
"# Enable the Vertex AI API and Compute Engine API, if not already.\n",
|
||||
"! gcloud services enable aiplatform.googleapis.com compute.googleapis.com\n",
|
||||
"\n",
|
||||
"# Cloud Storage bucket for storing the experiment artifacts.\n",
|
||||
"# A unique GCS bucket will be created for the purpose of this notebook. If you\n",
|
||||
"# prefer using your own GCS bucket, please change the value yourself below.\n",
|
||||
"now = datetime.now().strftime(\"%Y%m%d%H%M%S\")\n",
|
||||
"BUCKET_URI = \"gs://\" # @param {type: \"string\"}\n",
|
||||
"BUCKET_NAME = \"/\".join(BUCKET_URI.split(\"/\")[:3])\n",
|
||||
"\n",
|
||||
"# Create a unique GCS bucket for this notebook, if not specified by the user.\n",
|
||||
"if BUCKET_URI is None or BUCKET_URI.strip() == \"\" or BUCKET_URI == \"gs://\":\n",
|
||||
" BUCKET_URI = f\"gs://{PROJECT_ID}-tmp-{now}-{str(uuid.uuid4())[:4]}\"\n",
|
||||
" BUCKET_NAME = \"/\".join(BUCKET_URI.split(\"/\")[:3])\n",
|
||||
" ! gsutil mb -l {REGION} {BUCKET_URI}\n",
|
||||
"else:\n",
|
||||
" assert BUCKET_URI.startswith(\"gs://\"), \"BUCKET_URI must start with `gs://`.\"\n",
|
||||
" shell_output = ! gsutil ls -Lb {BUCKET_NAME} | grep \"Location constraint:\" | sed \"s/Location constraint://\"\n",
|
||||
" bucket_region = shell_output[0].strip().lower()\n",
|
||||
" if bucket_region != REGION:\n",
|
||||
" raise ValueError(\n",
|
||||
" \"Bucket region %s is different from notebook region %s\"\n",
|
||||
" % (bucket_region, REGION)\n",
|
||||
" )\n",
|
||||
"\n",
|
||||
"print(f\"Using this GCS Bucket: {BUCKET_URI}\")\n",
|
||||
"\n",
|
||||
"# Set up the default SERVICE_ACCOUNT.\n",
|
||||
"SERVICE_ACCOUNT = None\n",
|
||||
"shell_output = ! gcloud projects describe $PROJECT_ID\n",
|
||||
"project_number = shell_output[-1].split(\":\")[1].strip().replace(\"'\", \"\")\n",
|
||||
"SERVICE_ACCOUNT = f\"{project_number}-compute@developer.gserviceaccount.com\"\n",
|
||||
"\n",
|
||||
"print(\"Using this default Service Account:\", SERVICE_ACCOUNT)\n",
|
||||
"\n",
|
||||
"# Provision permissions to the SERVICE_ACCOUNT with the GCS bucket\n",
|
||||
"! gsutil iam ch serviceAccount:{SERVICE_ACCOUNT}:roles/storage.admin $BUCKET_NAME\n",
|
||||
"\n",
|
||||
"aiplatform.init(project=PROJECT_ID, location=REGION, staging_bucket=BUCKET_URI)\n",
|
||||
"\n",
|
||||
"if \"google.colab\" in sys.modules:\n",
|
||||
" from google.colab import auth\n",
|
||||
"\n",
|
||||
" auth.authenticate_user(project_id=PROJECT_ID)\n",
|
||||
"\n",
|
||||
"# The pre-built serving docker image. It contains serving scripts and models.\n",
|
||||
"SERVE_DOCKER_URI = \"us-docker.pkg.dev/vertex-ai/vertex-vision-model-garden-dockers/pytorch-diffusers-serve-opt:20240605_1400_RC00\"\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"def deploy_model(model_id, task):\n",
|
||||
" model_name = \"text-to-video\"\n",
|
||||
" endpoint = aiplatform.Endpoint.create(display_name=f\"{model_name}-endpoint\")\n",
|
||||
" serving_env = {\n",
|
||||
" \"MODEL_ID\": model_id,\n",
|
||||
" \"TASK\": task,\n",
|
||||
" \"DEPLOY_SOURCE\": \"notebook\",\n",
|
||||
" }\n",
|
||||
" model = aiplatform.Model.upload(\n",
|
||||
" display_name=model_name,\n",
|
||||
" serving_container_image_uri=SERVE_DOCKER_URI,\n",
|
||||
" serving_container_ports=[7080],\n",
|
||||
" serving_container_predict_route=\"/predictions/diffusers_serving\",\n",
|
||||
" serving_container_health_route=\"/ping\",\n",
|
||||
" serving_container_environment_variables=serving_env,\n",
|
||||
" )\n",
|
||||
" model.deploy(\n",
|
||||
" endpoint=endpoint,\n",
|
||||
" machine_type=\"g2-standard-8\",\n",
|
||||
" accelerator_type=\"NVIDIA_L4\",\n",
|
||||
" accelerator_count=1,\n",
|
||||
" deploy_request_timeout=1800,\n",
|
||||
" )\n",
|
||||
" return model, endpoint"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"cellView": "form",
|
||||
"id": "fc3a118f0927"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# @title Upload and deploy models\n",
|
||||
"\n",
|
||||
"# @markdown This section uploads the model to Model Registry and deploys it on the Endpoint. It takes ~15 minutes to finish.\n",
|
||||
"# @markdown Click \"Show Code\" to see more details.\n",
|
||||
"\n",
|
||||
"model, endpoint = deploy_model(\n",
|
||||
" model_id=\"damo-vilab/text-to-video-ms-1.7b\", task=\"text-to-video\"\n",
|
||||
")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"cellView": "form",
|
||||
"id": "a927dfe74a71"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# @title Predict\n",
|
||||
"\n",
|
||||
"# @markdown Once deployment succeeds, you can send requests to the endpoint with text prompts to generate videos.\n",
|
||||
"\n",
|
||||
"# @markdown When deployed on one L4 GPU (the default machine type), the averaged inference time of a request is ~15 seconds.\n",
|
||||
"\n",
|
||||
"# @markdown Example:\n",
|
||||
"\n",
|
||||
"# @markdown ```\n",
|
||||
"# @markdown Prompt: Spiderman is surfing\n",
|
||||
"# @markdown ```\n",
|
||||
"\n",
|
||||
"# @markdown You may adjust the parameters below to achieve best video quality.\n",
|
||||
"\n",
|
||||
"prompt = \"Spiderman is surfing\" # @param {type: \"string\"}\n",
|
||||
"number_inference_steps = 25 # @param {type:\"number\"}\n",
|
||||
"\n",
|
||||
"instances = [\n",
|
||||
" {\"prompt\": prompt, \"number_inference_steps\": number_inference_steps},\n",
|
||||
"]\n",
|
||||
"response = endpoint.predict(instances=instances)\n",
|
||||
"\n",
|
||||
"html = \"\"\n",
|
||||
"for video in response.predictions:\n",
|
||||
" html += \"<video controls>\"\n",
|
||||
" html += f'<source src=\"data:video/mp4;base64,{video}\" type=\"video/mp4\">'\n",
|
||||
" html += \"</video>\"\n",
|
||||
"HTML(html)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"cellView": "form",
|
||||
"id": "2ccf3714dbe9"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# @title Clean up resources\n",
|
||||
"\n",
|
||||
"# @markdown Delete the experiment models and endpoints to recycle the resources\n",
|
||||
"# @markdown and avoid unnecessary continouous charges that may incur.\n",
|
||||
"\n",
|
||||
"try:\n",
|
||||
" # Undeploy model and delete endpoint.\n",
|
||||
" endpoint.delete(force=True)\n",
|
||||
"\n",
|
||||
" # Delete model.\n",
|
||||
" model.delete()\n",
|
||||
"\n",
|
||||
"except Exception as e:\n",
|
||||
" print(e)\n",
|
||||
"\n",
|
||||
"# Delete bucket.\n",
|
||||
"delete_bucket = False # @param {type:\"boolean\"}\n",
|
||||
"if delete_bucket:\n",
|
||||
" ! gsutil -m rm -r $BUCKET_NAME"
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"colab": {
|
||||
"name": "model_garden_pytorch_text_to_video.ipynb",
|
||||
"toc_visible": true
|
||||
},
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3",
|
||||
"name": "python3"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 0
|
||||
}
|
||||
@@ -402,8 +402,9 @@
|
||||
" detection_endpoint=None,\n",
|
||||
" label_map=None,\n",
|
||||
" output_bucket=None,\n",
|
||||
" model_type=\"CUSTOM\",\n",
|
||||
" model_type=\"MODEL_GARDEN\",\n",
|
||||
" save_video_results=1,\n",
|
||||
" downscale_factor=1.0,\n",
|
||||
"):\n",
|
||||
" \"\"\"\n",
|
||||
" Deploy a model to a real-time prediction endpoint.\n",
|
||||
@@ -413,6 +414,9 @@
|
||||
" label_map: Mapping of class IDs to class names.\n",
|
||||
" output_bucket: GCS bucket to save results.\n",
|
||||
" save_video_results: Whether to save video results.\n",
|
||||
" downscale_factor: A float representing the degree to which the image\n",
|
||||
" should be downscaled before making an IDO prediction.\n",
|
||||
" For example, values can include 1.0, 0.5, or 0.25.\n",
|
||||
"\n",
|
||||
" Returns:\n",
|
||||
" The created endpoint and deployed model objects.\n",
|
||||
@@ -428,6 +432,7 @@
|
||||
" \"OUTPUT_BUCKET\": output_bucket,\n",
|
||||
" \"SAVE_VIDEO_RESULTS\": save_video_results,\n",
|
||||
" \"DEPLOY_SOURCE\": \"notebook\",\n",
|
||||
" \"DOWNSCALE_FACTOR\": downscale_factor,\n",
|
||||
" }\n",
|
||||
" model = aiplatform.Model.upload(\n",
|
||||
" display_name=task,\n",
|
||||
@@ -530,6 +535,7 @@
|
||||
"-e LABEL_MAP=f\"{ENDPOINT_LABEL_MAP}\" \\\n",
|
||||
"-e OUTPUT_BUCKET=f\"gs://{GCS_BUCKET}/{OUTPUT_GCS_FOLDER}\" \\\n",
|
||||
"-e SAVE_VIDEO_RESULTS=1 \\\n",
|
||||
"-e DOWNSCALE_FACTOR=1.0 \\\n",
|
||||
"-e CUDA_VISIBLE_DEVICES=0 \\\n",
|
||||
"{SERVE_DOCKER_URI}"
|
||||
]
|
||||
@@ -572,6 +578,7 @@
|
||||
"-e DETECTION_ENDPOINT=f\"{DETECTION_ENDPOINT}\" \\\n",
|
||||
"-e OUTPUT_BUCKET=f\"gs://{GCS_BUCKET}/{OUTPUT_GCS_FOLDER}\" \\\n",
|
||||
"-e SAVE_VIDEO_RESULTS=1 \\\n",
|
||||
"-e DOWNSCALE_FACTOR=1.0 \\\n",
|
||||
"-e CUDA_VISIBLE_DEVICES=0 \\\n",
|
||||
"{SERVE_DOCKER_URI}"
|
||||
]
|
||||
|
||||
@@ -29,26 +29,29 @@
|
||||
"id": "JAPoU8Sm5E6e"
|
||||
},
|
||||
"source": [
|
||||
"# Vertex AI SDK for Python: AutoML Tabular training and prediction\n",
|
||||
"# Vertex AI SDK for Python: AutoML tabular training and prediction\n",
|
||||
"\n",
|
||||
"<table align=\"left\">\n",
|
||||
" <td>\n",
|
||||
" <td style=\"text-align: center\">\n",
|
||||
" <a href=\"https://colab.research.google.com/github/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/official/automl/automl-tabular-classification.ipynb\">\n",
|
||||
" <img src=\"https://cloud.google.com/ml-engine/images/colab-logo-32px.png\" alt=\"Colab logo\"> Run in Colab\n",
|
||||
" <img src=\"https://cloud.google.com/ml-engine/images/colab-logo-32px.png\" alt=\"Google Colaboratory logo\"><br> Open in Colab\n",
|
||||
" </a>\n",
|
||||
" </td>\n",
|
||||
" <td>\n",
|
||||
" <td style=\"text-align: center\">\n",
|
||||
" <a href=\"https://console.cloud.google.com/vertex-ai/colab/import/https:%2F%2Fraw.githubusercontent.com%2FGoogleCloudPlatform%2Fvertex-ai-samples%2Fmain%2Fnotebooks%2Fofficial%2Fautoml%2Fautoml-tabular-classification.ipynb\">\n",
|
||||
" <img src=\"https://cloud.google.com/ml-engine/images/colab-enterprise-logo-32px.png\" alt=\"Google Cloud Colab Enterprise logo\"><br> Open in Colab Enterprise\n",
|
||||
" </a>\n",
|
||||
" </td>\n",
|
||||
" <td style=\"text-align: center\">\n",
|
||||
" <a href=\"https://github.com/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/official/automl/automl-tabular-classification.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/automl/automl-tabular-classification.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",
|
||||
" <img src=\"https://cloud.google.com/ml-engine/images/github-logo-32px.png\" alt=\"GitHub logo\"><br> View on GitHub\n",
|
||||
" </a>\n",
|
||||
" </td>\n",
|
||||
" <td style=\"text-align: center\">\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/automl/automl-tabular-classification.ipynb\" target='_blank'>\n",
|
||||
" <img src=\"https://lh3.googleusercontent.com/UiNooY4LUgW_oTvpsNhPpQzsstV5W8F7rYgxgGBD85cWJoLmrOzhVs_ksK_vgx40SHs7jCqkTkCk=e14-rj-sc0xffffff-h130-w32\" alt=\"Vertex AI logo\"><br> Open in Vertex AI Workbench\n",
|
||||
" </a>\n",
|
||||
" </td>\n",
|
||||
"</table>\n",
|
||||
"<br/><br/><br/>"
|
||||
]
|
||||
@@ -76,7 +79,7 @@
|
||||
"source": [
|
||||
"### Objective\n",
|
||||
"\n",
|
||||
"In this tutorial, you learn how to train and make predictions on an AutoML model based on a tabular dataset. Alternatively, you can train and make predictions on models by using the `gcloud` command-line tool or by using the online Cloud Console.\n",
|
||||
"In this tutorial, you learn how to train and make predictions on an AutoML model based on a tabular dataset. Alternatively, you can train and make predictions on models by using the `gcloud` command-line tool or by using the Google Cloud Console.\n",
|
||||
"\n",
|
||||
"This tutorial uses the following Google Cloud ML services and resources:\n",
|
||||
"\n",
|
||||
@@ -116,11 +119,9 @@
|
||||
"* Vertex AI\n",
|
||||
"* Cloud Storage\n",
|
||||
"\n",
|
||||
"Learn about [Vertex AI\n",
|
||||
"pricing](https://cloud.google.com/vertex-ai/pricing) and [Cloud Storage\n",
|
||||
"pricing](https://cloud.google.com/storage/pricing), and use the [Pricing\n",
|
||||
"Calculator](https://cloud.google.com/products/calculator/)\n",
|
||||
"to generate a cost estimate based on your projected usage."
|
||||
"Learn about [Vertex AI pricing](https://cloud.google.com/vertex-ai/pricing) and \n",
|
||||
"[Cloud Storage pricing](https://cloud.google.com/storage/pricing), and use the \n",
|
||||
"[Pricing Calculator](https://cloud.google.com/products/calculator/) to generate a cost estimate based on your projected usage."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -150,135 +151,89 @@
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "restart"
|
||||
"id": "5eec42e37bcf"
|
||||
},
|
||||
"source": [
|
||||
"### Colab only: Uncomment the following cell to restart the kernel"
|
||||
"### Restart runtime (Colab only)\n",
|
||||
"To use the newly installed packages, you must restart the runtime on Google Colab."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "D-ZBOjErv5mM"
|
||||
"id": "dcc98768955f"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Automatically restart kernel after installs so that your environment can access the new packages\n",
|
||||
"# 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": "yfEglUHQk9S3"
|
||||
"id": "4de1bd77992b"
|
||||
},
|
||||
"source": [
|
||||
"## Before you begin\n",
|
||||
"\n",
|
||||
"### 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)"
|
||||
"<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>"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "befa6ca14bc0"
|
||||
},
|
||||
"source": [
|
||||
"### Authenticate your notebook environment (Colab only)\n",
|
||||
"Authenticate your environment on Google Colab."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "set_project_id"
|
||||
"id": "7de6ef0fac42"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"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": "80b8daedb2c6"
|
||||
},
|
||||
"source": [
|
||||
"### Set Google Cloud project information\n",
|
||||
"To get started using Vertex AI, you must have an existing Google Cloud project. 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": "575f9339da1d"
|
||||
},
|
||||
"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": "gcp_authenticate"
|
||||
},
|
||||
"source": [
|
||||
"### Authenticate your Google Cloud account\n",
|
||||
"\n",
|
||||
"Depending on your Jupyter environment, you may have to manually authenticate. Follow the relevant instructions below.\n",
|
||||
"\n",
|
||||
"**1. Vertex AI Workbench**\n",
|
||||
"* Do nothing as you are already authenticated.\n",
|
||||
"\n",
|
||||
"**2. Local JupyterLab instance, uncomment and run:**"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "ce6043da7b33"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# ! gcloud auth login"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "0367eac06a10"
|
||||
},
|
||||
"source": [
|
||||
"**3. Colab, uncomment and run:**"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "21ad4dbb4a61"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# from google.colab import auth\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."
|
||||
"LOCATION = \"us-central1\" # @param {type:\"string\"}"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -300,7 +255,9 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"BUCKET_URI = f\"gs://your-bucket-name-{PROJECT_ID}-unique\" # @param {type:\"string\"}"
|
||||
"BUCKET_URI = (\n",
|
||||
" f\"gs://your-bucket-name-unique-{PROJECT_ID}-unique\" # @param {type:\"string\"}\n",
|
||||
")"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -320,7 +277,7 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"! gsutil mb -l $REGION $BUCKET_URI"
|
||||
"! gsutil mb -l $LOCATION $BUCKET_URI"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -365,11 +322,9 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import os\n",
|
||||
"\n",
|
||||
"from google.cloud import aiplatform\n",
|
||||
"\n",
|
||||
"aiplatform.init(project=PROJECT_ID, location=REGION)"
|
||||
"aiplatform.init(project=PROJECT_ID, location=LOCATION)"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -380,7 +335,7 @@
|
||||
"source": [
|
||||
"## Tutorial\n",
|
||||
"\n",
|
||||
"Now you are ready to create your AutoML Tabular model."
|
||||
"Now you're ready to create your AutoML Tabular model."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -389,7 +344,7 @@
|
||||
"id": "8f4f50a0112c"
|
||||
},
|
||||
"source": [
|
||||
"### Create a Managed Tabular dataset from a CSV\n",
|
||||
"### Create a Managed tabular dataset from a CSV\n",
|
||||
"\n",
|
||||
"This section creates a dataset from a CSV file stored on your GCS bucket."
|
||||
]
|
||||
@@ -416,7 +371,7 @@
|
||||
"id": "ba5011d50ac7"
|
||||
},
|
||||
"source": [
|
||||
"### Launch a training job to create a Model\n",
|
||||
"### Launch a training job to create a model\n",
|
||||
"\n",
|
||||
"Once you've defined your training script, you'll create a model. The `run` function creates a training pipeline that trains and creates a model object. After the training pipeline completes, the `run` function returns the model object."
|
||||
]
|
||||
@@ -605,7 +560,7 @@
|
||||
"# Delete the endpoint\n",
|
||||
"endpoint.delete()\n",
|
||||
"\n",
|
||||
"if delete_bucket or os.getenv(\"IS_TESTING\"):\n",
|
||||
"if delete_bucket:\n",
|
||||
" ! gsutil -m rm -r $BUCKET_URI"
|
||||
]
|
||||
}
|
||||
|
||||
@@ -29,39 +29,30 @@
|
||||
"id": "mThXALJl9Yue"
|
||||
},
|
||||
"source": [
|
||||
"# Tabular Workflow for Forecasting\n",
|
||||
"# AutoML Tabular Workflows for Forecasting\n",
|
||||
"\n",
|
||||
"<table align=\"left\">\n",
|
||||
" <td>\n",
|
||||
" <a href=\"https://colab.research.google.com/github/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/official/automl/automl_tabular_on_vertex_pipelines.ipynb\">\n",
|
||||
" <img src=\"https://cloud.google.com/ml-engine/images/colab-logo-32px.png\" alt=\"Colab logo\"> Run in Colab\n",
|
||||
" <td style=\"text-align: center\">\n",
|
||||
" <a href=\"https://colab.research.google.com/github/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/official/automl/automl_forecasting_on_vertex_pipelines.ipynb\">\n",
|
||||
" <img src=\"https://cloud.google.com/ml-engine/images/colab-logo-32px.png\" alt=\"Google Colaboratory logo\"><br> Open in Colab\n",
|
||||
" </a>\n",
|
||||
" </td>\n",
|
||||
" <td>\n",
|
||||
" <a href=\"https://github.com/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/official/automl/automl_forecasting_on_vertex_pipelines.ipynb\">\n",
|
||||
" <img src=\"https://cloud.google.com/ml-engine/images/github-logo-32px.png\" alt=\"GitHub logo\">\n",
|
||||
" View on GitHub\n",
|
||||
" <td style=\"text-align: center\">\n",
|
||||
" <a href=\"https://console.cloud.google.com/vertex-ai/colab/import/https:%2F%2Fraw.githubusercontent.com%2FGoogleCloudPlatform%2Fvertex-ai-samples%2Fmain%2Fnotebooks%2Fofficial%2Fautoml%2Fautoml_forecasting_on_vertex_pipelines.ipynb\">\n",
|
||||
" <img width=\"32px\" src=\"https://cloud.google.com/ml-engine/images/colab-enterprise-logo-32px.png\" alt=\"Google Cloud Colab Enterprise logo\"><br> Open in Colab Enterprise\n",
|
||||
" </a>\n",
|
||||
" </td>\n",
|
||||
" <td>\n",
|
||||
" </td> \n",
|
||||
" <td style=\"text-align: center\">\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/automl/automl_forecasting_on_vertex_pipelines.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",
|
||||
" <img src=\"https://lh3.googleusercontent.com/UiNooY4LUgW_oTvpsNhPpQzsstV5W8F7rYgxgGBD85cWJoLmrOzhVs_ksK_vgx40SHs7jCqkTkCk=e14-rj-sc0xffffff-h130-w32\" alt=\"Vertex AI logo\"><br> Open in Workbench\n",
|
||||
" </a>\n",
|
||||
" </td>\n",
|
||||
"</table>\n",
|
||||
"<br/><br/><br/>"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "962e636b5cee"
|
||||
},
|
||||
"source": [
|
||||
"**_NOTE_**: This notebook has been tested in the following environment:\n",
|
||||
"\n",
|
||||
"* Python version = 3.9"
|
||||
" <td style=\"text-align: center\">\n",
|
||||
" <a href=\"https://github.com/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/official/automl/automl_forecasting_on_vertex_pipelines.ipynb\">\n",
|
||||
" <img src=\"https://cloud.google.com/ml-engine/images/github-logo-32px.png\" alt=\"GitHub logo\"><br> View on GitHub\n",
|
||||
" </a>\n",
|
||||
" </td>\n",
|
||||
"</table>"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -74,7 +65,11 @@
|
||||
"\n",
|
||||
"This tutorial demonstrates how you can use Vertex AI Tabular Workflow for Forecasting to train an AutoML model. You can choose between the following model types: Time Series Dense Encoder (TiDE), Learn to Learn (L2L), Sequence to Sequence (Seq2Seq+), and Temporal Fusion Transformer (TFT).\n",
|
||||
"\n",
|
||||
"Learn more about [Tabular Workflow for Forecasting](https://cloud.google.com/vertex-ai/docs/tabular-data/tabular-workflows/forecasting)."
|
||||
"Learn more about [Tabular Workflow for Forecasting](https://cloud.google.com/vertex-ai/docs/tabular-data/tabular-workflows/forecasting).\n",
|
||||
"\n",
|
||||
"**_NOTE_**: This notebook has been tested in the following environment:\n",
|
||||
"\n",
|
||||
"* Python version = 3.9"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -83,14 +78,14 @@
|
||||
"id": "8b54ba90629a"
|
||||
},
|
||||
"source": [
|
||||
"### Compared to Vertex Forecasting managed service.\n",
|
||||
"### Advantages of tabular workflows\n",
|
||||
"\n",
|
||||
"Compared to Vertex Forecasting managed service, Tabular Workflow for Forecasting has the following advantages:\n",
|
||||
"1. Composite time series id columns are supported. You can use a combination of multiple columns as the time series id, for example, you can use either `['sku_id']` or `['sku_id', 'store_id']` as the time series id columns.\n",
|
||||
"2. Model architecture search can be skipped. You can reuse the previous model architecture search tuning result to train the model directly.\n",
|
||||
"3. Hardware customization. You can override the machine spec of the tuning and the training step, so that you can tune the training speed. You are also able to control the parallelism of the training process and the number of the final selected trials during the ensemble step.\n",
|
||||
"3. Hardware customization. You can override the machine spec of the tuning and the training step, so that you can tune the training speed. You're also able to control the parallelism of the training process and the number of the final selected trials during the ensemble step.\n",
|
||||
"4. Unlimited time steps support in one single time series. There's no 3000 time steps limit in the training dataset.\n",
|
||||
"5. No upper limit for the training dataset. There's no 100MM rows limit or 100GB limit in dataset size.\n",
|
||||
"5. No upper limit for the training dataset. There's no 100M rows limit or 100GB limit in dataset size.\n",
|
||||
"6. Use all advanced features from the Vertex AI Pipelines."
|
||||
]
|
||||
},
|
||||
@@ -102,9 +97,9 @@
|
||||
"source": [
|
||||
"### Objective\n",
|
||||
"\n",
|
||||
"In this tutorial, you learn how to create AutoML Forecasting models using [Vertex AI Pipelines](https://cloud.google.com/vertex-ai/docs/pipelines/introduction) downloaded from [Google Cloud Pipeline Components](https://cloud.google.com/vertex-ai/docs/pipelines/components-introduction) (GCPC). These pipelines are Vertex AI Tabular Workflow pipelines that are maintained by Google. These pipelines showcases different ways to customize the Vertex AI Tabular training process.\n",
|
||||
"In this tutorial, you learn how to create AutoML forecasting models using [Vertex AI Pipelines](https://cloud.google.com/vertex-ai/docs/pipelines/introduction) downloaded from [Google Cloud Pipeline Components](https://cloud.google.com/vertex-ai/docs/pipelines/components-introduction) (GCPC). These pipelines are Vertex AI Tabular Workflow pipelines that are maintained by Google. These pipelines showcase different ways to customize the Vertex AI Tabular training process.\n",
|
||||
"\n",
|
||||
"This tutorial uses the following Google Cloud ML services:\n",
|
||||
"This tutorial uses the following Vertex AI services:\n",
|
||||
"\n",
|
||||
"- AutoML training\n",
|
||||
"- Vertex AI Pipelines\n",
|
||||
@@ -116,7 +111,7 @@
|
||||
"- Create a training pipeline with Learn-to-learn(L2L) algorithm.\n",
|
||||
"- Create a training pipeline with Seq2seq(Sequence to sequence) algorithm.\n",
|
||||
"- Create a training pipeline with TFT(Temporal Fusion Transformer) algorithm.\n",
|
||||
"- Perform the batch prediction using the trained model in the above steps."
|
||||
"- Perform the batch prediction using the trained model from the above steps."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -147,20 +142,29 @@
|
||||
"\n",
|
||||
"Learn about [Vertex AI\n",
|
||||
"pricing](https://cloud.google.com/vertex-ai/pricing), [Cloud Storage\n",
|
||||
"pricing](https://cloud.google.com/storage/pricing), and [BigQuery](https://cloud.google.com/bigquery), and use the [Pricing\n",
|
||||
"pricing](https://cloud.google.com/storage/pricing), [BigQuery pricing](https://cloud.google.com/bigquery), and [Dataflow pricing](https://cloud.google.com/dataflow/pricing) and use the [Pricing\n",
|
||||
"Calculator](https://cloud.google.com/products/calculator/)\n",
|
||||
"to generate a cost estimate based on your projected usage."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "d1ea81ac77f0"
|
||||
},
|
||||
"source": [
|
||||
"## Get started"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "e85f0288a6df"
|
||||
},
|
||||
"source": [
|
||||
"## Install additional packages\n",
|
||||
"### Install Vertex AI SDK for Python and other required packages\n",
|
||||
"\n",
|
||||
"Install the Google Cloud Pipeline Components (GCPC) SDK not earlier than `2.3.0`.\n"
|
||||
"**Note**: Install the Google Cloud Pipeline Components (GCPC) SDK not earlier than `2.3.0`.\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -181,7 +185,9 @@
|
||||
"id": "Bj5O0S5RTxzY"
|
||||
},
|
||||
"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."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -192,32 +198,53 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Automatically restart kernel after installs so that your environment can access the new packages\n",
|
||||
"# 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": "yfEglUHQk9S3"
|
||||
"id": "c87a2a5d7e35"
|
||||
},
|
||||
"source": [
|
||||
"## Before you begin\n",
|
||||
"<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": "5dccb1c8feb6"
|
||||
},
|
||||
"source": [
|
||||
"### Authenticate your notebook environment (Colab only)\n",
|
||||
"\n",
|
||||
"### Set up your Google Cloud project\n",
|
||||
"Authenticate your environment on Google Colab.\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "cc7251520a07"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import sys\n",
|
||||
"\n",
|
||||
"**The following steps are required, regardless of your notebook environment.**\n",
|
||||
"if \"google.colab\" in sys.modules:\n",
|
||||
"\n",
|
||||
"1. [Select or create a Google Cloud project](https://console.cloud.google.com/cloud-resource-manager).\n",
|
||||
" from google.colab import auth\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=ml.googleapis.com,dataflow.googleapis.com,compute_component,storage-component.googleapis.com).\n",
|
||||
"\n",
|
||||
"4. If you are running this notebook locally, you need to install the [Cloud SDK](https://cloud.google.com/sdk)."
|
||||
" auth.authenticate_user()"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -226,11 +253,11 @@
|
||||
"id": "zebLBGXOky2A"
|
||||
},
|
||||
"source": [
|
||||
"## Notes about service account and permission\n",
|
||||
"### Notes about service account and permission\n",
|
||||
"\n",
|
||||
"For full details of the permission setup, refer to https://cloud.google.com/vertex-ai/docs/tabular-data/tabular-workflows/service-accounts\n",
|
||||
"For full details of the permission setup, refer to [Service accounts for Tabular Workflows](https://cloud.google.com/vertex-ai/docs/tabular-data/tabular-workflows/service-accounts).\n",
|
||||
"\n",
|
||||
"**By default no configuration is required**, if you run into any permission related issue, please make sure the service accounts above have the required roles:\n",
|
||||
"**By default no configuration is required**, if you run into any permission related issue, make sure the service accounts below have the required roles:\n",
|
||||
"\n",
|
||||
"|Service account email|Description|Roles|\n",
|
||||
"|---|---|---|\n",
|
||||
@@ -238,14 +265,21 @@
|
||||
"|service-PROJECT_NUMBER@gcp-sa-aiplatform.iam.gserviceaccount.com|AI Platform Service Agent|Vertex AI Service Agent|\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"1. Goto https://console.cloud.google.com/iam-admin/iam.\n",
|
||||
"1. Go to [IAM console](https://console.cloud.google.com/iam-admin/iam).\n",
|
||||
"2. Check the \"Include Google-provided role grants\" checkbox.\n",
|
||||
"3. Find the above emails.\n",
|
||||
"4. Grant the corresponding roles.\n",
|
||||
"\n",
|
||||
"4. Grant the corresponding roles."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "2ef7eccf0e6a"
|
||||
},
|
||||
"source": [
|
||||
"### Using data source from a different project\n",
|
||||
"- For the BQ data source, grant both service accounts the \"BigQuery Data Viewer\" role.\n",
|
||||
"- For the CSV data source, grant both service accounts the \"Storage Object Viewer\" role.\n"
|
||||
"- For the CSV data source, grant both service accounts the \"Storage Object Viewer\" role."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -254,12 +288,9 @@
|
||||
"id": "95cb7ffd6895"
|
||||
},
|
||||
"source": [
|
||||
"### Set your project ID\n",
|
||||
"### Set Google Cloud project information\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)"
|
||||
"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)."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -271,103 +302,7 @@
|
||||
"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": "b12f508d97c6"
|
||||
},
|
||||
"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": "8e8b7997de7a"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"REGION = \"us-central1\" # @param {type: \"string\"}"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "eu0e2TRVxjHb"
|
||||
},
|
||||
"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": "d118c95af93f"
|
||||
},
|
||||
"source": [
|
||||
"**1. Vertex AI Workbench**\n",
|
||||
"* Do nothing since you're already authenticated."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "3035286fcdda"
|
||||
},
|
||||
"source": [
|
||||
"**2. Local JupyterLab instance, uncomment and run:**"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "455882ec0f11"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# ! gcloud auth login"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "5097f3233d53"
|
||||
},
|
||||
"source": [
|
||||
"**3. Colab, uncomment and run:**"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "2b88e46ac2c8"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# from google.colab import auth\n",
|
||||
"# auth.authenticate_user()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "fcdbb8929927"
|
||||
},
|
||||
"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."
|
||||
"LOCATION = \"us-central1\" # @param {type:\"string\"}"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -409,7 +344,7 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"! gsutil mb -l {REGION} -p {PROJECT_ID} {BUCKET_URI}"
|
||||
"! gsutil mb -l {LOCATION} -p {PROJECT_ID} {BUCKET_URI}"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -439,7 +374,8 @@
|
||||
" or SERVICE_ACCOUNT == \"[your-service-account]\"\n",
|
||||
"):\n",
|
||||
" import sys\n",
|
||||
" IS_COLAB = 'google.colab' in sys.modules\n",
|
||||
"\n",
|
||||
" IS_COLAB = \"google.colab\" in sys.modules\n",
|
||||
"\n",
|
||||
" # Get your service account from gcloud\n",
|
||||
" if not IS_COLAB:\n",
|
||||
@@ -482,7 +418,7 @@
|
||||
"id": "fbbc3479a1da"
|
||||
},
|
||||
"source": [
|
||||
"## Import libraries and define constants"
|
||||
"### Import libraries and define constants"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -510,7 +446,7 @@
|
||||
"id": "c0423f260423"
|
||||
},
|
||||
"source": [
|
||||
"## Initialize Vertex AI SDK for Python\n",
|
||||
"### Initialize Vertex AI SDK for Python\n",
|
||||
"\n",
|
||||
"Initialize the Vertex SDK for Python for your project."
|
||||
]
|
||||
@@ -523,7 +459,7 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"aiplatform.init(project=PROJECT_ID, location=REGION)"
|
||||
"aiplatform.init(project=PROJECT_ID, location=LOCATION)"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -556,7 +492,7 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Dataflow's fully qualified subnetwork name, when empty the default subnetwork will be used.\n",
|
||||
"# Dataflow's fully qualified subnetwork name, when empty the default subnetwork is used.\n",
|
||||
"# Fully qualified subnetwork name is in the form of\n",
|
||||
"# https://www.googleapis.com/compute/v1/projects/HOST_PROJECT_ID/regions/REGION_NAME/subnetworks/SUBNETWORK_NAME\n",
|
||||
"# reference: https://cloud.google.com/dataflow/docs/guides/specifying-networks#example_network_and_subnetwork_specifications\n",
|
||||
@@ -591,7 +527,7 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Below functions will serve as the utility functions.\n",
|
||||
"# Below functions serve as the utility functions.\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"# Fetch the tuple of GCS bucket and object URI.\n",
|
||||
@@ -620,7 +556,7 @@
|
||||
"\n",
|
||||
"\n",
|
||||
"# This is the example to set non-auto transformations.\n",
|
||||
"# For more details about the transformations, please check:\n",
|
||||
"# For more details about the transformations, check:\n",
|
||||
"# https://cloud.google.com/vertex-ai/docs/datasets/data-types-tabular#transformations\n",
|
||||
"def generate_transformation(\n",
|
||||
" auto_column_names: Optional[List[str]] = None,\n",
|
||||
@@ -657,12 +593,15 @@
|
||||
" return task_detail\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"# Retrieve the URI of the model.\n",
|
||||
"def get_deployed_model_uri(\n",
|
||||
"# Retrieve the model resource name\n",
|
||||
"def get_deployed_model_resource(\n",
|
||||
" task_details,\n",
|
||||
"):\n",
|
||||
" ensemble_task = get_task_detail(task_details, \"model-upload\")\n",
|
||||
" return ensemble_task.outputs[\"model\"].artifacts[0].uri\n",
|
||||
" if ensemble_task is None:\n",
|
||||
" ensemble_task = get_task_detail(task_details, \"model-upload-2\")\n",
|
||||
" if ensemble_task:\n",
|
||||
" return ensemble_task.outputs[\"model\"].artifacts[0].metadata[\"resourceName\"]\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"# Retrieve the feature importance details from GCS.\n",
|
||||
@@ -767,10 +706,10 @@
|
||||
"\n",
|
||||
"\n",
|
||||
"Currently, four model types are supported in the APIs/SDK with the utility functions:\n",
|
||||
"1. `time_series_dense_encoder`(`TiDE`): `get_time_series_dense_encoder_forecasting_pipeline_and_parameters`\n",
|
||||
"2. `learn_to_learn`(`L2L`): `get_learn_to_learn_forecasting_pipeline_and_parameters`\n",
|
||||
"3. `sequence_to_sequence`(`seq2seq`): `get_sequence_to_sequence_forecasting_pipeline_and_parameters`\n",
|
||||
"4. `temporal_fusion_transformer`(`TFT`): `get_temporal_fusion_transformer_forecasting_pipeline_and_parameters`"
|
||||
"1. `time_series_dense_encoder`(**TiDE**): `get_time_series_dense_encoder_forecasting_pipeline_and_parameters`\n",
|
||||
"2. `learn_to_learn`(**L2L**): `get_learn_to_learn_forecasting_pipeline_and_parameters`\n",
|
||||
"3. `sequence_to_sequence`(**seq2seq**): `get_sequence_to_sequence_forecasting_pipeline_and_parameters`\n",
|
||||
"4. `temporal_fusion_transformer`(**TFT**): `get_temporal_fusion_transformer_forecasting_pipeline_and_parameters`"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -791,7 +730,7 @@
|
||||
"# Construct a Vertex Pipeline job.\n",
|
||||
"job = aiplatform.PipelineJob(\n",
|
||||
" ...\n",
|
||||
" location=REGION, # launches the pipeline job in the specified region\n",
|
||||
" location=LOCATION, # launches the pipeline job in the specified region\n",
|
||||
" template_path=template_path,\n",
|
||||
" ...\n",
|
||||
" pipeline_root=root_dir,\n",
|
||||
@@ -904,7 +843,7 @@
|
||||
" `bq://project.dataset`. The dataset needs to be created first.\n",
|
||||
" window_predefined_column: The column that indicate the start of each window.\n",
|
||||
" window_stride_length: The stride length to generate the window.\n",
|
||||
" window_max_count: The maximum number of windows that will be generated.\n",
|
||||
" window_max_count: The maximum number of windows that are generated.\n",
|
||||
" holiday_regions: The geographical regions where the holiday effect is\n",
|
||||
" applied in modeling.\n",
|
||||
" stage_1_num_parallel_trials: Number of parallel trails for stage 1.\n",
|
||||
@@ -948,7 +887,7 @@
|
||||
" stage_2_trainer_worker_pool_specs_override: The dictionary for overriding\n",
|
||||
" stage 2 trainer worker pool spec.\n",
|
||||
" enable_probabilistic_inference: If probabilistic inference is enabled, the\n",
|
||||
" model will fit a distribution that captures the uncertainty of a\n",
|
||||
" model fits a distribution that captures the uncertainty of a\n",
|
||||
" prediction. If quantiles are specified, then the quantiles of the\n",
|
||||
" distribution are also returned.\n",
|
||||
" quantiles: Quantiles to use for probabilistic inference. Up to 5 quantiles\n",
|
||||
@@ -960,12 +899,18 @@
|
||||
" run_evaluation: `True` to evaluate the ensembled model on the test split.\n",
|
||||
" \"\"\"\n",
|
||||
" ...\n",
|
||||
"```\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"```"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "6e4af5adfd39"
|
||||
},
|
||||
"source": [
|
||||
"### Use holiday regions\n",
|
||||
"\n",
|
||||
"For some use cases, forecasting data can be affected by holidays in regional areas. See https://cloud.google.com/vertex-ai/docs/tabular-data/tabular-workflows/forecasting-train#holiday-regions for more information on holiday regions supported by forecasting.\n",
|
||||
"For some use cases, forecasting data can be affected by holidays in regional areas. See [model training with Tabular Workflow for forecasting](https://cloud.google.com/vertex-ai/docs/tabular-data/tabular-workflows/forecasting-train#holiday-regions) for more information on holiday regions supported by forecasting.\n",
|
||||
"\n",
|
||||
"Pass in a list of strings `holiday_regions` to the pipeline parameter builder to incorporate holiday data into your training pipeline."
|
||||
]
|
||||
@@ -978,7 +923,7 @@
|
||||
"source": [
|
||||
"## Customize the training configurations\n",
|
||||
"\n",
|
||||
"You can create a Forecasting pipeline with the following customizations: \n",
|
||||
"You can create a forecasting pipeline with the following customizations: \n",
|
||||
"- Change machine type and tuning / training parallelism\n",
|
||||
"- Skip evaluation\n",
|
||||
"- Skip model architecture search\n",
|
||||
@@ -1056,7 +1001,7 @@
|
||||
"\n",
|
||||
"Time series Dense Encoder (TiDE) is an optimized dense DNN-based encoder-decoder model, which has great model quality with fast training and inference, especially for long contexts and horizons.\n",
|
||||
"\n",
|
||||
"For more details, see https://ai.googleblog.com/2023/04/recent-advances-in-deep-long-horizon.html\n",
|
||||
"For more details, see [Recent advances in deep long-horizon forecasting](https://ai.googleblog.com/2023/04/recent-advances-in-deep-long-horizon.html).\n",
|
||||
"\n",
|
||||
"In this tutorial, run the TiDE training pipeline twice:\n",
|
||||
"1. With model architecture search\n",
|
||||
@@ -1087,14 +1032,14 @@
|
||||
" parameter_values,\n",
|
||||
") = automl_forecasting_utils.get_time_series_dense_encoder_forecasting_pipeline_and_parameters(\n",
|
||||
" project=PROJECT_ID,\n",
|
||||
" location=REGION,\n",
|
||||
" location=LOCATION,\n",
|
||||
" root_dir=root_dir,\n",
|
||||
" target_column=target_column,\n",
|
||||
" # `minimize-quantile-loss`\n",
|
||||
" optimization_objective=optimization_objective,\n",
|
||||
" transformations=transformations,\n",
|
||||
" train_budget_milli_node_hours=train_budget_milli_node_hours,\n",
|
||||
" # Do not set `data_source_csv_filenames` and\n",
|
||||
" # Don't set `data_source_csv_filenames` and\n",
|
||||
" # `data_source_bigquery_table_path` if you want to use Vertex managed\n",
|
||||
" # dataset by commenting out the following two lines.\n",
|
||||
" data_source_csv_filenames=data_source_csv_filenames,\n",
|
||||
@@ -1124,9 +1069,9 @@
|
||||
")\n",
|
||||
"\n",
|
||||
"job_id = \"tide-forecasting-{}\".format(uuid.uuid4())\n",
|
||||
"job = aiplatform.PipelineJob(\n",
|
||||
"architecture_search_pipeline_job = aiplatform.PipelineJob(\n",
|
||||
" display_name=job_id,\n",
|
||||
" location=REGION, # launches the pipeline job in the specified region\n",
|
||||
" location=LOCATION, # launches the pipeline job in the specified region\n",
|
||||
" template_path=template_path,\n",
|
||||
" job_id=job_id,\n",
|
||||
" pipeline_root=root_dir,\n",
|
||||
@@ -1136,10 +1081,11 @@
|
||||
" # input_artifacts={'vertex_dataset': vertex_dataset_artifact_id},\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"job.run(service_account=SERVICE_ACCOUNT)\n",
|
||||
"architecture_search_pipeline_job.run(service_account=SERVICE_ACCOUNT)\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"pipeline_task_details = job.gca_resource.job_detail.task_details"
|
||||
"architecture_search_pipeline_task_details = (\n",
|
||||
" architecture_search_pipeline_job.gca_resource.job_detail.task_details\n",
|
||||
")"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -1148,15 +1094,8 @@
|
||||
"id": "c5F12ZL_uZZ3"
|
||||
},
|
||||
"source": [
|
||||
"### Run the TiDE pipeline without the model architecture search\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "c24aa07ead0a"
|
||||
},
|
||||
"source": [
|
||||
"### Run the TiDE pipeline without the model architecture search\n",
|
||||
"\n",
|
||||
"After retrieving the tuning result from the stage 1 tuner, you can use it to skip the model architecture search."
|
||||
]
|
||||
},
|
||||
@@ -1170,7 +1109,7 @@
|
||||
"source": [
|
||||
"# Retrieve the tuning result output from the previous training pipeline.\n",
|
||||
"stage_1_tuner_task = get_task_detail(\n",
|
||||
" pipeline_task_details, \"automl-forecasting-stage-1-tuner\"\n",
|
||||
" architecture_search_pipeline_task_details, \"automl-forecasting-stage-1-tuner\"\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"stage_1_tuning_result_artifact_uri = (\n",
|
||||
@@ -1184,7 +1123,7 @@
|
||||
" parameter_values,\n",
|
||||
") = automl_forecasting_utils.get_time_series_dense_encoder_forecasting_pipeline_and_parameters(\n",
|
||||
" project=PROJECT_ID,\n",
|
||||
" location=REGION,\n",
|
||||
" location=LOCATION,\n",
|
||||
" root_dir=root_dir,\n",
|
||||
" target_column=target_column,\n",
|
||||
" optimization_objective=optimization_objective,\n",
|
||||
@@ -1214,9 +1153,9 @@
|
||||
")\n",
|
||||
"\n",
|
||||
"job_id = \"tide-forecasting-skip-architecture-search-{}\".format(uuid.uuid4())\n",
|
||||
"job = aiplatform.PipelineJob(\n",
|
||||
"skip_architecture_search_pipeline_job = aiplatform.PipelineJob(\n",
|
||||
" display_name=job_id,\n",
|
||||
" location=REGION, # launches the pipeline job in the specified region\n",
|
||||
" location=LOCATION, # launches the pipeline job in the specified region\n",
|
||||
" template_path=template_path,\n",
|
||||
" job_id=job_id,\n",
|
||||
" pipeline_root=root_dir,\n",
|
||||
@@ -1224,11 +1163,11 @@
|
||||
" enable_caching=False,\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"job.run(service_account=SERVICE_ACCOUNT)\n",
|
||||
"skip_architecture_search_pipeline_job.run(service_account=SERVICE_ACCOUNT)\n",
|
||||
"\n",
|
||||
"# Get model URI\n",
|
||||
"skip_architecture_search_pipeline_task_details = (\n",
|
||||
" job.gca_resource.job_detail.task_details\n",
|
||||
" skip_architecture_search_pipeline_job.gca_resource.job_detail.task_details\n",
|
||||
")"
|
||||
]
|
||||
},
|
||||
@@ -1238,15 +1177,8 @@
|
||||
"id": "xiftLomOwGda"
|
||||
},
|
||||
"source": [
|
||||
"## L2L training\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "2bfe9f2568c7"
|
||||
},
|
||||
"source": [
|
||||
"## L2L training\n",
|
||||
"\n",
|
||||
"Learn-to-Learn (L2L) is a good choice for a wide range of the time series forecasting use cases."
|
||||
]
|
||||
},
|
||||
@@ -1265,7 +1197,7 @@
|
||||
" parameter_values,\n",
|
||||
") = automl_forecasting_utils.get_learn_to_learn_forecasting_pipeline_and_parameters(\n",
|
||||
" project=PROJECT_ID,\n",
|
||||
" location=REGION,\n",
|
||||
" location=LOCATION,\n",
|
||||
" root_dir=root_dir,\n",
|
||||
" target_column=target_column,\n",
|
||||
" optimization_objective=optimization_objective,\n",
|
||||
@@ -1296,9 +1228,9 @@
|
||||
")\n",
|
||||
"\n",
|
||||
"job_id = \"l2l-forecasting-{}\".format(uuid.uuid4())\n",
|
||||
"job = aiplatform.PipelineJob(\n",
|
||||
"l2l_pipeline_job = aiplatform.PipelineJob(\n",
|
||||
" display_name=job_id,\n",
|
||||
" location=REGION, # launches the pipeline job in the specified region\n",
|
||||
" location=LOCATION, # launches the pipeline job in the specified region\n",
|
||||
" template_path=template_path,\n",
|
||||
" job_id=job_id,\n",
|
||||
" pipeline_root=root_dir,\n",
|
||||
@@ -1306,10 +1238,9 @@
|
||||
" enable_caching=False,\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"job.run(service_account=SERVICE_ACCOUNT)\n",
|
||||
"l2l_pipeline_job.run(service_account=SERVICE_ACCOUNT)\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"pipeline_task_details = job.gca_resource.job_detail.task_details"
|
||||
"l2l_pipeline_task_details = l2l_pipeline_job.gca_resource.job_detail.task_details"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -1338,7 +1269,7 @@
|
||||
" parameter_values,\n",
|
||||
") = automl_forecasting_utils.get_sequence_to_sequence_forecasting_pipeline_and_parameters(\n",
|
||||
" project=PROJECT_ID,\n",
|
||||
" location=REGION,\n",
|
||||
" location=LOCATION,\n",
|
||||
" root_dir=root_dir,\n",
|
||||
" target_column=target_column,\n",
|
||||
" optimization_objective=optimization_objective,\n",
|
||||
@@ -1368,9 +1299,9 @@
|
||||
")\n",
|
||||
"\n",
|
||||
"job_id = \"seq2seq-forecasting-{}\".format(uuid.uuid4())\n",
|
||||
"job = aiplatform.PipelineJob(\n",
|
||||
"seq2seq_pipeline_job = aiplatform.PipelineJob(\n",
|
||||
" display_name=job_id,\n",
|
||||
" location=REGION, # launches the pipeline job in the specified region\n",
|
||||
" location=LOCATION, # launches the pipeline job in the specified region\n",
|
||||
" template_path=template_path,\n",
|
||||
" job_id=job_id,\n",
|
||||
" pipeline_root=root_dir,\n",
|
||||
@@ -1378,10 +1309,11 @@
|
||||
" enable_caching=False,\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"job.run(service_account=SERVICE_ACCOUNT)\n",
|
||||
"seq2seq_pipeline_job.run(service_account=SERVICE_ACCOUNT)\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"pipeline_task_details = job.gca_resource.job_detail.task_details"
|
||||
"seq2seq_pipeline_task_details = (\n",
|
||||
" seq2seq_pipeline_job.gca_resource.job_detail.task_details\n",
|
||||
")"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -1394,7 +1326,7 @@
|
||||
"\n",
|
||||
"TFT stands for \"Temporal Fusion Transformer\", which is an attention-based DNN model designed to produce high accuracy and interpretability by aligning the model with the general multi-horizon forecasting task.\n",
|
||||
"\n",
|
||||
"With this model, you don't need to explicitly enable the explanability support during serving to get the feature importance for each feature column."
|
||||
"With this model, you don't need to explicitly enable the explainability support during serving to get the feature importance for each feature column."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -1412,7 +1344,7 @@
|
||||
" parameter_values,\n",
|
||||
") = automl_forecasting_utils.get_temporal_fusion_transformer_forecasting_pipeline_and_parameters(\n",
|
||||
" project=PROJECT_ID,\n",
|
||||
" location=REGION,\n",
|
||||
" location=LOCATION,\n",
|
||||
" root_dir=root_dir,\n",
|
||||
" target_column=target_column,\n",
|
||||
" optimization_objective=optimization_objective,\n",
|
||||
@@ -1425,8 +1357,8 @@
|
||||
" training_fraction=training_fraction,\n",
|
||||
" validation_fraction=validation_fraction,\n",
|
||||
" test_fraction=test_fraction,\n",
|
||||
" # Please note that TFT model will ONLY ensemble the model from\n",
|
||||
" # the top one trial, so `num_selected_trials` can not be set for TFT model.\n",
|
||||
" # Note that TFT model ONLY ensembles the model from\n",
|
||||
" # the top one trial, so `num_selected_trials` can't be set for TFT model.\n",
|
||||
" # num_selected_trials=num_selected_trials,\n",
|
||||
" time_column=time_column,\n",
|
||||
" time_series_identifier_columns=[time_series_identifier_column],\n",
|
||||
@@ -1444,9 +1376,9 @@
|
||||
")\n",
|
||||
"\n",
|
||||
"job_id = \"tft-forecasting-{}\".format(uuid.uuid4())\n",
|
||||
"job = aiplatform.PipelineJob(\n",
|
||||
"tft_pipeline_job = aiplatform.PipelineJob(\n",
|
||||
" display_name=job_id,\n",
|
||||
" location=REGION, # launches the pipeline job in the specified region\n",
|
||||
" location=LOCATION, # launches the pipeline job in the specified region\n",
|
||||
" template_path=template_path,\n",
|
||||
" job_id=job_id,\n",
|
||||
" pipeline_root=root_dir,\n",
|
||||
@@ -1454,10 +1386,9 @@
|
||||
" enable_caching=False,\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"job.run(service_account=SERVICE_ACCOUNT)\n",
|
||||
"tft_pipeline_job.run(service_account=SERVICE_ACCOUNT)\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"pipeline_task_details = job.gca_resource.job_detail.task_details"
|
||||
"tft_pipeline_task_details = tft_pipeline_job.gca_resource.job_detail.task_details"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -1470,7 +1401,7 @@
|
||||
"\n",
|
||||
"Enable the batch explain feature by simply setting `generate_explanation=True` in the `batch_predict` API.\n",
|
||||
"\n",
|
||||
"Use the following code to retrieve the trained Forecasting model from the pipeline:"
|
||||
"Use the following code to retrieve the trained forecasting model from the pipeline:"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -1481,7 +1412,7 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"upload_model_task = get_task_detail(pipeline_task_details, \"model-upload-2\")\n",
|
||||
"upload_model_task = get_task_detail(tft_pipeline_task_details, \"model-upload-2\")\n",
|
||||
"\n",
|
||||
"forecasting_mp_model_artifact = upload_model_task.outputs[\"model\"].artifacts[0]\n",
|
||||
"\n",
|
||||
@@ -1566,16 +1497,9 @@
|
||||
"id": "KtcHUmcZIi9g"
|
||||
},
|
||||
"source": [
|
||||
"## Upload with parent model for different model versions"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "6qht5Rdx6fuj"
|
||||
},
|
||||
"source": [
|
||||
"To upload this model to a parent Vertex AI model, you need the `parent_model_resource_name` resource_name of the parent Vertex AI model."
|
||||
"## Upload with parent model for different model versions\n",
|
||||
"\n",
|
||||
"To upload this model to a parent Vertex AI model, you need the `parent_model_resource_name` i.e., the resource name of the parent Vertex AI model."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -1591,7 +1515,7 @@
|
||||
"\n",
|
||||
"if parent_model_resource_name:\n",
|
||||
" parent_model_artifact = aiplatform.Artifact.get_with_uri(\n",
|
||||
" \"https://us-central1-aiplatform.googleapis.com/v1/\" + parent_model_resource_name\n",
|
||||
" f\"https://{LOCATION}-aiplatform.googleapis.com/v1/\" + parent_model_resource_name\n",
|
||||
" )\n",
|
||||
" parent_model_artifact_id = str(\n",
|
||||
" parent_model_artifact.gca_resource.name.split(\"artifacts/\")[1]\n",
|
||||
@@ -1604,13 +1528,13 @@
|
||||
" parameter_values,\n",
|
||||
" ) = automl_forecasting_utils.get_time_series_dense_encoder_forecasting_pipeline_and_parameters(\n",
|
||||
" project=PROJECT_ID,\n",
|
||||
" location=REGION,\n",
|
||||
" location=LOCATION,\n",
|
||||
" root_dir=root_dir,\n",
|
||||
" target_column=target_column,\n",
|
||||
" optimization_objective=optimization_objective,\n",
|
||||
" transformations=transformations,\n",
|
||||
" train_budget_milli_node_hours=train_budget_milli_node_hours,\n",
|
||||
" # Do not set `data_source_csv_filenames` and\n",
|
||||
" # Don't set `data_source_csv_filenames` and\n",
|
||||
" # `data_source_bigquery_table_path` if you want to use Vertex managed\n",
|
||||
" # dataset by commenting out the following two lines.\n",
|
||||
" data_source_csv_filenames=data_source_csv_filenames,\n",
|
||||
@@ -1640,7 +1564,7 @@
|
||||
" job_id = \"tide-forecasting-with-parent-model-{}\".format(uuid.uuid4())\n",
|
||||
" job = aiplatform.PipelineJob(\n",
|
||||
" display_name=job_id,\n",
|
||||
" location=REGION, # launches the pipeline job in the specified region\n",
|
||||
" location=LOCATION, # launches the pipeline job in the specified region\n",
|
||||
" template_path=template_path,\n",
|
||||
" job_id=job_id,\n",
|
||||
" pipeline_root=root_dir,\n",
|
||||
@@ -1658,9 +1582,9 @@
|
||||
"id": "8Bu0wywvPYkD"
|
||||
},
|
||||
"source": [
|
||||
"## Integrate Tabular Workflow for Forecasting into your existing KFP pipeline\n",
|
||||
"## Integrate Tabular Workflow for Forecasting with your existing KFP pipeline\n",
|
||||
"\n",
|
||||
"This is implemented using [the pipeline-as-component feature](https://www.kubeflow.org/docs/components/pipelines/v2/load-and-share-components/) of KFP."
|
||||
"In this section, you define and run a sample KFP pipeline with your Tabular Workflow pipeline integrated as a component. For this, use [the pipeline-as-component feature](https://www.kubeflow.org/docs/components/pipelines/v2/load-and-share-components/) of KFP."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -1680,7 +1604,7 @@
|
||||
" parameter_values,\n",
|
||||
") = automl_forecasting_utils.get_time_series_dense_encoder_forecasting_pipeline_and_parameters(\n",
|
||||
" project=PROJECT_ID,\n",
|
||||
" location=REGION,\n",
|
||||
" location=LOCATION,\n",
|
||||
" root_dir=root_dir,\n",
|
||||
" target_column=target_column,\n",
|
||||
" optimization_objective=optimization_objective,\n",
|
||||
@@ -1734,9 +1658,9 @@
|
||||
"\n",
|
||||
"\n",
|
||||
"job_id = \"run-forecasting-pipeline-inside-pipeline-{}\".format(uuid.uuid4())\n",
|
||||
"job = aiplatform.PipelineJob(\n",
|
||||
"tabular_workflow_pipeline_job = aiplatform.PipelineJob(\n",
|
||||
" display_name=job_id,\n",
|
||||
" location=REGION, # launches the pipeline job in the specified region\n",
|
||||
" location=LOCATION, # launches the pipeline job in the specified region\n",
|
||||
" template_path=outer_pipeline_template_path,\n",
|
||||
" job_id=job_id,\n",
|
||||
" pipeline_root=root_dir,\n",
|
||||
@@ -1744,7 +1668,11 @@
|
||||
" enable_caching=False,\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"job.run(service_account=SERVICE_ACCOUNT)"
|
||||
"tabular_workflow_pipeline_job.run(service_account=SERVICE_ACCOUNT)\n",
|
||||
"\n",
|
||||
"tabular_workflow_pipeline_task_details = (\n",
|
||||
" tabular_workflow_pipeline_job.gca_resource.job_detail.task_details\n",
|
||||
")"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -1761,6 +1689,82 @@
|
||||
"Otherwise, you can delete the individual resources you created in this tutorial:"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "1b9030d9a080"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Delete resources from TiDE pipeline with architecture search\n",
|
||||
"model_resource_name = get_deployed_model_resource(\n",
|
||||
" architecture_search_pipeline_task_details\n",
|
||||
")\n",
|
||||
"# Load the model resource\n",
|
||||
"model = aiplatform.Model(model_resource_name)\n",
|
||||
"if model:\n",
|
||||
" # Delete the model\n",
|
||||
" model.delete()\n",
|
||||
"# Delete the pipeline job\n",
|
||||
"architecture_search_pipeline_job.delete()\n",
|
||||
"\n",
|
||||
"# Delete resources from TiDE pipeline without architecture search\n",
|
||||
"model_resource_name = get_deployed_model_resource(\n",
|
||||
" skip_architecture_search_pipeline_task_details\n",
|
||||
")\n",
|
||||
"# Load the model resource\n",
|
||||
"model = aiplatform.Model(model_resource_name)\n",
|
||||
"if model:\n",
|
||||
" # Delete the model\n",
|
||||
" model.delete()\n",
|
||||
"# Delete the pipeline job\n",
|
||||
"skip_architecture_search_pipeline_job.delete()\n",
|
||||
"\n",
|
||||
"# Delete resources from L2L training pipeline\n",
|
||||
"model_resource_name = get_deployed_model_resource(l2l_pipeline_task_details)\n",
|
||||
"# Load the model resource\n",
|
||||
"model = aiplatform.Model(model_resource_name)\n",
|
||||
"if model:\n",
|
||||
" # Delete the model\n",
|
||||
" model.delete()\n",
|
||||
"# Delete the pipeline job\n",
|
||||
"l2l_pipeline_job.delete()\n",
|
||||
"\n",
|
||||
"# Delete resources from seq2seq training pipeline\n",
|
||||
"model_resource_name = get_deployed_model_resource(seq2seq_pipeline_task_details)\n",
|
||||
"# Load the model resource\n",
|
||||
"model = aiplatform.Model(model_resource_name)\n",
|
||||
"if model:\n",
|
||||
" # Delete the model\n",
|
||||
" model.delete()\n",
|
||||
"# Delete the pipeline job\n",
|
||||
"seq2seq_pipeline_job.delete()\n",
|
||||
"\n",
|
||||
"# Delete resources from TFT training pipeline\n",
|
||||
"model_resource_name = get_deployed_model_resource(tft_pipeline_task_details)\n",
|
||||
"# Load the model resource\n",
|
||||
"model = aiplatform.Model(model_resource_name)\n",
|
||||
"if model:\n",
|
||||
" # Delete the model\n",
|
||||
" model.delete()\n",
|
||||
"# Delete the pipeline job\n",
|
||||
"tft_pipeline_job.delete()\n",
|
||||
"\n",
|
||||
"# Delete resources from Tabular Workflow training pipeline\n",
|
||||
"model_resource_name = get_deployed_model_resource(\n",
|
||||
" tabular_workflow_pipeline_task_details\n",
|
||||
")\n",
|
||||
"# Load the model resource\n",
|
||||
"model = aiplatform.Model(model_resource_name)\n",
|
||||
"if model:\n",
|
||||
" # Delete the model\n",
|
||||
" model.delete()\n",
|
||||
"\n",
|
||||
"# Delete the pipeline job\n",
|
||||
"tabular_workflow_pipeline_job.delete()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
@@ -1769,11 +1773,15 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import os\n",
|
||||
"# Delete the batch prediction job\n",
|
||||
"batch_prediction_job.delete()\n",
|
||||
"\n",
|
||||
"# Delete the Vertex AI Dataset\n",
|
||||
"vertex_dataset.delete()\n",
|
||||
"\n",
|
||||
"# Delete Cloud Storage objects that were created\n",
|
||||
"delete_bucket = False\n",
|
||||
"if delete_bucket or os.getenv(\"IS_TESTING\"):\n",
|
||||
"delete_bucket = True\n",
|
||||
"if delete_bucket:\n",
|
||||
" ! gsutil -m rm -r $BUCKET_URI"
|
||||
]
|
||||
}
|
||||
|
||||
@@ -32,25 +32,27 @@
|
||||
"# AutoML training image classification model for batch prediction\n",
|
||||
"\n",
|
||||
"<table align=\"left\">\n",
|
||||
" <td>\n",
|
||||
" <td style=\"text-align: center\">\n",
|
||||
" <a href=\"https://colab.research.google.com/github/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/official/automl/automl_image_classification_batch_prediction.ipynb\">\n",
|
||||
" <img src=\"https://cloud.google.com/ml-engine/images/colab-logo-32px.png\" alt=\"Colab logo\"> Run in Colab\n",
|
||||
" <img src=\"https://cloud.google.com/ml-engine/images/colab-logo-32px.png\" alt=\"Google Colaboratory logo\"><br> Open in Colab\n",
|
||||
" </a>\n",
|
||||
" </td>\n",
|
||||
" <td>\n",
|
||||
" <td style=\"text-align: center\">\n",
|
||||
" <a href=\"https://console.cloud.google.com/vertex-ai/colab/import/https:%2F%2Fraw.githubusercontent.com%2FGoogleCloudPlatform%2Fvertex-ai-samples%2Fmain%2Fnotebooks%2Fofficial%2Fautoml%2Fautoml_image_classification_batch_prediction.ipynb\">\n",
|
||||
" <img width=\"32px\" src=\"https://cloud.google.com/ml-engine/images/colab-enterprise-logo-32px.png\" alt=\"Google Cloud Colab Enterprise logo\"><br> Open in Colab Enterprise\n",
|
||||
" </a>\n",
|
||||
" </td> \n",
|
||||
" <td style=\"text-align: center\">\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/automl/automl_image_classification_batch_prediction.ipynb\">\n",
|
||||
" <img src=\"https://lh3.googleusercontent.com/UiNooY4LUgW_oTvpsNhPpQzsstV5W8F7rYgxgGBD85cWJoLmrOzhVs_ksK_vgx40SHs7jCqkTkCk=e14-rj-sc0xffffff-h130-w32\" alt=\"Vertex AI logo\"><br> Open in Workbench\n",
|
||||
" </a>\n",
|
||||
" </td>\n",
|
||||
" <td style=\"text-align: center\">\n",
|
||||
" <a href=\"https://github.com/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/official/automl/automl_image_classification_batch_prediction.ipynb\">\n",
|
||||
" <img src=\"https://cloud.google.com/ml-engine/images/github-logo-32px.png\" alt=\"GitHub logo\">\n",
|
||||
" View on GitHub\n",
|
||||
" <img src=\"https://cloud.google.com/ml-engine/images/github-logo-32px.png\" alt=\"GitHub logo\"><br> 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/automl/automl_image_classification_online_prediction.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>\n",
|
||||
"<br/><br/><br/>"
|
||||
"</table>"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -79,14 +81,14 @@
|
||||
"\n",
|
||||
"The steps performed include:\n",
|
||||
"\n",
|
||||
"- Create a Vertex `Dataset` resource.\n",
|
||||
"- Create a Vertex dataset resource.\n",
|
||||
"- Train the model.\n",
|
||||
"- View the model evaluation.\n",
|
||||
"- Make a batch prediction.\n",
|
||||
"\n",
|
||||
"There is one key difference between using batch prediction and using online prediction:\n",
|
||||
"There's one key difference between using batch prediction and using online prediction:\n",
|
||||
"\n",
|
||||
"* Prediction Service: Does an on-demand prediction for the entire set of instances (i.e., one or more data items) and returns the results in real-time.\n",
|
||||
"* Online Prediction Service: Does an on-demand prediction for the entire set of instances (i.e., one or more data items) and returns the results in real-time.\n",
|
||||
"\n",
|
||||
"* Batch Prediction Service: Does a queued (batch) prediction for the entire set of instances in the background and stores the results in a Cloud Storage bucket when ready."
|
||||
]
|
||||
@@ -99,7 +101,7 @@
|
||||
"source": [
|
||||
"### Dataset\n",
|
||||
"\n",
|
||||
"The dataset used for this tutorial is the [Flowers dataset](https://www.tensorflow.org/datasets/catalog/tf_flowers) from [TensorFlow Datasets](https://www.tensorflow.org/datasets/catalog/overview). The version of the dataset you will use in this tutorial is stored in a public Cloud Storage bucket. The trained model predicts the type of flower an image is from a class of five flowers: daisy, dandelion, rose, sunflower, or tulip."
|
||||
"The dataset used for this tutorial is the [Flowers dataset](https://www.tensorflow.org/datasets/catalog/tf_flowers) from [TensorFlow Datasets](https://www.tensorflow.org/datasets/catalog/overview). The version of the dataset you use in this tutorial is stored in a public Cloud Storage bucket. The trained model predicts the type of flower an image is from a class of five flowers: *daisy*, *dandelion*, *rose*, *sunflower*, or *tulip*."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -125,12 +127,19 @@
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "install_aip:mbsdk"
|
||||
"id": "61RBz8LLbxCR"
|
||||
},
|
||||
"source": [
|
||||
"## Installation\n",
|
||||
"\n",
|
||||
"Install the latest version of Vertex AI SDK for Python."
|
||||
"## Get started"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "No17Cw5hgx12"
|
||||
},
|
||||
"source": [
|
||||
"### Install Vertex AI SDK for Python and other required packages\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -141,59 +150,87 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import os\n",
|
||||
"\n",
|
||||
"! pip3 install --upgrade --quiet google-cloud-aiplatform\n",
|
||||
"\n",
|
||||
"if os.environ[\"IS_TESTING\"]:\n",
|
||||
" ! pip3 install --upgrade --quiet tensorflow"
|
||||
"! pip3 install --upgrade --quiet google-cloud-aiplatform \\\n",
|
||||
" tensorflow==2.15.1"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "restart"
|
||||
"id": "R5Xep4W9lq-Z"
|
||||
},
|
||||
"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": "D-ZBOjErv5mM"
|
||||
"id": "XRvKdaPDTznN"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Automatically restart kernel after installs so that your environment can access the new packages\n",
|
||||
"# 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": "before_you_begin:nogpu"
|
||||
"id": "SbmM4z7FOBpM"
|
||||
},
|
||||
"source": [
|
||||
"## Before you begin"
|
||||
"<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": "before_you_begin:nogpu"
|
||||
"id": "dmWOrTJ3gx13"
|
||||
},
|
||||
"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": "NyKGtVQjgx13"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"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": "DF4l8DTdWgPY"
|
||||
},
|
||||
"source": [
|
||||
"### Set Google Cloud project information\n",
|
||||
"\n",
|
||||
"To get started using Vertex AI, you must have an existing Google Cloud project. Learn more about [setting up a project and a development environment](https://cloud.google.com/vertex-ai/docs/start/cloud-environment)."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -205,105 +242,12 @@
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"PROJECT_ID = \"[your-project-id]\" # @param {type:\"string\"}\n",
|
||||
"LOCATION = \"us-central1\" # @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": "gcp_authenticate"
|
||||
},
|
||||
"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": "FvQeFm3Gv5mR"
|
||||
},
|
||||
"source": [
|
||||
"**1. Vertex AI Workbench**\n",
|
||||
"* Do nothing as you are already authenticated."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "ad1138a125ea"
|
||||
},
|
||||
"source": [
|
||||
"**2. Local JupyterLab instance, uncomment and run:**"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "ce6043da7b33"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# ! gcloud auth login"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "0367eac06a10"
|
||||
},
|
||||
"source": [
|
||||
"**3. Colab, uncomment and run:**"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "21ad4dbb4a61"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# from google.colab import auth\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": {
|
||||
@@ -332,7 +276,7 @@
|
||||
"id": "create_bucket"
|
||||
},
|
||||
"source": [
|
||||
"**Only if your bucket doesn't already exist**: Run the following cell to create your Cloud Storage bucket."
|
||||
"**If your bucket doesn't already exist**: Run the following cell to create your Cloud Storage bucket."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -343,7 +287,7 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"! gsutil mb -l $REGION $BUCKET_URI"
|
||||
"! gsutil mb -l {LOCATION} -p {PROJECT_ID} {BUCKET_URI}"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -366,7 +310,7 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import google.cloud.aiplatform as aiplatform"
|
||||
"from google.cloud import aiplatform"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -397,9 +341,9 @@
|
||||
"id": "tutorial_start:automl"
|
||||
},
|
||||
"source": [
|
||||
"# Tutorial\n",
|
||||
"## Tutorial\n",
|
||||
"\n",
|
||||
"Now you are ready to start creating your own AutoML image classification model."
|
||||
"Now you're ready to start creating your own AutoML image classification model."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -421,9 +365,7 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"IMPORT_FILE = (\n",
|
||||
" \"gs://cloud-samples-data/vision/automl_classification/flowers/all_data_v2.csv\"\n",
|
||||
")"
|
||||
"IMPORT_FILE = \"gs://cloud-samples-data/ai-platform/flowers/flowers.csv\""
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -436,7 +378,7 @@
|
||||
"\n",
|
||||
"This tutorial uses a version of the Flowers dataset that is stored in a public Cloud Storage bucket, using a CSV index file.\n",
|
||||
"\n",
|
||||
"Start by doing a quick peek at the data. You count the number of examples by counting the number of rows in the CSV index file (`wc -l`) and then peek at the first few rows."
|
||||
"Begin by taking a quick look at the data. First, count the number of examples by determining the number of rows in the CSV index file using the (`wc -l`) command. Then, preview the first few rows of the file."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -467,10 +409,10 @@
|
||||
"source": [
|
||||
"### Create the Dataset\n",
|
||||
"\n",
|
||||
"Next, create the `Dataset` resource using the `create` method for the `ImageDataset` class, which takes the following parameters:\n",
|
||||
"Next, create the dataset resource using the `create` method for the `ImageDataset` class, which takes the following parameters:\n",
|
||||
"\n",
|
||||
"- `display_name`: The human readable name for the `Dataset` resource.\n",
|
||||
"- `gcs_source`: A list of one or more dataset index files to import the data items into the `Dataset` resource.\n",
|
||||
"- `display_name`: The human readable name for the dataset resource.\n",
|
||||
"- `gcs_source`: A list of one or more dataset index files to import the data items into the dataset resource.\n",
|
||||
"- `import_schema_uri`: The data labeling schema for the data items.\n",
|
||||
"\n",
|
||||
"This operation may take several minutes."
|
||||
@@ -507,11 +449,11 @@
|
||||
"\n",
|
||||
"An AutoML training pipeline is created with the `AutoMLImageTrainingJob` class, with the following parameters:\n",
|
||||
"\n",
|
||||
"- `display_name`: The human readable name for the `TrainingJob` resource.\n",
|
||||
"- `prediction_type`: The type task to train the model for.\n",
|
||||
"- `display_name`: The human readable name for the training job resource.\n",
|
||||
"- `prediction_type`: The type of task for which the model is trained.\n",
|
||||
" - `classification`: An image classification model.\n",
|
||||
" - `object_detection`: An image object detection model.\n",
|
||||
"- `multi_label`: If a classification task, whether single (`False`) or multi-labeled (`True`).\n",
|
||||
"- `multi_label`: For a classification task, specify whether it's multi-labeled (`True`) or single-labeled (`False`).\n",
|
||||
"- `model_type`: The type of model for deployment.\n",
|
||||
" - `CLOUD`: Deployment on Google Cloud\n",
|
||||
" - `CLOUD_HIGH_ACCURACY_1`: Optimized for accuracy over latency for deployment on Google Cloud.\n",
|
||||
@@ -519,7 +461,7 @@
|
||||
" - `MOBILE_TF_VERSATILE_1`: Deployment on an edge device.\n",
|
||||
" - `MOBILE_TF_HIGH_ACCURACY_1`:Optimized for accuracy over latency for deployment on an edge device.\n",
|
||||
" - `MOBILE_TF_LOW_LATENCY_1`: Optimized for latency over accuracy for deployment on an edge device.\n",
|
||||
"- `base_model`: (optional) Transfer learning from existing `Model` resource -- supported for image classification only.\n",
|
||||
"- `base_model`: (optional) Transfer learning from existing model resource -- supported for image classification only.\n",
|
||||
"\n",
|
||||
"The instantiated object is the DAG (directed acyclic graph) for the training job."
|
||||
]
|
||||
@@ -551,17 +493,17 @@
|
||||
"source": [
|
||||
"#### Run the training pipeline\n",
|
||||
"\n",
|
||||
"Next, you run the DAG to start the training job by invoking the method `run`, with the following parameters:\n",
|
||||
"Next, run the DAG to start the training job by invoking the `run` method, with the following parameters:\n",
|
||||
"\n",
|
||||
"- `dataset`: The `Dataset` resource to train the model.\n",
|
||||
"- `dataset`: The dataset resource to train the model.\n",
|
||||
"- `model_display_name`: The human readable name for the trained model.\n",
|
||||
"- `training_fraction_split`: The percentage of the dataset to use for training.\n",
|
||||
"- `test_fraction_split`: The percentage of the dataset to use for test (holdout data).\n",
|
||||
"- `validation_fraction_split`: The percentage of the dataset to use for validation.\n",
|
||||
"- `budget_milli_node_hours`: (optional) Maximum training time specified in unit of millihours (1000 = hour).\n",
|
||||
"- `disable_early_stopping`: If `True`, training maybe completed before using the entire budget if the service believes it cannot further improve on the model objective measurements.\n",
|
||||
"- `disable_early_stopping`: By default, the model training stops early if the model performance doesn't improve. Setting `disable_early_stopping` = `True` overrides this behavior, allowing the model to train for the entire specified duration.\n",
|
||||
"\n",
|
||||
"The `run` method when completed returns the `Model` resource."
|
||||
"The `run` method, upon completion, returns the model resource"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -591,7 +533,7 @@
|
||||
"source": [
|
||||
"## Review model evaluation scores\n",
|
||||
"\n",
|
||||
"After your model training has finished, you can review the evaluation scores for it using the `list_model_evaluations()` method. This method will return an iterator for each evaluation slice."
|
||||
"After your model training is complete, you can review the evaluation scores for it using the `list_model_evaluations()` method. This method returns an iterator for each evaluation slice."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -627,7 +569,7 @@
|
||||
"source": [
|
||||
"### Get test item(s)\n",
|
||||
"\n",
|
||||
"Now do a batch prediction to your Vertex model. You will use arbitrary examples out of the dataset as a test items. Don't be concerned that the examples were likely used in training the model -- we just want to demonstrate how to make a prediction."
|
||||
"Now generate a batch prediction for your Vertex AI model. Use arbitrary examples from the dataset as test items. Don't be concerned that the example was likely used in training the model -- the point is to demonstrate how to make a prediction."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -685,12 +627,12 @@
|
||||
"id": "make_batch_file:automl,image"
|
||||
},
|
||||
"source": [
|
||||
"### Make the batch input file\n",
|
||||
"### Create the batch input file\n",
|
||||
"\n",
|
||||
"Now make a batch input file, which you will store in your local Cloud Storage bucket. The batch input file can be either CSV or JSONL. You will use JSONL in this tutorial. For JSONL file, you make one dictionary entry per line for each data item (instance). The dictionary contains the key/value pairs:\n",
|
||||
"Now make a batch input file, which is stored in your local Cloud Storage bucket. The batch input file can be either CSV or JSONL. Use JSONL in this tutorial. For JSONL file, make one dictionary entry per line for each data item (instance). The dictionary contains the key/value pairs:\n",
|
||||
"\n",
|
||||
"- `content`: The Cloud Storage path to the image.\n",
|
||||
"- `mime_type`: The content type. In our example, it is a `jpeg` file.\n",
|
||||
"- `mime_type`: The content type. In our example, it's a `jpeg` file.\n",
|
||||
"\n",
|
||||
"For example:\n",
|
||||
"\n",
|
||||
@@ -728,12 +670,12 @@
|
||||
"source": [
|
||||
"### Make the batch prediction request\n",
|
||||
"\n",
|
||||
"Now that your Model resource is trained, you can make a batch prediction by invoking the batch_predict() method, with the following parameters:\n",
|
||||
"Now that your model resource is trained, make a batch prediction by invoking the `batch_predict()` method, with the following parameters:\n",
|
||||
"\n",
|
||||
"- `job_display_name`: The human readable name for the batch prediction job.\n",
|
||||
"- `gcs_source`: A list of one or more batch request input files.\n",
|
||||
"- `gcs_destination_prefix`: The Cloud Storage location for storing the batch prediction resuls.\n",
|
||||
"- `sync`: If set to True, the call will block while waiting for the asynchronous batch job to complete."
|
||||
"- `gcs_destination_prefix`: The Cloud Storage location for storing the batch prediction results.\n",
|
||||
"- `sync`: Set `True` to wait until the completion of the job."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -762,7 +704,7 @@
|
||||
"source": [
|
||||
"### Wait for completion of batch prediction job\n",
|
||||
"\n",
|
||||
"Next, wait for the batch job to complete. Alternatively, one can set the parameter `sync` to `True` in the `batch_predict()` method to block until the batch prediction job is completed."
|
||||
"Next, wait for the batch job to complete. Alternatively, you can set the parameter `sync` to `True` in the `batch_predict()` method to block until the batch prediction job is completed."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -786,11 +728,11 @@
|
||||
"\n",
|
||||
"Next, get the results from the completed batch prediction job.\n",
|
||||
"\n",
|
||||
"The results are written to the Cloud Storage output bucket you specified in the batch prediction request. You call the method iter_outputs() to get a list of each Cloud Storage file generated with the results. Each file contains one or more prediction requests in a JSON format:\n",
|
||||
"The results are written to the Cloud Storage output bucket specified in the batch prediction request. Call the `iter_outputs()` method to get a list of each Cloud Storage file generated with the results. Each file contains one or more prediction requests in a JSON format:\n",
|
||||
"\n",
|
||||
"- `content`: The prediction request.\n",
|
||||
"- `prediction`: The prediction response.\n",
|
||||
" - `ids`: The internal assigned unique identifiers for each prediction request.\n",
|
||||
" - `ids`: The internally assigned unique identifiers for each prediction request.\n",
|
||||
" - `displayNames`: The class names for each class label.\n",
|
||||
" - `confidences`: The predicted confidence, between 0 and 1, per class label."
|
||||
]
|
||||
@@ -846,8 +788,6 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"delete_bucket = False\n",
|
||||
"\n",
|
||||
"# Delete the dataset using the Vertex dataset object\n",
|
||||
"dataset.delete()\n",
|
||||
"\n",
|
||||
@@ -860,7 +800,9 @@
|
||||
"# Delete the batch prediction job\n",
|
||||
"batch_predict_job.delete()\n",
|
||||
"\n",
|
||||
"if delete_bucket or os.getenv(\"IS_TESTING\"):\n",
|
||||
"# Delete the cloud storage bucket\n",
|
||||
"delete_bucket = False # set True for deletion\n",
|
||||
"if delete_bucket:\n",
|
||||
" ! gsutil rm -r $BUCKET_URI"
|
||||
]
|
||||
}
|
||||
|
||||
@@ -32,20 +32,25 @@
|
||||
"# AutoML training image object detection model for export to edge\n",
|
||||
"\n",
|
||||
"<table align=\"left\">\n",
|
||||
" <td>\n",
|
||||
" <td style=\"text-align: center\">\n",
|
||||
" <a href=\"https://colab.research.google.com/github/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/official/automl/automl_image_object_detection_export_edge.ipynb\">\n",
|
||||
" <img src=\"https://cloud.google.com/ml-engine/images/colab-logo-32px.png\" alt=\"Colab logo\"> Run in Colab\n",
|
||||
" <img src=\"https://cloud.google.com/ml-engine/images/colab-logo-32px.png\" alt=\"Colab logo\"><br> Open in Colab\n",
|
||||
" </a>\n",
|
||||
" </td>\n",
|
||||
" <td>\n",
|
||||
" <td style=\"text-align: center\">\n",
|
||||
" <a href=\"https://console.cloud.google.com/vertex-ai/colab/import/https:%2F%2Fraw.githubusercontent.com%2FGoogleCloudPlatform%2Fvertex-ai-samples%2Fmain%2Fnotebooks%2Fofficial%2Fautoml%2Fautoml_image_object_detection_export_edge.ipynb\">\n",
|
||||
" <img width=\"32px\" src=\"https://cloud.google.com/ml-engine/images/colab-enterprise-logo-32px.png\" alt=\"Google Cloud Colab Enterprise logo\"><br> Open in Colab Enterprise\n",
|
||||
" </a>\n",
|
||||
" </td> \n",
|
||||
" <td style=\"text-align: center\">\n",
|
||||
" <a href=\"https://github.com/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/official/automl/automl_image_object_detection_export_edge.ipynb\">\n",
|
||||
" <img src=\"https://cloud.google.com/ml-engine/images/github-logo-32px.png\" alt=\"GitHub logo\">\n",
|
||||
" <img src=\"https://cloud.google.com/ml-engine/images/github-logo-32px.png\" alt=\"GitHub logo\"><br>\n",
|
||||
" View on GitHub\n",
|
||||
" </a>\n",
|
||||
" </td>\n",
|
||||
" <td>\n",
|
||||
" <td style=\"text-align: center\">\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/automl//automl_image_object_detection_export_edge.ipynb\">\n",
|
||||
" <img src=\"https://lh3.googleusercontent.com/UiNooY4LUgW_oTvpsNhPpQzsstV5W8F7rYgxgGBD85cWJoLmrOzhVs_ksK_vgx40SHs7jCqkTkCk=e14-rj-sc0xffffff-h130-w32\" alt=\"Vertex AI logo\">\n",
|
||||
" <img src=\"https://lh3.googleusercontent.com/UiNooY4LUgW_oTvpsNhPpQzsstV5W8F7rYgxgGBD85cWJoLmrOzhVs_ksK_vgx40SHs7jCqkTkCk=e14-rj-sc0xffffff-h130-w32\" alt=\"Vertex AI logo\"><br>\n",
|
||||
" Open in Vertex AI Workbench\n",
|
||||
" </a>\n",
|
||||
" </td>\n",
|
||||
@@ -75,16 +80,16 @@
|
||||
"\n",
|
||||
"In this tutorial, you create an AutoML image object detection model from a Python script using the Vertex SDK, and then export the model as an Edge model in TFLite format. You can alternatively create models with AutoML using the `gcloud` command-line tool or online using the Cloud Console.\n",
|
||||
"\n",
|
||||
"This tutorial uses the following Google Cloud ML services:\n",
|
||||
"This tutorial uses the following Google Cloud Vertex AI services:\n",
|
||||
"\n",
|
||||
"- Vertex AI `Datasets`\n",
|
||||
"- AutoML Image\n",
|
||||
"- Vertex AI datasets\n",
|
||||
"- AutoML image\n",
|
||||
"\n",
|
||||
"The steps performed include:\n",
|
||||
"\n",
|
||||
"- Create a Vertex `Dataset` resource.\n",
|
||||
"- Create a Vertex dataset resource.\n",
|
||||
"- Train the model.\n",
|
||||
"- Export the `Edge` model from the `Model` resource to Cloud Storage.\n",
|
||||
"- Export the edge model from the model resource to Cloud Storage.\n",
|
||||
"- Download the model locally.\n",
|
||||
"- Make a local prediction."
|
||||
]
|
||||
@@ -123,183 +128,121 @@
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "install_aip:mbsdk"
|
||||
"id": "f0316df526f8"
|
||||
},
|
||||
"source": [
|
||||
"## Installation\n",
|
||||
"\n",
|
||||
"Install the latest version of Vertex AI SDK for Python."
|
||||
"## Get started"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "a2c2cb2109a0"
|
||||
},
|
||||
"source": [
|
||||
"### Install Vertex AI SDK for Python and other required packages\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "install_aip:mbsdk"
|
||||
"id": "6dca41de7a4d"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import os\n",
|
||||
"\n",
|
||||
"! pip3 install --upgrade --quiet google-cloud-aiplatform\n",
|
||||
"\n",
|
||||
"if os.environ[\"IS_TESTING\"]:\n",
|
||||
" ! pip3 install --upgrade tensorflow $USER_FLAG"
|
||||
"! pip3 install --upgrade --quiet google-cloud-aiplatform tensorflow gcsfs"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "restart"
|
||||
"id": "ff555b32bab8"
|
||||
},
|
||||
"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": "D-ZBOjErv5mM"
|
||||
"id": "f09b4dff629a"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Automatically restart kernel after installs so that your environment can access the new packages\n",
|
||||
"# 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": "before_you_begin:nogpu"
|
||||
"id": "ee775571c2b5"
|
||||
},
|
||||
"source": [
|
||||
"## Before you begin"
|
||||
"<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": "before_you_begin:nogpu"
|
||||
"id": "92e68cfc3a90"
|
||||
},
|
||||
"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": "set_project_id"
|
||||
"id": "46604f70e831"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"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": "4f872cd812d0"
|
||||
},
|
||||
"source": [
|
||||
"### Set Google Cloud project information and initialize Vertex AI SDK for Python\n",
|
||||
"\n",
|
||||
"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": "294fe4e5a671"
|
||||
},
|
||||
"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": "gcp_authenticate"
|
||||
},
|
||||
"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": "FvQeFm3Gv5mR"
|
||||
},
|
||||
"source": [
|
||||
"**1. Vertex AI Workbench**\n",
|
||||
"* Do nothing as you are already authenticated."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "ad1138a125ea"
|
||||
},
|
||||
"source": [
|
||||
"**2. Local JupyterLab instance, uncomment and run:**"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "ce6043da7b33"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# ! gcloud auth login"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "0367eac06a10"
|
||||
},
|
||||
"source": [
|
||||
"**3. Colab, uncomment and run:**"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "21ad4dbb4a61"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# from google.colab import auth\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."
|
||||
"LOCATION = \"us-central1\" # @param {type:\"string\"}"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -330,7 +273,7 @@
|
||||
"id": "create_bucket"
|
||||
},
|
||||
"source": [
|
||||
"**Only if your bucket doesn't already exist**: Run the following cell to create your Cloud Storage bucket."
|
||||
"**If your bucket doesn't already exist**: Run the following cell to create your Cloud Storage bucket."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -341,32 +284,7 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"! gsutil mb -l $REGION $BUCKET_URI"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "setup_vars"
|
||||
},
|
||||
"source": [
|
||||
"### Set up variables\n",
|
||||
"\n",
|
||||
"Next, set up some variables used throughout the tutorial.\n",
|
||||
"### Import libraries and define constants"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "import_aip:mbsdk"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import os\n",
|
||||
"\n",
|
||||
"import google.cloud.aiplatform as aiplatform"
|
||||
"! gsutil mb -l $LOCATION $BUCKET_URI"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -375,7 +293,7 @@
|
||||
"id": "init_aip:mbsdk"
|
||||
},
|
||||
"source": [
|
||||
"## Initialize Vertex AI SDK for Python\n",
|
||||
"### Initialize Vertex AI SDK for Python\n",
|
||||
"\n",
|
||||
"Initialize the Vertex AI SDK for Python for your project and corresponding bucket."
|
||||
]
|
||||
@@ -388,7 +306,9 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"aiplatform.init(project=PROJECT_ID, staging_bucket=BUCKET_URI)"
|
||||
"from google.cloud import aiplatform\n",
|
||||
"\n",
|
||||
"aiplatform.init(project=PROJECT_ID, location=LOCATION, staging_bucket=BUCKET_URI)"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -427,12 +347,104 @@
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "quick_peek:csv"
|
||||
"id": "f41a55981d90"
|
||||
},
|
||||
"source": [
|
||||
"### Copying data between Google Cloud Storage Buckets \n",
|
||||
"\n",
|
||||
"In this step, you prevent access issues for the images in your original dataset. The code below extracts folder paths from image paths, constructs destination paths for Google Cloud Storage (GCS), copies images using gsutil commands, updates image paths in the DataFrame, and finally saves the modified DataFrame back to GCS as a CSV file."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "df98442ace03"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import pandas as pd\n",
|
||||
"\n",
|
||||
"# Read the CSV file\n",
|
||||
"df = pd.read_csv(IMPORT_FILE, header=None)\n",
|
||||
"\n",
|
||||
"# Extract folder paths from image paths\n",
|
||||
"df[\"folder_path\"] = df.iloc[:, 0].apply(lambda x: \"/\".join(x.split(\"/\")[:-1]))\n",
|
||||
"\n",
|
||||
"# Construct destination paths in your bucket (adding a trailing slash for directories)\n",
|
||||
"df[\"destination_path\"] = (\n",
|
||||
" BUCKET_URI\n",
|
||||
" + \"/img/openimage/\"\n",
|
||||
" + df[\"folder_path\"].apply(lambda x: x.split(\"/\")[-1])\n",
|
||||
" + \"/\"\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"# Copy images using gsutil commands directly\n",
|
||||
"for src, dest in zip(df.iloc[:, 0], df[\"destination_path\"]):\n",
|
||||
" ! gsutil -m cp {src} {dest}\n",
|
||||
"\n",
|
||||
"print(f\"Files copied to {BUCKET_URI}\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "7ca1626de99f"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Combine the destination folder paths with the original image filenames\n",
|
||||
"df[\"new_image_path\"] = df[\"destination_path\"] + df.iloc[:, 0].apply(\n",
|
||||
" lambda x: x.split(\"/\")[-1]\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"# Replace the original image path column with the new full paths\n",
|
||||
"df.iloc[:, 0] = df[\"new_image_path\"]\n",
|
||||
"\n",
|
||||
"# Drop the temporary columns\n",
|
||||
"df = df.drop(columns=[\"new_image_path\", \"destination_path\", \"folder_path\"])\n",
|
||||
"\n",
|
||||
"# Specify the destination file path in your bucket for the updated CSV\n",
|
||||
"CSV_DESTINATION_PATH = f\"{BUCKET_URI}/vision/salads.csv\"\n",
|
||||
"\n",
|
||||
"# Save the updated DataFrame directly to GCS\n",
|
||||
"df.to_csv(CSV_DESTINATION_PATH, index=False, header=None)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "ecc97105a2d7"
|
||||
},
|
||||
"source": [
|
||||
"#### Location of Cloud Storage training data.\n",
|
||||
"\n",
|
||||
"Redefining the variable `IMPORT_FILE` to the location of the CSV index file in Cloud Storage."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "028fb6ec54e0"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"IMPORT_FILE = CSV_DESTINATION_PATH\n",
|
||||
"\n",
|
||||
"print(IMPORT_FILE)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "f4fd562be838"
|
||||
},
|
||||
"source": [
|
||||
"#### Quick peek at your data\n",
|
||||
"\n",
|
||||
"This tutorial uses a version of the Salads dataset that is stored in a public Cloud Storage bucket, using a CSV index file.\n",
|
||||
"This tutorial uses a version of salads dataset which is copied to the project's Cloud Storage Bucket.\n",
|
||||
"\n",
|
||||
"Start by doing a quick peek at the data. You count the number of examples by counting the number of rows in the CSV index file (`wc -l`) and then peek at the first few rows."
|
||||
]
|
||||
@@ -441,7 +453,7 @@
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "quick_peek:csv"
|
||||
"id": "1db4b3d511a6"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
@@ -465,10 +477,10 @@
|
||||
"source": [
|
||||
"### Create the Dataset\n",
|
||||
"\n",
|
||||
"Next, create the `Dataset` resource using the `create` method for the `ImageDataset` class, which takes the following parameters:\n",
|
||||
"Next, create the dataset resource using the `create` method for the `ImageDataset` class, which takes the following parameters:\n",
|
||||
"\n",
|
||||
"- `display_name`: The human readable name for the `Dataset` resource.\n",
|
||||
"- `gcs_source`: A list of one or more dataset index files to import the data items into the `Dataset` resource.\n",
|
||||
"- `display_name`: The human readable name for the dataset resource.\n",
|
||||
"- `gcs_source`: A list of one or more dataset index files to import the data items into the dataset resource.\n",
|
||||
"- `import_schema_uri`: The data labeling schema for the data items.\n",
|
||||
"\n",
|
||||
"This operation may take several minutes."
|
||||
@@ -505,7 +517,7 @@
|
||||
"\n",
|
||||
"An AutoML training pipeline is created with the `AutoMLImageTrainingJob` class, with the following parameters:\n",
|
||||
"\n",
|
||||
"- `display_name`: The human readable name for the `TrainingJob` resource.\n",
|
||||
"- `display_name`: The human readable name for the TrainingJob resource.\n",
|
||||
"- `prediction_type`: The type task to train the model for.\n",
|
||||
" - `classification`: An image classification model.\n",
|
||||
" - `object_detection`: An image object detection model.\n",
|
||||
@@ -517,7 +529,7 @@
|
||||
" - `MOBILE_TF_VERSATILE_1`: Deployment on an edge device.\n",
|
||||
" - `MOBILE_TF_HIGH_ACCURACY_1`:Optimized for accuracy over latency for deployment on an edge device.\n",
|
||||
" - `MOBILE_TF_LOW_LATENCY_1`: Optimized for latency over accuracy for deployment on an edge device.\n",
|
||||
"- `base_model`: (optional) Transfer learning from existing `Model` resource -- supported for image classification only.\n",
|
||||
"- `base_model`: (optional) Transfer learning from existing model resource -- supported for image classification only.\n",
|
||||
"\n",
|
||||
"The instantiated object is the DAG (directed acyclic graph) for the training job."
|
||||
]
|
||||
@@ -551,15 +563,15 @@
|
||||
"\n",
|
||||
"Next, you run the DAG to start the training job by invoking the method `run`, with the following parameters:\n",
|
||||
"\n",
|
||||
"- `dataset`: The `Dataset` resource to train the model.\n",
|
||||
"- `dataset`: The dataset resource to train the model.\n",
|
||||
"- `model_display_name`: The human readable name for the trained model.\n",
|
||||
"- `training_fraction_split`: The percentage of the dataset to use for training.\n",
|
||||
"- `test_fraction_split`: The percentage of the dataset to use for test (holdout data).\n",
|
||||
"- `validation_fraction_split`: The percentage of the dataset to use for validation.\n",
|
||||
"- `budget_milli_node_hours`: (optional) Maximum training time specified in unit of millihours (1000 = hour).\n",
|
||||
"- `disable_early_stopping`: If `True`, training maybe completed before using the entire budget if the service believes it cannot further improve on the model objective measurements.\n",
|
||||
"- `disable_early_stopping`: If `True`, the entire budget is used. Else, training maybe completed before using the entire budget if the service believes it cannot further improve on the model objective measurements.\n",
|
||||
"\n",
|
||||
"The `run` method when completed returns the `Model` resource.\n",
|
||||
"The `run` method when completed returns the model resource.\n",
|
||||
"\n",
|
||||
"The execution of the training pipeline will take upto 60 minutes."
|
||||
]
|
||||
@@ -616,7 +628,7 @@
|
||||
"source": [
|
||||
"## Export as Edge model\n",
|
||||
"\n",
|
||||
"You can export an AutoML image object detection model as a `Edge` model which you can then custom deploy to an edge device or download locally. Use the method `export_model()` to export the model to Cloud Storage, which takes the following parameters:\n",
|
||||
"You can export an AutoML image object detection model as a edge model which you can then custom deploy to an edge device or download locally. Use the method `export_model()` to export the model to Cloud Storage, which takes the following parameters:\n",
|
||||
"\n",
|
||||
"- `artifact_destination`: The Cloud Storage location to store the SavedFormat model artifacts to.\n",
|
||||
"- `export_format_id`: The format to save the model format as. For AutoML image object detection there is just one option:\n",
|
||||
@@ -811,7 +823,7 @@
|
||||
"# Delete the AutoML trainig job\n",
|
||||
"dag.delete()\n",
|
||||
"\n",
|
||||
"if delete_bucket or os.getenv(\"IS_TESTING\"):\n",
|
||||
"if delete_bucket:\n",
|
||||
" ! gsutil rm -r $BUCKET_URI"
|
||||
]
|
||||
}
|
||||
|
||||
@@ -29,28 +29,30 @@
|
||||
"id": "title:generic,gcp"
|
||||
},
|
||||
"source": [
|
||||
"# Get started with AutoML Training\n",
|
||||
"# Get started with AutoML training\n",
|
||||
"\n",
|
||||
"<table align=\"left\">\n",
|
||||
" <td>\n",
|
||||
" <a href=\"https://github.com/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/official/automl/get_started_automl_training.ipynb\">\n",
|
||||
" <img src=\"https://cloud.google.com/ml-engine/images/github-logo-32px.png\" alt=\"GitHub logo\">\n",
|
||||
" View on GitHub\n",
|
||||
" <td style=\"text-align: center\">\n",
|
||||
" <a href=\"https://colab.research.google.com/github/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/official/automl/get_started_automl_training.ipynb\">\n",
|
||||
" <img src=\"https://cloud.google.com/ml-engine/images/colab-logo-32px.png\" alt=\"Google Colaboratory logo\"><br> Open in Colab\n",
|
||||
" </a>\n",
|
||||
" </td>\n",
|
||||
" <td>\n",
|
||||
" <a href=\"https://colab.research.google.com/github/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/official/automl/get_started_automl_training.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",
|
||||
" <td style=\"text-align: center\">\n",
|
||||
" <a href=\"https://console.cloud.google.com/vertex-ai/colab/import/https:%2F%2Fraw.githubusercontent.com%2FGoogleCloudPlatform%2Fvertex-ai-samples%2Fmain%2Fnotebooks%2Fofficial%2Fautoml%2Fget_started_automl_training.ipynb\">\n",
|
||||
" <img width=\"32px\" src=\"https://cloud.google.com/ml-engine/images/colab-enterprise-logo-32px.png\" alt=\"Google Cloud Colab Enterprise logo\"><br> Open in Colab Enterprise\n",
|
||||
" </a>\n",
|
||||
" </td> \n",
|
||||
" <td style=\"text-align: center\">\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/automl/get_started_automl_training.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",
|
||||
" <img src=\"https://lh3.googleusercontent.com/UiNooY4LUgW_oTvpsNhPpQzsstV5W8F7rYgxgGBD85cWJoLmrOzhVs_ksK_vgx40SHs7jCqkTkCk=e14-rj-sc0xffffff-h130-w32\" alt=\"Vertex AI logo\"><br> Open in Workbench\n",
|
||||
" </a>\n",
|
||||
" </td>\n",
|
||||
"</table>\n",
|
||||
"<br/><br/><br/>"
|
||||
" <td style=\"text-align: center\">\n",
|
||||
" <a href=\"https://github.com/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/official/automl/get_started_automl_training.ipynb\">\n",
|
||||
" <img src=\"https://cloud.google.com/ml-engine/images/github-logo-32px.png\" alt=\"GitHub logo\"><br> View on GitHub\n",
|
||||
" </a>\n",
|
||||
" </td>\n",
|
||||
"</table>"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -79,7 +81,7 @@
|
||||
"\n",
|
||||
"This tutorial uses the following Google Cloud ML services:\n",
|
||||
"\n",
|
||||
"- `AutoML Training`\n",
|
||||
"- `AutoML training`\n",
|
||||
"- `Vertex AI Datasets`\n",
|
||||
"\n",
|
||||
"The steps performed include:\n",
|
||||
@@ -147,15 +149,22 @@
|
||||
"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": "2b9e4bcab250"
|
||||
},
|
||||
"source": [
|
||||
"## Get Started"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "install_mlops"
|
||||
},
|
||||
"source": [
|
||||
"## Installations\n",
|
||||
"\n",
|
||||
"Install the packages required for executing this notebook."
|
||||
"### Install Vertex AI SDK for Python and other required packages"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -177,41 +186,80 @@
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "restart"
|
||||
"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": "D-ZBOjErv5mM"
|
||||
"id": "157953ab28f0"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Automatically restart kernel after installs so that your environment can access the new packages\n",
|
||||
"# 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": "yfEglUHQk9S3"
|
||||
"id": "b96b39fd4d7b"
|
||||
},
|
||||
"source": [
|
||||
"## Before you begin\n",
|
||||
"<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>"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "ff666ce4051c"
|
||||
},
|
||||
"source": [
|
||||
"### Authenticate your notebook environment (Colab only)\n",
|
||||
"\n",
|
||||
"### Set your project ID\n",
|
||||
"Authenticate your environment on Google Colab."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "cc7251520a07"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import sys\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)"
|
||||
"if \"google.colab\" in sys.modules:\n",
|
||||
"\n",
|
||||
" from google.colab import auth\n",
|
||||
"\n",
|
||||
" auth.authenticate_user()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "b02382a1fea6"
|
||||
},
|
||||
"source": [
|
||||
"### Set Google Cloud project information\n",
|
||||
"\n",
|
||||
"To get started using Vertex AI, you must have an existing Google Cloud project. Learn more about [setting up a project and a development environment](https://cloud.google.com/vertex-ai/docs/start/cloud-environment)."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -223,89 +271,7 @@
|
||||
"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": "gcp_authenticate"
|
||||
},
|
||||
"source": [
|
||||
"### Authenticate your Google Cloud account\n",
|
||||
"\n",
|
||||
"Depending on your Jupyter environment, you may have to manually authenticate. Follow the relevant instructions below.\n",
|
||||
"\n",
|
||||
"**1. Vertex AI Workbench**\n",
|
||||
"* Do nothing as you are already authenticated.\n",
|
||||
"\n",
|
||||
"**2. Local JupyterLab instance, uncomment and run:**"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "ce6043da7b33"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# ! gcloud auth login"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "0367eac06a10"
|
||||
},
|
||||
"source": [
|
||||
"**3. Colab, uncomment and run:**"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "21ad4dbb4a61"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# from google.colab import auth\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."
|
||||
"LOCATION = \"us-central1\" # @param {type: \"string\"}"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -347,7 +313,7 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"! gsutil mb -l $REGION $BUCKET_URI"
|
||||
"! gsutil mb -l $LOCATION $BUCKET_URI"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -591,9 +557,7 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"IMPORT_FILE = (\n",
|
||||
" \"gs://cloud-samples-data/vision/automl_classification/flowers/all_data_v2.csv\"\n",
|
||||
")"
|
||||
"IMPORT_FILE = \"gs://cloud-samples-data/ai-platform/flowers/flowers.csv\""
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -745,6 +709,8 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import os\n",
|
||||
"\n",
|
||||
"if os.getenv(\"IS_TESTING\"):\n",
|
||||
" sys.exit(0)"
|
||||
]
|
||||
@@ -834,7 +800,7 @@
|
||||
"source": [
|
||||
"### Get test item\n",
|
||||
"\n",
|
||||
"You will use an arbitrary example out of the dataset as a test item. Don't be concerned that the example was likely used in training the model. You are just looking at how to make a prediction."
|
||||
"You use an arbitrary example out of the dataset as a test item. Don't be concerned that the example was likely used in training the model. You're just looking at how to make a prediction."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -949,7 +915,7 @@
|
||||
"source": [
|
||||
"#### Undeploy the model\n",
|
||||
"\n",
|
||||
"When you are done doing predictions, you undeploy the model from the `Endpoint` resouce. This deprovisions all compute resources and ends billing for the deployed model."
|
||||
"When you're done doing predictions, undeploy the model from the `Endpoint` resource. This deprovisions all compute resources and ends billing for the deployed model."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -1351,7 +1317,7 @@
|
||||
"source": [
|
||||
"#### Undeploy the model\n",
|
||||
"\n",
|
||||
"When you are done doing predictions, you undeploy the model from the `Endpoint` resouce. This deprovisions all compute resources and ends billing for the deployed model."
|
||||
"When you're done doing predictions, undeploy the model from the `Endpoint` resource. This deprovisions all compute resources and ends billing for the deployed model."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -1851,7 +1817,7 @@
|
||||
"source": [
|
||||
"#### Undeploy the model\n",
|
||||
"\n",
|
||||
"When you are done doing predictions, you undeploy the model from the `Endpoint` resouce. This deprovisions all compute resources and ends billing for the deployed model."
|
||||
"When you're done doing predictions, undeploy the model from the `Endpoint` resource. This deprovisions all compute resources and ends billing for the deployed model."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -2341,10 +2307,9 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Set this to true only if you'd like to delete your bucket\n",
|
||||
"delete_bucket = False\n",
|
||||
"\n",
|
||||
"if delete_bucket or os.getenv(\"IS_TESTING\"):\n",
|
||||
"# Delete the Cloud Storage bucket\n",
|
||||
"delete_bucket = False # Set True for deletion\n",
|
||||
"if delete_bucket:\n",
|
||||
" ! gsutil rm -r $BUCKET_URI"
|
||||
]
|
||||
}
|
||||
|
||||
@@ -32,23 +32,26 @@
|
||||
"# Vertex AI SDK: AutoML training tabular regression model for online prediction using BigQuery\n",
|
||||
"\n",
|
||||
"<table align=\"left\">\n",
|
||||
" <td>\n",
|
||||
"<a href=\"https://colab.research.google.com/github/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/official/automl/sdk_automl_tabular_regression_online_bq.ipynb\" target='_blank'>\n",
|
||||
" <img src=\"https://cloud.google.com/ml-engine/images/colab-logo-32px.png\" alt=\"Colab logo\"> Run in Colab\n",
|
||||
" <td style=\"text-align: center\">\n",
|
||||
" <a href=\"https://colab.research.google.com/github/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/official/pipelines/lightweight_functions_component_io_kfp.ipynb\">\n",
|
||||
" <img src=\"https://cloud.google.com/ml-engine/images/colab-logo-32px.png\" alt=\"Google Colaboratory logo\"><br> Open in Colab\n",
|
||||
" </a>\n",
|
||||
" </td>\n",
|
||||
" <td>\n",
|
||||
"<a href=\"https://github.com/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/official/automl/sdk_automl_tabular_regression_online_bq.ipynb\" target='_blank'> \n",
|
||||
" <img src=\"https://cloud.google.com/ml-engine/images/github-logo-32px.png\" alt=\"GitHub logo\">\n",
|
||||
" View on GitHub\n",
|
||||
" <td style=\"text-align: center\">\n",
|
||||
" <a href=\"https://console.cloud.google.com/vertex-ai/colab/import/https:%2F%2Fraw.githubusercontent.com%2FGoogleCloudPlatform%2Fvertex-ai-samples%2Fmain%2Fnotebooks%2Fnotebook_template.ipynb\">\n",
|
||||
" <img src=\"https://cloud.google.com/ml-engine/images/colab-enterprise-logo-32px.png\" alt=\"Google Cloud Colab Enterprise logo\"><br> Open in Colab Enterprise\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/automl/sdk_automl_tabular_regression_online_bq.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",
|
||||
" <td style=\"text-align: center\">\n",
|
||||
" <a href=\"https://github.com/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/official/pipelines/lightweight_functions_component_io_kfp.ipynb\">\n",
|
||||
" <img src=\"https://cloud.google.com/ml-engine/images/github-logo-32px.png\" alt=\"GitHub logo\"><br> View on GitHub\n",
|
||||
" </a>\n",
|
||||
" </td>\n",
|
||||
" <td style=\"text-align: center\">\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/pipelines/lightweight_functions_component_io_kfp.ipynb\" target='_blank'>\n",
|
||||
" <img src=\"https://lh3.googleusercontent.com/UiNooY4LUgW_oTvpsNhPpQzsstV5W8F7rYgxgGBD85cWJoLmrOzhVs_ksK_vgx40SHs7jCqkTkCk=e14-rj-sc0xffffff-h130-w32\" alt=\"Vertex AI logo\"><br> Open in Vertex AI Workbench\n",
|
||||
" </a>\n",
|
||||
" </td>\n",
|
||||
"</table>\n",
|
||||
"<br/><br/><br/>"
|
||||
]
|
||||
@@ -79,12 +82,12 @@
|
||||
"\n",
|
||||
"The steps performed include:\n",
|
||||
"\n",
|
||||
"- Create a Vertex `Dataset` resource.\n",
|
||||
"- Create a Vertex dataset resource.\n",
|
||||
"- Train the model.\n",
|
||||
"- View the model evaluation.\n",
|
||||
"- Deploy the `Model` resource to a serving `Endpoint` resource.\n",
|
||||
"- Deploy the model resource to a serving Endpoint resource.\n",
|
||||
"- Make a prediction.\n",
|
||||
"- Undeploy the `Model`."
|
||||
"- Undeploy the model."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -111,11 +114,9 @@
|
||||
"* Vertex AI\n",
|
||||
"* Cloud Storage\n",
|
||||
"\n",
|
||||
"Learn about [Vertex AI\n",
|
||||
"pricing](https://cloud.google.com/vertex-ai/pricing) and [Cloud Storage\n",
|
||||
"pricing](https://cloud.google.com/storage/pricing), and use the [Pricing\n",
|
||||
"Calculator](https://cloud.google.com/products/calculator/)\n",
|
||||
"to generate a cost estimate based on your projected usage."
|
||||
"Learn about [Vertex AI pricing](https://cloud.google.com/vertex-ai/pricing) and \n",
|
||||
"[Cloud Storage pricing](https://cloud.google.com/storage/pricing), and use the \n",
|
||||
"[Pricing Calculator](https://cloud.google.com/products/calculator/) to generate a cost estimate based on your projected usage."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -124,9 +125,8 @@
|
||||
"id": "install_aip:mbsdk"
|
||||
},
|
||||
"source": [
|
||||
"## Installation\n",
|
||||
"\n",
|
||||
"Install the latest version of Vertex AI SDK for Python."
|
||||
"## Get Started\n",
|
||||
"Install Vertex AI SDK for Python and other required packages"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -147,7 +147,8 @@
|
||||
"id": "restart"
|
||||
},
|
||||
"source": [
|
||||
"### Colab only: Uncomment the following cell to restart the kernel"
|
||||
"### Restart runtime (Colab only)\n",
|
||||
"To use the newly installed packages, you must restart the runtime on Google Colab."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -158,11 +159,52 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Automatically restart kernel after installs so that your environment can access the new packages\n",
|
||||
"# 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": "4de1bd77992b"
|
||||
},
|
||||
"source": [
|
||||
"<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>"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "befa6ca14bc0"
|
||||
},
|
||||
"source": [
|
||||
"### Authenticate your notebook environment (Colab only)\n",
|
||||
"Authenticate your environment on Google Colab."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "7de6ef0fac42"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import sys\n",
|
||||
"\n",
|
||||
"if \"google.colab\" in sys.modules:\n",
|
||||
"\n",
|
||||
" from google.colab import auth\n",
|
||||
"\n",
|
||||
" auth.authenticate_user()"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -171,19 +213,8 @@
|
||||
"id": "before_you_begin:nogpu"
|
||||
},
|
||||
"source": [
|
||||
"## Before you begin\n",
|
||||
"\n",
|
||||
"### GPU runtime\n",
|
||||
"\n",
|
||||
"This tutorial does not require a GPU runtime.\n",
|
||||
"\n",
|
||||
"### Set up your Google Cloud project\n",
|
||||
"\n",
|
||||
"**If you dont know your project ID,** try the following\n",
|
||||
"\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)\n"
|
||||
"### Set Google Cloud project information\n",
|
||||
"Learn more about [setting up a project and a development environment.](https://cloud.google.com/vertex-ai/docs/start/cloud-environment)\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -195,89 +226,7 @@
|
||||
"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": "gcp_authenticate"
|
||||
},
|
||||
"source": [
|
||||
"### Authenticate your Google Cloud account\n",
|
||||
"\n",
|
||||
"Depending on your Jupyter environment, you may have to manually authenticate. Follow the relevant instructions below.\n",
|
||||
"\n",
|
||||
"**1. Vertex AI workbench** \n",
|
||||
"- Do nothing as you are already authenticated.\n",
|
||||
"\n",
|
||||
"**2. Local JupyterLab Instance, uncomment and run:**"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "457c78b08293"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# ! gcloud auth login"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "d3e571ce6c56"
|
||||
},
|
||||
"source": [
|
||||
"**3. Colab, uncomment and run:**"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "984a0526fb68"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# from google.colab import auth\n",
|
||||
"# auth.authenticate_user()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "764c0ac706e1"
|
||||
},
|
||||
"source": [
|
||||
"**4. Service account or other**\n",
|
||||
"- See all the authentication options here: [Google Cloud Platform Jupyter Notebook Authentication Guide](https://github.com/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/notebook_authentication_guide.ipynb)"
|
||||
"LOCATION = \"us-central1\""
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -298,7 +247,7 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"BUCKET_URI = \"gs://test-bucket-unique\" # @param {type:\"string\"}"
|
||||
"BUCKET_URI = f\"gs://your-bucket-name-{PROJECT_ID}-unique\" # @param {type:\"string\"}"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -307,7 +256,7 @@
|
||||
"id": "create_bucket"
|
||||
},
|
||||
"source": [
|
||||
"**Only if your bucket doesn't already exist**: Run the following cell to create your Cloud Storage bucket."
|
||||
"**If your bucket doesn't already exist**: Run the following cell to create your Cloud Storage bucket."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -318,7 +267,7 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"! gsutil mb -l $REGION $BUCKET_URI"
|
||||
"! gsutil mb -l $LOCATION $BUCKET_URI"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -338,8 +287,6 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import os\n",
|
||||
"\n",
|
||||
"import google.cloud.aiplatform as aiplatform\n",
|
||||
"\n",
|
||||
"display_name = \"gsod_unique\""
|
||||
@@ -364,7 +311,7 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"aiplatform.init(project=PROJECT_ID, location=REGION)"
|
||||
"aiplatform.init(project=PROJECT_ID, location=LOCATION)"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -408,10 +355,10 @@
|
||||
"source": [
|
||||
"### Create the Dataset\n",
|
||||
"\n",
|
||||
"Next, create the `Dataset` resource using the `create` method for the `TabularDataset` class, which takes the following parameters:\n",
|
||||
"Next, create the dataset resource using the `create` method for the `TabularDataset` class, which takes the following parameters:\n",
|
||||
"\n",
|
||||
"- `display_name`: The human readable name for the `Dataset` resource.\n",
|
||||
"- `bq_source`: Alternatively, import data items from a BigQuery table into the `Dataset` resource.\n",
|
||||
"- `display_name`: The human readable name for the dataset resource.\n",
|
||||
"- `bq_source`: Alternatively, import data items from a BigQuery table into the dataset resource.\n",
|
||||
"\n",
|
||||
"This operation may take several minutes."
|
||||
]
|
||||
@@ -471,9 +418,9 @@
|
||||
" - `regression`: A tabular regression model.\n",
|
||||
"- `column_transformations`: (Optional): Transformations to apply to the input columns\n",
|
||||
"- `optimization_objective`: The optimization objective to minimize or maximize.\n",
|
||||
" - binary classification:\n",
|
||||
" - `binary classification`:\n",
|
||||
" - `minimize-log-loss`\n",
|
||||
" - `maximize-au-roc`\n",
|
||||
" -`maximize-au-roc`\n",
|
||||
" - `maximize-au-prc`\n",
|
||||
" - `maximize-precision-at-recall`\n",
|
||||
" - `maximize-recall-at-precision`\n",
|
||||
@@ -515,18 +462,18 @@
|
||||
"\n",
|
||||
"Next, you run the DAG (object 'job') to start the training job by invoking the method `run`, with the following parameters:\n",
|
||||
"\n",
|
||||
"- `dataset`: The `Dataset` resource to train the model.\n",
|
||||
"- `dataset`: The dataset resource to train the model.\n",
|
||||
"- `model_display_name`: The human readable name for the trained model.\n",
|
||||
"- `training_fraction_split`: The percentage of the dataset to use for training.\n",
|
||||
"- `test_fraction_split`: The percentage of the dataset to use for test (holdout data).\n",
|
||||
"- `validation_fraction_split`: The percentage of the dataset to use for validation.\n",
|
||||
"- `target_column`: The name of the column to train as the label.\n",
|
||||
"- `budget_milli_node_hours`: (optional) Maximum training time specified in unit of millihours (1000 = hour).\n",
|
||||
"- `disable_early_stopping`: If `True`, training maybe completed before using the entire budget if the service believes it cannot further improve on the model objective measurements.\n",
|
||||
"- `disable_early_stopping`: If `True`, training maybe completed before using the entire budget if the service believes it can't further improve on the model objective measurements.\n",
|
||||
"\n",
|
||||
"The `run` method when completed returns the `Model` resource.\n",
|
||||
"\n",
|
||||
"The execution of the training pipeline will take upto 8 hours."
|
||||
"The execution of the training pipeline takes upto 8 hours."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -556,7 +503,7 @@
|
||||
},
|
||||
"source": [
|
||||
"## Review model evaluation scores\n",
|
||||
"After your model training has finished, you can review the evaluation scores for it using the list_model_evaluations() method."
|
||||
"After your model training has finished, you can review the evaluation scores for it using the `list_model_evaluations()` method."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -617,7 +564,7 @@
|
||||
"source": [
|
||||
"### Make test item\n",
|
||||
"\n",
|
||||
"You will use synthetic data as a test data item. Don't be concerned that we are using synthetic data -- we just want to demonstrate how to make a prediction."
|
||||
"You use synthetic data as a test data item. Don't be concerned that you're using synthetic data -- you just want to demonstrate how to make a prediction."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -733,9 +680,9 @@
|
||||
"# Delete the AutoML trainig job\n",
|
||||
"job.delete()\n",
|
||||
"\n",
|
||||
"delete_bucket = False\n",
|
||||
"delete_bucket = False # set True to delete bucket\n",
|
||||
"\n",
|
||||
"if delete_bucket or os.getenv(\"IS_TESTING\"):\n",
|
||||
"if delete_bucket:\n",
|
||||
" ! gsutil rm -r $BUCKET_URI"
|
||||
]
|
||||
}
|
||||
|
||||
@@ -32,23 +32,28 @@
|
||||
"# Vertex AI SDK for Python: AutoML training video object tracking model for batch prediction\n",
|
||||
"\n",
|
||||
"<table align=\"left\">\n",
|
||||
" <td>\n",
|
||||
" <td style=\"text-align: center\">\n",
|
||||
" <a href=\"https://colab.research.google.com/github/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/official/automl/sdk_automl_video_object_tracking_batch.ipynb\">\n",
|
||||
" <img src=\"https://cloud.google.com/ml-engine/images/colab-logo-32px.png\" alt=\"Colab logo\"> Run in Colab\n",
|
||||
" <img src=\"https://cloud.google.com/ml-engine/images/colab-logo-32px.png\" alt=\"Google Colaboratory logo\"><br> Open in Colab\n",
|
||||
" </a>\n",
|
||||
" </td>\n",
|
||||
" <td>\n",
|
||||
" <a href=\"https://github.com/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/official/automl/sdk_automl_video_object_tracking_batch.ipynb\">\n",
|
||||
" <img src=\"https://cloud.google.com/ml-engine/images/github-logo-32px.png\" alt=\"GitHub logo\">\n",
|
||||
" View on GitHub\n",
|
||||
" <td style=\"text-align: center\">\n",
|
||||
" <a href=\"https://console.cloud.google.com/vertex-ai/colab/import/https:%2F%2Fraw.githubusercontent.com%2FGoogleCloudPlatform%2Fvertex-ai-samples%2Fmain%2Fnotebooks%2Fofficial%2Fautoml%2Fsdk_automl_video_object_tracking_batch.ipynb\">\n",
|
||||
" <img width=\"32px\" src=\"https://cloud.google.com/ml-engine/images/colab-enterprise-logo-32px.png\" alt=\"Google Cloud Colab Enterprise logo\"><br> Open in Colab Enterprise\n",
|
||||
" </a>\n",
|
||||
" </td>\n",
|
||||
" <td>\n",
|
||||
" <td style=\"text-align: center\">\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/automl/sdk_automl_video_object_tracking_batch.ipynb\" target='_blank'>\n",
|
||||
" <img src=\"https://lh3.googleusercontent.com/UiNooY4LUgW_oTvpsNhPpQzsstV5W8F7rYgxgGBD85cWJoLmrOzhVs_ksK_vgx40SHs7jCqkTkCk=e14-rj-sc0xffffff-h130-w32\" alt=\"Vertex AI logo\">\n",
|
||||
" <img src=\"https://lh3.googleusercontent.com/UiNooY4LUgW_oTvpsNhPpQzsstV5W8F7rYgxgGBD85cWJoLmrOzhVs_ksK_vgx40SHs7jCqkTkCk=e14-rj-sc0xffffff-h130-w32\" alt=\"Vertex AI logo\"><br>\n",
|
||||
" Open in Vertex AI Workbench\n",
|
||||
" </a>\n",
|
||||
" </td>\n",
|
||||
" <td style=\"text-align: center\">\n",
|
||||
" <a href=\"https://github.com/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/official/automl/sdk_automl_video_object_tracking_batch.ipynb\">\n",
|
||||
" <img src=\"https://cloud.google.com/ml-engine/images/github-logo-32px.png\" alt=\"GitHub logo\"><br>\n",
|
||||
" View on GitHub\n",
|
||||
" </a>\n",
|
||||
" </td>\n",
|
||||
"</table>\n",
|
||||
"<br/><br/><br/>"
|
||||
]
|
||||
@@ -75,15 +80,16 @@
|
||||
"source": [
|
||||
"### Objective\n",
|
||||
"\n",
|
||||
"In this tutorial, you learn how to create an AutoML video object tracking model from a Python script, and then do a batch prediction using the Vertex AI SDK. You can alternatively create and deploy models using the `gcloud` command-line tool or online using the Cloud Console.\n",
|
||||
"In this tutorial, you learn how to create an AutoML video object tracking model from a Python script, and then do a batch prediction using the Vertex AI SDK for Python. You can alternatively create and deploy models using the `gcloud` command-line tool or online using the Cloud Console.\n",
|
||||
"\n",
|
||||
"This tutorial uses the following Google Cloud ML services and resources:\n",
|
||||
"This tutorial uses the following Google Cloud services and resources:\n",
|
||||
"\n",
|
||||
"- Vertex AI\n",
|
||||
"- Google Cloud Storage\n",
|
||||
"\n",
|
||||
"The steps performed include:\n",
|
||||
"\n",
|
||||
"- Create a Vertex `Dataset` resource.\n",
|
||||
"- Create a Vertex dataset resource.\n",
|
||||
"- Train the model.\n",
|
||||
"- View the model evaluation.\n",
|
||||
"- Make a batch prediction.\n",
|
||||
@@ -129,160 +135,121 @@
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "install_aip:mbsdk"
|
||||
"id": "f0316df526f8"
|
||||
},
|
||||
"source": [
|
||||
"## Installation\n",
|
||||
"\n",
|
||||
"Install the following packages required to execute this notebook."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "install_aip:mbsdk"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import os\n",
|
||||
"\n",
|
||||
"! pip3 install --upgrade --quiet google-cloud-aiplatform \\\n",
|
||||
" google-cloud-storage"
|
||||
"## Get started"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "restart"
|
||||
"id": "a2c2cb2109a0"
|
||||
},
|
||||
"source": [
|
||||
"### Colab only: Uncomment the following cell to restart the kernel"
|
||||
"### Install Vertex AI SDK for Python and other required packages\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "D-ZBOjErv5mM"
|
||||
"id": "514a03ed1a82"
|
||||
},
|
||||
"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)"
|
||||
"! pip3 install --upgrade --quiet google-cloud-aiplatform google-cloud-storage"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "yfEglUHQk9S3"
|
||||
"id": "ff555b32bab8"
|
||||
},
|
||||
"source": [
|
||||
"## Before you begin\n",
|
||||
"### Restart runtime (Colab only)\n",
|
||||
"\n",
|
||||
"### 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)"
|
||||
"To use the newly installed packages, you must restart the runtime on Google Colab."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "set_project_id"
|
||||
"id": "f09b4dff629a"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import sys\n",
|
||||
"\n",
|
||||
"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": "ee775571c2b5"
|
||||
},
|
||||
"source": [
|
||||
"<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": "92e68cfc3a90"
|
||||
},
|
||||
"source": [
|
||||
"### Authenticate your notebook environment (Colab only)\n",
|
||||
"\n",
|
||||
"Authenticate your environment on Google Colab.\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "46604f70e831"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"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": "4f872cd812d0"
|
||||
},
|
||||
"source": [
|
||||
"### Set Google Cloud project information and initialize Vertex AI SDK for Python\n",
|
||||
"\n",
|
||||
"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": "294fe4e5a671"
|
||||
},
|
||||
"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": "gcp_authenticate"
|
||||
},
|
||||
"source": [
|
||||
"### Authenticate your Google Cloud account\n",
|
||||
"\n",
|
||||
"Depending on your Jupyter environment, you may have to manually authenticate. Follow the relevant instructions below.\n",
|
||||
"\n",
|
||||
"**1. Vertex AI Workbench**\n",
|
||||
"* Do nothing as you are already authenticated.\n",
|
||||
"\n",
|
||||
"**2. Local JupyterLab instance, uncomment and run:**"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "ce6043da7b33"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# ! gcloud auth login"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "0367eac06a10"
|
||||
},
|
||||
"source": [
|
||||
"**3. Colab, uncomment and run:**"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "21ad4dbb4a61"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# from google.colab import auth\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."
|
||||
"LOCATION = \"us-central1\" # @param {type:\"string\"}"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -313,7 +280,7 @@
|
||||
"id": "create_bucket"
|
||||
},
|
||||
"source": [
|
||||
"**Only if your bucket doesn't already exist**: Run the following cell to create your Cloud Storage bucket."
|
||||
"**If your bucket doesn't already exist**: Run the following cell to create your Cloud Storage bucket."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -324,7 +291,31 @@
|
||||
},
|
||||
"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": "init_aip:mbsdk"
|
||||
},
|
||||
"source": [
|
||||
"### Initialize Vertex AI SDK for Python\n",
|
||||
"\n",
|
||||
"Initialize the Vertex AI SDK for Python for your project and corresponding bucket."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "init_aip:mbsdk"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from google.cloud import aiplatform\n",
|
||||
"\n",
|
||||
"aiplatform.init(project=PROJECT_ID, location=LOCATION, staging_bucket=BUCKET_URI)"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -345,34 +336,10 @@
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import json\n",
|
||||
"import os\n",
|
||||
"\n",
|
||||
"import google.cloud.aiplatform as aiplatform\n",
|
||||
"from google.cloud import storage"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "init_aip:mbsdk"
|
||||
},
|
||||
"source": [
|
||||
"## Initialize Vertex AI SDK for Python\n",
|
||||
"\n",
|
||||
"Initialize the Vertex AI SDK for Python for your project and corresponding bucket."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "init_aip:mbsdk"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"aiplatform.init(project=PROJECT_ID, staging_bucket=BUCKET_URI)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
@@ -515,12 +482,12 @@
|
||||
"\n",
|
||||
"Next, you start the training job by invoking the method `run`, with the following parameters:\n",
|
||||
"\n",
|
||||
"- `dataset`: The `Dataset` resource to train the model.\n",
|
||||
"- `dataset`: The dataset resource to train the model.\n",
|
||||
"- `model_display_name`: The human readable name for the trained model.\n",
|
||||
"- `training_fraction_split`: The percentage of the dataset to use for training.\n",
|
||||
"- `test_fraction_split`: The percentage of the dataset to use for test (holdout data).\n",
|
||||
"\n",
|
||||
"The `run` method when completed returns the `Model` resource.\n",
|
||||
"The `run` method when completed returns the model resource.\n",
|
||||
"\n",
|
||||
"The execution of the training pipeline will take upto 4 hours."
|
||||
]
|
||||
@@ -832,7 +799,7 @@
|
||||
"batch_predict_job.delete()\n",
|
||||
"\n",
|
||||
"delete_bucket = False\n",
|
||||
"if delete_bucket or os.getenv(\"IS_TESTING\"):\n",
|
||||
"if delete_bucket:\n",
|
||||
" ! gsutil -m rm -r $BUCKET_URI"
|
||||
]
|
||||
}
|
||||
|
||||
@@ -38,7 +38,7 @@
|
||||
" </a>\n",
|
||||
" </td>\n",
|
||||
" <td style=\"text-align: center\">\n",
|
||||
" <a href=\"https://console.cloud.google.com/vertex-ai/colab/import/https:%2F%2Fgithub.com%2FGoogleCloudPlatform%2Fvertex-ai-samples%2Fblob%2Fmain%2Fnotebooks%2Fofficial%2Fcustom%2Fget_started_with_vertex_endpoint_and_shared_vm.ipynb\">\n",
|
||||
" <a href=\"https://console.cloud.google.com/vertex-ai/colab/import/https:%2F%2Fraw.githubusercontent.com%2FGoogleCloudPlatform%2Fvertex-ai-samples%2F%2Fmain%2Fnotebooks%2Fofficial%2Fcustom%2Fget_started_with_vertex_endpoint_and_shared_vm.ipynb\">\n",
|
||||
" <img width=\"32px\" src=\"https://cloud.google.com/ml-engine/images/colab-enterprise-logo-32px.png\" alt=\"Google Cloud Colab Enterprise logo\"><br> Open in Colab Enterprise\n",
|
||||
" </a>\n",
|
||||
" </td> \n",
|
||||
|
||||
+131
-202
@@ -33,23 +33,28 @@
|
||||
"\n",
|
||||
"<table align=\"left\">\n",
|
||||
"\n",
|
||||
" <td>\n",
|
||||
" <td style=\"text-align: center\">\n",
|
||||
" <a href=\"https://colab.research.google.com/github/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/official/experiments/get_started_with_custom_training_autologging_local_script.ipynb\">\n",
|
||||
" <img src=\"https://cloud.google.com/ml-engine/images/colab-logo-32px.png\" alt=\"Colab logo\"> Run in Colab\n",
|
||||
" <img src=\"https://cloud.google.com/ml-engine/images/colab-logo-32px.png\" alt=\"Colab logo\"><br> Run in Colab\n",
|
||||
" </a>\n",
|
||||
" </td>\n",
|
||||
" <td>\n",
|
||||
" <a href=\"https://github.com/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/official/experiments/get_started_with_custom_training_autologging_local_script.ipynb\">\n",
|
||||
" <img src=\"https://cloud.google.com/ml-engine/images/github-logo-32px.png\" alt=\"GitHub logo\">\n",
|
||||
" View on GitHub\n",
|
||||
" <td style=\"text-align: center\">\n",
|
||||
" <a href=\"https://console.cloud.google.com/vertex-ai/colab/import/https:%2F%2Fraw.githubusercontent.com%2FGoogleCloudPlatform%2Fvertex-ai-samples%2Fmain%2Fnotebooks%2Fofficial%2Fexperiments%2Fget_started_with_custom_training_autologging_local_script.ipynb\">\n",
|
||||
" <img width=\"32px\" src=\"https://cloud.google.com/ml-engine/images/colab-enterprise-logo-32px.png\" alt=\"Google Cloud Colab Enterprise logo\"><br> Open in Colab Enterprise\n",
|
||||
" </a>\n",
|
||||
" </td>\n",
|
||||
" <td>\n",
|
||||
" <td style=\"text-align: center\">\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/experiments/get_started_with_custom_training_autologging_local_script.ipynb\">\n",
|
||||
" <img src=\"https://lh3.googleusercontent.com/UiNooY4LUgW_oTvpsNhPpQzsstV5W8F7rYgxgGBD85cWJoLmrOzhVs_ksK_vgx40SHs7jCqkTkCk=e14-rj-sc0xffffff-h130-w32\" alt=\"Vertex AI logo\">\n",
|
||||
" <img src=\"https://lh3.googleusercontent.com/UiNooY4LUgW_oTvpsNhPpQzsstV5W8F7rYgxgGBD85cWJoLmrOzhVs_ksK_vgx40SHs7jCqkTkCk=e14-rj-sc0xffffff-h130-w32\" alt=\"Vertex AI logo\"><br>\n",
|
||||
" Open in Vertex AI Workbench\n",
|
||||
" </a>\n",
|
||||
" </td>\n",
|
||||
" <td style=\"text-align: center\">\n",
|
||||
" <a href=\"https://github.com/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/official/experiments/get_started_with_custom_training_autologging_local_script.ipynb\">\n",
|
||||
" <img src=\"https://cloud.google.com/ml-engine/images/github-logo-32px.png\" alt=\"GitHub logo\"><br>\n",
|
||||
" View on GitHub\n",
|
||||
" </a>\n",
|
||||
" </td>\n",
|
||||
"</table>\n",
|
||||
"<br/>"
|
||||
]
|
||||
@@ -134,15 +139,22 @@
|
||||
"to generate a cost estimate based on your projected usage."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "d1ea81ac77f0"
|
||||
},
|
||||
"source": [
|
||||
"## Get started"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "i7EUnXsZhAGF"
|
||||
},
|
||||
"source": [
|
||||
"## Installation\n",
|
||||
"\n",
|
||||
"Install the following packages required to execute this notebook."
|
||||
"### Install Vertex AI SDK for Python and other required packages\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -164,7 +176,9 @@
|
||||
"id": "58707a750154"
|
||||
},
|
||||
"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."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -175,184 +189,76 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Automatically restart kernel after installs so that your environment can access the new packages\n",
|
||||
"# 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": [
|
||||
"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": "c2fc3d7b6bfa"
|
||||
},
|
||||
"source": [
|
||||
"### Set Google Cloud project information and initialize Vertex AI SDK for Python\n",
|
||||
"\n",
|
||||
"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": "f02130bff721"
|
||||
},
|
||||
"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": "1s_lfsWxhctH"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"REGION = \"us-central1\" # @param {type: \"string\"}"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "-_8nZXd7NqIj"
|
||||
},
|
||||
"source": [
|
||||
"### UUID\n",
|
||||
"If you’re in a live tutorial session, you may be using a shared test account or project. To avoid name collisions between users on resources created, create a Universal Unique Identifier (uuid) for each instance session. Append the UUID to the name of the resources you create in this tutorial."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "dY-WpyyzNtS0"
|
||||
},
|
||||
"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()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"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."
|
||||
"LOCATION = \"us-central1\" # @param {type:\"string\"}"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -394,7 +300,59 @@
|
||||
},
|
||||
"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": "-_8nZXd7NqIj"
|
||||
},
|
||||
"source": [
|
||||
"### UUID\n",
|
||||
"If you’re in a live tutorial session, you may be using a shared test account or project. To avoid name collisions between users on resources created, create a Universal Unique Identifier (uuid) for each instance session. Append the UUID to the name of the resources you create in this tutorial."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "dY-WpyyzNtS0"
|
||||
},
|
||||
"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()"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -431,6 +389,9 @@
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"IS_COLAB = False\n",
|
||||
"if \"google.colab\" in sys.modules:\n",
|
||||
" IS_COLAB = True\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"if (\n",
|
||||
" SERVICE_ACCOUNT == \"\"\n",
|
||||
@@ -541,9 +502,7 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import os\n",
|
||||
"\n",
|
||||
"from google.cloud import aiplatform as vertex_ai"
|
||||
"import os"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -567,9 +526,7 @@
|
||||
"EXPERIMENT_NAME = f\"glass-classification-{UUID}\"\n",
|
||||
"TRAIN_SCRIPT_PATH = os.path.join(TUTORIAL_DIR, \"task.py\")\n",
|
||||
"JOB_DISPLAY_NAME = f\"sklearn-autologged-custom-job-{UUID}\"\n",
|
||||
"PRE_BUILT_TRAINING_CONTAINER_IMAGE_URI = (\n",
|
||||
" f\"{REGION.split('-')[0]}-docker.pkg.dev/vertex-ai/training/tf-cpu.2-12.py310:latest\"\n",
|
||||
")\n",
|
||||
"PRE_BUILT_TRAINING_CONTAINER_IMAGE_URI = f\"{LOCATION.split('-')[0]}-docker.pkg.dev/vertex-ai/training/tf-cpu.2-12.py310:latest\"\n",
|
||||
"MODEL_FILE_URI = f\"{BUCKET_URI}/models/model.joblib\"\n",
|
||||
"DESTINATION_DATA_PATH = DESTINATION_DATA_URL.replace(\"gs://\", \"/gcs/\")\n",
|
||||
"MODEL_FILE_PATH = MODEL_FILE_URI.replace(\"gs://\", \"/gcs/\")\n",
|
||||
@@ -578,28 +535,6 @@
|
||||
"TRAINING_JOBS_URI = f\"{BUCKET_URI}/jobs\""
|
||||
]
|
||||
},
|
||||
{
|
||||
"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": "kyr59QdyhctK"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"vertex_ai.init(project=PROJECT_ID, location=REGION, staging_bucket=BUCKET_URI)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
@@ -621,7 +556,7 @@
|
||||
"source": [
|
||||
"vertex_ai.init(\n",
|
||||
" project=PROJECT_ID,\n",
|
||||
" location=REGION,\n",
|
||||
" location=LOCATION,\n",
|
||||
" staging_bucket=BUCKET_URI,\n",
|
||||
" experiment=EXPERIMENT_NAME,\n",
|
||||
")"
|
||||
@@ -823,7 +758,7 @@
|
||||
"id": "ffcedb5809e4"
|
||||
},
|
||||
"source": [
|
||||
"Also you can get custom training job metadata associated with the experiment you run. You resume the logged experiments and use `get_logged_custom_jobs()` to get all `CustomJobs` resources associated to this experiment run. Then you use `job_spec` to print custom job metadata such as the training python package, training resources and more. \n"
|
||||
"Also you can get custom training job metadata associated with the experiment you run. You use `job_spec` property of the `CustomJob` class to print custom job metadata such as the training python package, training resources and more. \n"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -834,13 +769,7 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"experiment_run = experiment_df.run_name.iloc[0]\n",
|
||||
"\n",
|
||||
"with vertex_ai.start_run(experiment_run, resume=True) as run:\n",
|
||||
" # get the latest logged custom job\n",
|
||||
" logged_job = run.get_logged_custom_jobs()[-1]\n",
|
||||
"\n",
|
||||
"print(logged_job.job_spec)"
|
||||
"job.job_spec"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
||||
@@ -38,7 +38,7 @@
|
||||
" </a>\n",
|
||||
" </td>\n",
|
||||
" <td style=\"text-align: center\">\n",
|
||||
" <a href=\"https://console.cloud.google.com/vertex-ai/colab/import/https:%2F%2Fraw.githubusercontent.com%2FGoogleCloudPlatform%2Fvertex-ai-samples%2Fblob%2Fmain%2Fnotebooks%2Fofficial%2Fgenerative_ai%2Fanthropic_claude_3_intro.ipynb\">\n",
|
||||
" <a href=\"https://console.cloud.google.com/vertex-ai/colab/import/https:%2F%2Fraw.githubusercontent.com%2FGoogleCloudPlatform%2Fvertex-ai-samples%2Fmain%2Fnotebooks%2Fofficial%2Fgenerative_ai%2Fanthropic_claude_3_intro.ipynb\">\n",
|
||||
" <img width=\"32px\" src=\"https://cloud.google.com/ml-engine/images/colab-enterprise-logo-32px.png\" alt=\"Google Cloud Colab Enterprise logo\"><br> Open in Colab Enterprise\n",
|
||||
" </a>\n",
|
||||
" </td> \n",
|
||||
|
||||
@@ -67,7 +67,7 @@
|
||||
"\n",
|
||||
"The distilling step-by-step (DSS) method ([paper](https://arxiv.org/abs/2305.02301v1)) can enrich customer’s data by eliciting the reasoning process (rationales) from a large language model (LLM). This new mechanism has shown to be able to (a) train smaller models that outperform LLMs, and (b) achieves so by leveraging less training data needed by fine-tuning or distillation. This method extracts LLM rationales as additional supervision within a multi-task training framework.\n",
|
||||
"\n",
|
||||
"Learn more about [distill-text-models](https://cloud.google.com/vertex-ai/docs/generative-ai/models/distill-text-models).\n",
|
||||
"Learn more about [distill-text-models](https://cloud.google.com/vertex-ai/generative-ai/docs/models/distill-text-models).\n",
|
||||
"\n",
|
||||
"**_NOTE_**: This notebook is tested in the following environment:\n",
|
||||
"\n",
|
||||
|
||||
@@ -70,12 +70,12 @@
|
||||
"\n",
|
||||
"You can stream your Mistral AI model responses to reduce the end-user latency perception. A streamed response uses server-sent events (SSE) to incrementally stream the response.\n",
|
||||
"\n",
|
||||
"Learn more about [Vertex AI](https://cloud.google.com/vertex-ai). \n",
|
||||
"Learn more about [Vertex AI](https://cloud.google.com/vertex-ai).\n",
|
||||
"\n",
|
||||
"### Available Mistral AI models\n",
|
||||
"\n",
|
||||
"* ### Mistral Large (2407)\n",
|
||||
"Complex tasks that require large reasoning capabilities or are highly specialized (synthetic text Generation, code generation, RAG, or agents).\n",
|
||||
"Complex tasks that require large reasoning capabilities or are highly specialized (synthetic text Generation, code generation, RAG, or agents). [Blog Post](https://mistral.ai/news/mistral-large-2407/)\n",
|
||||
"\n",
|
||||
"* ### Mistral Nemo\n",
|
||||
"Reasoning, world knowledge, and coding performance are state-of-the-art in its size category.\n",
|
||||
@@ -88,7 +88,13 @@
|
||||
"\n",
|
||||
"This notebook shows how to use **Vertex AI API** to call the Mistral AI models on Vertex AI API with the Large, Nemo, and Codestral models.\n",
|
||||
"\n",
|
||||
"For more information, see the [Use Mistral's](https://docs.mistral.ai/) documentation and [Mistral's models](https://cloud.google.com/vertex-ai/generative-ai/docs/partner-models/mistral) on Google Cloud.\n"
|
||||
"For more information, see the [Use Mistral's](https://docs.mistral.ai/) documentation and [Mistral's models](https://cloud.google.com/vertex-ai/generative-ai/docs/partner-models/mistral) on Google Cloud.\n",
|
||||
"\n",
|
||||
"- Mistral on Model Garden supports the same API calls as Mistral’s own API endpoints, except for the `safe_prompt` parameter that will return an error if specified in the input. So do not include `safe_prompt` in input requests.\n",
|
||||
"- Documentation links\n",
|
||||
" - [Mistral APIs](https://docs.mistral.ai/api/)\n",
|
||||
" - [Chat Completions](https://docs.mistral.ai/api/#operation/createChatCompletion) operations supported by Mistral Large, Mistral Nemo and Codestral\n",
|
||||
" - [Fill-in-the-middle](https://docs.mistral.ai/api/#operation/createFIMCompletion) operations supported by Codestral"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -106,7 +112,7 @@
|
||||
"id": "nwYvaaW25jYS"
|
||||
},
|
||||
"source": [
|
||||
"## Get Started\n"
|
||||
"## Get Started - Required first steps\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -115,9 +121,7 @@
|
||||
"id": "6a5bea26f60f"
|
||||
},
|
||||
"source": [
|
||||
"### Authenticate your notebook environment (Colab only)\n",
|
||||
"\n",
|
||||
"Authenticate your environment on Google Colab.\n"
|
||||
"### Authenticate your notebook environment (Colab only)\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -142,7 +146,7 @@
|
||||
"id": "2fxZn4SAbxdl"
|
||||
},
|
||||
"source": [
|
||||
"#### Select Mistral AI model"
|
||||
"### Select one of Mistral AI models"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -171,7 +175,7 @@
|
||||
"id": "bpuX3sKtexlK"
|
||||
},
|
||||
"source": [
|
||||
"#### Select a location"
|
||||
"### Select a location and a version from the dropdown"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -194,23 +198,26 @@
|
||||
"\n",
|
||||
"dropdown_ver = widgets.Dropdown(\n",
|
||||
" options=available_versions,\n",
|
||||
" description=\"Select a Model version (optional):\",\n",
|
||||
" description=\"Select the model version (optional):\",\n",
|
||||
" font_weight=\"bold\",\n",
|
||||
" style={\"description_width\": \"initial\"},\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"def dropdown_loc_eventhandler(change):\n",
|
||||
" global LOCATION\n",
|
||||
" if change[\"type\"] == \"change\" and change[\"name\"] == \"value\":\n",
|
||||
" LOCATION = change.new\n",
|
||||
" print(\"Selected:\", change.new)\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"def dropdown_ver_eventhandler(change):\n",
|
||||
" global MODEL_VERSION\n",
|
||||
" if change[\"type\"] == \"change\" and change[\"name\"] == \"value\":\n",
|
||||
" MODEL_VERSION = change.new\n",
|
||||
" print(\"Selected:\", change.new)\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"LOCATION = dropdown_loc.value\n",
|
||||
"dropdown_loc.observe(dropdown_loc_eventhandler, names=\"value\")\n",
|
||||
"display(dropdown_loc)\n",
|
||||
@@ -226,7 +233,7 @@
|
||||
"id": "3q58icinBjoK"
|
||||
},
|
||||
"source": [
|
||||
"#### Set Google Cloud project and model information\n",
|
||||
"### Set Google Cloud project and model information\n",
|
||||
"\n",
|
||||
"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)."
|
||||
]
|
||||
@@ -253,7 +260,7 @@
|
||||
"id": "4NAstKRFBt4N"
|
||||
},
|
||||
"source": [
|
||||
"#### Import required libraries"
|
||||
"### Import required libraries"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -264,7 +271,19 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import json"
|
||||
"import json\n",
|
||||
"import subprocess\n",
|
||||
"\n",
|
||||
"import requests"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "vVVnOhvE_PA6"
|
||||
},
|
||||
"source": [
|
||||
"## Sample Requests"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -306,6 +325,59 @@
|
||||
"!curl -X POST -H \"Authorization: Bearer $(gcloud auth print-access-token)\" -H \"Content-Type: application/json\" {ENDPOINT}/v1/projects/{PROJECT_ID}/locations/{LOCATION}/publishers/mistralai/models/{MODEL}{SELECTED_MODEL_VERSION}:rawPredict -d '{request}'"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "tf56XLe1EZos"
|
||||
},
|
||||
"source": [
|
||||
"With a pretty response"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "L90Kdr-PEYr3"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Get the access token\n",
|
||||
"process = subprocess.Popen(\n",
|
||||
" \"gcloud auth print-access-token\", stdout=subprocess.PIPE, shell=True\n",
|
||||
")\n",
|
||||
"(access_token_bytes, err) = process.communicate()\n",
|
||||
"access_token = access_token_bytes.decode(\"utf-8\").strip() # Strip newline\n",
|
||||
"\n",
|
||||
"# Define query headers\n",
|
||||
"headers = {\n",
|
||||
" \"Authorization\": f\"Bearer {access_token}\",\n",
|
||||
" \"Accept\": \"application/json\",\n",
|
||||
"}\n",
|
||||
"\n",
|
||||
"# Replace with your actual values\n",
|
||||
"url = f\"{ENDPOINT}/v1/projects/{PROJECT_ID}/locations/{LOCATION}/publishers/mistralai/models/{MODEL}{SELECTED_MODEL_VERSION}:rawPredict\"\n",
|
||||
"data = {\n",
|
||||
" \"model\": MODEL,\n",
|
||||
" \"messages\": [{\"role\": \"user\", \"content\": \"who is the best French painter?\"}],\n",
|
||||
" \"stream\": False,\n",
|
||||
"}\n",
|
||||
"\n",
|
||||
"# Make the POST request\n",
|
||||
"response = requests.post(url, headers=headers, json=data)\n",
|
||||
"\n",
|
||||
"# Check status code and try to parse the response as JSON\n",
|
||||
"if response.status_code == 200:\n",
|
||||
" try:\n",
|
||||
" response_dict = response.json()\n",
|
||||
" print(response_dict[\"choices\"][0][\"message\"][\"content\"])\n",
|
||||
" except json.JSONDecodeError as e:\n",
|
||||
" print(\"Error decoding JSON:\", e)\n",
|
||||
" print(\"Raw response:\", response.text) # Print raw response if parsing fails\n",
|
||||
"else:\n",
|
||||
" print(f\"Request failed with status code: {response.status_code}\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
@@ -335,6 +407,526 @@
|
||||
"request = json.dumps(PAYLOAD)\n",
|
||||
"!curl -X POST -H \"Authorization: Bearer $(gcloud auth print-access-token)\" -H \"Content-Type: application/json\" {ENDPOINT}/v1/projects/{PROJECT_ID}/locations/{LOCATION}/publishers/mistralai/models/{MODEL}{SELECTED_MODEL_VERSION}:streamRawPredict -d '{request}'"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "X6HolebUhShT"
|
||||
},
|
||||
"source": [
|
||||
"### Code generation\n",
|
||||
"\n",
|
||||
"Mistral Large, Mistral Nemo and Codestral support code generation with the Chat Completions operations covered above.\n",
|
||||
"\n",
|
||||
"With Codestral, you can also do Fill-in-the-middle operations."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "deUZQwgSheEr"
|
||||
},
|
||||
"source": [
|
||||
"#### Fill-in-the-middle (FIM)\n",
|
||||
"With this feature, users can define the starting point of the code using a `prompt`, and the ending point of the code using an optional `suffix` and an optional `stop`.\n",
|
||||
"\n",
|
||||
"The Codestral model will then generate the code that fits in between, making it ideal for tasks that require a specific piece of code to be generated.\n",
|
||||
"\n",
|
||||
"More information on FIM:\n",
|
||||
"- [Mistral API Documentation FIM](https://docs.mistral.ai/api/#operation/createFIMCompletion)\n",
|
||||
"- [Mistral FIM Documentation](https://docs.mistral.ai/capabilities/code_generation/#fill-in-the-middle-endpoint)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "5JzsH7TmujqR"
|
||||
},
|
||||
"source": [
|
||||
"Example 1"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "0zXL4RrnhRLG"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"MODEL = \"codestral\"\n",
|
||||
"SELECTED_MODEL_VERSION = \"\"\n",
|
||||
"\n",
|
||||
"PAYLOAD = {\n",
|
||||
" \"model\": MODEL,\n",
|
||||
" \"prompt\": \"def say_hello(name: str) -> str\",\n",
|
||||
" \"suffix\": \"return n_words\",\n",
|
||||
"}\n",
|
||||
"\n",
|
||||
"request = json.dumps(PAYLOAD)\n",
|
||||
"!curl -X POST -H \"Authorization: Bearer $(gcloud auth print-access-token)\" -H \"Content-Type: application/json\" {ENDPOINT}/v1/projects/{PROJECT_ID}/locations/{LOCATION}/publishers/mistralai/models/{MODEL}{SELECTED_MODEL_VERSION}:streamRawPredict -d '{request}'"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "n6kMTcg5ulJD"
|
||||
},
|
||||
"source": [
|
||||
"Example 2 with pretty response"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "ciJKueNfDman"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"MODEL = \"codestral\"\n",
|
||||
"SELECTED_MODEL_VERSION = \"\"\n",
|
||||
"\n",
|
||||
"# Get the access token\n",
|
||||
"process = subprocess.Popen(\n",
|
||||
" \"gcloud auth print-access-token\", stdout=subprocess.PIPE, shell=True\n",
|
||||
")\n",
|
||||
"(access_token_bytes, err) = process.communicate()\n",
|
||||
"access_token = access_token_bytes.decode(\"utf-8\").strip() # Strip newline\n",
|
||||
"\n",
|
||||
"# Define query headers\n",
|
||||
"headers = {\n",
|
||||
" \"Authorization\": f\"Bearer {access_token}\",\n",
|
||||
" \"Accept\": \"application/json\",\n",
|
||||
"}\n",
|
||||
"\n",
|
||||
"# Replace with your actual values\n",
|
||||
"url = f\"{ENDPOINT}/v1/projects/{PROJECT_ID}/locations/{LOCATION}/publishers/mistralai/models/{MODEL}{SELECTED_MODEL_VERSION}:rawPredict\"\n",
|
||||
"data = {\n",
|
||||
" \"model\": MODEL,\n",
|
||||
" \"prompt\": \"def f(\",\n",
|
||||
" \"suffix\": \"return a + b\",\n",
|
||||
" \"max_tokens\": 64,\n",
|
||||
" \"temperature\": 0,\n",
|
||||
"}\n",
|
||||
"\n",
|
||||
"# Make the POST request\n",
|
||||
"response = requests.post(url, headers=headers, json=data)\n",
|
||||
"\n",
|
||||
"# Check status code and try to parse the response as JSON\n",
|
||||
"if response.status_code == 200:\n",
|
||||
" try:\n",
|
||||
" response_dict = response.json()\n",
|
||||
" print(response_dict[\"choices\"][0][\"message\"][\"content\"])\n",
|
||||
" except json.JSONDecodeError as e:\n",
|
||||
" print(\"Error decoding JSON:\", e)\n",
|
||||
" print(\"Raw response:\", response.text) # Print raw response if parsing fails\n",
|
||||
"else:\n",
|
||||
" print(f\"Request failed with status code: {response.status_code}\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "qsJLkqyyztR_"
|
||||
},
|
||||
"source": [
|
||||
"## Model Capabilities"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "Z9EvH5iez_n_"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Get the access token\n",
|
||||
"process = subprocess.Popen(\n",
|
||||
" \"gcloud auth print-access-token\", stdout=subprocess.PIPE, shell=True\n",
|
||||
")\n",
|
||||
"(access_token_bytes, err) = process.communicate()\n",
|
||||
"access_token = access_token_bytes.decode(\"utf-8\").strip() # Strip newline\n",
|
||||
"\n",
|
||||
"headers = {\n",
|
||||
" \"Authorization\": f\"Bearer {access_token}\",\n",
|
||||
" \"Content-Type\": \"application/json\",\n",
|
||||
"}"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "TfmwOYBXxcQG"
|
||||
},
|
||||
"source": [
|
||||
"### Function Calling with Mistral Large\n",
|
||||
"\n",
|
||||
"Function calling allows Mistral models to connect to external tools. By integrating Mistral models with external tools such as user defined functions or APIs, users can easily build applications catering to specific use cases and practical problems.\n",
|
||||
"\n",
|
||||
"This guide is the one Mistral provides [here](https://docs.mistral.ai/capabilities/function_calling/). We write two functions for tracking payment status and payment date. We can use these two tools to provide answers for payment-related queries."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "nLmoam_z0OfN"
|
||||
},
|
||||
"source": [
|
||||
"#### Step 1. User: specify tools\n",
|
||||
"\n",
|
||||
"Define sample data like this was stored in a sample database."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "Oak2cRcgx7VB"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import pandas as pd\n",
|
||||
"\n",
|
||||
"# Assuming we have the following data\n",
|
||||
"data = {\n",
|
||||
" \"transaction_id\": [\"T1001\", \"T1002\", \"T1003\", \"T1004\", \"T1005\"],\n",
|
||||
" \"customer_id\": [\"C001\", \"C002\", \"C003\", \"C002\", \"C001\"],\n",
|
||||
" \"payment_amount\": [125.50, 89.99, 120.00, 54.30, 210.20],\n",
|
||||
" \"payment_date\": [\n",
|
||||
" \"2021-10-05\",\n",
|
||||
" \"2021-10-06\",\n",
|
||||
" \"2021-10-07\",\n",
|
||||
" \"2021-10-05\",\n",
|
||||
" \"2021-10-08\",\n",
|
||||
" ],\n",
|
||||
" \"payment_status\": [\"Paid\", \"Unpaid\", \"Paid\", \"Paid\", \"Pending\"],\n",
|
||||
"}\n",
|
||||
"\n",
|
||||
"# Create DataFrame\n",
|
||||
"df = pd.DataFrame(data)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "cxb7_m9UxxmW"
|
||||
},
|
||||
"source": [
|
||||
"Define the functions that will be used as tools."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "pfOuDWlYxdvL"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def retrieve_payment_status(df: data, transaction_id: str) -> str:\n",
|
||||
" if transaction_id in df.transaction_id.values:\n",
|
||||
" return json.dumps(\n",
|
||||
" {\"status\": df[df.transaction_id == transaction_id].payment_status.item()}\n",
|
||||
" )\n",
|
||||
" return json.dumps({\"error\": \"transaction id not found.\"})\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"def retrieve_payment_date(df: data, transaction_id: str) -> str:\n",
|
||||
" if transaction_id in df.transaction_id.values:\n",
|
||||
" return json.dumps(\n",
|
||||
" {\"date\": df[df.transaction_id == transaction_id].payment_date.item()}\n",
|
||||
" )\n",
|
||||
" return json.dumps({\"error\": \"transaction id not found.\"})"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "66OSfAKVyJnn"
|
||||
},
|
||||
"source": [
|
||||
"Define the tools for those functions following the right JSON format."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "o5xBTtIGxzRS"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"tools = [\n",
|
||||
" {\n",
|
||||
" \"type\": \"function\",\n",
|
||||
" \"function\": {\n",
|
||||
" \"name\": \"retrieve_payment_status\",\n",
|
||||
" \"description\": \"Get payment status of a transaction\",\n",
|
||||
" \"parameters\": {\n",
|
||||
" \"type\": \"object\",\n",
|
||||
" \"properties\": {\n",
|
||||
" \"transaction_id\": {\n",
|
||||
" \"type\": \"string\",\n",
|
||||
" \"description\": \"The transaction id.\",\n",
|
||||
" }\n",
|
||||
" },\n",
|
||||
" \"required\": [\"transaction_id\"],\n",
|
||||
" },\n",
|
||||
" },\n",
|
||||
" },\n",
|
||||
" {\n",
|
||||
" \"type\": \"function\",\n",
|
||||
" \"function\": {\n",
|
||||
" \"name\": \"retrieve_payment_date\",\n",
|
||||
" \"description\": \"Get payment date of a transaction\",\n",
|
||||
" \"parameters\": {\n",
|
||||
" \"type\": \"object\",\n",
|
||||
" \"properties\": {\n",
|
||||
" \"transaction_id\": {\n",
|
||||
" \"type\": \"string\",\n",
|
||||
" \"description\": \"The transaction id.\",\n",
|
||||
" }\n",
|
||||
" },\n",
|
||||
" \"required\": [\"transaction_id\"],\n",
|
||||
" },\n",
|
||||
" },\n",
|
||||
" },\n",
|
||||
"]"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "9DCJP8hN2X-c"
|
||||
},
|
||||
"source": [
|
||||
"#### Step 2. Model: Generate the right tool and arguments with Mistral Large"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "XyrgEDRu2gc2"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"MODEL = \"mistral-large\"\n",
|
||||
"SELECTED_MODEL_VERSION = \"\""
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "fmhX-3FDy1vS"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"url = f\"{ENDPOINT}/v1/projects/{PROJECT_ID}/locations/{LOCATION}/publishers/mistralai/models/{MODEL}{SELECTED_MODEL_VERSION}:rawPredict\"\n",
|
||||
"data = {\n",
|
||||
" \"model\": MODEL,\n",
|
||||
" \"messages\": [\n",
|
||||
" {\"role\": \"user\", \"content\": \"What is the status of my transaction T1001?\"}\n",
|
||||
" ],\n",
|
||||
" \"tools\": tools,\n",
|
||||
" \"tool_choice\": \"any\",\n",
|
||||
"}\n",
|
||||
"function_name = None\n",
|
||||
"function_params = None\n",
|
||||
"\n",
|
||||
"# Make the POST request\n",
|
||||
"response = requests.post(url, headers=headers, json=data)\n",
|
||||
"\n",
|
||||
"# Check status code and try to parse the response as JSON\n",
|
||||
"if response.status_code == 200:\n",
|
||||
" try:\n",
|
||||
" response_dict = response.json()\n",
|
||||
" tool_call = response_dict[\"choices\"][0][\"message\"][\"tool_calls\"][0]\n",
|
||||
" function_name = tool_call[\"function\"][\"name\"]\n",
|
||||
" function_params = json.loads(tool_call[\"function\"][\"arguments\"])\n",
|
||||
" except json.JSONDecodeError as e:\n",
|
||||
" print(\"Error decoding JSON:\", e)\n",
|
||||
" print(\"Raw response:\", response.text) # Print raw response if parsing fails\n",
|
||||
"else:\n",
|
||||
" print(f\"Request failed with status code: {response.status_code}\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "EkG59Jsg1hnE"
|
||||
},
|
||||
"source": [
|
||||
"#### Step 3. User: Extract the tool function name, the params and execute the tool function"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "rQHeV3Qz032W"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"if function_name and function_params:\n",
|
||||
" print(\"\\nfunction_name: \", function_name, \"\\nfunction_params: \", function_params)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "5-nOJR6hCpYz"
|
||||
},
|
||||
"source": [
|
||||
"Map function names returned by Mistral model to the actual function object in the environment."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "W30ACckn2N_F"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import functools\n",
|
||||
"\n",
|
||||
"names_to_functions = {\n",
|
||||
" \"retrieve_payment_status\": functools.partial(retrieve_payment_status, df=df),\n",
|
||||
" \"retrieve_payment_date\": functools.partial(retrieve_payment_date, df=df),\n",
|
||||
"}"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "78S2e-TOCyQn"
|
||||
},
|
||||
"source": [
|
||||
"Call the right function with the parameters suggested by Mistral's model."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "5Pk7XszE1nXI"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"if function_name and function_params:\n",
|
||||
" function_result = names_to_functions[function_name](**function_params)\n",
|
||||
" function_result"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "nbEuWr3M922W"
|
||||
},
|
||||
"source": [
|
||||
"### JSON Output Mode\n",
|
||||
"\n",
|
||||
"You can force the response format to JSON by adding `\"response_format\": {\"type\": \"json_object\"}` in the JSON payload of the request\n",
|
||||
"See Mistral's documentation on JSON mode\n",
|
||||
"\n",
|
||||
"* See Mistral's [documentation](https://docs.mistral.ai/capabilities/json_mode/) on JSON mode\n",
|
||||
"* See Mistral's API [documentation](https://docs.mistral.ai/api/#operation/createChatCompletion)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "M2-4H54gnizf"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"MODEL = \"mistral-large\"\n",
|
||||
"SELECTED_MODEL_VERSION = \"\""
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "Y-695Dyt-Q9V"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"PAYLOAD = {\n",
|
||||
" \"model\": MODEL,\n",
|
||||
" \"messages\": [\n",
|
||||
" {\n",
|
||||
" \"role\": \"user\",\n",
|
||||
" \"content\": \"What is the best French cheese? Return the product and produce location in JSON format\",\n",
|
||||
" }\n",
|
||||
" ],\n",
|
||||
" \"response_format\": {\"type\": \"json_object\"},\n",
|
||||
"}\n",
|
||||
"\n",
|
||||
"request = json.dumps(PAYLOAD)\n",
|
||||
"!curl -X POST -H \"Authorization: Bearer $(gcloud auth print-access-token)\" -H \"Content-Type: application/json\" {ENDPOINT}/v1/projects/{PROJECT_ID}/locations/{LOCATION}/publishers/mistralai/models/{MODEL}{SELECTED_MODEL_VERSION}:rawPredict -d '{request}'"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "7DQlJjz7DXDu"
|
||||
},
|
||||
"source": [
|
||||
"Pretty response"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "ky1TDqhc-bur"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Get the access token\n",
|
||||
"process = subprocess.Popen(\n",
|
||||
" \"gcloud auth print-access-token\", stdout=subprocess.PIPE, shell=True\n",
|
||||
")\n",
|
||||
"(access_token_bytes, err) = process.communicate()\n",
|
||||
"access_token = access_token_bytes.decode(\"utf-8\").strip() # Strip newline\n",
|
||||
"\n",
|
||||
"# Replace with your actual values\n",
|
||||
"url = f\"{ENDPOINT}/v1/projects/{PROJECT_ID}/locations/{LOCATION}/publishers/mistralai/models/{MODEL}{SELECTED_MODEL_VERSION}:rawPredict\"\n",
|
||||
"data = {\n",
|
||||
" \"model\": MODEL,\n",
|
||||
" \"messages\": [\n",
|
||||
" {\n",
|
||||
" \"role\": \"user\",\n",
|
||||
" \"content\": \"What is the best French cheese? Return the product and produce location in JSON format\",\n",
|
||||
" }\n",
|
||||
" ],\n",
|
||||
" \"response_format\": {\"type\": \"json_object\"},\n",
|
||||
"}\n",
|
||||
"headers = {\n",
|
||||
" \"Authorization\": f\"Bearer {access_token}\",\n",
|
||||
" \"Content-Type\": \"application/json\",\n",
|
||||
"}\n",
|
||||
"\n",
|
||||
"# Make the POST request\n",
|
||||
"response = requests.post(url, headers=headers, json=data)\n",
|
||||
"\n",
|
||||
"# Check status code and try to parse the response as JSON\n",
|
||||
"if response.status_code == 200:\n",
|
||||
" try:\n",
|
||||
" response_dict = response.json()\n",
|
||||
" print(response_dict[\"choices\"][0][\"message\"][\"content\"])\n",
|
||||
" except json.JSONDecodeError as e:\n",
|
||||
" print(\"Error decoding JSON:\", e)\n",
|
||||
" print(\"Raw response:\", response.text) # Print raw response if parsing fails\n",
|
||||
"else:\n",
|
||||
" print(f\"Request failed with status code: {response.status_code}\")"
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"cellView": "form",
|
||||
"id": "9f0d0f32-23b4-41a6-b364-579da297c326"
|
||||
},
|
||||
"outputs": [],
|
||||
@@ -61,7 +62,7 @@
|
||||
"\n",
|
||||
"This tutorial demonstrates how to use Vertex AI to tune a PEFT large-language model (LLM) and make a prediction. This workflow improves a model's accuracy by fine-tuning a base model with a training dataset.\n",
|
||||
"\n",
|
||||
"Learn more about [Tune text models by using supervised tuning](https://cloud.google.com/vertex-ai/docs/generative-ai/models/tune-text-models-supervised)."
|
||||
"Learn more about [Tune text models by using supervised tuning](https://cloud.google.com/vertex-ai/generative-ai/docs/models/tune-text-models-supervised)."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -72,23 +73,23 @@
|
||||
"source": [
|
||||
"### Objective\n",
|
||||
"\n",
|
||||
"In this tutorial, you learn to use `Vertex AI LLM` to tune and deploy a PEFT large language model.\n",
|
||||
"In this tutorial, you learn to use Vertex AI LLM to tune and deploy a PEFT large language model.\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"This tutorial uses the following Google Cloud ML services:\n",
|
||||
"\n",
|
||||
"- `Vertex AI LLM`\n",
|
||||
"- `Vertex AI Model Garden`\n",
|
||||
"- `Vertex AI Prediction`\n",
|
||||
"- Vertex AI LLM\n",
|
||||
"- Vertex AI Model Garden\n",
|
||||
"- Vertex AI Prediction\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"The steps performed include:\n",
|
||||
"\n",
|
||||
"- Get the Vertex AI LLM model.\n",
|
||||
"- Tune the model.\n",
|
||||
" - This will automatically create a Vertex AI endpoint and deploy the model to it.\n",
|
||||
"- Make a prediction using `Vertex AI LLM`.\n",
|
||||
"- Make a prediction using `Vertex AI Prediction`"
|
||||
" - This automatically creates a Vertex AI endpoint and deploy the model to it.\n",
|
||||
"- Make a prediction using Vertex AI LLM.\n",
|
||||
"- Make a prediction using Vertex AI Prediction."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -231,7 +232,7 @@
|
||||
"Depending on your Jupyter environment, you may have to manually authenticate. Follow the relevant instructions below.\n",
|
||||
"\n",
|
||||
"**1. Vertex AI Workbench**\n",
|
||||
"* Do nothing as you are already authenticated.\n",
|
||||
"* Do nothing since you're already authenticated.\n",
|
||||
"\n",
|
||||
"**2. Local JupyterLab instance, uncomment and run:**"
|
||||
]
|
||||
@@ -306,7 +307,7 @@
|
||||
"id": "bf01f385-3c69-45ca-b72b-84fb45b15f25"
|
||||
},
|
||||
"source": [
|
||||
"**Only if your bucket doesn't already exist**: Run the following cell to create your Cloud Storage bucket."
|
||||
"**If your bucket doesn't already exist**: Run the following cell to create your Cloud Storage bucket."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -471,10 +472,10 @@
|
||||
"source": [
|
||||
"### Tune the model\n",
|
||||
"\n",
|
||||
"Next, you tune the model using the `tune_model()` method, with the following parameters:\n",
|
||||
"Next, tune the model using the `tune_model()` method, with the following parameters:\n",
|
||||
"\n",
|
||||
"`training_data`: A pandas Dataframe or Cloud Storage location of the training data for tuning the model.<br>\n",
|
||||
"`learning_rate_multiplier`: A multiplier to apply to the recommended learning rate. To use the recommended learning rate, use 1.0. <br>\n",
|
||||
"`learning_rate_multiplier`: A multiplier to apply to the recommended learning rate. The recommended learning rate to use is 1.0. <br>\n",
|
||||
"`train_steps`: The number of steps to run for model tuning. The batch size varies by tuning location:<br>\n",
|
||||
"- us-central1 has a batch size of 8.\n",
|
||||
"- europe-west4 has a batch size of 24.<br>\n",
|
||||
@@ -503,7 +504,7 @@
|
||||
")\n",
|
||||
"model.tune_model(\n",
|
||||
" training_data=f\"{BUCKET_URI}/peft_train_sample.jsonl\",\n",
|
||||
" # set to 1 for fast iteration and demo purpose. For this sample training dataset, we recommend at least 100 steps\n",
|
||||
" # set to 1 for fast iteration and demo purpose. For this sample training dataset, at least 100 steps are recommended.\n",
|
||||
" train_steps=100,\n",
|
||||
" learning_rate_multiplier=1.0,\n",
|
||||
" tuning_job_location=\"europe-west4\",\n",
|
||||
@@ -552,9 +553,9 @@
|
||||
"id": "ae39e95e-9553-4cd8-98cc-602a0d70e940"
|
||||
},
|
||||
"source": [
|
||||
"### Get the deployed Vertex AI Endpoint resource\n",
|
||||
"### Get the deployed Vertex AI endpoint resource\n",
|
||||
"\n",
|
||||
"Next, get the Vertex AI Endpoint resource that the model was automatically deployed to."
|
||||
"Next, get the Vertex AI endpoint resource that the model was automatically deployed to."
|
||||
]
|
||||
},
|
||||
{
|
||||
|
||||
@@ -39,7 +39,7 @@
|
||||
" </a>\n",
|
||||
" </td>\n",
|
||||
" <td style=\"text-align: center\">\n",
|
||||
" <a href=\"https://console.cloud.google.com/vertex-ai/colab/import/https:%2F%2Fgithub.com%2FGoogleCloudPlatform%2Fvertex-ai-samples%2Fblob%2Fmain%2Fnotebooks%2Fofficial%2Fgenerative_ai%2Fvertex_sdk_llm_snippets.ipynb\">\n",
|
||||
" <a href=\"https://console.cloud.google.com/vertex-ai/colab/import/https:%2F%2Fraw.githubusercontent.com%2FGoogleCloudPlatform%2Fvertex-ai-samples%2Fmain%2Fnotebooks%2Fofficial%2Fgenerative_ai%2Fvertex_sdk_llm_snippets.ipynb\">\n",
|
||||
" <img width=\"32px\" src=\"https://lh3.googleusercontent.com/JmcxdQi-qOpctIvWKgPtrzZdJJK-J3sWE1RsfjZNwshCFgE_9fULcNpuXYTilIR2hjwN\" alt=\"Google Cloud Colab Enterprise logo\"><br> Open in Colab Enterprise\n",
|
||||
" </a>\n",
|
||||
" </td>\n",
|
||||
|
||||
@@ -39,7 +39,7 @@
|
||||
" </a>\n",
|
||||
" </td>\n",
|
||||
" <td style=\"text-align: center\">\n",
|
||||
" <a href=\"https://console.cloud.google.com/vertex-ai/colab/import/https:%2F%2Fgithub.com%2FGoogleCloudPlatform%2Fvertex-ai-samples%2Fblob%2Fmain%2Fnotebooks%2Fofficial%2Fmigration%2Fsdk-automl-video-classification-batch-prediction.ipynb\">\n",
|
||||
" <a href=\"https://console.cloud.google.com/vertex-ai/colab/import/https:%2F%2Fraw.githubusercontent.com%2FGoogleCloudPlatform%2Fvertex-ai-samples%2Fmain%2Fnotebooks%2Fofficial%2Fmigration%2Fsdk-automl-video-classification-batch-prediction.ipynb\">\n",
|
||||
" <img width=\"32px\" src=\"https://cloud.google.com/ml-engine/images/colab-enterprise-logo-32px.png\" alt=\"Google Cloud Colab Enterprise logo\"><br> Open in Colab Enterprise\n",
|
||||
" </a>\n",
|
||||
" </td>\n",
|
||||
|
||||
@@ -39,7 +39,7 @@
|
||||
" </a>\n",
|
||||
" </td>\n",
|
||||
" <td style=\"text-align: center\">\n",
|
||||
" <a href=\"https://console.cloud.google.com/vertex-ai/colab/import/https:%2F%2Fgithub.com%2FGoogleCloudPlatform/%2Fvertex-ai-samples%2Fblob%2Fmain%2Fnotebooks%2Fofficial%2Fmodel_monitoring%2Fbatch_prediction_model_monitoring.ipynb\">\n",
|
||||
" <a href=\"https://console.cloud.google.com/vertex-ai/colab/import/https:%2F%2Fraw.githubusercontent.com%2FGoogleCloudPlatform%2Fvertex-ai-samples%2Fmain%2Fnotebooks%2Fofficial%2Fmodel_monitoring%2Fbatch_prediction_model_monitoring.ipynb\">\n",
|
||||
" <img width=\"32px\" src=\"https://cloud.google.com/ml-engine/images/colab-enterprise-logo-32px.png\" alt=\"Google Cloud Colab Enterprise logo\"><br> Open in Colab Enterprise\n",
|
||||
" </a>\n",
|
||||
" </td>\n",
|
||||
|
||||
+164
-218
File diff suppressed because one or more lines are too long
@@ -39,7 +39,8 @@
|
||||
" </a>\n",
|
||||
" </td>\n",
|
||||
" <td style=\"text-align: center\">\n",
|
||||
" <a href=\"https://console.cloud.google.com/vertex-ai/colab/import/https:%2F%2Fgithub.com%2FGoogleCloudPlatform%2Fvertex-ai-samples%2Fblob%2Fmain%2Fnotebooks%2Fofficial%2Fpipelines%2Fget_started_with_machine_management.ipynb\">\n",
|
||||
" <a href=\"\n",
|
||||
"https://console.cloud.google.com/vertex-ai/colab/import/https:%2F%2Fraw.githubusercontent.com%2FGoogleCloudPlatform%2Fvertex-ai-samples%2Fmain%2Fnotebooks%2Fofficial%2Fpipelines%2Fget_started_with_machine_management.ipynb\">\n",
|
||||
" <img width=\"32px\" src=\"https://cloud.google.com/ml-engine/images/colab-enterprise-logo-32px.png\" alt=\"Google Cloud Colab Enterprise logo\"><br> Open in Colab Enterprise\n",
|
||||
" </a>\n",
|
||||
" </td> \n",
|
||||
|
||||
@@ -38,7 +38,7 @@
|
||||
" </a>\n",
|
||||
" </td>\n",
|
||||
" <td style=\"text-align: center\">\n",
|
||||
" <a href=\"https://console.cloud.google.com/vertex-ai/colab/import/https:%2F%2Fgithub.com%2FGoogleCloudPlatform%2Fvertex-ai-samples%2Fblob%2Fmain%2Fnotebooks%2Fofficial/pipelines%2Fgoogle_cloud_pipeline_components_automl_images.ipynb\">\n",
|
||||
" <a href=\"https://console.cloud.google.com/vertex-ai/colab/import/https:%2F%2Fraw.githubusercontent.com%2FGoogleCloudPlatform%2Fvertex-ai-samples%2Fmain%2Fnotebooks%2Fofficial%2Fpipelines%2Fgoogle_cloud_pipeline_components_automl_images.ipynb\">\n",
|
||||
" <img width=\"32px\" src=\"https://cloud.google.com/ml-engine/images/colab-enterprise-logo-32px.png\" alt=\"Google Cloud Colab Enterprise logo\"><br> Open in Colab Enterprise\n",
|
||||
" </a>\n",
|
||||
" </td> \n",
|
||||
|
||||
+1
-1
@@ -38,7 +38,7 @@
|
||||
" </a>\n",
|
||||
" </td>\n",
|
||||
" <td style=\"text-align: center\">\n",
|
||||
" <a href=\"https://console.cloud.google.com/vertex-ai/colab/import/https:%2F%2Fgithub.com%2FGoogleCloudPlatform%2Fvertex-ai-samples%2Fblob%2Fmain%2Fnotebooks%2Fofficial%2Fpipelines%2Fgoogle_cloud_pipeline_components_model_train_upload_deploy.ipynb\">\n",
|
||||
" <a href=\"https://console.cloud.google.com/vertex-ai/colab/import/https:%2F%2Fraw.githubusercontent.com%2FGoogleCloudPlatform%2Fvertex-ai-samples%2Fmain%2Fnotebooks%2Fofficial%2Fpipelines%2Fgoogle_cloud_pipeline_components_model_train_upload_deploy.ipynb\">\n",
|
||||
" <img width=\"32px\" src=\"https://cloud.google.com/ml-engine/images/colab-enterprise-logo-32px.png\" alt=\"Google Cloud Colab Enterprise logo\"><br> Open in Colab Enterprise\n",
|
||||
" </a>\n",
|
||||
" <td style=\"text-align: center\">\n",
|
||||
|
||||
@@ -39,7 +39,7 @@
|
||||
" </a>\n",
|
||||
" </td>\n",
|
||||
" <td style=\"text-align: center\">\n",
|
||||
" <a href=\"https://console.cloud.google.com/vertex-ai/colab/import/https:%2F%2Fgithub.com%2FGoogleCloudPlatform%2Fvertex-ai-samples%2Fblob%2Fmain%2Fnotebooks%2Fofficial%2Fprediction%2Fget_started_with_nvidia_triton_serving.ipynb\">\n",
|
||||
" <a href=\"https://console.cloud.google.com/vertex-ai/colab/import/https:%2F%2Fraw.githubusercontent.com%2FGoogleCloudPlatform%2Fvertex-ai-samples%2F%2Fmain%2Fnotebooks%2Fofficial%2Fprediction%2Fget_started_with_nvidia_triton_serving.ipynb\">\n",
|
||||
" <img width=\"32px\" src=\"https://cloud.google.com/ml-engine/images/colab-enterprise-logo-32px.png\" alt=\"Google Cloud Colab Enterprise logo\"><br> Open in Colab Enterprise\n",
|
||||
" </a>\n",
|
||||
" </td> \n",
|
||||
|
||||
@@ -38,7 +38,7 @@
|
||||
" </a>\n",
|
||||
" </td>\n",
|
||||
" <td style=\"text-align: center\">\n",
|
||||
" <a href=\"https://console.cloud.google.com/vertex-ai/colab/import/https:%2F%2Fgithub.com%2FGoogleCloudPlatform%2Fvertex-ai-samples%2Fblob%2Fmain%2Fnotebooks%2Fofficial%2Fsdk%2FSDK_AutoML_Video_Classification.ipynb\">\n",
|
||||
" <a href=\"https://console.cloud.google.com/vertex-ai/colab/import/https:%2F%2Fraw.githubusercontent.com%2FGoogleCloudPlatform%2Fvertex-ai-samples%2Fmain%2Fnotebooks%2Fofficial%2Fsdk%2FSDK_AutoML_Video_Classification.ipynb\">\n",
|
||||
" <img width=\"32px\" src=\"https://cloud.google.com/ml-engine/images/colab-enterprise-logo-32px.png\" alt=\"Google Cloud Colab Enterprise logo\"><br> Open in Colab Enterprise\n",
|
||||
" </a>\n",
|
||||
" </td> \n",
|
||||
|
||||
@@ -29,23 +29,28 @@
|
||||
"id": "title"
|
||||
},
|
||||
"source": [
|
||||
"# Train a BigQuery ML ARIMA_PLUS Model using Vertex AI Tabular Workflows\n",
|
||||
"# Train a BigQuery ML ARIMA_PLUS Model using Vertex AI tabular workflows\n",
|
||||
"\n",
|
||||
"<table align=\"left\">\n",
|
||||
" <td>\n",
|
||||
" <td style=\"text-align: center\">\n",
|
||||
" <a href=\"https://colab.research.google.com/github/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/official/tabular_workflows/bqml_arima_plus.ipynb\">\n",
|
||||
" <img src=\"https://cloud.google.com/ml-engine/images/colab-logo-32px.png\" alt=\"Colab logo\"> Run in Colab\n",
|
||||
" <img src=\"https://cloud.google.com/ml-engine/images/colab-logo-32px.png\" alt=\"Colab logo\"><br> Run in Colab\n",
|
||||
" </a>\n",
|
||||
" </td>\n",
|
||||
" <td>\n",
|
||||
" <td style=\"text-align: center\">\n",
|
||||
" <a href=\"https://console.cloud.google.com/vertex-ai/colab/import/https:%2F%2Fgithub.com%2FGoogleCloudPlatform%2Fvertex-ai-samples%2Fblob%2Fmain%2Fnotebooks%2Fofficial%2Ftabular_workflows%2Fbqml_arima_plus.ipynb\">\n",
|
||||
" <img width=\"32px\" src=\"https://cloud.google.com/ml-engine/images/colab-enterprise-logo-32px.png\" alt=\"Google Cloud Colab Enterprise logo\"><br> Open in Colab Enterprise\n",
|
||||
" </a>\n",
|
||||
" </td>\n",
|
||||
" <td style=\"text-align: center\">\n",
|
||||
" <a href=\"https://github.com/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/official/tabular_workflows/bqml_arima_plus.ipynb\">\n",
|
||||
" <img src=\"https://cloud.google.com/ml-engine/images/github-logo-32px.png\" alt=\"GitHub logo\">\n",
|
||||
" <img src=\"https://cloud.google.com/ml-engine/images/github-logo-32px.png\" alt=\"GitHub logo\"><br>\n",
|
||||
" View on GitHub\n",
|
||||
" </a>\n",
|
||||
" </td>\n",
|
||||
" <td>\n",
|
||||
" <td style=\"text-align: center\">\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/tabular_workflows/bqml_arima_plus.ipynb\">\n",
|
||||
" <img src=\"https://lh3.googleusercontent.com/UiNooY4LUgW_oTvpsNhPpQzsstV5W8F7rYgxgGBD85cWJoLmrOzhVs_ksK_vgx40SHs7jCqkTkCk=e14-rj-sc0xffffff-h130-w32\" alt=\"Vertex AI logo\">\n",
|
||||
" <img src=\"https://lh3.googleusercontent.com/UiNooY4LUgW_oTvpsNhPpQzsstV5W8F7rYgxgGBD85cWJoLmrOzhVs_ksK_vgx40SHs7jCqkTkCk=e14-rj-sc0xffffff-h130-w32\" alt=\"Vertex AI logo\"><br>\n",
|
||||
" Open in Vertex AI Workbench\n",
|
||||
" </a>\n",
|
||||
" </td>\n",
|
||||
@@ -61,7 +66,7 @@
|
||||
"source": [
|
||||
"## Overview\n",
|
||||
"\n",
|
||||
"In this tutorial, you take on the role of a store planner who must determine how much inventory they will need to order for each of their products and stores for November 2019. You accomplish this by training a BigQuery ML (BQML) [ARIMA_PLUS](https://cloud.google.com/bigquery-ml/docs/reference/standard-sql/bigqueryml-syntax-create-time-series) forecasting model using historical sales data. A BQML ARIMA_PLUS model is useful if you need to perform many quick iterations of model training or if you need an inexpensive baseline to measure other models against.\"\n",
|
||||
"In this tutorial, you take on the role of a store planner who determines how much inventory for each product needs to be ordered for each store for November 2019. You accomplish this by training a BigQuery ML (BQML) [ARIMA_PLUS](https://cloud.google.com/bigquery-ml/docs/reference/standard-sql/bigqueryml-syntax-create-time-series) forecasting model using historical sales data. A BQML ARIMA_PLUS model is useful if you need to perform many quick iterations of model training or if you need an inexpensive baseline to measure other models against.\n",
|
||||
"\n",
|
||||
"Learn more about [BQML ARIMA+ forecasting for tabular data](https://cloud.google.com/vertex-ai/docs/tabular-data/forecasting-arima/overview)."
|
||||
]
|
||||
@@ -74,7 +79,7 @@
|
||||
"source": [
|
||||
"### Objective\n",
|
||||
"\n",
|
||||
"In this tutorial, you learn how to create an BigQuery ML ARIMA_PLUS model using a training [Vertex AI Pipeline](https://cloud.google.com/vertex-ai/docs/pipelines/introduction) from [Google Cloud Pipeline Components](https://cloud.google.com/vertex-ai/docs/pipelines/components-introduction) (GCPC), and then do a batch prediction using the corresponding prediction pipeline. You then train a Vertex AI Forecasting model using the same data and compare the evaluation metrics.\n",
|
||||
"In this notebook, you learn how to create the BigQuery ML ARIMA_PLUS model using a training [Vertex AI Pipeline](https://cloud.google.com/vertex-ai/docs/pipelines/introduction) from [Google Cloud Pipeline Components](https://cloud.google.com/vertex-ai/docs/pipelines/components-introduction) (GCPC), and then do a batch prediction using the corresponding prediction pipeline. You then train a Vertex AI forecasting model using the same data and compare the evaluation metrics.\n",
|
||||
"\n",
|
||||
"This tutorial uses the following Google Cloud ML services and resources:\n",
|
||||
"\n",
|
||||
@@ -96,7 +101,7 @@
|
||||
"source": [
|
||||
"### Dataset\n",
|
||||
"\n",
|
||||
"To demonstrate the tradeoffs between using BigQuery ML and Vertex AI Forecasting, this tutorial will use a synthetic dataset where product sales are dependent on a variety of factors such as advertisements, holidays, and locations. You see how well a univariate model like ARIMA_PLUS can forecast future sales without knowing information about these factors explicitly, and how well a multivariate model like Vertex AI Forecasting can perform when these factors are known."
|
||||
"To demonstrate the tradeoffs between BigQuery ML and Vertex AI forecasting, this tutorial uses a synthetic dataset where product sales are dependent on a variety of factors such as advertisements, holidays, and locations. You see how well a univariate model like ARIMA_PLUS can forecast future sales without knowing information about these factors explicitly, and how well a multivariate model like Vertex AI forecasting can perform when these factors are known."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -116,8 +121,16 @@
|
||||
"Learn about [Vertex AI\n",
|
||||
"pricing](https://cloud.google.com/vertex-ai/pricing), [Cloud Storage\n",
|
||||
"pricing](https://cloud.google.com/storage/pricing), and [BigQuery pricing](https://cloud.google.com/bigquery/pricing) and use the [Pricing\n",
|
||||
"Calculator](https://cloud.google.com/products/calculator/)\n",
|
||||
"to generate a cost estimate based on your projected usage."
|
||||
"Calculator](https://cloud.google.com/products/calculator/) to generate a cost estimate based on your projected usage."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "2927eacc7883"
|
||||
},
|
||||
"source": [
|
||||
"### Get started"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -126,9 +139,7 @@
|
||||
"id": "install_aip:mbsdk"
|
||||
},
|
||||
"source": [
|
||||
"## Installation\n",
|
||||
"\n",
|
||||
"Install the following packages required to execute this notebook."
|
||||
"### Install Vertex AI SDK for Python and other required packages"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -139,10 +150,11 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"! (pip3 install --upgrade --quiet \\\n",
|
||||
" google-cloud-aiplatform==1.40.0 \\\n",
|
||||
" google-cloud-bigquery[pandas]==3.17.1 \\\n",
|
||||
" google-cloud-pipeline-components==2.9.0)"
|
||||
"# Install required packages.\n",
|
||||
"! pip3 install --quiet --upgrade google-cloud-aiplatform \\\n",
|
||||
" google-cloud-bigquery \\\n",
|
||||
" google-cloud-pipeline-components \\\n",
|
||||
" db-dtypes"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -151,7 +163,49 @@
|
||||
"id": "restart"
|
||||
},
|
||||
"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": "f09b4dff629a"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import sys\n",
|
||||
"\n",
|
||||
"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": "4a2b7b59bbf7"
|
||||
},
|
||||
"source": [
|
||||
"<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>"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "f82e28c631cc"
|
||||
},
|
||||
"source": [
|
||||
"### Authenticate your notebook environment (Colab only)\n",
|
||||
"\n",
|
||||
"Authenticate your environment on Google Colab."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -162,11 +216,13 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Automatically restart kernel after installs so that your environment can access the new packages\n",
|
||||
"# 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",
|
||||
" from google.colab import auth\n",
|
||||
"\n",
|
||||
" auth.authenticate_user()"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -175,14 +231,9 @@
|
||||
"id": "before_you_begin:nogpu"
|
||||
},
|
||||
"source": [
|
||||
"## Before you begin\n",
|
||||
"### Set Google Cloud project information\n",
|
||||
"\n",
|
||||
"### 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)"
|
||||
"Learn more about [setting up a project and a development environment](https://cloud.google.com/vertex-ai/docs/start/cloud-environment)."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -194,90 +245,8 @@
|
||||
"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": "2dw8q9fdQEH5"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"REGION = \"us-central1\"\n",
|
||||
"DATA_REGION = \"US\""
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "gcp_authenticate"
|
||||
},
|
||||
"source": [
|
||||
"### Authenticate your Google Cloud account\n",
|
||||
"\n",
|
||||
"Depending on your Jupyter environment, you may have to manually authenticate. Follow the relevant instructions below.\n",
|
||||
"\n",
|
||||
"**1. Vertex AI Workbench**\n",
|
||||
"* Do nothing as you are already authenticated.\n",
|
||||
"\n",
|
||||
"**2. Local JupyterLab instance, uncomment and run:**"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "ce6043da7b33"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# ! gcloud auth login"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "0367eac06a10"
|
||||
},
|
||||
"source": [
|
||||
"**3. Colab, uncomment and run:**"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "21ad4dbb4a61"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# from google.colab import auth\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."
|
||||
"LOCATION = \"us-central1\" # @param {type:\"string\"}\n",
|
||||
"DATA_LOCATION = \"US\""
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -299,7 +268,7 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"BUCKET_URI = \"gs://your-bucket-name-unique\" # @param {type:\"string\"}"
|
||||
"BUCKET_URI = f\"gs://your-bucket-name-{PROJECT_ID}-unique\" # @param {type:\"string\"}"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -308,7 +277,7 @@
|
||||
"id": "autoset_bucket"
|
||||
},
|
||||
"source": [
|
||||
"**Only if your bucket doesn't already exist**: Run the following cell to create your Cloud Storage bucket."
|
||||
"**If your bucket doesn't already exist**: Run the following cell to create your Cloud Storage bucket."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -319,7 +288,7 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"! gsutil mb -l {REGION} -p {PROJECT_ID} {BUCKET_URI}"
|
||||
"! gsutil mb -l {LOCATION} -p {PROJECT_ID} {BUCKET_URI}"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -328,9 +297,9 @@
|
||||
"id": "85c4ecfd133a"
|
||||
},
|
||||
"source": [
|
||||
"#### Service Account \n",
|
||||
"#### Service Account\n",
|
||||
"\n",
|
||||
"You use a service account to create Vertex AI Pipeline jobs."
|
||||
"**If you don't know your service account**, try to get your service account using `gcloud` command by executing the second cell below."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -442,7 +411,7 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"aiplatform.init(project=PROJECT_ID, location=REGION, staging_bucket=BUCKET_URI)"
|
||||
"aiplatform.init(project=PROJECT_ID, location=LOCATION, staging_bucket=BUCKET_URI)"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -474,15 +443,15 @@
|
||||
"arima_dataset_name = \"forecasting_demo_arima\"\n",
|
||||
"arima_dataset_path = \".\".join([PROJECT_ID, arima_dataset_name])\n",
|
||||
"\n",
|
||||
"# Must be same region as TRAINING_DATASET_BQ_PATH.\n",
|
||||
"# Must be same location as TRAINING_DATASET_BQ_PATH.\n",
|
||||
"client = bigquery.Client(project=PROJECT_ID)\n",
|
||||
"bq_dataset_pre = bigquery.Dataset(arima_dataset_path)\n",
|
||||
"bq_dataset_pre.location = DATA_REGION\n",
|
||||
"bq_dataset_pre.location = DATA_LOCATION\n",
|
||||
"try:\n",
|
||||
" bq_dataset = client.create_dataset(bq_dataset_pre)\n",
|
||||
"except:\n",
|
||||
" bq_dataset = client.get_dataset(bq_dataset_pre)\n",
|
||||
"print(f\"Created bigquery dataset {arima_dataset_path} in {DATA_REGION}\")"
|
||||
"print(f\"Created bigquery dataset {arima_dataset_path} in {DATA_LOCATION}\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -493,9 +462,9 @@
|
||||
"source": [
|
||||
"### Prepare training data in BigQuery\n",
|
||||
"\n",
|
||||
"Before training a model, you must first generate our dataset of store sales. This dataset will include multiple products and stores, and it will also simulate factors such as advertisements and holiday effects. The data will be split into `TRAIN`, `VALIDATE`, `TEST`, and `PREDICT` sets, where the last three sets are all 1 month in duration.\n",
|
||||
"Before training a model, you must first generate our dataset of store sales. This dataset includes multiple products and stores, and it also simulates factors such as advertisements and holiday effects. The data is then split into `TRAIN`, `VALIDATE`, `TEST`, and `PREDICT` sets, where the last three sets are all 1 month in duration.\n",
|
||||
"\n",
|
||||
"#### Begin by defining the subqueries that will create this base sales data."
|
||||
"#### Begin by defining the subqueries that creates this base sales data."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -604,7 +573,7 @@
|
||||
"id": "IdWrtxYtqdsh"
|
||||
},
|
||||
"source": [
|
||||
"#### Next, convert this base sales data into a dataset you use to train a model, and a dataset you pass to a trained model at serving time. The training dataset will include the `TRAIN`, `VALIDATE`, and `TEST` splits, while the prediction dataset will include the `PREDICT` split and also the `TEST` split to provide context information."
|
||||
"Next, convert this base sales data into a dataset you use to train a model, and a dataset you pass to a trained model at serving time. The training dataset includes the `TRAIN`, `VALIDATE`, and `TEST` splits, while the prediction dataset includes the `PREDICT` split and also the `TEST` split to provide context information."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -651,7 +620,7 @@
|
||||
"id": "Gwr0eDQjpR4c"
|
||||
},
|
||||
"source": [
|
||||
"You can take a look at the sales data that was generated. Later in this tutorial, we will visualize the time series along with our forecast.\n",
|
||||
"You can take a look at the sales data that was generated. Later in this tutorial, you can see the visualization of the time series along with the forecast.\n",
|
||||
"\n",
|
||||
"The model is trained with data from January 2017 to October 2019 inclusive.\n",
|
||||
"\n",
|
||||
@@ -703,13 +672,13 @@
|
||||
"\n",
|
||||
"Now you are ready to start creating your own BigQuery ML ARIMA_PLUS model.\n",
|
||||
"\n",
|
||||
"Like with Vertex AI Forecasting, the pipeline you run will train evaluation models using the training and validation sets and use backtesting to create evaluation metrics on the test set. Finally, a serving model will be produced that uses all available data.\n",
|
||||
"Like with Vertex AI forecasting, the pipeline you run trains evaluation models using the training and validation sets and use backtesting to create evaluation metrics on the test set. Finally, a serving model that uses all available data can be produced.\n",
|
||||
"\n",
|
||||
"**How do you estimate the cost?**\n",
|
||||
"\n",
|
||||
"Backtesting involves training a single BigQuery ML model for each period in the test set, so the cost is a function of the length of the test set after any downsampling done by the windowing strategy. The cost is also multiplied by the number of candidate models trained, which is determined by `max_order`.\n",
|
||||
"\n",
|
||||
"According to [BQ pricing](https://cloud.google.com/bigquery-ml/pricing), BigQuery ML model creation costs $250 per TB. We'll use a max order of 3, which translates to 20 candidate models when there are multiple time series. Our demo dataset is 3 MB in size, and includes 31 test periods. We window with a stride length of 1, so all periods are used for evaluation.\n",
|
||||
"According to [BQ pricing](https://cloud.google.com/bigquery-ml/pricing), BigQuery ML model creation costs $250 per TB. You can use a max order of 3, which translates to 20 candidate models when there are multiple time series. The demo dataset is 3 MB in size, and includes 31 test periods. \n",
|
||||
"\n",
|
||||
"In this tutorial, the model create stage of the pipeline costs `3 MB * ($250 / 1024^2) * (31 / 1) periods * 20 candidates = $0.44`."
|
||||
]
|
||||
@@ -730,10 +699,10 @@
|
||||
"\n",
|
||||
"The training pipeline expects the following parameters:\n",
|
||||
"\n",
|
||||
"- `bigquery_destination_uri`: (optional) BigQuery Dataset URI. Used to export the metrics table and model. If not given, we will create one for the user.\n",
|
||||
"- `bigquery_destination_uri`: (optional) BigQuery Dataset URI. Used to export the metrics table and model. If not given, You can create one for the user.\n",
|
||||
"- `data_granularity_unit`: Enum used to specify the time granularity (hour, day, week, month, etc).\n",
|
||||
"- `data_source_csv_filenames` or `data_source_bigquery_table_path`: A URI for either a CSV stored in GCR or a BigQuery table, respectively.\n",
|
||||
"- `evaluated_examples_destination_uri\t`: (optional) BigQuery Dataset URI OR Table URI. Used to export the evaluated examples table. Will use bigquery_destination_uri if not provided.\n",
|
||||
"- `evaluated_examples_destination_uri`: (optional) BigQuery Dataset URI OR Table URI. Used to export the evaluated examples table. Uses bigquery_destination_uri, if not provided.\n",
|
||||
"- `forecast_horizon`: Integer number of periods to predict.\n",
|
||||
"- A data splitting strategy of either:\n",
|
||||
" - `predefined_split_key`: A column containing `TRAIN`, `VALIDATE`, or `TEST` to denote the splits for each row.\n",
|
||||
@@ -741,7 +710,7 @@
|
||||
" - `timestamp_split_key` plus the fractions in the previous option to perform fractional splitting on a column other than the time column.\n",
|
||||
"- A windowing strategy of either:\n",
|
||||
" - `window_column`: A boolean column decides whether or now each row gets considered when calculating the evaluation metrics.\n",
|
||||
" - `window_stride_length`: Every N rows will be used to compute the evaluation metrics.\n",
|
||||
" - `window_stride_length`: Every N rows are used to compute the evaluation metrics.\n",
|
||||
" - `window_max_count`: Downsample rows such that only the given number are used to calculate the evaluation metrics.\n",
|
||||
"- `target_column`: Name of target column.\n",
|
||||
"- `time_column`: Name of time column.\n",
|
||||
@@ -750,7 +719,7 @@
|
||||
"\n",
|
||||
"For a full list of parameters, see the GCPC SDK [documentation](https://google-cloud-pipeline-components.readthedocs.io/en/google-cloud-pipeline-components-2.9.0/api/v1/automl/forecasting.html#v1.automl.forecasting.get_bqml_arima_train_pipeline_and_parameters).\n",
|
||||
"\n",
|
||||
"The execution of the training pipeline may take around **20 minutes**."
|
||||
"The execution of the training pipeline can take up to 20 minutes or more."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -776,7 +745,7 @@
|
||||
" train_parameter_values,\n",
|
||||
") = utils.get_bqml_arima_train_pipeline_and_parameters(\n",
|
||||
" project=PROJECT_ID,\n",
|
||||
" location=REGION,\n",
|
||||
" location=LOCATION,\n",
|
||||
" root_dir=os.path.join(BUCKET_URI, \"pipeline_root\"),\n",
|
||||
" time_column=time_column,\n",
|
||||
" time_series_identifier_column=time_series_identifier_column,\n",
|
||||
@@ -800,9 +769,9 @@
|
||||
"source": [
|
||||
"### Run the training pipeline\n",
|
||||
"\n",
|
||||
"Use the Vertex AI Python SDK to kick off a training pipeline run. Once the run has started, the following cell outputs a link that will allow you to monitor the run. The link should look like this: \n",
|
||||
"Use the Vertex AI Python SDK to kick off a training pipeline run. Once the run has started, the following cell outputs a link that lets you monitor the run. The link should look like this: \n",
|
||||
"\n",
|
||||
"`https://console.cloud.google.com/vertex-ai/locations/[REGION]/pipelines/runs/[DISPLAY_NAME]`"
|
||||
"`https://console.cloud.google.com/vertex-ai/locations/[LOCATION]/pipelines/runs/[TRAIN_DISPLAY_NAME]`"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -814,12 +783,12 @@
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# The display name should be unique even if this cell is rerun.\n",
|
||||
"DISPLAY_NAME = f\"forecasting-demo-train-{str(uuid.uuid1())}\"\n",
|
||||
"TRAIN_DISPLAY_NAME = f\"forecasting-demo-train-{str(uuid.uuid1())}\"\n",
|
||||
"\n",
|
||||
"job = aiplatform.PipelineJob(\n",
|
||||
" job_id=DISPLAY_NAME,\n",
|
||||
" display_name=DISPLAY_NAME,\n",
|
||||
" pipeline_root=os.path.join(BUCKET_URI, DISPLAY_NAME),\n",
|
||||
" job_id=TRAIN_DISPLAY_NAME,\n",
|
||||
" display_name=TRAIN_DISPLAY_NAME,\n",
|
||||
" pipeline_root=os.path.join(BUCKET_URI, TRAIN_DISPLAY_NAME),\n",
|
||||
" template_path=train_job_spec_path,\n",
|
||||
" parameter_values=train_parameter_values,\n",
|
||||
" enable_caching=False,\n",
|
||||
@@ -895,14 +864,14 @@
|
||||
"### Create prediction job\n",
|
||||
"Now that your Model resource is trained, you can make a batch prediction using the prediction pipeline, with the following parameters:\n",
|
||||
"\n",
|
||||
"- `bigquery_destination_uri`: (optional) BigQuery Dataset URI. Used to export the metrics table and model. If not given, we will create one for the user.\n",
|
||||
"- `bigquery_destination_uri`: (optional) BigQuery Dataset URI. Used to export the metrics table and model. If not given, You can create one for the user.\n",
|
||||
"- `data_source_csv_filenames` or `data_source_bigquery_table_path`: A URI for either a CSV stored in GCR or a BigQuery table, respectively.\n",
|
||||
"- `generate_explanation`: If True, the predictions table will have some extra xAI columns.\n",
|
||||
"- `generate_explanation`: If True, the predictions table can have some extra explanations columns.\n",
|
||||
"- `model_name`: Name of an existing BigQuery ML ARIMA_PLUS model to use for predictions.\n",
|
||||
"\n",
|
||||
"For a full list of parameters, see the GCPC SDK [documentation](https://google-cloud-pipeline-components.readthedocs.io/en/google-cloud-pipeline-components-2.9.0/api/v1/automl/forecasting.html#v1.automl.forecasting.get_bqml_arima_predict_pipeline_and_parameters).\n",
|
||||
"\n",
|
||||
"The execution of the prediction pipeline may take around **5 minutes**."
|
||||
"The execution of the prediction pipeline can take up to 5 minutes or more."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -913,8 +882,7 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Get the model name programmatically, you can find this by looking at the\n",
|
||||
"# execution graph in Vertex AI Pipelines.\n",
|
||||
"# Get the model name programmatically, you can find this by looking at the execution graph in Vertex AI Pipelines.\n",
|
||||
"for task_detail in job.gca_resource.job_detail.task_details:\n",
|
||||
" if task_detail.task_name == \"bigquery-create-model-job\":\n",
|
||||
" model_name = task_detail.outputs[\"model\"].artifacts[0].metadata[\"modelId\"]\n",
|
||||
@@ -928,7 +896,7 @@
|
||||
" predict_parameter_values,\n",
|
||||
") = utils.get_bqml_arima_predict_pipeline_and_parameters(\n",
|
||||
" project=PROJECT_ID,\n",
|
||||
" location=REGION,\n",
|
||||
" location=LOCATION,\n",
|
||||
" model_name=f\"{arima_dataset_path}.{model_name}\",\n",
|
||||
" data_source_bigquery_table_path=PREDICTION_DATASET_BQ_PATH,\n",
|
||||
" bigquery_destination_uri=arima_dataset_path,\n",
|
||||
@@ -943,9 +911,9 @@
|
||||
"source": [
|
||||
"### Run the prediction pipeline\n",
|
||||
"\n",
|
||||
"Use the Vertex AI Python SDK to kick off a prediction pipeline run. Once the run has started, the following cell outputs a link that will allow you to monitor the run. The link should look like this: \n",
|
||||
"Use the Vertex AI Python SDK to kick off a prediction pipeline run. Once the run has started, the following cell outputs a link that lets you monitor the run. The link should look like this: \n",
|
||||
"\n",
|
||||
"`https://console.cloud.google.com/vertex-ai/locations/[REGION]/pipelines/runs/[DISPLAY_NAME]`"
|
||||
"`https://console.cloud.google.com/vertex-ai/locations/[LOCATION]/pipelines/runs/[PRED_DISPLAY_NAME]`"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -957,18 +925,18 @@
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# The display name should be unique even if this cell is rerun.\n",
|
||||
"DISPLAY_NAME = f\"forecasting-demo-predict-{str(uuid.uuid1())}\"\n",
|
||||
"PRED_DISPLAY_NAME = f\"forecasting-demo-predict-{str(uuid.uuid1())}\"\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"job = aiplatform.PipelineJob(\n",
|
||||
" job_id=DISPLAY_NAME,\n",
|
||||
" display_name=DISPLAY_NAME,\n",
|
||||
" pipeline_root=os.path.join(BUCKET_URI, DISPLAY_NAME),\n",
|
||||
"pred_job = aiplatform.PipelineJob(\n",
|
||||
" job_id=PRED_DISPLAY_NAME,\n",
|
||||
" display_name=PRED_DISPLAY_NAME,\n",
|
||||
" pipeline_root=os.path.join(BUCKET_URI, PRED_DISPLAY_NAME),\n",
|
||||
" template_path=predict_job_spec_path,\n",
|
||||
" parameter_values=predict_parameter_values,\n",
|
||||
" enable_caching=False,\n",
|
||||
")\n",
|
||||
"job.run(service_account=SERVICE_ACCOUNT)"
|
||||
"pred_job.run(service_account=SERVICE_ACCOUNT)"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -990,9 +958,8 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Get the prediction table programmatically, you can find this by looking at the\n",
|
||||
"# execution graph in Vertex AI Pipelines.\n",
|
||||
"for task_detail in job.gca_resource.job_detail.task_details:\n",
|
||||
"# Get the prediction table programmatically, you can find this by looking at the execution graph in Vertex AI Pipelines.\n",
|
||||
"for task_detail in pred_job.gca_resource.job_detail.task_details:\n",
|
||||
" if task_detail.task_name == \"bigquery-query-job\":\n",
|
||||
" pred_table = (\n",
|
||||
" task_detail.outputs[\"destination_table\"].artifacts[0].metadata[\"tableId\"]\n",
|
||||
@@ -1121,10 +1088,7 @@
|
||||
"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:\n",
|
||||
"\n",
|
||||
"- Cloud Storage Bucket\n",
|
||||
"- BigQuery tables"
|
||||
"Otherwise, you can delete the individual resources that you created in this tutorial"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -1138,8 +1102,11 @@
|
||||
"# Delete output datasets\n",
|
||||
"client.delete_dataset(arima_dataset_path, delete_contents=True, not_found_ok=True)\n",
|
||||
"\n",
|
||||
"delete_bucket = False\n",
|
||||
"if delete_bucket or os.getenv(\"IS_TESTING\"):\n",
|
||||
"job.delete()\n",
|
||||
"pred_job.delete()\n",
|
||||
"\n",
|
||||
"delete_bucket = True\n",
|
||||
"if delete_bucket:\n",
|
||||
" ! gsutil -m rm -r $BUCKET_URI"
|
||||
]
|
||||
}
|
||||
|
||||
+1
-1
@@ -38,7 +38,7 @@
|
||||
" </a>\n",
|
||||
" </td>\n",
|
||||
" <td style=\"text-align: center\">\n",
|
||||
" <a href=\"https://console.cloud.google.com/vertex-ai/colab/import/https:%2F%2Fgithub.com%2FGoogleCloudPlatform%2Fvertex-ai-samples%2Fblob%2Fmain%2Fnotebooks%2Fofficial%2Ftensorboard%2Ftensorboard_custom_training_with_prebuilt_container.ipynb\">\n",
|
||||
" <a href=\"https://console.cloud.google.com/vertex-ai/colab/import/https:%2F%2Fraw.githubusercontent.com%2FGoogleCloudPlatform%2Fvertex-ai-samples%2Fmain%2Fnotebooks%2Fofficial%2Ftensorboard%2Ftensorboard_custom_training_with_prebuilt_container.ipynb\">\n",
|
||||
" <img width=\"32px\" src=\"https://cloud.google.com/ml-engine/images/colab-enterprise-logo-32px.png\" alt=\"Google Cloud Colab Enterprise logo\"><br> Open in Colab Enterprise\n",
|
||||
" </a>\n",
|
||||
" </td> \n",
|
||||
|
||||
@@ -29,7 +29,7 @@
|
||||
"id": "l2mMvIUG9meX"
|
||||
},
|
||||
"source": [
|
||||
"# Profile model training performance using Vertex AI TensorBoard Profiler\n",
|
||||
"# Profile model training performance using Cloud Profiler\n",
|
||||
"\n",
|
||||
"<table align=\"left\">\n",
|
||||
" <td style=\"text-align: center\">\n",
|
||||
@@ -63,9 +63,9 @@
|
||||
"source": [
|
||||
"## Overview\n",
|
||||
"\n",
|
||||
"Vertex AI TensorBoard profiler lets you monitor and optimize your model training performance by helping you understand the resource consumption of training operations. This tutorial demonstrates how to enable Vertex AI TensorBoard profiler so you can debug model training performance for your custom training jobs.\n",
|
||||
"Cloud Profiler lets you monitor and optimize your model training performance by helping you understand the resource consumption of training operations. This tutorial demonstrates how to enable Profiler so you can debug model training performance for your custom training jobs.\n",
|
||||
"\n",
|
||||
"Learn more about [Vertex AI TensorBoard profiler](https://cloud.google.com/vertex-ai/docs/training/tensorboard-profiler)."
|
||||
"Learn more about [Profiler](https://cloud.google.com/vertex-ai/docs/training/tensorboard-profiler)."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -76,7 +76,7 @@
|
||||
"source": [
|
||||
"### Objective\n",
|
||||
"\n",
|
||||
"In this tutorial, you learn how to enable Vertex AI TensorBoard profiler for custom training jobs.\n",
|
||||
"In this tutorial, you learn how to enable Profiler for custom training jobs.\n",
|
||||
"\n",
|
||||
"This tutorial uses the following Google Cloud AI services:\n",
|
||||
"\n",
|
||||
@@ -87,8 +87,8 @@
|
||||
"\n",
|
||||
"- Setup a service account and a Cloud Storage bucket\n",
|
||||
"- Create a TensorBoard instance\n",
|
||||
"- Create and run a custom training job that enables TensorBoard profiler\n",
|
||||
"- View the TensorBoard profiler dashboard to debug your model training performance\n"
|
||||
"- Create and run a custom training job that enables Profiler\n",
|
||||
"- View the Profiler dashboard to debug your model training performance\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -597,7 +597,7 @@
|
||||
" histogram_freq=1) \n",
|
||||
"`AIP_TENSORBOARD_LOG_DIR` is in the `BASE_OUTPUT_DIR` that you provide when creating the custom training job.\n",
|
||||
"\n",
|
||||
"To enable Vertex AI TensorBoard profiler for your training job, add the following to your training script:\n",
|
||||
"To enable Profiler for your training job, add the following to your training script:\n",
|
||||
"\n",
|
||||
"Add the cloud_profiler import at your top level imports:\n",
|
||||
"\n",
|
||||
@@ -840,11 +840,11 @@
|
||||
"id": "JkEe2Nb_85UD"
|
||||
},
|
||||
"source": [
|
||||
"## View the TensorBoard Profiler dashboard\n",
|
||||
"## View the Profiler dashboard\n",
|
||||
"\n",
|
||||
"When the custom job state switches to running, you can access the Vertex AI TensorBoard profiler dashboard through the Custom jobs page or the Experiments page on the Google Cloud console. \n",
|
||||
"When the custom job state switches to running, you can access the Profiler dashboard through the Custom jobs page or the Experiments page on the Google Cloud console. \n",
|
||||
"\n",
|
||||
"The Google Cloud guide to [Profile model training performance using profiler](https://cloud.google.com/vertex-ai/docs/training/tensorboard-profiler) provides detailed instructions for accessing the Vertex AI TensorBoard profiler dashboard and capturing a profiling session. \n"
|
||||
"The Google Cloud guide to [Profile model training performance using Cloud Profiler](https://cloud.google.com/vertex-ai/docs/training/tensorboard-profiler) provides detailed instructions for accessing the Profiler dashboard and capturing a profiling session. \n"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
||||
@@ -39,7 +39,7 @@
|
||||
" </a>\n",
|
||||
" </td>\n",
|
||||
" <td style=\"text-align: center\">\n",
|
||||
" <a href=\"https://console.cloud.google.com/vertex-ai/colab/import/https:%2F%2Fgithub.com%2FGoogleCloudPlatform%2Fvertex-ai-samples%2Fblob%2Fmain%2Fnotebooks%2Fofficial%2Ftraining%2Fhyperparameter_tuning_tensorflow.ipynb\">\n",
|
||||
" <a href=\"https://console.cloud.google.com/vertex-ai/colab/import/https:%2F%2Fraw.githubusercontent.com%2FGoogleCloudPlatform%2Fvertex-ai-samples%2Fmain%2Fnotebooks%2Fofficial%2Ftraining%2Fhyperparameter_tuning_tensorflow.ipynb\">\n",
|
||||
" <img width=\"32px\" src=\"https://cloud.google.com/ml-engine/images/colab-enterprise-logo-32px.png\" alt=\"Google Cloud Colab Enterprise logo\"><br> Open in Colab Enterprise\n",
|
||||
" </a>\n",
|
||||
" </td> \n",
|
||||
|
||||
+79
-157
@@ -31,25 +31,28 @@
|
||||
"source": [
|
||||
"# Train a multi-class classification model for ads-targeting\n",
|
||||
"<table align=\"left\">\n",
|
||||
"\n",
|
||||
" <td>\n",
|
||||
" <a href=\"https://colab.research.google.com/github/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/official/workbench/ads_targetting/training-multi-class-classification-model-for-ads-targeting-usecase.ipynb\">\n",
|
||||
" <img src=\"https://cloud.google.com/ml-engine/images/colab-logo-32px.png\" alt=\"Colab logo\"> Run in Colab\n",
|
||||
" <td style=\"text-align: center\">\n",
|
||||
" <a href=\"https://colab.research.google.com/github/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/official/pipelines/lightweight_functions_component_io_kfp.ipynb\">\n",
|
||||
" <img src=\"https://cloud.google.com/ml-engine/images/colab-logo-32px.png\" alt=\"Google Colaboratory logo\"><br> Open in Colab\n",
|
||||
" </a>\n",
|
||||
" </td>\n",
|
||||
" <td>\n",
|
||||
" <a href=\"https://github.com/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/official/workbench/ads_targetting/training-multi-class-classification-model-for-ads-targeting-usecase.ipynb\">\n",
|
||||
" <img src=\"https://cloud.google.com/ml-engine/images/github-logo-32px.png\" alt=\"GitHub logo\">\n",
|
||||
" View on GitHub\n",
|
||||
" <td style=\"text-align: center\">\n",
|
||||
" <a href=\"https://console.cloud.google.com/vertex-ai/colab/import/https:%2F%2Fraw.githubusercontent.com%2FGoogleCloudPlatform%2Fvertex-ai-samples%2Fmain%2Fnotebooks%2Fnotebook_template.ipynb\">\n",
|
||||
" <img src=\"https://cloud.google.com/ml-engine/images/colab-enterprise-logo-32px.png\" alt=\"Google Cloud Colab Enterprise logo\"><br> Open in Colab Enterprise\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/workbench/ads_targetting/training-multi-class-classification-model-for-ads-targeting-usecase.ipynb\" target='_blank'>\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",
|
||||
" <td style=\"text-align: center\">\n",
|
||||
" <a href=\"https://github.com/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/official/pipelines/lightweight_functions_component_io_kfp.ipynb\">\n",
|
||||
" <img src=\"https://cloud.google.com/ml-engine/images/github-logo-32px.png\" alt=\"GitHub logo\"><br> View on GitHub\n",
|
||||
" </a>\n",
|
||||
" </td> \n",
|
||||
"</table>"
|
||||
" </td>\n",
|
||||
" <td style=\"text-align: center\">\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/pipelines/lightweight_functions_component_io_kfp.ipynb\" target='_blank'>\n",
|
||||
" <img src=\"https://lh3.googleusercontent.com/UiNooY4LUgW_oTvpsNhPpQzsstV5W8F7rYgxgGBD85cWJoLmrOzhVs_ksK_vgx40SHs7jCqkTkCk=e14-rj-sc0xffffff-h130-w32\" alt=\"Vertex AI logo\"><br> Open in Vertex AI Workbench\n",
|
||||
" </a>\n",
|
||||
" </td>\n",
|
||||
"</table>\n",
|
||||
"<br/><br/><br/>\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -121,12 +124,10 @@
|
||||
"* BigQuery\n",
|
||||
"* Cloud Storage\n",
|
||||
"\n",
|
||||
"Learn about [Vertex AI\n",
|
||||
"pricing](https://cloud.google.com/vertex-ai/pricing), [BigQuery\n",
|
||||
"pricing](https://cloud.google.com/bigquery/pricing) and [Cloud Storage\n",
|
||||
"pricing](https://cloud.google.com/storage/pricing), and use the [Pricing\n",
|
||||
"Calculator](https://cloud.google.com/products/calculator/)\n",
|
||||
"to generate a cost estimate based on your projected usage."
|
||||
"Learn about [Vertex AI pricing](https://cloud.google.com/vertex-ai/pricing), \n",
|
||||
"[BigQuery pricing](https://cloud.google.com/bigquery/pricing) and \n",
|
||||
"[Cloud Storage pricing](https://cloud.google.com/storage/pricing), and use the \n",
|
||||
"[Pricing Calculator](https://cloud.google.com/products/calculator/) to generate a cost estimate based on your projected usage."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -160,7 +161,8 @@
|
||||
"id": "58707a750154"
|
||||
},
|
||||
"source": [
|
||||
"### Colab only: Uncomment the following cell to restart the kernel."
|
||||
"### Restart runtime (Colab only)\n",
|
||||
"To use the newly installed packages, you must restart the runtime on Google Colab."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -171,153 +173,74 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Automatically restart kernel after installs so that your environment can access the new packages\n",
|
||||
"# 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": "WReHDGG5g0XY"
|
||||
"id": "4de1bd77992b"
|
||||
},
|
||||
"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)"
|
||||
"<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>"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "c96fdfa3e8d0"
|
||||
},
|
||||
"source": [
|
||||
"### Authenticate your notebook environment (Colab only)\n",
|
||||
"Authenticate your environment on Google Colab.\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "oM1iC_MfAts1"
|
||||
"id": "7de6ef0fac42"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"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": "bd0e79ceaea2"
|
||||
},
|
||||
"source": [
|
||||
"### Set Google Cloud project information\n",
|
||||
"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": "c6f5a2e08b2a"
|
||||
},
|
||||
"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": [
|
||||
"#### Set the region\n",
|
||||
"\n",
|
||||
"**Optional**: Update the 'REGION' variable to specify the region that you want to use. Learn more about [Vertex AI regions](https://cloud.google.com/vertex-ai/docs/general/locations)."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "nsN5NJKSu-GU"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"REGION = \"us-central1\" # @param {type: \"string\"}"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "b2b04f364669"
|
||||
},
|
||||
"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": "a9ee95826661"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import random\n",
|
||||
"import string\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"# Generate a uuid of a specifed length(default=8)\n",
|
||||
"def generate_uuid(length: int = 8) -> str:\n",
|
||||
" return \"\".join(random.choices(string.ascii_lowercase + string.digits, k=length))\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"UUID = generate_uuid()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "sBCra4QMA2wR"
|
||||
},
|
||||
"source": [
|
||||
"### Authenticate your Google Cloud account\n",
|
||||
"\n",
|
||||
"To authenticate your Google Cloud account, follow the instructions for your Jupyter environment:\n",
|
||||
"\n",
|
||||
"**1. Vertex AI Workbench**\n",
|
||||
"<br>You are already authenticated.\n",
|
||||
"\n",
|
||||
"**2. Local JupyterLab instance**\n",
|
||||
"<br>Uncomment and run the following code:"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "254614fa0c46"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# ! gcloud auth login"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "ef21552ccea8"
|
||||
},
|
||||
"source": [
|
||||
"**3. Colab**\n",
|
||||
"<br>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."
|
||||
"LOCATION = \"us-central1\" # @param {type:\"string\"}"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -348,7 +271,7 @@
|
||||
"id": "create_bucket"
|
||||
},
|
||||
"source": [
|
||||
"**Only if your bucket doesn't already exist**: Run the following cell to create your Cloud Storage bucket."
|
||||
"**If your bucket doesn't already exist**: Run the following cell to create your Cloud Storage bucket."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -359,7 +282,7 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"! gsutil mb -l $REGION -p $PROJECT_ID $BUCKET_URI"
|
||||
"! gsutil mb -l $LOCATION -p $PROJECT_ID $BUCKET_URI"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -379,7 +302,6 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import os\n",
|
||||
"import warnings\n",
|
||||
"\n",
|
||||
"import pandas as pd\n",
|
||||
@@ -834,11 +756,11 @@
|
||||
"source": [
|
||||
"### Automating the execution of the notebook using executor in Vertex AI Workbench managed notebooks instance\n",
|
||||
"\n",
|
||||
"If you are using Vertex AI Workbench managed notebooks instance, the executor can help you run a notebook file from start to end, with your choice of the environment, machine type, input parameters, and other characteristics. After setting up an execution, the notebook is executed as a job in Vertex AI custom training. Your jobs can be monitored from the <b>Notebook Executor</b> pane in the menu on the left.\n",
|
||||
"If you are using a Vertex AI Workbench managed notebooks instance, the executor can help you run a notebook file from start to end, with your choice of the environment, machine type, input parameters, and other characteristics. After setting up an execution, the notebook is executed as a job in Vertex AI custom training. Your jobs can be monitored from the <b>Notebook Executor</b> panel in the menu on the left.\n",
|
||||
"\n",
|
||||
"<img src=\"images/executor.png\"></img>\n",
|
||||
"\n",
|
||||
"Executor lets you choose the environment and machine type while automating the runs similar to Vertex AI training jobs without switching to the training jobs UI. Apart from the custom container that replicates the existing kernel by default, pre-built environments like TensorFlow Enterprise, PyTorch, and others can also be selected to run the notebook. Furthermore the required compute power can be specified by choosing from the list of machine types available, including GPUs."
|
||||
"Executor lets you choose the environment and machine type while automating the runs similar to Vertex AI training jobs without switching to the training jobs in in the Google Cloud console. Apart from the custom container that replicates the existing kernel by default, pre-built environments like TensorFlow Enterprise, PyTorch, and others can also be selected to run the notebook. Furthermore, the required compute power can be specified by choosing from the list of machine types available, including GPUs."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -889,7 +811,7 @@
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"GCS_PATH = BUCKET_URI + \"/path-to-save/\"\n",
|
||||
"model.save(GCS_PATH)"
|
||||
"model.export(GCS_PATH)"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -917,7 +839,7 @@
|
||||
"# Delete the Cloud Storage bucket\n",
|
||||
"\n",
|
||||
"delete_bucket = False\n",
|
||||
"if delete_bucket or os.getenv(\"IS_TESTING\"):\n",
|
||||
"if delete_bucket:\n",
|
||||
" ! gsutil -m rm -r $BUCKET_URI"
|
||||
]
|
||||
}
|
||||
|
||||
+93
-126
@@ -33,20 +33,25 @@
|
||||
"\n",
|
||||
"<table align=\"left\">\n",
|
||||
"\n",
|
||||
" <td>\n",
|
||||
" <td style=\"text-align: center\">\n",
|
||||
" <a href=\"https://colab.research.google.com/github/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/official/workbench/chicago_taxi_fare_prediction/chicago_taxi_fare_prediction.ipynb\">\n",
|
||||
" <img src=\"https://cloud.google.com/ml-engine/images/colab-logo-32px.png\" alt=\"Colab logo\"> Run in Colab\n",
|
||||
" <img src=\"https://cloud.google.com/ml-engine/images/colab-logo-32px.png\" alt=\"Colab logo\"><br> Run in Colab\n",
|
||||
" </a>\n",
|
||||
" </td>\n",
|
||||
" <td>\n",
|
||||
" <td style=\"text-align: center\">\n",
|
||||
" <a href=\"https://console.cloud.google.com/vertex-ai/colab/import/https:%2F%2Fgithub.com%2FGoogleCloudPlatform%2Fvertex-ai-samples%2Fblob%2Fmain%2Fnotebooks%2Fofficial%2Fworkbench%2Fchicago_taxi_fare_prediction%2Fchicago_taxi_fare_prediction.ipynb\">\n",
|
||||
" <img width=\"32px\" src=\"https://cloud.google.com/ml-engine/images/colab-enterprise-logo-32px.png\\\" alt=\"Google Cloud Colab Enterprise logo\\\"><br> Open in Colab Enterprise\n",
|
||||
" </a>\n",
|
||||
" </td>\n",
|
||||
" <td style=\"text-align: center\">\n",
|
||||
" <a href=\"https://github.com/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/official/workbench/chicago_taxi_fare_prediction/chicago_taxi_fare_prediction.ipynb\">\n",
|
||||
" <img src=\"https://cloud.google.com/ml-engine/images/github-logo-32px.png\" alt=\"GitHub logo\">\n",
|
||||
" <img src=\"https://cloud.google.com/ml-engine/images/github-logo-32px.png\" alt=\"GitHub logo\"><br>\n",
|
||||
" View on GitHub\n",
|
||||
" </a>\n",
|
||||
" </td>\n",
|
||||
" <td>\n",
|
||||
" <td style=\"text-align: center\">\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/workbench/chicago_taxi_fare_prediction/chicago_taxi_fare_prediction.ipynb\" target='_blank'>\n",
|
||||
" <img src=\"https://lh3.googleusercontent.com/UiNooY4LUgW_oTvpsNhPpQzsstV5W8F7rYgxgGBD85cWJoLmrOzhVs_ksK_vgx40SHs7jCqkTkCk=e14-rj-sc0xffffff-h130-w32\" alt=\"Vertex AI logo\">\n",
|
||||
" <img src=\"https://lh3.googleusercontent.com/UiNooY4LUgW_oTvpsNhPpQzsstV5W8F7rYgxgGBD85cWJoLmrOzhVs_ksK_vgx40SHs7jCqkTkCk=e14-rj-sc0xffffff-h130-w32\" alt=\"Vertex AI logo\"><br>\n",
|
||||
" Open in Vertex AI Workbench\n",
|
||||
" </a>\n",
|
||||
" </td> \n",
|
||||
@@ -81,9 +86,9 @@
|
||||
"\n",
|
||||
"This tutorial uses the following Google Cloud ML services and resources:\n",
|
||||
"\n",
|
||||
"- `Vertex AI Model` resource\n",
|
||||
"- `Vertex AI Endpoint` resource\n",
|
||||
"- `Vertex Explainable AI`\n",
|
||||
"- Vertex AI model resource\n",
|
||||
"- Vertex AI endpoint resource\n",
|
||||
"- Vertex Explainable AI\n",
|
||||
"- Google Cloud Storage\n",
|
||||
"- BigQuery\n",
|
||||
"\n",
|
||||
@@ -111,7 +116,7 @@
|
||||
"\n",
|
||||
"For more information about this dataset and how it was created, see the [Chicago Digital website](http://digital.cityofchicago.org/index.php/chicago-taxi-data-released).\n",
|
||||
"\n",
|
||||
"The original dataset considered for this tutorial is a large and noisy one and so data from a specific date range will be used. Based on various online resources, the data from around May 2018 gave some really good results compared to the other date ranges. While there are also some complicated models proposed for the same problem, like considering the weather data, holidays and seasons, the current notebook only explores a simple linear regression model. Our main objective is to demonstrate the model deployment with Vertex Explainable AI configured on Vertex AI.\n",
|
||||
"The original dataset considered for this tutorial is a large and noisy one and so data from a specific date range is used. Based on various online resources, the data from around May 2018 gave some really good results compared to the other date ranges. While there are also some complicated models proposed for the same problem, like considering the weather data, holidays and seasons, the current notebook only explores a simple linear regression model. Our main objective is to demonstrate the model deployment with Vertex Explainable AI configured on Vertex AI.\n",
|
||||
"\n",
|
||||
"The chosen dataset consists of the following fields:\n",
|
||||
"\n",
|
||||
@@ -126,7 +131,7 @@
|
||||
"- `pickup_community_area`: The Community Area where the trip began.\n",
|
||||
"- `dropoff_community_area`: The Community Area where the trip ended.\n",
|
||||
"- `fare`: The fare for the trip.\n",
|
||||
"- `tips`: The tip for the trip. Cash tips generally will not be recorded.\n",
|
||||
"- `tips`: The tip for the trip. Cash tips are generally not recorded.\n",
|
||||
"- `tolls`: The tolls for the trip.\n",
|
||||
"- `extras`: Extra charges for the trip.\n",
|
||||
"- `trip_total`: Total cost of the trip, the total of the fare, tips, tolls, and extras.\n",
|
||||
@@ -158,8 +163,16 @@
|
||||
"Learn about [Vertex AI\n",
|
||||
"pricing](https://cloud.google.com/vertex-ai/pricing), [BigQuery pricing](https://cloud.google.com/bigquery/pricing) and [Cloud Storage\n",
|
||||
"pricing](https://cloud.google.com/storage/pricing), and use the [Pricing\n",
|
||||
"Calculator](https://cloud.google.com/products/calculator/)\n",
|
||||
"to generate a cost estimate based on your projected usage."
|
||||
"Calculator](https://cloud.google.com/products/calculator/) to generate a cost estimate based on your projected usage."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "2927eacc7883"
|
||||
},
|
||||
"source": [
|
||||
"### Get started"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -168,9 +181,7 @@
|
||||
"id": "23976b1be293"
|
||||
},
|
||||
"source": [
|
||||
"## Installation\n",
|
||||
"\n",
|
||||
"Install the following packages required to execute this notebook. "
|
||||
"### Install Vertex AI SDK for Python and other required packages"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -185,11 +196,12 @@
|
||||
" google-cloud-aiplatform \\\n",
|
||||
" google-cloud-storage \\\n",
|
||||
" seaborn==0.12.0 \\\n",
|
||||
" numpy==1.26.4 \\\n",
|
||||
" scikit-learn \\\n",
|
||||
" pandas \\\n",
|
||||
" fsspec \\\n",
|
||||
" pandas==2.0.3 \\\n",
|
||||
" fsspec==2024.6.0 \\\n",
|
||||
" db-dtypes \\\n",
|
||||
" pyarrow "
|
||||
" pyarrow==14.0.0 "
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -198,22 +210,66 @@
|
||||
"id": "58707a750154"
|
||||
},
|
||||
"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": "f09b4dff629a"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Automatically restart kernel after installs so that your environment can access the new packages\n",
|
||||
"# 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": "4a2b7b59bbf7"
|
||||
},
|
||||
"source": [
|
||||
"<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>"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "f82e28c631cc"
|
||||
},
|
||||
"source": [
|
||||
"### Authenticate your notebook environment (Colab only)\n",
|
||||
"\n",
|
||||
"Authenticate your environment on Google Colab."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "46604f70e831"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import sys\n",
|
||||
"\n",
|
||||
"if \"google.colab\" in sys.modules:\n",
|
||||
"\n",
|
||||
" from google.colab import auth\n",
|
||||
"\n",
|
||||
" auth.authenticate_user()"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -222,12 +278,9 @@
|
||||
"id": "WReHDGG5g0XY"
|
||||
},
|
||||
"source": [
|
||||
"#### Set your project ID\n",
|
||||
"### Set Google Cloud project information\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)"
|
||||
"Learn more about [setting up a project and a development environment](https://cloud.google.com/vertex-ai/docs/start/cloud-environment)."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -239,31 +292,7 @@
|
||||
"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": [
|
||||
"#### Set the region\n",
|
||||
"\n",
|
||||
"**Optional**: Update the 'REGION' variable to specify the region that you want to use. Learn more about [Vertex AI regions](https://cloud.google.com/vertex-ai/docs/general/locations)."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "nsN5NJKSu-GU"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"REGION = \"us-central1\" # @param {type: \"string\"}"
|
||||
"LOCATION = \"us-central1\" # @param {type: \"string\"}"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -297,67 +326,6 @@
|
||||
"UUID = generate_uuid()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "sBCra4QMA2wR"
|
||||
},
|
||||
"source": [
|
||||
"### Authenticate your Google Cloud account\n",
|
||||
"\n",
|
||||
"To authenticate your Google Cloud account, follow the instructions for your Jupyter environment:\n",
|
||||
"\n",
|
||||
"**1. Vertex AI Workbench**\n",
|
||||
"<br>You are already authenticated.\n",
|
||||
"\n",
|
||||
"**2. Local JupyterLab instance**\n",
|
||||
"<br>Uncomment and run the following code:"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "254614fa0c46"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# ! gcloud auth login"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "ef21552ccea8"
|
||||
},
|
||||
"source": [
|
||||
"**3. Colab**\n",
|
||||
"<br>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": {
|
||||
@@ -386,7 +354,7 @@
|
||||
"id": "create_bucket"
|
||||
},
|
||||
"source": [
|
||||
"**Only if your bucket doesn't already exist**: Run the following cell to create your Cloud Storage bucket."
|
||||
"**If your bucket doesn't already exist**: Run the following cell to create your Cloud Storage bucket."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -397,7 +365,7 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"! gsutil mb -l $REGION -p $PROJECT_ID $BUCKET_URI"
|
||||
"! gsutil mb -l $LOCATION -p $PROJECT_ID $BUCKET_URI"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -417,7 +385,6 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import os\n",
|
||||
"import pickle\n",
|
||||
"\n",
|
||||
"import matplotlib.pyplot as plt\n",
|
||||
@@ -432,7 +399,7 @@
|
||||
"%matplotlib inline\n",
|
||||
"\n",
|
||||
"from sklearn.linear_model import LinearRegression\n",
|
||||
"from sklearn.metrics import mean_squared_error, r2_score\n",
|
||||
"from sklearn.metrics import r2_score, root_mean_squared_error\n",
|
||||
"from sklearn.model_selection import train_test_split"
|
||||
]
|
||||
},
|
||||
@@ -1116,10 +1083,10 @@
|
||||
"# print test R2 score\n",
|
||||
"y_train_pred = reg.predict(X_train)\n",
|
||||
"train_score = r2_score(y_train, y_train_pred)\n",
|
||||
"train_rmse = mean_squared_error(y_train, y_train_pred, squared=False)\n",
|
||||
"train_rmse = root_mean_squared_error(y_train, y_train_pred)\n",
|
||||
"y_test_pred = reg.predict(X_test)\n",
|
||||
"test_score = r2_score(y_test, y_test_pred)\n",
|
||||
"test_rmse = mean_squared_error(y_test, y_test_pred, squared=False)\n",
|
||||
"test_rmse = root_mean_squared_error(y_test, y_test_pred)\n",
|
||||
"print(\"Train R2-score:\", train_score, \"Train RMSE:\", train_rmse)\n",
|
||||
"print(\"Test R2-score:\", test_score, \"Test RMSE:\", test_rmse)"
|
||||
]
|
||||
@@ -1240,7 +1207,7 @@
|
||||
"source": [
|
||||
"# Create a Vertex AI model resource with support for Vertex Explainable AI\n",
|
||||
"\n",
|
||||
"aiplatform.init(project=PROJECT_ID, location=REGION)\n",
|
||||
"aiplatform.init(project=PROJECT_ID, location=LOCATION)\n",
|
||||
"\n",
|
||||
"model = aiplatform.Model.upload(\n",
|
||||
" display_name=MODEL_DISPLAY_NAME,\n",
|
||||
@@ -1284,7 +1251,7 @@
|
||||
" ENDPOINT_DISPLAY_NAME = \"taxi_fare_prediction_endpoint\"\n",
|
||||
"\n",
|
||||
"endpoint = aiplatform.Endpoint.create(\n",
|
||||
" display_name=ENDPOINT_DISPLAY_NAME, project=PROJECT_ID, location=REGION\n",
|
||||
" display_name=ENDPOINT_DISPLAY_NAME, project=PROJECT_ID, location=LOCATION\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"print(endpoint.display_name)\n",
|
||||
@@ -1447,7 +1414,7 @@
|
||||
"\n",
|
||||
"\n",
|
||||
"test_json = [X_test.iloc[0].tolist(), X_test.iloc[1].tolist()]\n",
|
||||
"prediction = explain_tabular_sample(PROJECT_ID, REGION, endpoint, test_json)"
|
||||
"prediction = explain_tabular_sample(PROJECT_ID, LOCATION, endpoint, test_json)"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -1499,7 +1466,7 @@
|
||||
"# Set this to true only if you'd like to delete your bucket\n",
|
||||
"delete_bucket = False\n",
|
||||
"\n",
|
||||
"if delete_bucket or os.getenv(\"IS_TESTING\"):\n",
|
||||
"if delete_bucket:\n",
|
||||
" ! gsutil -m rm -r $BUCKET_URI"
|
||||
]
|
||||
}
|
||||
|
||||
@@ -39,7 +39,8 @@
|
||||
" </a>\n",
|
||||
" </td>\n",
|
||||
" <td style=\"text-align: center\">\n",
|
||||
" <a href=\"https://console.cloud.google.com/vertex-ai/colab/import/https:%2F%2Fgithub.com%2FGoogleCloudPlatform%2Fvertex-ai-samples%2Fblob%2Fmain%2Fnotebooks%2Fofficial%2Fworkbench%2Ffraud_detection%2Ffraud-detection-model.ipynb\">\n",
|
||||
" <a href=\"\n",
|
||||
"https://console.cloud.google.com/vertex-ai/colab/import/https:%2F%2Fraw.githubusercontent.com%2FGoogleCloudPlatform%2Fvertex-ai-samples%2Fmain%2Fnotebooks%2Fofficial%2Fworkbench%2Ffraud_detection%2Ffraud-detection-model.ipynb\">\n",
|
||||
" <img width=\"32px\" src=\"https://cloud.google.com/ml-engine/images/colab-enterprise-logo-32px.png\" alt=\"Google Cloud Colab Enterprise logo\"><br> Open in Colab Enterprise\n",
|
||||
" </a>\n",
|
||||
" </td>\n",
|
||||
|
||||
@@ -23,6 +23,17 @@
|
||||
"# limitations under the License."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "78fad7a79180"
|
||||
},
|
||||
"source": [
|
||||
"<div class=\"alert alert-block alert-warning\">\n",
|
||||
"<b>⚠️ Vertex AI Workbench user-managed notebooks is <a href=\"https://cloud.google.com/vertex-ai/docs/deprecations\">deprecated</a>. On January 30, 2025, support for user-managed notebooks will end and the ability to create user-managed notebooks instances will be removed. Existing instances will continue to function but patches, updates, and upgrades won't be available. To continue using Vertex AI Workbench, complete the steps on this page to <a href=\"https://cloud.google.com/vertex-ai/docs/workbench/user-managed/migrate-to-instances\">migrate your user-managed notebooks instances to Vertex AI Workbench instances.</a>⚠️</b>\n",
|
||||
"</div>\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
@@ -31,27 +42,28 @@
|
||||
"source": [
|
||||
"# Inventory prediction on ecommerce data using Vertex AI\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"<table align=\"left\">\n",
|
||||
"\n",
|
||||
" <td>\n",
|
||||
" <td style=\"text-align: center\">\n",
|
||||
" <a href=\"https://colab.research.google.com/github/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/official/workbench/inventory-prediction/inventory_prediction.ipynb\">\n",
|
||||
" <img src=\"https://cloud.google.com/ml-engine/images/colab-logo-32px.png\" alt=\"Colab logo\"> Run in Colab\n",
|
||||
" <img src=\"https://cloud.google.com/ml-engine/images/colab-logo-32px.png\" alt=\"Google Colaboratory logo\"><br> Open in Colab\n",
|
||||
" </a>\n",
|
||||
" </td>\n",
|
||||
" <td>\n",
|
||||
" <td style=\"text-align: center\">\n",
|
||||
" <a href=\"https://console.cloud.google.com/vertex-ai/colab/import/https:%2F%2Fraw.githubusercontent.com%2FGoogleCloudPlatform%2Fvertex-ai-samples%2Fmain%2Fnotebooks%2Fofficial%2Fworkbench%2Finventory-prediction%2Finventory_prediction.ipynb\">\n",
|
||||
" <img width=\"32px\" src=\"https://cloud.google.com/ml-engine/images/colab-enterprise-logo-32px.png\" alt=\"Google Cloud Colab Enterprise logo\"><br> Open in Colab Enterprise\n",
|
||||
" </a>\n",
|
||||
" </td> \n",
|
||||
" <td style=\"text-align: center\">\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/workbench/inventory-prediction/inventory_prediction.ipynb\">\n",
|
||||
" <img src=\"https://lh3.googleusercontent.com/UiNooY4LUgW_oTvpsNhPpQzsstV5W8F7rYgxgGBD85cWJoLmrOzhVs_ksK_vgx40SHs7jCqkTkCk=e14-rj-sc0xffffff-h130-w32\" alt=\"Vertex AI logo\"><br> Open in Workbench\n",
|
||||
" </a>\n",
|
||||
" </td>\n",
|
||||
" <td style=\"text-align: center\">\n",
|
||||
" <a href=\"https://github.com/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/official/workbench/inventory-prediction/inventory_prediction.ipynb\">\n",
|
||||
" <img src=\"https://cloud.google.com/ml-engine/images/github-logo-32px.png\" alt=\"GitHub logo\">\n",
|
||||
" View on GitHub\n",
|
||||
" <img src=\"https://cloud.google.com/ml-engine/images/github-logo-32px.png\" alt=\"GitHub logo\"><br> 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/workbench/inventory-prediction/inventory_prediction.ipynb\" target='_blank'>\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>"
|
||||
"</table>\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -77,7 +89,7 @@
|
||||
"\n",
|
||||
"Learn more about [Vertex AI Workbench](https://cloud.google.com/vertex-ai/docs/workbench/introduction) and [Vertex AI Training](https://cloud.google.com/vertex-ai/docs/training/custom-training).\n",
|
||||
"\n",
|
||||
"Note: The What-IF tool widget is tested on Colab and Vertex AI workbench's managed instances. It may not work on user-managed instances."
|
||||
"**Note**: The What-IF tool widget is tested on Colab and Vertex AI workbench's managed instances. It may not work on user-managed instances."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -103,7 +115,7 @@
|
||||
"* Load the dataset from BigQuery using the \"BigQuery in Notebooks\" integration.\n",
|
||||
"* Analyze the dataset.\n",
|
||||
"* Preprocess the features in the dataset.\n",
|
||||
"* Build a random forest classifier model that predicts whether a product will get sold in the next 60 days.\n",
|
||||
"* Build a random forest classifier model that predicts whether a product is sold in the next 60 days.\n",
|
||||
"* Evaluate the model.\n",
|
||||
"* Deploy the model using Vertex AI.\n",
|
||||
"* Configure and test with the What-If Tool."
|
||||
@@ -130,7 +142,7 @@
|
||||
"- `product_department`: Department to which the product belonged to\n",
|
||||
"- `product_distribution_center_id`: Which distribution center (an approximation of regions) the product was sold from\n",
|
||||
"\n",
|
||||
"The dataset is encoded to hide any private information. For example, the distribution centers have been assigned ID numbers ranging from 1 to 10."
|
||||
"The dataset is encoded to hide any private information. For example, ID numbers ranging from 1 to 10 are assigned to the distribution centers."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -158,12 +170,19 @@
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "0c522634d632"
|
||||
"id": "61RBz8LLbxCR"
|
||||
},
|
||||
"source": [
|
||||
"## Installation\n",
|
||||
"\n",
|
||||
"Install the following packages required to execute this notebook. "
|
||||
"## Get started"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "No17Cw5hgx12"
|
||||
},
|
||||
"source": [
|
||||
"### Install Vertex AI SDK for Python and other required packages\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -182,7 +201,8 @@
|
||||
" witwidget \\\n",
|
||||
" pyarrow \\\n",
|
||||
" db-dtypes \\\n",
|
||||
" gcsfs\n",
|
||||
" gcsfs \\\n",
|
||||
" matplotlib\n",
|
||||
"\n",
|
||||
"! pip3 install scikit-learn==1.2 protobuf==3.20.1"
|
||||
]
|
||||
@@ -190,60 +210,80 @@
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "e9255e3b156f"
|
||||
"id": "R5Xep4W9lq-Z"
|
||||
},
|
||||
"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": "0c0b2427998a"
|
||||
"id": "XRvKdaPDTznN"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Automatically restart kernel after installs so that your environment can access the new packages\n",
|
||||
"# 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": "c587f3b9e1e9"
|
||||
"id": "SbmM4z7FOBpM"
|
||||
},
|
||||
"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": "435b8e413535"
|
||||
"id": "dmWOrTJ3gx13"
|
||||
},
|
||||
"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": "NyKGtVQjgx13"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"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": "DF4l8DTdWgPY"
|
||||
},
|
||||
"source": [
|
||||
"### Set Google Cloud project information\n",
|
||||
"\n",
|
||||
"To get started using Vertex AI, you must have an existing Google Cloud project. Learn more about [setting up a project and a development environment](https://cloud.google.com/vertex-ai/docs/start/cloud-environment)."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -255,92 +295,12 @@
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"PROJECT_ID = \"[your-project-id]\" # @param {type:\"string\"}\n",
|
||||
"LOCATION = \"us-central1\" # @param {type:\"string\"}\n",
|
||||
"\n",
|
||||
"# set the project id\n",
|
||||
"! gcloud config set project {PROJECT_ID}"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "2e6b8b324ce1"
|
||||
},
|
||||
"source": [
|
||||
"#### Region\n",
|
||||
"\n",
|
||||
"You can also change the `REGION` variable used by Vertex AI. \n",
|
||||
"Learn more about [Vertex AI regions](https://cloud.google.com/vertex-ai/docs/general/locations)."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "ae43d96c4b1b"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"REGION = \"us-central1\" # @param {type: \"string\"}"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "6c43a8673066"
|
||||
},
|
||||
"source": [
|
||||
"### Authenticate your Google Cloud account\n",
|
||||
"\n",
|
||||
"Depending on your Jupyter environment, you may have to manually authenticate. Follow the relevant instructions below.\n",
|
||||
"\n",
|
||||
"**1. Vertex AI Workbench** \n",
|
||||
"- Do nothing as you are already authenticated.\n",
|
||||
"\n",
|
||||
"**2. Local JupyterLab Instance,** uncomment and run."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "fbc9cd30cc4b"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# ! gcloud auth login"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "cd0da2c26879"
|
||||
},
|
||||
"source": [
|
||||
"**3. Colab,** uncomment and run:"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "a336a05c6149"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# from google.colab import auth\n",
|
||||
"# auth.authenticate_user()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "0461097edfa5"
|
||||
},
|
||||
"source": [
|
||||
"**4. Service Account or other**\n",
|
||||
"- See all the authentication options here: [Google Cloud Platform Jupyter Notebook Authentication Guide](https://github.com/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/notebook_authentication_guide.ipynb)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
@@ -380,7 +340,7 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"! gsutil mb -l {REGION} -p {PROJECT_ID} {BUCKET_URI}"
|
||||
"! gsutil mb -l {LOCATION} -p {PROJECT_ID} {BUCKET_URI}"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -434,7 +394,7 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"aiplatform.init(project=PROJECT_ID, location=REGION, staging_bucket=BUCKET_URI)"
|
||||
"aiplatform.init(project=PROJECT_ID, location=LOCATION, staging_bucket=BUCKET_URI)"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -447,7 +407,7 @@
|
||||
"\n",
|
||||
"The following cell integrates with BigQuery data from the same project through the Vertex AI's \"BigQuery in Notebooks\" integration. It can run an SQL query as it would run in the BigQuery console. \n",
|
||||
"\n",
|
||||
"*Note:* This feature only works in a notebook running on a Vertex AI Workbench managed-notebook instance."
|
||||
"**Note:** This feature only works in a notebook running on a Vertex AI Workbench managed-notebook instance."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -558,7 +518,7 @@
|
||||
"id": "730f6c2954f5"
|
||||
},
|
||||
"source": [
|
||||
"Apart from the `sold_at` datetime field, there aren't any fields that consist of null values in the dataset. As you are dealing with the inventory-item data, it is absolutely plausible that there will be some items that haven't been sold yet and hence the null values.\n",
|
||||
"Apart from the `sold_at` datetime field, there aren't any fields that consist of null values in the dataset. As you're dealing with the inventory-item data, it's absolutely plausible that there would be some items that haven't been sold yet and hence the null values.\n",
|
||||
"\n",
|
||||
"### Clean the datetime fields\n",
|
||||
"Next, convert the date fields to a proper date format to process them in the next steps."
|
||||
@@ -631,7 +591,7 @@
|
||||
"id": "5d9a610a5c10"
|
||||
},
|
||||
"source": [
|
||||
"Calculate the average number of days a product had been in the inventory until it was sold."
|
||||
"Calculate the average number of days a product is in inventory until it's sold."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -803,7 +763,7 @@
|
||||
"\n",
|
||||
"## Feature preprocessing\n",
|
||||
"\n",
|
||||
"Next, aggregate the data based on suitable categorical fields in the data and take the average number of days it took for the product to get sold. For a given `product_id`, there can be multiple item `id`'s in this dataset and you want to predict at the product level whether that particular product is going to be sold in the next couple of months. You are aggregating the data based on each of the product configurations present in this dataset like the price, cost, category and at which center it is being sold. This way the model can predict whether a product with certain properties is going to be sold in the next couple of months.\n",
|
||||
"Next, aggregate the data based on suitable categorical fields in the data and take the average number of days it took for the product to get sold. For a given `product_id`, there can be multiple item `id`'s in this dataset and you want to predict at the product level whether that particular product is going to be sold in the next couple of months. You're aggregating the data based on each of the product configurations present in this dataset like the price, cost, category and at which center it's sold. This way the model can predict whether a product with certain properties is going to be sold in the next couple of months.\n",
|
||||
"\n",
|
||||
"### Generate aggregate features\n",
|
||||
"\n",
|
||||
@@ -904,7 +864,7 @@
|
||||
"id": "8acaa01b21bd"
|
||||
},
|
||||
"source": [
|
||||
"Here, you can see that most of the products are sold within 60 days since they've arrived in the inventory/store. In this tutorial, you will train a machine learning model that predicts the probability of a product being sold within 60 days.\n",
|
||||
"Here, you can see that most of the products are sold within 60 days since they've arrived in the inventory/store. In this tutorial, you're going to train a machine learning model that predicts the probability of a product being sold within 60 days.\n",
|
||||
"\n",
|
||||
"### Encode the categorical fields\n",
|
||||
"\n",
|
||||
@@ -1377,7 +1337,7 @@
|
||||
"\n",
|
||||
"The What-If Tool can be used to analyze the model predictions on test data. In this tutorial, the What-If Tool is configured and run on the model deployed on Vertex AI Endpoints in the previous steps.\n",
|
||||
"\n",
|
||||
"WitConfigBuilder provides the set_ai_platform_model() method to configure the What-If Tool with a model deployed as a version on Ai Platform models. This feature currently supports only Ai Platform but not Vertex AI models. Fortunately, there is also an option to pass a custom function for generating predictions through the set_custom_predict_fn() method where either the locally trained model or a function that returns predictions from a Vertex AI model can be passed.\n",
|
||||
"`WitConfigBuilder` provides the `set_ai_platform_model()` method to configure the What-If Tool with a model deployed as a version on Ai Platform models. This feature currently supports only Ai Platform but not Vertex AI models. Fortunately, there's also an option to pass a custom function for generating predictions through the `set_custom_predict_fn()` method where either the locally trained model or a function that returns predictions from a Vertex AI model can be passed.\n",
|
||||
"\n",
|
||||
"Learn more about [What-If Tool](https://pair-code.github.io/what-if-tool/get-started/).\n",
|
||||
"\n",
|
||||
@@ -1507,10 +1467,13 @@
|
||||
"# Delete the model\n",
|
||||
"model.delete()\n",
|
||||
"\n",
|
||||
"# Delete locally generated files\n",
|
||||
"! rm -rf model.pkl\n",
|
||||
"\n",
|
||||
"# Set this to true only if you'd like to delete your bucket\n",
|
||||
"delete_bucket = False\n",
|
||||
"\n",
|
||||
"if delete_bucket or os.getenv(\"IS_TESTING\"):\n",
|
||||
"if delete_bucket:\n",
|
||||
" ! gsutil -m rm -r $BUCKET_URI"
|
||||
]
|
||||
}
|
||||
|
||||
+111
-120
@@ -32,21 +32,24 @@
|
||||
"# Predictive maintenance using Vertex AI\n",
|
||||
"\n",
|
||||
"<table align=\"left\">\n",
|
||||
" <td>\n",
|
||||
" <a href=\"https://github.com/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/official/workbench/predictive_maintainance/predictive_maintenance_usecase.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",
|
||||
" <td style=\"text-align: center\">\n",
|
||||
" <a href=\"https://colab.research.google.com/github/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/official/workbench/predictive_maintainance/predictive_maintenance_usecase.ipynb\">\n",
|
||||
" <img src=\"https://cloud.google.com/ml-engine/images/colab-logo-32px.png\\\" alt=\"Colab logo\"> Run in Colab\n",
|
||||
" <img src=\"https://cloud.google.com/ml-engine/images/colab-logo-32px.png\" alt=\"Google Colaboratory logo\"><br> Open in Colab\n",
|
||||
" </a>\n",
|
||||
" </td>\n",
|
||||
" <td>\n",
|
||||
" <td style=\"text-align: center\">\n",
|
||||
" <a href=\"https://console.cloud.google.com/vertex-ai/colab/import/https:%2F%2Fraw.githubusercontent.com%2FGoogleCloudPlatform%2Fvertex-ai-samples%2Fmain%2Fnotebooks%2Fofficial%2Fworkbench%2Fpredictive_maintainance%2Fpredictive_maintenance_usecase.ipynb\">\n",
|
||||
" <img width=\"32px\" src=\"https://cloud.google.com/ml-engine/images/colab-enterprise-logo-32px.png\" alt=\"Google Cloud Colab Enterprise logo\"><br> Open in Colab Enterprise\n",
|
||||
" </a>\n",
|
||||
" </td>\n",
|
||||
" <td style=\"text-align: center\">\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/workbench/predictive_maintainance/predictive_maintenance_usecase.ipynb\" target='_blank'>\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",
|
||||
" <img src=\"https://lh3.googleusercontent.com/UiNooY4LUgW_oTvpsNhPpQzsstV5W8F7rYgxgGBD85cWJoLmrOzhVs_ksK_vgx40SHs7jCqkTkCk=e14-rj-sc0xffffff-h130-w32\" alt=\"Vertex AI logo\"><br> Open in Workbench\n",
|
||||
" </a>\n",
|
||||
" </td>\n",
|
||||
" <td style=\"text-align: center\">\n",
|
||||
" <a href=\"https://github.com/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/official/workbench/predictive_maintainance/predictive_maintenance_usecase.ipynb\">\n",
|
||||
" <img src=\"https://cloud.google.com/ml-engine/images/github-logo-32px.png\" alt=\"GitHub logo\"><br> View on GitHub\n",
|
||||
" </a>\n",
|
||||
" </td>\n",
|
||||
"</table>\n",
|
||||
@@ -83,7 +86,7 @@
|
||||
"\n",
|
||||
"*Note: This notebook file is developed to run in a [Vertex AI Workbench managed notebooks](https://console.cloud.google.com/vertex-ai/workbench/list/managed) instance using the XGBoost (Local) kernel. Some components of this notebook may not work in other notebook environments.*\n",
|
||||
"\n",
|
||||
"Learn more about [Vertex AI Workbench](https://cloud.google.com/vertex-ai/docs/workbench/introduction) and [Vertex AI Training](https://cloud.google.com/vertex-ai/docs/training/custom-training)."
|
||||
"Learn more about [Vertex AI Workbench](https://cloud.google.com/vertex-ai/docs/workbench/introduction) and [Vertex AI training](https://cloud.google.com/vertex-ai/docs/training/custom-training)."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -99,8 +102,8 @@
|
||||
"\n",
|
||||
"This tutorial uses the following Google Cloud ML services:\n",
|
||||
"\n",
|
||||
"- `Vertex AI Training`\n",
|
||||
"- `Vertex AI Model Evaluation`\n",
|
||||
"- Vertex AI training\n",
|
||||
"- Vertex AI model evaluation\n",
|
||||
"\n",
|
||||
"The steps performed are:\n",
|
||||
"\n",
|
||||
@@ -169,12 +172,19 @@
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "16bee0754628"
|
||||
"id": "d1ea81ac77f0"
|
||||
},
|
||||
"source": [
|
||||
"## Installation\n",
|
||||
"\n",
|
||||
"Install the following packages to run this notebook outside Vertex AI Workbench's managed instances."
|
||||
"## Get started"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "e5d353aa47ac"
|
||||
},
|
||||
"source": [
|
||||
"### Install Vertex AI SDK for Python and other required packages\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -187,7 +197,7 @@
|
||||
"source": [
|
||||
"! pip3 install --quiet --upgrade google-cloud-aiplatform \\\n",
|
||||
" google-cloud-storage \\\n",
|
||||
" xgboost \\\n",
|
||||
" xgboost==1.7.1 \\\n",
|
||||
" seaborn \\\n",
|
||||
" scikit-learn \\\n",
|
||||
" fsspec \\\n",
|
||||
@@ -198,41 +208,80 @@
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "e9255e3b156f"
|
||||
"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": "0c0b2427998a"
|
||||
"id": "157953ab28f0"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Automatically restart kernel after installs so that your environment can access the new packages\n",
|
||||
"# 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": "435b8e413535"
|
||||
"id": "c87a2a5d7e35"
|
||||
},
|
||||
"source": [
|
||||
"### Before you begin\n",
|
||||
"<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": "5dccb1c8feb6"
|
||||
},
|
||||
"source": [
|
||||
"### Authenticate your notebook environment (Colab only)\n",
|
||||
"\n",
|
||||
"#### Set your project ID\n",
|
||||
"Authenticate your environment on Google Colab.\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "cc7251520a07"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import sys\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)"
|
||||
"if \"google.colab\" in sys.modules:\n",
|
||||
"\n",
|
||||
" from google.colab import auth\n",
|
||||
"\n",
|
||||
" auth.authenticate_user()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "c2fc3d7b6bfa"
|
||||
},
|
||||
"source": [
|
||||
"### Set Google Cloud project information and initialize Vertex AI SDK for Python\n",
|
||||
"\n",
|
||||
"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)."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -246,88 +295,9 @@
|
||||
"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": "2e6b8b324ce1"
|
||||
},
|
||||
"source": [
|
||||
"#### Region\n",
|
||||
"! gcloud config set project $PROJECT_ID\n",
|
||||
"\n",
|
||||
"You can also change the `REGION` variable used by Vertex AI. \n",
|
||||
"Learn more about [Vertex AI regions](https://cloud.google.com/vertex-ai/docs/general/locations)."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "ae43d96c4b1b"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"REGION = \"[your-region]\" # @param {type: \"string\"}"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "6c43a8673066"
|
||||
},
|
||||
"source": [
|
||||
"### Authenticate your Google Cloud account\n",
|
||||
"\n",
|
||||
"Depending on your Jupyter environment, you may have to manually authenticate. Follow the relevant instructions below.\n",
|
||||
"\n",
|
||||
"**1. Vertex AI Workbench** \n",
|
||||
"- Do nothing as you are already authenticated.\n",
|
||||
"\n",
|
||||
"**2. Local JupyterLab Instance,** uncomment and run."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "fbc9cd30cc4b"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# ! gcloud auth login"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "cd0da2c26879"
|
||||
},
|
||||
"source": [
|
||||
"**3. Colab,** uncomment and run:"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "a336a05c6149"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# from google.colab import auth\n",
|
||||
"# auth.authenticate_user()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "0461097edfa5"
|
||||
},
|
||||
"source": [
|
||||
"**4. Service Account or other**\n",
|
||||
"- See all the authentication options here: [Google Cloud Platform Jupyter Notebook Authentication Guide](https://github.com/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/notebook_authentication_guide.ipynb)"
|
||||
"LOCATION = \"us-central1\" # @param {type: \"string\"}"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -369,7 +339,7 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"! gsutil mb -l {REGION} {BUCKET_URI}"
|
||||
"! gsutil mb -l {LOCATION} {BUCKET_URI}"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -380,7 +350,7 @@
|
||||
"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."
|
||||
"To avoid name collisions between users on created resources, create a UUID for each session instance. Append these UUIDs to the respective names of the resources created in this tutorial."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -434,6 +404,26 @@
|
||||
"from sklearn.model_selection import train_test_split"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "4df7ab06a373"
|
||||
},
|
||||
"source": [
|
||||
"## Initialize Vertex AI SDK for Python\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "32414d85ae7c"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"aiplatform.init(project=PROJECT_ID, location=LOCATION)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
@@ -1061,12 +1051,11 @@
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Create a Vertex AI model resource\n",
|
||||
"aiplatform.init(project=PROJECT_ID, location=REGION)\n",
|
||||
"\n",
|
||||
"model = aiplatform.Model.upload(\n",
|
||||
" display_name=MODEL_DISPLAY_NAME,\n",
|
||||
" artifact_uri=ARTIFACT_GCS_PATH,\n",
|
||||
" serving_container_image_uri=\"us-docker.pkg.dev/vertex-ai/prediction/xgboost-cpu.1-1:latest\",\n",
|
||||
" serving_container_image_uri=\"us-docker.pkg.dev/vertex-ai/prediction/xgboost-cpu.1-7:latest\",\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"model.wait()\n",
|
||||
@@ -1227,8 +1216,10 @@
|
||||
"\n",
|
||||
"# Delete the Cloud Storage bucket\n",
|
||||
"delete_bucket = False\n",
|
||||
"if delete_bucket or os.getenv(\"IS_TESTING\"):\n",
|
||||
" ! gsutil -m rm -r $BUCKET_URI"
|
||||
"if delete_bucket:\n",
|
||||
" ! gsutil -m rm -r $BUCKET_URI\n",
|
||||
"\n",
|
||||
"!rm -rf model.bst"
|
||||
]
|
||||
}
|
||||
],
|
||||
|
||||
Reference in New Issue
Block a user