在 Vertex AI 的 Ray 叢集中開發應用程式

您可以連線至 Vertex AI 上的 Ray 叢集,並使用下列方法開發應用程式:

  • 使用包含 Ray 用戶端功能的 Vertex AI SDK for Python 版本,透過 Ray 用戶端連線至 Vertex AI 上的 Ray 叢集。如果您偏好使用互動式 Python 開發環境,請使用這個選項。

    • 在 Google Cloud 控制台的 Colab Enterprise 筆記本中使用 Python 適用的 Vertex AI SDK。

    • 在 Python 工作階段、shell 或 Jupyter Notebook 中使用 Python 適用的 Vertex AI SDK。

  • 編寫 Python 指令碼,然後使用 Ray Jobs API 將指令碼提交至 Vertex AI 上的 Ray 叢集。如果您想以程式輔助方式提交工作,請使用這個選項。

開始前,請務必參閱 Ray on Vertex AI 總覽,並設定所有必要工具。

透過 Ray 用戶端連線至 Ray 叢集

如要使用互動式 Ray 用戶端,請連線至 Vertex AI 上的 Ray 叢集。連線環境的網路取決於叢集的網路設定。只要叢集可存取公開網際網路,連線環境就沒有任何限制。也就是說,在建立叢集時未指定 VPC 網路。不過,如果叢集位於與 Vertex AI 對等的私人 VPC 網路中,連線環境必須與叢集位於相同的 VPC 網路中。

用戶端的 Ray 版本必須與叢集的 Ray 版本相符。pip install "google-cloud-aiplatform[ray]" 預設會在用戶端上安裝 Ray 2.42 版。如果叢集的 Ray 版本較舊 (例如 2.33),您必須使用 pip install ray==2.33.0 將用戶端的 Ray 版本與叢集的 Ray 版本比對。

控制台

根據 OSS Ray 最佳做法的建議,我們強制在 Ray 主節點上將邏輯 CPU 數量設為 0,以免在主節點上執行任何工作負載。

  1. 前往 Google Cloud 控制台的「Vertex AI 中的 Ray」頁面。

    前往「Ray on Vertex AI」頁面

  2. 在所建立叢集的資料列中,按一下「在 Colab Enterprise 中開啟」

  3. Colab Enterprise 筆記本會隨即開啟。請按照操作說明,瞭解如何使用 Python 適用的 Vertex AI SDK 連線至 Vertex AI 上的 Ray 叢集。

    • 如果對話方塊畫面要求您啟用 API,請按一下「啟用」

    • 如果是首次連線至叢集,請按一下「連線」,如果是重新連線至叢集,請按一下「重新連線」。筆記本需要幾分鐘才能連線至 Runtime。

    • 按一下「+ 建立」,建立新的筆記本。

    • 按一下 Ray on Vertex AI 面板 開啟「Ray on Vertex AI」面板。
      顯示現有叢集。

    • 選取叢集,然後按一下「連線」
      程式碼會顯示在已開啟的筆記本中,並連結至所選叢集。

    • 其他操作 (選用):如要開啟 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.42
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 叢集後,您可以按照為一般 OSS Ray 後端開發 Ray 程式的方式,開發 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.42.0。

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

請注意,您只能在對等 VPC 網路中使用 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)) ) 以重新整理驗證權杖。

支援虛擬私有雲網路對等互連和自訂服務帳戶

Vertex AI 上的 Ray 支援在公開網路中支援 Ray 用戶端和 Ray Jobs API (JobSubmissionClient),適用於預設服務代理和自訂服務帳戶。

當 Ray 叢集是使用虛擬私有雲網路建立時,Ray 在 Vertex AI 上支援虛擬私有雲網路對等互連功能,如表格所示:

虛擬私有雲對等互連 預設服務代理 自訂服務帳戶
Ray 用戶端 (互動模式)
Ray JobSubmissionClient

您必須完成額外的設定,才能使用 VPC Service Controls (VPC-SC)。詳情請參閱「私人與公開連線」。

在 Ray 程式碼中使用網路檔案系統 (NFS)

如果您在建立 Ray 叢集時設定NFS 掛接點,就可以在應用程式程式碼中讀取及寫入這些 NFS 磁碟區。

RayClient

本節將說明如何在 Ray 程式碼中使用網路檔案系統 (NFS)。

  1. 在 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))
  2. 執行工作指令碼

    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 叢集中開發應用程式」。

後續步驟