|
| 1 | +--- |
| 2 | +name: vertex-tuning |
| 3 | +description: > |
| 4 | + Vertex AI Model Tuning. Use when you need to fine-tune models |
| 5 | + using Vertex AI's infrastructure. |
| 6 | +--- |
| 7 | + |
| 8 | +# Vertex AI Model Tuning |
| 9 | + |
| 10 | +## Overview |
| 11 | + |
| 12 | +This skill provides procedural knowledge for fine-tuning Large Language Models |
| 13 | +(LLMs) using Vertex AI's tuning service. It covers the entire lifecycle from |
| 14 | +environment setup and data preparation to job configuration, monitoring, and |
| 15 | +deployment. |
| 16 | + |
| 17 | +## Workflow Decision Tree |
| 18 | + |
| 19 | +1. **Environment Check**: Has the environment (Auth, APIs, IAM, Venv) been |
| 20 | + initialized? |
| 21 | + |
| 22 | + - **No** → Go to [Phase 0: Environment & IAM Setup](#phase-0). |
| 23 | + - **Yes** → Proceed. |
| 24 | + |
| 25 | +2. **Dataset Status**: Is the dataset ready in JSONL format and uploaded to |
| 26 | + GCS? |
| 27 | + |
| 28 | + - **No** → Go to [Phase 1: Dataset Preparation & Upload](#phase-1). |
| 29 | + - **Yes** → Proceed. |
| 30 | + |
| 31 | +3. **Configuration**: Have the target model and hyperparameters been decided? |
| 32 | + |
| 33 | + - **No** → Go to [Phase 2: Recommendations & Configuration](#phase-2). |
| 34 | + - **Yes** → Proceed. |
| 35 | + |
| 36 | +4. **Job Status**: Has the tuning job been submitted? |
| 37 | + |
| 38 | + - **No** → Go to |
| 39 | + [Phase 3: Tuning Job Execution](#phase-3-tuning-job-execution). |
| 40 | + - **Yes** → Proceed. |
| 41 | + |
| 42 | +5. **Job Completion**: Is the tuning job complete? |
| 43 | + |
| 44 | + - **No** → Go to [Phase 4: Monitoring](#phase-4-monitoring). |
| 45 | + - **Yes** → Proceed. |
| 46 | + |
| 47 | +6. **Deployment**: Has the tuned model been deployed to an endpoint? |
| 48 | + |
| 49 | + - **No** → Go to [Phase 5: Model Deployment](#phase-5-model-deployment). |
| 50 | + - **Yes** → Task Complete. |
| 51 | + |
| 52 | +-------------------------------------------------------------------------------- |
| 53 | + |
| 54 | +## Phase 0: Environment & IAM Setup {#phase-0} |
| 55 | + |
| 56 | +Ensure the foundational environment is ready before proceeding. |
| 57 | + |
| 58 | +### 0.1 Authentication & Project Context |
| 59 | + |
| 60 | +- Check if `gcloud` CLI is installed. If it is not installed, prompt the user |
| 61 | + for permission to install it before proceeding. |
| 62 | +- Verify `gcloud auth list`. If not authenticated, run `gcloud auth login`. |
| 63 | +- Ensure `project` and `location` are known. Use `gcloud config get project` |
| 64 | + to retrieve the current project (and `gcloud config get compute/region` for |
| 65 | + region). |
| 66 | +- **CRITICAL: Ask for Confirmation.** You must prompt the user to confirm the |
| 67 | + retrieved project and region before proceeding, in case they want to switch |
| 68 | + to a different one. |
| 69 | + |
| 70 | +### 0.2 Enable APIs |
| 71 | + |
| 72 | +Ensure `aiplatform.googleapis.com` and `storage.googleapis.com` are enabled. |
| 73 | +`bash gcloud services enable aiplatform.googleapis.com storage.googleapis.com |
| 74 | +--project=YOUR_PROJECT` |
| 75 | + |
| 76 | +### 0.3 IAM Permissions |
| 77 | + |
| 78 | +Verify the following identities have the required roles. |
| 79 | + |
| 80 | +- **Vertex AI Service Agent**: |
| 81 | + `service-PROJECT_NUMBER@gcp-sa-aiplatform.iam.gserviceaccount.com` |
| 82 | +- **Managed OSS Fine Tuning Service Agent**: |
| 83 | + `service-PROJECT_NUMBER@gcp-sa-vertex-moss-ft.iam.gserviceaccount.com` |
| 84 | +- **User Identity**: The account running the commands. |
| 85 | + |
| 86 | +### 0.4 Virtual Environment |
| 87 | + |
| 88 | +Create and use a virtual environment named `tuning_agent_venv` in the home |
| 89 | +directory. Install dependencies from `references/requirements.txt`. `bash |
| 90 | +python3 -m venv ~/tuning_agent_venv source ~/tuning_agent_venv/bin/activate pip |
| 91 | +install -r references/requirements.txt` |
| 92 | + |
| 93 | +-------------------------------------------------------------------------------- |
| 94 | + |
| 95 | +## Phase 1: Dataset Preparation & Upload {#phase-1} |
| 96 | + |
| 97 | +Vertex AI requires valid JSONL format in GCS. |
| 98 | + |
| 99 | +### 1.0 Dataset Discovery & Confirmation |
| 100 | + |
| 101 | +- **Ask the User First:** Ask the user if they already have a dataset they |
| 102 | + want to use. |
| 103 | +- **Auto-Discovery:** If the user does not have a dataset, search the |
| 104 | + authenticated project's GCS buckets to find if any existing file has a |
| 105 | + reasonable dataset that can do the job the user prompted initially. |
| 106 | +- **CRITICAL: Ask for Confirmation.** Do not proceed with dataset preparation |
| 107 | + or upload until you present the found or provided dataset to the user and |
| 108 | + they confirm the dataset to use. |
| 109 | + |
| 110 | +### 1.1 Formatting & Validation |
| 111 | + |
| 112 | +- **Conversion**: If data is in CSV or JSON, use `vertex-tuning/scripts/prepare_dataset.py` |
| 113 | + to convert. |
| 114 | +- **Validation Split Confirmation**: If the user only provides a training |
| 115 | + dataset, **you must prompt the user** to seek permission to split the |
| 116 | + training dataset 80/20 to form a validation dataset (using |
| 117 | + `--validation_split 0.2`). If they agree, proceed with the split. If they |
| 118 | + decline, just use the training dataset without a validation dataset. |
| 119 | +- **Validation**: If data is already in JSONL, validate it before uploading: |
| 120 | + `bash python3 vertex-tuning/scripts/prepare_dataset.py |
| 121 | + \ --input my_data.jsonl \ --format messages \ --validate_only` |
| 122 | +- Refer to [Data Preparation Guide](references/data_prep.md) for required |
| 123 | + schemas. |
| 124 | + |
| 125 | +### 1.2 Upload |
| 126 | + |
| 127 | +Upload formatted `.jsonl` files to GCS using a unique directory (e.g., with a |
| 128 | +datetime timestamp) to avoid overwriting outputs from different runs. |
| 129 | + |
| 130 | +```bash |
| 131 | +gcloud storage cp dataset.jsonl gs://YOUR_BUCKET/tuning_agent_job_<datetime>/dataset.jsonl |
| 132 | +``` |
| 133 | + |
| 134 | +-------------------------------------------------------------------------------- |
| 135 | + |
| 136 | +## Phase 2: Recommendations & Configuration {#phase-2} |
| 137 | + |
| 138 | +Help the user choose the best model and parameters. **Always seek user |
| 139 | +confirmation before submitting the job.** |
| 140 | + |
| 141 | +### 2.1 Model Selection |
| 142 | + |
| 143 | +- If the user does not specify a model in their prompt, recommend a model |
| 144 | + based on the user's prompt by referencing the |
| 145 | + [Models Catalog](references/models.md). |
| 146 | +- **Prompt for Confirmation:** Present the recommended model to the user and |
| 147 | + ask for their confirmation. |
| 148 | + |
| 149 | +### 2.2 Hyperparameters |
| 150 | + |
| 151 | +- If the user does not specify hyperparameters, recommend `tuning_mode`, |
| 152 | + `epochs`, `learning_rate`, and `adapter_size` based on the |
| 153 | + [Tuning Guide](references/tuning_guide.md) and model-specific baselines in |
| 154 | + the [Models Catalog](references/models.md). |
| 155 | +- **Prompt for Confirmation:** Present the recommended hyperparameter |
| 156 | + configuration to the user and ask for their approval before proceeding to |
| 157 | + job submission. |
| 158 | + |
| 159 | +-------------------------------------------------------------------------------- |
| 160 | + |
| 161 | +## Phase 3: Tuning Job Execution |
| 162 | + |
| 163 | +Submit the job using `scripts/tune_model.py`. |
| 164 | + |
| 165 | +```bash |
| 166 | +python3 scripts/tune_model.py \ |
| 167 | + --project YOUR_PROJECT \ |
| 168 | + --location YOUR_LOCATION \ |
| 169 | + --bucket YOUR_STAGING_BUCKET \ |
| 170 | + --base_model BASE_MODEL_ID \ |
| 171 | + --train_dataset gs://YOUR_BUCKET/tuning_agent_job_<datetime>/dataset.jsonl \ |
| 172 | + --output_uri gs://YOUR_BUCKET/tuning_agent_job_<datetime>/output \ |
| 173 | + --epochs EPOCHS \ |
| 174 | + --learning_rate LR \ |
| 175 | + --tuning_mode MODE |
| 176 | +``` |
| 177 | + |
| 178 | +-------------------------------------------------------------------------------- |
| 179 | + |
| 180 | +## Phase 4: Monitoring |
| 181 | + |
| 182 | +Monitor the job via the Cloud Console link provided in the script output or by |
| 183 | +polling the job status. |
| 184 | + |
| 185 | +-------------------------------------------------------------------------------- |
| 186 | + |
| 187 | +## Phase 5: Model Deployment |
| 188 | + |
| 189 | +Once the job is `SUCCEEDED`, deploy the model using `scripts/deploy_model.py`. |
| 190 | + |
| 191 | +```bash |
| 192 | +python3 vertex_tuning/scripts/deploy_model.py \ |
| 193 | + --project YOUR_PROJECT \ |
| 194 | + --location YOUR_LOCATION \ |
| 195 | + --artifacts_uri gs://YOUR_BUCKET/tuning_agent_job_<datetime>/output/postprocess/node-0/checkpoints/final \ |
| 196 | + --machine_type MACHINE_TYPE \ |
| 197 | + --accelerator_type ACCELERATOR_TYPE \ |
| 198 | + --accelerator_count COUNT |
| 199 | +``` |
| 200 | + |
| 201 | +Refer to [Models Catalog](references/models.md) for hardware recommendations. |
| 202 | + |
| 203 | +-------------------------------------------------------------------------------- |
| 204 | + |
| 205 | +## Resources |
| 206 | + |
| 207 | +- [Data Preparation Guide](references/data_prep.md) |
| 208 | +- [Models Catalog](references/models.md) |
| 209 | +- [Tuning Guide](references/tuning_guide.md) |
| 210 | +- `scripts/prepare_dataset.py`: Data conversion & validation. |
| 211 | +- `scripts/tune_model.py`: Job submission. |
| 212 | +- `scripts/deploy_model.py`: Model deployment. |
0 commit comments