啟動 Agent Platform 類神經架構搜尋實驗前,請先設定環境。
事前準備
- 登入 Google Cloud 帳戶。如果您未曾使用過 Google Cloud,歡迎 建立帳戶,親自體驗實際使用我們產品的成效。新客戶還能獲得價值 $300 美元的免費抵免額,用於執行、測試及部署工作負載。
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
Enable the Agent Platform API, if it is not already enabled.
Roles required to enable APIs
To enable APIs, you need the
serviceusage.services.enablepermission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.-
Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init -
初始化 gcloud CLI 後,請更新 gcloud CLI 並安裝必要元件:
gcloud components update gcloud components install beta
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
Enable the Agent Platform API, if it is not already enabled.
Roles required to enable APIs
To enable APIs, you need the
serviceusage.services.enablepermission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.-
Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init -
初始化 gcloud CLI 後,請更新 gcloud CLI 並安裝必要元件:
gcloud components update gcloud components install beta
- 如要將 Agent Platform 使用者角色 (
roles/aiplatform.user) 授予所有 Agent Platform Neural Architecture Search 使用者,請與專案管理員聯絡。 - 安裝 Docker。
如果您使用的是 Ubuntu、Debian 等以 Linux 為基礎的作業系統,請將您的使用者名稱加入
docker群組,這樣就能在不使用sudo的情況下執行 Docker:sudo usermod -a -G docker ${USER}將自己加入
docker群組後,可能需要重新啟動系統。 - 開啟 Docker。如要確認 Docker 是否正在執行,請執行下列 Docker 指令,系統會傳回目前的日期和時間:
docker run busybox date
- 使用
gcloud做為 Docker 的憑證輔助程式:gcloud auth configure-docker
-
(選用) 如要在本機使用 GPU 執行容器,請安裝
nvidia-docker。
設定 Cloud Storage bucket
本節說明如何建立新的 bucket。您可以使用現有 bucket,但必須與執行 AI Platform 工作的區域相同。此外,如果該儲存貯體不屬於您用來執行 Agent Platform Neural Architecture Search 的專案,則必須明確授予 Agent Platform Neural Architecture Search 服務帳戶存取權。
-
指定新值區的名稱。Cloud Storage 中所有值區的名稱皆不得重複。
BUCKET_NAME="YOUR_BUCKET_NAME"
為確保 bucket 名稱不重複且無法預測,請在 bucket 名稱中使用隨機產生的通用專屬 ID (UUID),如以下範例所示。這項做法有助於防止 bucket 搶占。
PROJECT_ID="YOUR_PROJECT_ID" BUCKET_NAME="${PROJECT_ID}-vertexai-nas-$(uuidgen)"
-
檢查您建立的值區名稱。
echo $BUCKET_NAME
-
選取值區的地區,然後設定
REGION環境變數。請使用您打算執行 Agent Platform Neural Architecture Search 工作的區域。
例如,下列程式碼會建立
REGION並設為us-central1:REGION=us-central1
-
建立新值區:
gcloud storage buckets create gs://$BUCKET_NAME --location=$REGION
要求提高專案的裝置配額
教學課程會使用約五部 CPU 機器,不需要任何額外配額。執行教學課程後,請執行 Agent Platform Neural Architecture Search 工作。
Agent Platform Neural Architecture Search 工作會平行訓練一批模型。每個訓練模型都對應一項試驗。請參閱設定 number-of-parallel-trials 一節,瞭解如何估算搜尋工作所需的 CPU 和 GPU 數量。舉例來說,如果每個試驗使用 2 個 T4 GPU,且您將 number-of-parallel-trials 設為 20,則搜尋工作需要 40 個 T4 GPU 的總配額。此外,如果每個試驗都使用 highmem-16 CPU,則每個試驗需要 16 個 CPU 單位,20 個平行試驗則需要 320 個 CPU 單位。不過,我們要求至少 10 個平行試驗配額 (或 20 個 GPU 配額)。GPU 的預設初始配額因區域和 GPU 類型而異,Tesla_T4 通常為 0、6 或 12,Tesla_V100 則為 0 或 6。CPU 的預設初始配額因區域而異,通常為 20、450 或 2,200。
選用:如果打算同時執行多項搜尋工作,請提高配額需求。申請配額不會立即產生費用。執行工作後,系統就會向您收費。
如果配額不足,且嘗試啟動的工作所需資源超過配額,工作就不會啟動,並會顯示類似以下的錯誤訊息:
Exception: Starting job failed: {'code': 429, 'message': 'The following quota metrics exceed quota limits: aiplatform.googleapis.com/custom_model_training_cpus,aiplatform.googleapis.com/custom_model_training_nvidia_v100_gpus,aiplatform.googleapis.com/custom_model_training_pd_ssd', 'status': 'RESOURCE_EXHAUSTED', 'details': [{'@type': 'type.googleapis.com/google.rpc.DebugInfo', 'detail': '[ORIGINAL ERROR] generic::resource_exhausted: com.google.cloud.ai.platform.common.errors.AiPlatformException: code=RESOURCE_EXHAUSTED, message=The following quota metrics exceed quota limits: aiplatform.googleapis.com/custom_model_training_cpus,aiplatform.googleapis.com/custom_model_training_nvidia_v100_gpus,aiplatform.googleapis.com/custom_model_training_pd_ssd, cause=null [google.rpc.error_details_ext] { code: 8 message: "The following quota metrics exceed quota limits: aiplatform.googleapis.com/custom_model_training_cpus,aiplatform.googleapis.com/custom_model_training_nvidia_v100_gpus,aiplatform.googleapis.com/custom_model_training_pd_ssd" }'}]}
在某些情況下,如果同時啟動同一專案的多項工作,且配額不足以執行所有工作,則其中一項工作會維持在排入佇列狀態,不會開始訓練。在這種情況下,請取消排入佇列的工作,並要求更多配額,或等待先前的工作完成。
您可以在「配額」頁面申請提高裝置配額。您可以套用篩選器,找出要編輯的配額:

