No public description

PiperOrigin-RevId: 845941350
This commit is contained in:
Matej Aleksandrov
2025-12-17 15:25:49 -08:00
committed by Copybara-Service
parent 87f540ac53
commit 8471b5cb6f
3 changed files with 123 additions and 123 deletions
@@ -32,18 +32,18 @@
"source": [
"# Vertex AI Model Garden - Finetuning Tutorial\n",
"\n",
"\u003ctable\u003e\u003ctbody\u003e\u003ctr\u003e\n",
" \u003ctd style=\"text-align: center\"\u003e\n",
" \u003ca 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_finetuning_tutorial.ipynb\"\u003e\n",
" \u003cimg alt=\"Google Cloud Colab Enterprise logo\" src=\"https://lh3.googleusercontent.com/JmcxdQi-qOpctIvWKgPtrzZdJJK-J3sWE1RsfjZNwshCFgE_9fULcNpuXYTilIR2hjwN\" width=\"32px\"\u003e\u003cbr\u003e Run in Colab Enterprise\n",
" \u003c/a\u003e\n",
" \u003c/td\u003e\n",
" \u003ctd style=\"text-align: center\"\u003e\n",
" \u003ca href=\"https://github.com/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/community/model_garden/model_garden_finetuning_tutorial.ipynb\"\u003e\n",
" \u003cimg alt=\"GitHub logo\" src=\"https://github.githubassets.com/assets/GitHub-Mark-ea2971cee799.png\" width=\"32px\"\u003e\u003cbr\u003e View on GitHub\n",
" \u003c/a\u003e\n",
" \u003c/td\u003e\n",
"\u003c/tr\u003e\u003c/tbody\u003e\u003c/table\u003e"
"<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_finetuning_tutorial.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_finetuning_tutorial.ipynb\">\n",
" <img alt=\"GitHub logo\" src=\"https://github.githubassets.com/assets/GitHub-Mark-ea2971cee799.png\" width=\"32px\"><br> View on GitHub\n",
" </a>\n",
" </td>\n",
"</tr></tbody></table>"
]
},
{
@@ -168,11 +168,11 @@
"\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. For finetuning, **[click here](https://console.cloud.google.com/iam-admin/quotas?location=us-central1\u0026metric=aiplatform.googleapis.com%2Frestricted_image_training_nvidia_a100_80gb_gpus)** to check if your project already has the required 8 Nvidia A100 80 GB GPUs in the us-central1 region. If yes, then run this notebook in the us-central1 region. If you do not have 8 Nvidia A100 80 GPUs or have more GPU requirements than this, then schedule your job with Nvidia H100 GPUs via Dynamic Workload Scheduler using [these instructions](https://cloud.google.com/vertex-ai/docs/training/schedule-jobs-dws). For Dynamic Workload Scheduler, check the [us-central1](https://console.cloud.google.com/iam-admin/quotas?location=us-central1\u0026metric=aiplatform.googleapis.com%2Fcustom_model_training_preemptible_nvidia_h100_gpus) or [europe-west4](https://console.cloud.google.com/iam-admin/quotas?location=europe-west4\u0026metric=aiplatform.googleapis.com%2Fcustom_model_training_preemptible_nvidia_h100_gpus) quota for Nvidia H100 GPUs. If you do not have enough GPUs, then you can follow [these instructions](https://cloud.google.com/docs/quotas/view-manage#viewing_your_quota_console) to request quota.\n",
"# @markdown 2. For finetuning, **[click here](https://console.cloud.google.com/iam-admin/quotas?location=us-central1&metric=aiplatform.googleapis.com%2Frestricted_image_training_nvidia_a100_80gb_gpus)** to check if your project already has the required 8 Nvidia A100 80 GB GPUs in the us-central1 region. If yes, then run this notebook in the us-central1 region. If you do not have 8 Nvidia A100 80 GPUs or have more GPU requirements than this, then schedule your job with Nvidia H100 GPUs via Dynamic Workload Scheduler using [these instructions](https://cloud.google.com/vertex-ai/docs/training/schedule-jobs-dws). For Dynamic Workload Scheduler, check the [us-central1](https://console.cloud.google.com/iam-admin/quotas?location=us-central1&metric=aiplatform.googleapis.com%2Fcustom_model_training_preemptible_nvidia_h100_gpus) or [europe-west4](https://console.cloud.google.com/iam-admin/quotas?location=europe-west4&metric=aiplatform.googleapis.com%2Fcustom_model_training_preemptible_nvidia_h100_gpus) quota for Nvidia H100 GPUs. If you do not have enough GPUs, then you can follow [these instructions](https://cloud.google.com/docs/quotas/view-manage#viewing_your_quota_console) to request quota.\n",
"\n",
"# @markdown 3. For evaluation and deployment, **[click here](https://console.cloud.google.com/iam-admin/quotas?location=us-central1\u0026metric=aiplatform.googleapis.com%2Fcustom_model_serving_nvidia_l4_gpus)** to check if your project already has the required 1 L4 GPU in the us-central1 region. If yes, then run this notebook in the us-central1 region. If you need more L4 GPUs for your project, then you can follow [these instructions](https://cloud.google.com/docs/quotas/view-manage#viewing_your_quota_console) to request more. Alternatively, if you want to run predictions with A100 80GB or H100 GPUs, we recommend using the regions listed below. **NOTE:** Make sure you have associated quota in selected regions. Click the links to see your current quota for each GPU type: [Nvidia A100 80GB](https://console.cloud.google.com/iam-admin/quotas?metric=aiplatform.googleapis.com%2Fcustom_model_serving_nvidia_a100_80gb_gpus), [Nvidia H100 80GB](https://console.cloud.google.com/iam-admin/quotas?metric=aiplatform.googleapis.com%2Fcustom_model_serving_nvidia_h100_gpus).\n",
"# @markdown 3. For evaluation and deployment, **[click here](https://console.cloud.google.com/iam-admin/quotas?location=us-central1&metric=aiplatform.googleapis.com%2Fcustom_model_serving_nvidia_l4_gpus)** to check if your project already has the required 1 L4 GPU in the us-central1 region. If yes, then run this notebook in the us-central1 region. If you need more L4 GPUs for your project, then you can follow [these instructions](https://cloud.google.com/docs/quotas/view-manage#viewing_your_quota_console) to request more. Alternatively, if you want to run predictions with A100 80GB or H100 GPUs, we recommend using the regions listed below. **NOTE:** Make sure you have associated quota in selected regions. Click the links to see your current quota for each GPU type: [Nvidia A100 80GB](https://console.cloud.google.com/iam-admin/quotas?metric=aiplatform.googleapis.com%2Fcustom_model_serving_nvidia_a100_80gb_gpus), [Nvidia H100 80GB](https://console.cloud.google.com/iam-admin/quotas?metric=aiplatform.googleapis.com%2Fcustom_model_serving_nvidia_h100_gpus).\n",
"\n",
"# @markdown \u003e | Machine Type | Accelerator Type | Recommended Regions |\n",
"# @markdown > | Machine Type | Accelerator Type | Recommended Regions |\n",
"# @markdown | ----------- | ----------- | ----------- |\n",
"# @markdown | a2-ultragpu-1g | 1 NVIDIA_A100_80GB | us-central1, us-east4, europe-west4, asia-southeast1, us-east4 |\n",
"# @markdown | a3-highgpu-2g | 2 NVIDIA_H100_80GB | us-west1, asia-southeast1, europe-west4 |\n",
@@ -188,8 +188,8 @@
"REGION = \"\" # @param {type:\"string\"}\n",
"\n",
"# Import the necessary packages.\n",
"! rm -rf vertex-ai-samples \u0026\u0026 git clone https://github.com/GoogleCloudPlatform/vertex-ai-samples.git\n",
"! cd vertex-ai-samples \u0026\u0026 git reset --hard 0727e19520cf7957bceb701c248221bd3dbe4f1f\n",
"! rm -rf vertex-ai-samples && git clone https://github.com/GoogleCloudPlatform/vertex-ai-samples.git\n",
"! cd vertex-ai-samples && git reset --hard 0727e19520cf7957bceb701c248221bd3dbe4f1f\n",
"\n",
"import datetime\n",
"import importlib\n",
@@ -322,7 +322,7 @@
"| coqa | 0.1158 | 0.0137 | 0.1872 | 0.0150 |\n"
],
"text/plain": [
"\u003cIPython.core.display.Markdown object\u003e"
"<IPython.core.display.Markdown object>"
]
},
"metadata": {},
@@ -358,7 +358,7 @@
" lora_path: str = None,\n",
" max_num_seqs: int = 64,\n",
" eval_task: str = \"coqa\",\n",
") -\u003e str:\n",
") -> str:\n",
" \"\"\"Run lm-evaluation-harness to evaluate the model, and returns .\"\"\"\n",
"\n",
" if accelerator_type == \"NVIDIA_L4\":\n",
@@ -606,7 +606,7 @@
"version_minor": 0
},
"text/plain": [
"Downloading readme: 0%| | 0.00/8.20k [00:00\u003c?, ?B/s]"
"Downloading readme: 0%| | 0.00/8.20k [00:00<?, ?B/s]"
]
},
"metadata": {},
@@ -620,7 +620,7 @@
"version_minor": 0
},
"text/plain": [
"Downloading data: 0%| | 0.00/13.1M [00:00\u003c?, ?B/s]"
"Downloading data: 0%| | 0.00/13.1M [00:00<?, ?B/s]"
]
},
"metadata": {},
@@ -634,7 +634,7 @@
"version_minor": 0
},
"text/plain": [
"Generating train split: 0%| | 0/15011 [00:00\u003c?, ? examples/s]"
"Generating train split: 0%| | 0/15011 [00:00<?, ? examples/s]"
]
},
"metadata": {},
@@ -652,7 +652,7 @@
"| When was Tomoaki Komorida born? | Komorida was born in Kumamoto Prefecture on July 10, 1981. After graduating from high school, he joined the J1 League club Avispa Fukuoka in 2000. Although he debuted as a midfielder in 2001, he did not play much and the club was relegated to the J2 League at the end of the 2001 season. In 2002, he moved to the J2 club Oita Trinita. He became a regular player as a defensive midfielder and the club won the championship in 2002 and was promoted in 2003. He played many matches until 2005. In September 2005, he moved to the J2 club Montedio Yamagata. In 2006, he moved to the J2 club Vissel Kobe. Although he became a regular player as a defensive midfielder, his gradually was played less during the summer. In 2007, he moved to the Japan Football League club Rosso Kumamoto (later Roasso Kumamoto) based in his local region. He played as a regular player and the club was promoted to J2 in 2008. Although he did not play as much, he still played in many matches. In 2010, he moved to Indonesia and joined Persela Lamongan. In July 2010, he returned to Japan and joined the J2 club Giravanz Kitakyushu. He played often as a defensive midfielder and center back until 2012 when he retired. | Tomoaki Komorida was born on July 10,1981. | closed_qa |"
],
"text/plain": [
"\u003cIPython.core.display.Markdown object\u003e"
"<IPython.core.display.Markdown object>"
]
},
"metadata": {},
@@ -767,14 +767,14 @@
"name": "stdout",
"output_type": "stream",
"text": [
"('\u003c|begin_of_text|\u003e\u003c|start_header_id|\u003esystem\u003c|end_header_id|\u003e\\n'\n",
"('<|begin_of_text|><|start_header_id|>system<|end_header_id|>\\n'\n",
" '\\n'\n",
" 'You are a helpful '\n",
" 'assistant.\u003c|eot_id|\u003e\u003c|start_header_id|\u003euser\u003c|end_header_id|\u003e\\n'\n",
" 'assistant.<|eot_id|><|start_header_id|>user<|end_header_id|>\\n'\n",
" '\\n'\n",
" 'Hello, how are you?\u003c|eot_id|\u003e\u003c|start_header_id|\u003eassistant\u003c|end_header_id|\u003e\\n'\n",
" 'Hello, how are you?<|eot_id|><|start_header_id|>assistant<|end_header_id|>\\n'\n",
" '\\n'\n",
" 'I am doing well, thank you.\u003c|eot_id|\u003e')\n"
" 'I am doing well, thank you.<|eot_id|>')\n"
]
}
],
@@ -793,7 +793,7 @@
"# @markdown to translate the Jinja template for you. For example, you can ask:\n",
"\n",
"# @markdown ````\n",
"# @markdown Translate the following Jinja template to Python, where bos_token is \"\u003c|begin_of_text|\u003e\":\n",
"# @markdown Translate the following Jinja template to Python, where bos_token is \"<|begin_of_text|>\":\n",
"# @markdown ```\n",
"# @markdown {{- bos_token }}\n",
"# @markdown {#- This block extracts the system message, so we can slot it into the right place. #}\n",
@@ -804,14 +804,14 @@
"# @markdown {%- set system_message = \"\" %}\n",
"# @markdown {%- endif %}\n",
"# @markdown {#- System message #}\n",
"# @markdown {{- \"\u003c|start_header_id|\u003esystem\u003c|end_header_id|\u003e\\n\\n\" }}\n",
"# @markdown {{- \"<|start_header_id|>system<|end_header_id|>\\n\\n\" }}\n",
"# @markdown {{- system_message }}\n",
"# @markdown {{- \"\u003c|eot_id|\u003e\" }}\n",
"# @markdown {{- \"<|eot_id|>\" }}\n",
"# @markdown {%- for message in messages %}\n",
"# @markdown {{- '\u003c|start_header_id|\u003e' + message['role'] + '\u003c|end_header_id|\u003e\\n\\n'+ message['content'] | trim + '\u003c|eot_id|\u003e' }}\n",
"# @markdown {{- '<|start_header_id|>' + message['role'] + '<|end_header_id|>\\n\\n'+ message['content'] | trim + '<|eot_id|>' }}\n",
"# @markdown {%- endfor %}\n",
"# @markdown {%- if add_generation_prompt %}\n",
"# @markdown {{- '\u003c|start_header_id|\u003eassistant\u003c|end_header_id|\u003e\\n\\n' }}\n",
"# @markdown {{- '<|start_header_id|>assistant<|end_header_id|>\\n\\n' }}\n",
"# @markdown {%- endif %}\n",
"# @markdown ```\n",
"# @markdown ````\n",
@@ -820,7 +820,7 @@
"# @markdown `bos_token` is specified in the [tokenizer_config.json](https://huggingface.co/meta-llama/Llama-3.1-8B-Instruct/blob/main/tokenizer_config.json#L2052).\n",
"# @markdown ```\n",
"# @markdown def render_template(messages, add_generation_prompt=False):\n",
"# @markdown bos_token = \"\u003c|begin_of_text|\u003e\"\n",
"# @markdown bos_token = \"<|begin_of_text|>\"\n",
"# @markdown output = bos_token\n",
"# @markdown\n",
"# @markdown system_message = \"\"\n",
@@ -828,22 +828,22 @@
"# @markdown system_message = messages[0]['content'].strip()\n",
"# @markdown messages = messages[1:]\n",
"# @markdown\n",
"# @markdown output += \"\u003c|start_header_id|\u003esystem\u003c|end_header_id|\u003e\\n\\n\"\n",
"# @markdown output += \"<|start_header_id|>system<|end_header_id|>\\n\\n\"\n",
"# @markdown output += system_message\n",
"# @markdown output += \"\u003c|eot_id|\u003e\"\n",
"# @markdown output += \"<|eot_id|>\"\n",
"# @markdown\n",
"# @markdown for message in messages:\n",
"# @markdown output += f\"\u003c|start_header_id|\u003e{message['role']}\u003c|end_header_id|\u003e\\n\\n{message['content'].strip()}\u003c|eot_id|\u003e\"\n",
"# @markdown output += f\"<|start_header_id|>{message['role']}<|end_header_id|>\\n\\n{message['content'].strip()}<|eot_id|>\"\n",
"# @markdown\n",
"# @markdown if add_generation_prompt:\n",
"# @markdown output += \"\u003c|start_header_id|\u003eassistant\u003c|end_header_id|\u003e\\n\\n\"\n",
"# @markdown output += \"<|start_header_id|>assistant<|end_header_id|>\\n\\n\"\n",
"# @markdown\n",
"# @markdown return output\n",
"# @markdown ```\n",
"\n",
"\n",
"def render_template(messages, add_generation_prompt=False):\n",
" bos_token = \"\u003c|begin_of_text|\u003e\"\n",
" bos_token = \"<|begin_of_text|>\"\n",
" output = bos_token\n",
"\n",
" system_message = \"\"\n",
@@ -851,26 +851,26 @@
" system_message = messages[0][\"content\"].strip()\n",
" messages = messages[1:]\n",
"\n",
" output += \"\u003c|start_header_id|\u003esystem\u003c|end_header_id|\u003e\\n\\n\"\n",
" output += \"<|start_header_id|>system<|end_header_id|>\\n\\n\"\n",
" output += system_message\n",
" output += \"\u003c|eot_id|\u003e\"\n",
" output += \"<|eot_id|>\"\n",
"\n",
" for message in messages:\n",
" output += (\n",
" \"\u003c|start_header_id|\u003e\"\n",
" \"<|start_header_id|>\"\n",
" + message[\"role\"]\n",
" + \"\u003c|end_header_id|\u003e\\n\\n\"\n",
" + \"<|end_header_id|>\\n\\n\"\n",
" + message[\"content\"].strip()\n",
" + \"\u003c|eot_id|\u003e\"\n",
" + \"<|eot_id|>\"\n",
" )\n",
"\n",
" if add_generation_prompt:\n",
" output += \"\u003c|start_header_id|\u003eassistant\u003c|end_header_id|\u003e\\n\\n\"\n",
" output += \"<|start_header_id|>assistant<|end_header_id|>\\n\\n\"\n",
"\n",
" return output\n",
"\n",
"\n",
"# @markdown The `\u003c|begin_of_text|\u003e`, `\u003c|start_header_id|\u003e`, `\u003c|end_header_id|\u003e`, and `\u003c|eot_id|\u003e` tokens serve specific purposes in the context of text generation models.\n",
"# @markdown The `<|begin_of_text|>`, `<|start_header_id|>`, `<|end_header_id|>`, and `<|eot_id|>` tokens serve specific purposes in the context of text generation models.\n",
"# @markdown These tokens help the model delineate the boundaries of a text generation task. They provide clear markers for the start and end points, enabling the model to function effectively and produce coherent text.\n",
"\n",
"# @markdown Run this cell to show an example output of the template given the\n",
@@ -1029,15 +1029,15 @@
"name": "stdout",
"output_type": "stream",
"text": [
"('\u003c|begin_of_text|\u003e\u003c|start_header_id|\u003euser\u003c|end_header_id|\u003e\\n'\n",
"('<|begin_of_text|><|start_header_id|>user<|end_header_id|>\\n'\n",
" '\\n'\n",
" 'Hello, how are you? Context: This is a test '\n",
" 'context.\u003c|eot_id|\u003e\u003c|start_header_id|\u003eassistant\u003c|end_header_id|\u003e\\n'\n",
" 'context.<|eot_id|><|start_header_id|>assistant<|end_header_id|>\\n'\n",
" '\\n'\n",
" \"I'm doing well, thank \"\n",
" 'you!\u003c|eot_id|\u003e\u003c|start_header_id|\u003euser\u003c|end_header_id|\u003e\\n'\n",
" 'you!<|eot_id|><|start_header_id|>user<|end_header_id|>\\n'\n",
" '\\n'\n",
" 'Another question without context.\u003c|eot_id|\u003e')\n"
" 'Another question without context.<|eot_id|>')\n"
]
}
],
@@ -1051,15 +1051,15 @@
"# @markdown chat_template_string = r\"\"\"{{- bos_token }}\n",
"# @markdown\n",
"# @markdown {% for message in messages %}\n",
"# @markdown {{- '\u003c|start_header_id|\u003e' + message.role + '\u003c|end_header_id|\u003e\\n\\n' + message.content | trim }}\n",
"# @markdown {% if message.context and message.context | length \u003e 0 %}\n",
"# @markdown {{- '<|start_header_id|>' + message.role + '<|end_header_id|>\\n\\n' + message.content | trim }}\n",
"# @markdown {% if message.context and message.context | length > 0 %}\n",
"# @markdown {{- ' Context: ' + message.context }}\n",
"# @markdown {% endif %}\n",
"# @markdown {{- '\u003c|eot_id|\u003e' }}\n",
"# @markdown {{- '<|eot_id|>' }}\n",
"# @markdown {% endfor %}\n",
"# @markdown\n",
"# @markdown {% if add_generation_prompt %}\n",
"# @markdown {{- '\u003c|start_header_id|\u003eassistant\u003c|end_header_id|\u003e\\n\\n' }}\n",
"# @markdown {{- '<|start_header_id|>assistant<|end_header_id|>\\n\\n' }}\n",
"# @markdown {% endif %}\n",
"# @markdown \"\"\"\n",
"# @markdown ```\n",
@@ -1070,15 +1070,15 @@
"chat_template_string = r\"\"\"{{- bos_token }}\n",
"\n",
"{% for message in messages %}\n",
" {{- '\u003c|start_header_id|\u003e' + message.role + '\u003c|end_header_id|\u003e\\n\\n' + message.content | trim }}\n",
" {% if message.context and message.context | length \u003e 0 %}\n",
" {{- '<|start_header_id|>' + message.role + '<|end_header_id|>\\n\\n' + message.content | trim }}\n",
" {% if message.context and message.context | length > 0 %}\n",
" {{- ' Context: ' + message.context }}\n",
" {% endif %}\n",
" {{- '\u003c|eot_id|\u003e' }}\n",
" {{- '<|eot_id|>' }}\n",
"{% endfor %}\n",
"\n",
"{% if add_generation_prompt %}\n",
" {{- '\u003c|start_header_id|\u003eassistant\u003c|end_header_id|\u003e\\n\\n' }}\n",
" {{- '<|start_header_id|>assistant<|end_header_id|>\\n\\n' }}\n",
"{% endif %}\n",
"\"\"\"\n",
"\n",
@@ -1088,19 +1088,19 @@
"# @markdown output = bos_token\n",
"# @markdown\n",
"# @markdown for message in messages:\n",
"# @markdown output += f\"\u003c|start_header_id|\u003e{message['role']}\u003c|end_header_id|\u003e\\n\\n{message['content'].strip()}\"\n",
"# @markdown output += f\"<|start_header_id|>{message['role']}<|end_header_id|>\\n\\n{message['content'].strip()}\"\n",
"# @markdown if 'context' in message and message['context']:\n",
"# @markdown output += f\" Context: {message['context']}\"\n",
"# @markdown output += \"\u003c|eot_id|\u003e\"\n",
"# @markdown output += \"<|eot_id|>\"\n",
"# @markdown\n",
"# @markdown if add_generation_prompt:\n",
"# @markdown output += \"\u003c|start_header_id|\u003eassistant\u003c|end_header_id|\u003e\\n\\n\"\n",
"# @markdown output += \"<|start_header_id|>assistant<|end_header_id|>\\n\\n\"\n",
"# @markdown\n",
"# @markdown return output\n",
"# @markdown ```\n",
"\n",
"# @markdown Run this cell to show an example output of the template given the\n",
"# @markdown below `messages` and `bos_token=\"\u003c|begin_of_text|\u003e\"`.\n",
"# @markdown below `messages` and `bos_token=\"<|begin_of_text|>\"`.\n",
"# @markdown ```\n",
"# @markdown messages = [\n",
"# @markdown {\"role\": \"user\", \"content\": \"Hello, how are you?\", \"context\": \"This is a test context.\"},\n",
@@ -1116,13 +1116,13 @@
" output = bos_token\n",
"\n",
" for message in messages:\n",
" output += f\"\u003c|start_header_id|\u003e{message['role']}\u003c|end_header_id|\u003e\\n\\n{message['content'].strip()}\"\n",
" output += f\"<|start_header_id|>{message['role']}<|end_header_id|>\\n\\n{message['content'].strip()}\"\n",
" if \"context\" in message and message[\"context\"]:\n",
" output += f\" Context: {message['context']}\"\n",
" output += \"\u003c|eot_id|\u003e\"\n",
" output += \"<|eot_id|>\"\n",
"\n",
" if add_generation_prompt:\n",
" output += \"\u003c|start_header_id|\u003eassistant\u003c|end_header_id|\u003e\\n\\n\"\n",
" output += \"<|start_header_id|>assistant<|end_header_id|>\\n\\n\"\n",
"\n",
" return output\n",
"\n",
@@ -1137,7 +1137,7 @@
" {\"role\": \"user\", \"content\": \"Another question without context.\", \"context\": \"\"},\n",
"]\n",
"\n",
"rendered_text = render_template(messages, bos_token=\"\u003c|begin_of_text|\u003e\")\n",
"rendered_text = render_template(messages, bos_token=\"<|begin_of_text|>\")\n",
"pprint.pprint(rendered_text, width=80)"
]
},
@@ -1172,16 +1172,16 @@
"# @markdown template = {\n",
"# @markdown \"description\": \"Template used by Llama 3.1, accepting databricks dolly dataset.\",\n",
"# @markdown \"chat_template\": chat_template_string,\n",
"# @markdown \"instruction_separator\": \"\u003c|start_header_id|\u003euser\u003c|end_header_id|\u003e\\n\\n\",\n",
"# @markdown \"response_separator\": \"\u003c|start_header_id|\u003eassistant\u003c|end_header_id|\u003e\\n\\n\"\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",
"template_data = {\n",
" \"description\": \"Template used by Llama 3.1, accepting databricks dolly dataset.\",\n",
" \"chat_template\": chat_template_string,\n",
" \"instruction_separator\": \"\u003c|start_header_id|\u003euser\u003c|end_header_id|\u003e\\n\\n\",\n",
" \"response_separator\": \"\u003c|start_header_id|\u003eassistant\u003c|end_header_id|\u003e\\n\\n\",\n",
" \"instruction_separator\": \"<|start_header_id|>user<|end_header_id|>\\n\\n\",\n",
" \"response_separator\": \"<|start_header_id|>assistant<|end_header_id|>\\n\\n\",\n",
"}\n",
"\n",
"template_filename = \"template.json\"\n",
@@ -1345,7 +1345,7 @@
"\n",
"# @markdown **Note**:\n",
"# @markdown 1. We recommend setting `finetuning_precision_mode` to `float16`.\n",
"# @markdown 1. If `max_steps\u003e0`, it takes precedence over `epochs`. One can set a small `max_steps`\n",
"# @markdown 1. If `max_steps>0`, it takes precedence over `epochs`. One can set a small `max_steps`\n",
"# @markdown value to quickly check the pipeline.\n",
"\n",
"# @markdown Acceletor type to use for training.\n",
@@ -1392,7 +1392,7 @@
"# Set config file.\n",
"if replica_count == 1:\n",
" config_file = \"vertex_vision_model_garden_peft/llama_fsdp_8gpu.yaml\"\n",
"elif replica_count \u003c= 4:\n",
"elif replica_count <= 4:\n",
" config_file = (\n",
" \"vertex_vision_model_garden_peft/\"\n",
" f\"llama_hsdp_{replica_count * per_node_accelerator_count}gpu.yaml\"\n",
@@ -1653,7 +1653,7 @@
"\n",
"\n",
"# @markdown Expected evaluation results:\n",
"# @markdown \u003e | alias | exact_match | exact_match_stderr | f1 | f1_stderr |\n",
"# @markdown > | alias | exact_match | exact_match_stderr | f1 | f1_stderr |\n",
"# @markdown | --- | --- | --- | --- | --- |\n",
"# @markdown | coqa | 0.3213 | 0.0197 | 0.4660 | 0.0187 |\n",
"\n",
@@ -1716,7 +1716,7 @@
"max_model_len = 8192 # Maximum context length.\n",
"\n",
"# Ensure max_model_len does not exceed the limit.\n",
"if max_model_len \u003e 8192:\n",
"if max_model_len > 8192:\n",
" raise ValueError(\"max_model_len cannot exceed 8192\")\n",
"\n",
"\n",
@@ -1742,7 +1742,7 @@
" use_dedicated_endpoint: bool = False,\n",
" max_num_seqs: int = 256,\n",
" model_type: str = None,\n",
") -\u003e Tuple[aiplatform.Model, aiplatform.Endpoint]:\n",
") -> Tuple[aiplatform.Model, aiplatform.Endpoint]:\n",
" \"\"\"Deploys trained models with vLLM into Vertex AI.\"\"\"\n",
" endpoint = aiplatform.Endpoint.create(\n",
" display_name=f\"{model_name}-endpoint\",\n",
@@ -1786,7 +1786,7 @@
" if enable_prefix_cache:\n",
" vllm_args.append(\"--enable-prefix-caching\")\n",
"\n",
" if 0 \u003c host_prefix_kv_cache_utilization_target \u003c 1:\n",
" if 0 < host_prefix_kv_cache_utilization_target < 1:\n",
" vllm_args.append(\n",
" f\"--host-prefix-kv-cache-utilization-target={host_prefix_kv_cache_utilization_target}\"\n",
" )\n",
@@ -1901,7 +1901,7 @@
"output_type": "stream",
"text": [
"Prompt:\n",
"\u003c|start_header_id|\u003euser\u003c|end_header_id|\u003e\\n\\nWhat was Anya looking for? Context: Anya clutched the worn teddy bear, its button eye dangling precariously. She'd lost it in the park yesterday, and the thought of never seeing Mr. Snuggles again made her tummy ache. She retraced her steps, her eyes scanning the colorful playground equipment and the sprawling green lawn.\u003c|eot_id|\u003e\u003c|start_header_id|\u003eassistant\u003c|end_header_id|\u003e\n",
"<|start_header_id|>user<|end_header_id|>\\n\\nWhat was Anya looking for? Context: Anya clutched the worn teddy bear, its button eye dangling precariously. She'd lost it in the park yesterday, and the thought of never seeing Mr. Snuggles again made her tummy ache. She retraced her steps, her eyes scanning the colorful playground equipment and the sprawling green lawn.<|eot_id|><|start_header_id|>assistant<|end_header_id|>\n",
"Output:\n",
"Anya was looking for her teddy bear, Mr. Snuggles.\n"
]
@@ -1925,8 +1925,8 @@
"\n",
"prompt = \"What was Anya looking for? Context: Anya clutched the worn teddy bear, its button eye dangling precariously. She'd lost it in the park yesterday, and the thought of never seeing Mr. Snuggles again made her tummy ache. She retraced her steps, her eyes scanning the colorful playground equipment and the sprawling green lawn.\" # @param {type: \"string\"}\n",
"prompt_with_headers = (\n",
" f\"\u003c|start_header_id|\u003euser\u003c|end_header_id|\u003e\\\\n\\\\n{prompt}\u003c|eot_id|\u003e\"\n",
" \"\u003c|start_header_id|\u003eassistant\u003c|end_header_id|\u003e\\n\\n\"\n",
" f\"<|start_header_id|>user<|end_header_id|>\\\\n\\\\n{prompt}<|eot_id|>\"\n",
" \"<|start_header_id|>assistant<|end_header_id|>\\n\\n\"\n",
")\n",
"\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",
@@ -31,26 +31,26 @@
"source": [
"# Vertex AI Model Garden - Dolly V2 [Deprecated]\n",
"\n",
"\u003ctable align=\"left\"\u003e\n",
" \u003ctd\u003e\n",
" \u003ca href=\"https://colab.research.google.com/github/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/community/model_garden/model_garden_pytorch_dolly_v2.ipynb\"\u003e\n",
" \u003cimg src=\"https://cloud.google.com/ml-engine/images/colab-logo-32px.png\" alt=\"Colab logo\"\u003e Run in Colab\n",
" \u003c/a\u003e\n",
" \u003c/td\u003e\n",
" \u003ctd\u003e\n",
" \u003ca href=\"https://github.com/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/community/model_garden/model_garden_pytorch_dolly_v2.ipynb\"\u003e\n",
" \u003cimg src=\"https://github.githubassets.com/assets/GitHub-Mark-ea2971cee799.png\" alt=\"GitHub logo\"\u003e\n",
"<table align=\"left\">\n",
" <td>\n",
" <a href=\"https://colab.research.google.com/github/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/community/model_garden/model_garden_pytorch_dolly_v2.ipynb\">\n",
" <img src=\"https://cloud.google.com/ml-engine/images/colab-logo-32px.png\" alt=\"Colab logo\"> Run in Colab\n",
" </a>\n",
" </td>\n",
" <td>\n",
" <a href=\"https://github.com/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/community/model_garden/model_garden_pytorch_dolly_v2.ipynb\">\n",
" <img src=\"https://github.githubassets.com/assets/GitHub-Mark-ea2971cee799.png\" alt=\"GitHub logo\">\n",
" View on GitHub\n",
" \u003c/a\u003e\n",
" \u003c/td\u003e\n",
" \u003ctd\u003e\n",
" \u003ca href=\"https://console.cloud.google.com/vertex-ai/notebooks/deploy-notebook?download_url=https://raw.githubusercontent.com/GoogleCloudPlatform/vertex-ai-samples/main/notebooks/community/model_garden/model_garden_pytorch_dolly_v2.ipynb\"\u003e\n",
" \u003cimg src=\"https://lh3.googleusercontent.com/UiNooY4LUgW_oTvpsNhPpQzsstV5W8F7rYgxgGBD85cWJoLmrOzhVs_ksK_vgx40SHs7jCqkTkCk=e14-rj-sc0xffffff-h130-w32\" alt=\"Vertex AI logo\"\u003e\n",
" </a>\n",
" </td>\n",
" <td>\n",
" <a href=\"https://console.cloud.google.com/vertex-ai/notebooks/deploy-notebook?download_url=https://raw.githubusercontent.com/GoogleCloudPlatform/vertex-ai-samples/main/notebooks/community/model_garden/model_garden_pytorch_dolly_v2.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",
" \u003c/a\u003e\n",
" </a>\n",
" (a Python-3 CPU notebook is recommended)\n",
" \u003c/td\u003e\n",
"\u003c/table\u003e"
" </td>\n",
"</table>"
]
},
{
@@ -61,7 +61,7 @@
"source": [
"## Overview\n",
"\n",
"This notebook demonstrates downloading and deploying the pre-trained [Dolly-V2-3b](https://huggingface.co/databricks/dolly-v2-3b), [Dolly-V2-7b](https://huggingface.co/databricks/dolly-v2-7b) \u0026 [Dolly-V2-12b](https://huggingface.co/databricks/dolly-v2-12b) model on Vertex AI for online prediction. It also demonstrates the evaluation of popular benchmark datasets through Vertex CustomJobs using [EleutherAI’s evaluation harness](https://github.com/EleutherAI/lm-evaluation-harness).\n",
"This notebook demonstrates downloading and deploying the pre-trained [Dolly-V2-3b](https://huggingface.co/databricks/dolly-v2-3b), [Dolly-V2-7b](https://huggingface.co/databricks/dolly-v2-7b) & [Dolly-V2-12b](https://huggingface.co/databricks/dolly-v2-12b) model on Vertex AI for online prediction. It also demonstrates the evaluation of popular benchmark datasets through Vertex CustomJobs using [EleutherAI’s evaluation harness](https://github.com/EleutherAI/lm-evaluation-harness).\n",
"\n",
"\n",
"### Objective\n",
@@ -322,7 +322,7 @@
" return model, endpoint\n",
"\n",
"\n",
"def get_job_name_with_datetime(prefix: str) -\u003e str:\n",
"def get_job_name_with_datetime(prefix: str) -> str:\n",
" \"\"\"Gets the job name with date time when triggering training or deployment\n",
" jobs in Vertex AI.\n",
" \"\"\"\n",
@@ -463,7 +463,7 @@
"if \"12b\" in MODEL_ID:\n",
" accelerator_count = 2 # for dolly-v2-12b\n",
"else:\n",
" accelerator_count = 1 # for dolly-v2-3b \u0026 dolly-v2-7b\n",
" accelerator_count = 1 # for dolly-v2-3b & dolly-v2-7b\n",
"\n",
"replica_count = 1"
]
@@ -31,18 +31,18 @@
},
"source": [
"# Vertex AI Model Garden - WeatherNext Forecasting\n",
"\u003ctable\u003e\u003ctbody\u003e\u003ctr\u003e\n",
" \u003ctd style=\"text-align: center\"\u003e\n",
" \u003ca 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_weather_prediction_on_vertex.ipynb\"\u003e\n",
" \u003cimg alt=\"Google Cloud Colab Enterprise logo\" src=\"https://lh3.googleusercontent.com/JmcxdQi-qOpctIvWKgPtrzZdJJK-J3sWE1RsfjZNwshCFgE_9fULcNpuXYTilIR2hjwN\" width=\"32px\"\u003e\u003cbr\u003e Run in Colab Enterprise\n",
" \u003c/a\u003e\n",
" \u003c/td\u003e\n",
" \u003ctd style=\"text-align: center\"\u003e\n",
" \u003ca href=\"https://github.com/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/community/model_garden/model_garden_weather_prediction_on_vertex.ipynb\"\u003e\n",
" \u003cimg alt=\"GitHub logo\" src=\"https://github.githubassets.com/assets/GitHub-Mark-ea2971cee799.png\" width=\"32px\"\u003e\u003cbr\u003e View on GitHub\n",
" \u003c/a\u003e\n",
" \u003c/td\u003e\n",
"\u003c/tr\u003e\u003c/tbody\u003e\u003c/table\u003e"
"<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_weather_prediction_on_vertex.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_weather_prediction_on_vertex.ipynb\">\n",
" <img alt=\"GitHub logo\" src=\"https://github.githubassets.com/assets/GitHub-Mark-ea2971cee799.png\" width=\"32px\"><br> View on GitHub\n",
" </a>\n",
" </td>\n",
"</tr></tbody></table>"
]
},
{
@@ -81,7 +81,7 @@
"\n",
"### Request For TPU Quota\n",
"\n",
"By default, the quota for TPU training [Custom model training TPU v5e cores per region](https://console.cloud.google.com/iam-admin/quotas?location=us-central1\u0026metric=aiplatform.googleapis.com%2Fcustom_model_training_tpu_v5e) is 0. TPU quota is only available in `us-west1`, `us-west4`, `us-central1`. You can request for higher TPU quota following the instructions at [\"Request a higher quota\"](https://cloud.google.com/docs/quota/view-manage#requesting_higher_quota). It is suggested to request at least 4 v5e to run this notebook."
"By default, the quota for TPU training [Custom model training TPU v5e cores per region](https://console.cloud.google.com/iam-admin/quotas?location=us-central1&metric=aiplatform.googleapis.com%2Fcustom_model_training_tpu_v5e) is 0. TPU quota is only available in `us-west1`, `us-west4`, `us-central1`. You can request for higher TPU quota following the instructions at [\"Request a higher quota\"](https://cloud.google.com/docs/quota/view-manage#requesting_higher_quota). It is suggested to request at least 4 v5e to run this notebook."
]
},
{
@@ -117,7 +117,7 @@
"from google.cloud import aiplatform, storage\n",
"\n",
"# Upgrade Vertex AI SDK.\n",
"! pip3 install --upgrade --quiet 'google-cloud-aiplatform\u003e=1.64.0'\n",
"! pip3 install --upgrade --quiet 'google-cloud-aiplatform>=1.64.0'\n",
"if not os.path.exists(\"./vertex-ai-samples\"):\n",
" ! git clone https://github.com/GoogleCloudPlatform/vertex-ai-samples.git\n",
"! pip3 uninstall --quiet -y xarray\n",
@@ -181,7 +181,7 @@
"\n",
"\n",
"# Utility functions for vertex jobs.\n",
"def get_job_name_with_datetime(prefix: str) -\u003e str:\n",
"def get_job_name_with_datetime(prefix: str) -> str:\n",
" \"\"\"Gets the job name with date time when triggering training or deployment\n",
" jobs in Vertex AI.\n",
" \"\"\"\n",
@@ -232,7 +232,7 @@
"def get_existing_demo_step(\n",
" num_forecast_steps: int,\n",
" model_type: str = \"gen_small\",\n",
" ) -\u003e str:\n",
" ) -> str:\n",
" # The demo data only supports some steps, and we can only run the predictions\n",
" # if num_forecast_steps is smaller than or equal to the maximum supported setps.\n",
" if model_type == \"gen_small\":\n",
@@ -250,18 +250,18 @@
" # Find the proper demo data for forecasting.\n",
" found_supported_step = supported_demo_steps[-1]\n",
" for i, supported_step in enumerate(supported_demo_steps):\n",
" if num_forecast_steps \u003c= supported_step:\n",
" if num_forecast_steps <= supported_step:\n",
" found_supported_step = supported_step\n",
" break\n",
" if num_forecast_steps \u003e found_supported_step:\n",
" if num_forecast_steps > found_supported_step:\n",
" raise ValueError(f\"Supported demo steps for {model_type} in gs://dm_graphcast are {supported_demo_steps}. {num_forecast_steps} is too large, and could not find proper demo data.\")\n",
" return found_supported_step\n",
"\n",
"def get_suggested_machines(\n",
" num_forecast_steps: int,\n",
" model_type: str = \"gen_small\",) -\u003e Tuple[str, str, int]:\n",
" model_type: str = \"gen_small\",) -> Tuple[str, str, int]:\n",
" if model_type == \"gen_small\":\n",
" if num_forecast_steps \u003c= 16:\n",
" if num_forecast_steps <= 16:\n",
" machine_type = \"ct5lp-hightpu-4t\"\n",
" tpu_topology = \"2x2\"\n",
" accelerator_count = 4\n",
@@ -270,7 +270,7 @@
" tpu_topology = \"2x4\"\n",
" accelerator_count = 8\n",
" else:\n",
" if num_forecast_steps \u003c= 16:\n",
" if num_forecast_steps <= 16:\n",
" machine_type = \"ct5lp-hightpu-1t\"\n",
" tpu_topology = \"1x1\"\n",
" accelerator_count = 1\n",
@@ -285,11 +285,11 @@
" variable: str,\n",
" level: Optional[int] = None,\n",
" max_steps: Optional[int] = None\n",
" ) -\u003e xarray.Dataset:\n",
" ) -> xarray.Dataset:\n",
" data = data[variable]\n",
" if \"batch\" in data.dims:\n",
" data = data.isel(batch=0)\n",
" if max_steps is not None and \"time\" in data.sizes and max_steps \u003c data.sizes[\"time\"]:\n",
" if max_steps is not None and \"time\" in data.sizes and max_steps < data.sizes[\"time\"]:\n",
" data = data.isel(time=range(0, max_steps))\n",
" if level is not None and \"level\" in data.coords:\n",
" data = data.sel(level=level)\n",
@@ -299,7 +299,7 @@
" data: xarray.Dataset,\n",
" center: Optional[float] = None,\n",
" robust: bool = False,\n",
" ) -\u003e tuple[xarray.Dataset, matplotlib.colors.Normalize, str]:\n",
" ) -> tuple[xarray.Dataset, matplotlib.colors.Normalize, str]:\n",
" vmin = np.nanpercentile(data, (2 if robust else 0))\n",
" vmax = np.nanpercentile(data, (98 if robust else 100))\n",
" if center is not None:\n",
@@ -315,7 +315,7 @@
" plot_size: float = 5,\n",
" robust: bool = False,\n",
" cols: int = 4\n",
" ) -\u003e tuple[xarray.Dataset, matplotlib.colors.Normalize, str]:\n",
" ) -> tuple[xarray.Dataset, matplotlib.colors.Normalize, str]:\n",
"\n",
" first_data = next(iter(data.values()))[0]\n",
" max_steps = first_data.sizes.get(\"time\", 1)\n",