透過 Ray Client 連線至 Vertex AI 的 Ray 叢集, 使用包含 Ray Client 功能的 Vertex AI SDK for Python 版本。 如果您偏好互動式 Python 開發環境,請使用這個選項。
在 Google Cloud 控制台的 Colab Enterprise 筆記本中,使用 Python 適用的 Vertex AI SDK。
在 Python 工作階段、殼層或 Jupyter 筆記本中,使用 Python 適用的 Vertex AI SDK。
編寫 Python 指令碼,並使用 Ray Jobs API 將指令碼提交至 Vertex AI 的 Ray 叢集。如要以程式輔助方式提交工作,請使用這個選項。
透過 Ray Client 連線至 Ray 叢集
如要使用互動式 Ray 用戶端,請連線至 Vertex AI 上的 Ray 叢集。連線環境的網路取決於叢集的網路設定。只要叢集可存取公開網際網路,連線環境就不會受到限制。也就是說,您在建立叢集時未指定虛擬私有雲網路。不過,如果叢集位於與 Vertex AI 對等互連的私人虛擬私有雲網路,連線環境必須與叢集位於同一個虛擬私有雲網路。
用戶端的 Ray 版本必須與叢集的 Ray 版本相符。 pip install "google-cloud-aiplatform[ray]"
預設會在用戶端安裝 Ray 2.47 版。如果叢集的 Ray 版本較舊 (例如 2.42 以前),請使用 pip install ray==2.42.0
,將用戶端 Ray 版本與叢集的 Ray 版本相符。
控制台
根據 OSS Ray 最佳做法建議,系統會強制將 Ray 頭部節點的邏輯 CPU 數量設為 0,避免在頭部節點上執行任何工作負載。
前往 Google Cloud 控制台的 Ray on Vertex AI 頁面。
在您建立的叢集資料列中,按一下「在 Colab Enterprise 中開啟」。
系統會開啟 Colab Enterprise 筆記本。按照操作說明,使用 Python 適用的 Vertex AI SDK 連線至 Vertex AI 的 Ray 叢集。
如果對話方塊要求您啟用 API,請按一下「啟用」。
如果是初次連線至叢集,請按一下「連線」。如要重新連線至叢集,請按一下「重新連線」。 筆記本需要幾分鐘才能連線至執行階段。
按一下「+建立」即可建立新筆記本。
按一下
開啟 Vertex AI 上的 Ray 面板。
系統會顯示現有叢集。選取叢集,然後按一下「CONNECT」(連線)。
程式碼會顯示在已開啟的筆記本中,並連結至所選叢集。其他動作 (選用):如要開啟 Ray on Vertex AI 叢集清單頁面,請按一下「Ray on Vertex AI」面板中的「管理叢集」。
- 選取叢集,然後按一下
畫面會顯示更多選項:
「更多動作」選單。
- 選取叢集,然後按一下
執行「開始使用」程式碼儲存格,匯入 Python 適用的 Vertex AI SDK,並連線至 Vertex AI 上的 Ray 叢集。
Python
根據 OSS Ray 最佳做法建議,系統會強制將 Ray 頭部節點的邏輯 CPU 數量設為 0,避免在頭部節點上執行任何工作負載。
在 Python 互動式環境中:
import ray # Necessary even if aiplatform.* symbol is not directly used in your program. from google.cloud import aiplatform import vertex_ray import vertexai vertexai.init() # The CLUSTER_RESOURCE_NAME is the one returned from vertex_ray.create_ray_cluster. CLUSTER_RESOURCE_NAME='projects/{}/locations/{}/persistentResources/{}'.format(PROJECT_ID, LOCATION, CLUSTER_NAME) ray.init('vertex_ray://{}'.format(CLUSTER_RESOURCE_NAME))
其中:
LOCATION:您在 Vertex AI 上為 Ray 叢集指定的位置。
PROJECT_ID:您的 Google Cloud 專案 ID。在 Google Cloud 控制台的歡迎頁面中,找到專案 ID。
CLUSTER_NAME:Vertex AI 上的 Ray 叢集名稱,建立叢集時指定。前往Google Cloud 控制台,查看專案的叢集名稱清單。
您會看到類似以下的輸出內容:
Python version: 3.10.12 Ray version: 2.47 Vertex SDK version: 1.46.0 Dashboard: xxxx-dot-us-central1.aiplatform-training.googleusercontent.com
使用 Dashboard
網址,透過瀏覽器存取 Ray 資訊主頁。URI 的格式為 https://xxxx-dot-us-central1.aiplatform-training.googleusercontent.com/
。資訊主頁會顯示已提交的工作、GPU 或 CPU 數量,以及叢集中每部機器的磁碟空間。
連線至 Vertex AI 的 Ray 叢集後,即可開發 Ray 程式,方法與開發一般 OSS Ray 後端程式相同。
@ray.remote def square(x): print(x) return x * x # Launch four parallel square tasks. futures = [square.remote(i) for i in range(4)] print(ray.get(futures)) # Returns [0, 1, 4, 9]
使用 Ray Jobs API 開發應用程式
本節說明如何使用 Ray Jobs API,將 Python 程式提交至 Vertex AI 的 Ray 叢集。
編寫 Python 指令碼
在任何文字編輯器中,以 Python 指令碼形式開發應用程式。舉例來說,請將下列指令碼放在 my_script.py
檔案中:
import ray import time @ray.remote def hello_world(): return "hello world" @ray.remote def square(x): print(x) time.sleep(100) return x * x ray.init() # No need to specify address="vertex_ray://...." print(ray.get(hello_world.remote())) print(ray.get([square.remote(i) for i in range(4)]))
使用 Ray Jobs API 提交 Ray 工作
使用 Python、Ray Jobs CLI 或公開 Ray 資訊主頁位址提交 Ray 工作。
Python - 叢集資源名稱
使用 Python 環境提交 Ray 工作:
import ray import vertex_ray from ray.job_submission import JobSubmissionClient from google.cloud import aiplatform # Necessary even if aiplatform.* symbol is not directly used in your program. CLUSTER_RESOURCE_NAME='projects/{}/locations/REGION/persistentResources/{}'.format(PROJECT_ID, CLUSTER_NAME) client = JobSubmissionClient("vertex_ray://{}".format(CLUSTER_RESOURCE_NAME)) job_id = client.submit_job( # Entrypoint shell command to execute entrypoint="python my_script.py", # Path to the local directory that contains the my_script.py file. runtime_env={ "working_dir": "./directory-containing-my-script", "pip": ["numpy", "setuptools<70.0.0", "xgboost", "ray==CLUSTER_RAY_VERSION", # pin the Ray version to the same version as the cluster ] } ) # Ensure that the Ray job has been created. print(job_id)
其中:
REGION:您為 Vertex AI 上的 Ray 叢集指定的區域。
PROJECT_ID:您的 Google Cloud 專案編號。在 Google Cloud 控制台的歡迎頁面中,找到專案 ID。
CLUSTER_NAME:Vertex AI 上的 Ray 叢集名稱,建立叢集時指定。前往Google Cloud 控制台,查看專案的叢集名稱清單。
CLUSTER_RAY_VERSION:將 Ray 版本固定為與叢集相同的版本。例如 2.47.1。
Python - Ray 資訊主頁
您可以從虛擬私有雲外部存取 Ray 資訊主頁位址,包括公開網際網路。請注意,您必須使用 vertex_ray
才能自動取得驗證。
from ray.job_submission import JobSubmissionClient import vertex_ray DASHBOARD_ADDRESS=DASHBOARD_ADDRESS client = JobSubmissionClient( "vertex_ray://{}".format(DASHBOARD_ADDRESS), ) job_id = client.submit_job( # Entrypoint shell command to execute entrypoint="python my_script.py", # Path to the local directory that contains the my_script.py file runtime_env={ "working_dir": "./directory-containing-my-script", "pip": ["numpy", "setuptools<70.0.0", "xgboost", "ray==CLUSTER_RAY_VERSION", # pin the Ray version to the same version as the cluster ] } ) print(job_id)
其中:
DASHBOARD_ADDRESS:叢集的 Ray 資訊主頁地址。使用 Python 適用的 Vertex AI SDK 找出資訊主頁位址。
Ray Jobs CLI
只能在對等互連的虛擬私有雲網路中使用 Ray Jobs CLI 指令。
$ ray job submit --working-dir ./ --address vertex_ray://{CLUSTER_RESOURCE_NAME} -- python my_script.py
提交長時間執行的 Ray 工作後,如要使用 client.get_job_status(job_id)
監控工作狀態,請重新例項化 JobSubmissionClient(client = JobSubmissionClient("vertex_ray://{}".format(CLUSTER_RESOURCE_NAME)) )
來重新整理驗證權杖。
支援 VPC 對等互連和自訂服務帳戶
Vertex AI 上的 Ray 支援預設服務代理和自訂服務帳戶的公開網路,可使用 Ray Client 和 Ray Jobs API (JobSubmissionClient)。
下表說明使用虛擬私有雲網路建立 Ray 叢集時,Vertex AI 上的 Ray 對虛擬私有雲對等互連的支援情形:
虛擬私有雲對等互連 | 預設服務代理 | 自訂服務帳戶 |
---|---|---|
Ray Client (互動模式) | 是 | 否 |
Ray JobSubmissionClient | 是 | 是 |
VPC Service Controls (VPC-SC) 需要額外設定。 詳情請參閱「私人與公開連線」。
在 Ray 程式碼中使用網路檔案系統 (NFS)
如果在建立 Ray 叢集時設定 NFS 掛接,您可以在應用程式程式碼中讀取及寫入這些 NFS 磁碟區。
RayClient
本節說明如何在 Ray 程式碼中使用網路檔案系統 (NFS)。
在 Python 環境中初始化 RayClient
import ray from google.cloud import aiplatform import vertex_ray aiplatform.init(project=PROJECT_ID, location=REGION) ray.init(address='vertex_ray://projects/{}/locations/us-central1/persistentResources/{}'.format(PROJECT_NUMBER, PERSISTENT_RESOURCE_ID))
執行工作指令碼
import ray import logging import os import sys @ray.remote def main(): logging.info("list all files in mounted folder") return os.listdir("/mnt/nfs/test") print(''.join(ray.get(main.remote())))
使用 Python、Ray Jobs CLI 或公開的 Ray 資訊主頁位址提交 Ray 工作。詳情請參閱「在 Vertex AI 的 Ray 叢集上開發應用程式」。