請先設定環境,再啟動 Vertex AI 類神經架構搜尋實驗。
事前準備
- Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the Vertex AI API.
-
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
-
After initializing the gcloud CLI, update it and install the required components:
gcloud components update gcloud components install beta
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the Vertex AI API.
-
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
-
After initializing the gcloud CLI, update it and install the required components:
gcloud components update gcloud components install beta
- 如要將 Vertex AI 使用者角色 (
roles/aiplatform.user
) 授予所有神經架構搜尋使用者,請與專案管理員聯絡。 - 安裝 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_NAME="YOUR_BUCKET_NAME"
舉例來說,請使用您的專案名稱,並在後面附加
-vertexai-nas
:PROJECT_ID="YOUR_PROJECT_ID" BUCKET_NAME=${PROJECT_ID}-vertexai-nas
-
檢查您建立的值區名稱。
echo $BUCKET_NAME
-
選取值區的地區,然後設定
REGION
環境變數。使用的地區必須與您預計要執行類神經架構搜尋工作的地區相同。
例如,下列程式碼會建立
REGION
並設為us-central1
:REGION=us-central1
-
建立新值區:
gcloud storage buckets create gs://$BUCKET_NAME --location=$REGION
- 在「服務」部分,選取「Vertex AI API」。
- 在「區域」中,選取要篩選的區域。
- 在「配額」部分,選取前置字串為「Custom model training」的加速器名稱。
- 對於 V100 GPU,這個值為「Custom model training Nvidia V100 GPUs per region」。
- 針對 CPU,值可以是「自訂模型訓練 CPU,適用於各區域的 N1/E2 機器類型」。CPU 的數字代表 CPU 的單位。如果您需要 8 個
highmem-16
CPU,請提出 8 * 16 = 128 個 CPU 單位的配額要求。並輸入所需的區域值。
設定基本環境變數:
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
建立服務帳戶:
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
開啟新的 Shell 終端機。
執行 Git 複製指令:
git clone https://github.com/google/vertex-ai-nas.git
設定 Cloud Storage 值區
本節將說明如何建立新值區。您可以使用現有值區,但該值區的所在地區,必須與您執行 AI Platform 工作的地區相同。此外,如果該值區不屬於您用來執行類神經架構搜尋的專案,您就必須明確地將值區的存取權授予類神經架構搜尋服務帳戶。
為專案申請額外的裝置配額
教學課程使用約五部 CPU 機器,不需要任何額外配額。完成教學課程後,請執行類神經架構搜尋工作。
類神經架構搜尋作業會並行訓練一批模型。每個訓練模型都對應至一個試驗。請參閱設定 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" }'}]}
在某些情況下,如果同一個專案同時啟動多個工作,但配額不足以供所有工作使用,則其中一個工作會處於排隊狀態,不會開始訓練。在這種情況下,請取消排入佇列的工作,並要求更多配額,或是等待先前的工作完成。
您可以透過「配額」頁面申請額外的裝置配額。您可以套用篩選器,找出要編輯的所需配額:
建立配額要求後,您會收到 Case number
和後續電子郵件,說明要求的處理狀態。GPU 配額核准作業大約需要兩到五個工作天。一般來說,大約 20 到 30 個 GPU 的配額應可在約兩到三天內獲得核准,而大約 100 個 GPU 的配額可能需要五個工作天才能獲得核准。CPU 配額核准作業最多可能需要兩個工作天才能完成。不過,如果某個地區的 GPU 類型嚴重短缺,即使要求的配額很少,也無法保證一定能取得。在這種情況下,系統可能會要求您使用其他區域或 GPU 類型。一般來說,T4 GPU 比 V100 更容易取得。T4 GPU 耗時較長,但更具成本效益。
詳情請參閱「申請配額調整」。
為專案設定構件註冊中心
您必須為推送 Docker 映像檔的專案和區域設定Artifact Registry。
前往專案的「Artifact Registry」頁面。如果尚未啟用,請先為專案啟用 Artifact Registry API:
啟用後,請按一下「建立存放區」,開始建立新的存放區:
請將「名稱」設為「nas」、「格式」設為「Docker」,以及「位置類型」設為「區域」。在「區域」中,選取要執行工作的位置,然後按一下「建立」。
這應該會建立所需的 Docker 存放區,如下所示:
您也需要設定驗證機制,才能將 Docker 推送至這個存放區。請參閱下方的「本地環境設定」一節,瞭解這個步驟。
設定本機環境
您可以在本機環境中使用 Bash 殼層執行這些步驟,也可以在 Vertex AI Workbench 使用者自行管理的筆記本執行個體中執行這些步驟。
設定服務帳戶
您必須先設定服務帳戶,才能執行 NAS 工作。您可以在本機環境中使用 Bash 殼層執行這些步驟,也可以在 Vertex AI Workbench 使用者管理的筆記本執行個體中執行這些步驟。
舉例來說,下列指令會在專案 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
下載程式碼
如要開始進行神經網路架構搜尋實驗,您必須下載 Python 範例程式碼,其中包含預先建構的訓練器、搜尋空間定義和相關用戶端程式庫。
請按照下列步驟下載原始碼。