- 在「服務」部分,選取「Agent Platform API」。
- 在「region」(區域),選取要篩選的區域。
- 在「Quota」(配額) 部分,選取前置字串為「Custom model training」(自訂模型訓練) 的加速器名稱。
- 如果是 V100 GPU,值為「Custom model training Nvidia V100 GPUs per region」(每個區域的自訂模型訓練 Nvidia V100 GPU)。
- 如果是 CPU,值可以是「Custom model training CPUs for N1/E2 machine types per region」(每個區域中 N1/E2 機型的自訂模型訓練 CPU)。CPU 的數字代表 CPU 的單位。如需 8 個
highmem-16CPU,請要求 8 * 16 = 128 個 CPU 單位的配額。並輸入所需的區域值。
建立配額要求後,您會收到 Case number 和後續追蹤電子郵件,通知您要求的狀態。GPU 配額核准作業大約需要兩到五個工作天。一般來說,申請約 20 到 30 個 GPU 的配額,大約兩到三天內就能獲得核准;申請約 100 個 GPU 的配額,則可能需要五個工作天。CPU 配額核准作業最多可能需要兩個工作天。不過,如果某個區域嚴重缺乏某種 GPU 類型,即使要求少量配額,也無法保證能取得。在這種情況下,系統可能會要求您選擇其他區域或 GPU 類型。一般來說,T4 GPU 比 V100 更容易取得。T4 GPU 耗費的實際時間較長,但更具成本效益。
詳情請參閱「要求調整配額」。
為專案設定 Artifact Registry
您必須為專案和推送 Docker 映像檔的區域設定 Artifact Registry。
前往專案的「Artifact Registry」頁面。如果尚未啟用,請先為專案啟用 Artifact Registry API:

啟用後,請按一下「CREATE REPOSITORY」(建立存放區),開始建立新的存放區:

「名稱」選擇「nas」,「格式」選擇「Docker」,「位置類型」選擇「區域」。在「Region」(區域) 部分,選取執行工作的區域,然後按一下「CREATE」(建立)。

這會建立所需的 Docker 存放區,如下所示:

您也必須設定驗證機制,才能將 Docker 推送至這個存放區。下方的本機環境設定一節包含這個步驟。
設定本機環境
您可以在本機環境中使用 Bash 殼層執行這些步驟,也可以在 Gemini Enterprise Agent Platform Workbench 執行個體的筆記本中執行。
設定基本環境變數:
gcloud config set project PROJECT_ID gcloud auth login gcloud auth application-default login為 Artifact Registry 設定 Docker 驗證:
# example: REGION=europe-west4 gcloud auth configure-docker REGION-docker.pkg.dev(選用) 設定 Python 3 虛擬環境。建議使用 Python 3,但並非必要:
sudo apt install python3-pip && \ pip3 install virtualenv && \ python3 -m venv --system-site-packages ~/./nas_venv && \ source ~/./nas_venv/bin/activate安裝其他程式庫:
pip install google-cloud-storage==2.6.0 pip install pyglove==0.1.0
設定服務帳戶
執行 NAS 工作前,必須先設定服務帳戶。您可以在本機環境中使用 Bash 殼層執行這些步驟,也可以在 Gemini Enterprise Agent Platform Workbench 執行個體的筆記本中執行。
建立服務帳戶:
gcloud iam service-accounts create NAME \ --description=DESCRIPTION \ --display-name=DISPLAY_NAME將
aiplatform.user和storage.objectAdmin角色授予服務帳戶:gcloud projects add-iam-policy-binding PROJECT_ID \ --member=serviceAccount:NAME@PROJECT_ID.iam.gserviceaccount.com \ --role=roles/aiplatform.user gcloud projects add-iam-policy-binding PROJECT_ID \ --member=serviceAccount:NAME@PROJECT_ID.iam.gserviceaccount.com \ --role=roles/storage.objectAdmin
舉例來說,下列指令會在專案 my-nas-project 下建立名為 my-nas-sa 的服務帳戶,並授予 aiplatform.user 和 storage.objectAdmin 角色:
gcloud iam service-accounts create my-nas-sa \
--description="Service account for NAS" \
--display-name="NAS service account"
gcloud projects add-iam-policy-binding my-nas-project \
--member=serviceAccount:my-nas-sa@my-nas-project.iam.gserviceaccount.com \
--role=roles/aiplatform.user
gcloud projects add-iam-policy-binding my-nas-project \
--member=serviceAccount:my-nas-sa@my-nas-project.iam.gserviceaccount.com \
--role=roles/storage.objectAdmin
下載程式碼
如要啟動 Agent Platform Neural Architecture Search 實驗,您必須下載 Python 程式碼範例,其中包含預先建構的訓練器、搜尋空間定義和相關聯的用戶端程式庫。
請按照下列步驟下載原始碼。
開啟新的 Shell 終端機。
執行 Git clone 指令:
git clone https://github.com/google/vertex-ai-nas.git