Vertex AI의 Ray 클러스터에서 애플리케이션 개발

Vertex AI에서 Ray 클러스터에 연결하고 다음 방법을 사용하여 애플리케이션을 개발할 수 있습니다.

  • Ray Client의 기능이 포함된 Python용 Vertex AI SDK 버전을 사용하여 Vertex AI의 Ray 클러스터에 연결합니다. 대화형 Python 개발 환경을 원하는 경우 이 옵션을 사용합니다.

    • Google Cloud 콘솔의 Colab Enterprise 노트북 내에서 Python용 Vertex AI SDK를 사용합니다.

    • Python 세션, 셸 또는 Jupyter 노트북 내에서 Python용 Vertex AI SDK를 사용합니다.

  • Python 스크립트를 작성하고 Ray Jobs API를 사용하여 Vertex AI의 Ray 클러스터에 스크립트를 제출합니다. 프로그래매틱 방식으로 작업을 제출하려면 이 옵션을 선택합니다.

시작하기 전에 Vertex AI의 Ray 개요를 읽고 필요한 모든 기본 요건 도구를 설정해야 합니다.

Python용 Vertex AI SDK를 사용하여 애플리케이션 개발

Python용 Vertex AI SDK를 사용하여 Vertex AI의 Ray 클러스터에 연결하려면 연결 환경이 같은 피어링된 VPC 네트워크에 있어야 합니다.

콘솔

  1. Google Cloud 콘솔에서 Vertex AI의 Ray 페이지로 이동합니다.

    Vertex AI의 Ray 페이지로 이동

  2. 만든 클러스터의 행에서 Colab Enterprise에서 열기를 클릭합니다.

  3. Colab Enterprise 노트북이 열립니다. Python용 Vertex AI SDK를 사용하여 Vertex AI의 Ray 클러스터에 연결하는 방법에 대한 안내를 따릅니다.

    • API를 사용 설정하라는 대화상자 화면이 표시되면 사용 설정을 클릭합니다.

    • 클러스터에 처음 연결하는 경우 연결을 클릭하거나 클러스터에 다시 연결하는 경우 다시 연결을 클릭합니다. 노트북이 런타임에 연결되는 데 몇 분 정도 걸립니다.

    • 시작하기 코드 셀을 실행하여 Python용 Vertex AI SDK를 가져오고 Vertex AI의 Ray 클러스터에 연결합니다.

Python

대화형 Python 환경에서 다음 명령어를 실행합니다.

import ray

# Necessary even if aiplatform.* symbol is not directly used in your program.
from google.cloud import aiplatform
import vertex_ray

# The CLUSTER_RESOURCE_NAME is the one returned from vertex_ray.create_ray_cluster.
CLUSTER_RESOURCE_NAME='projects/{}/locations/{}/persistentResources/{}'.format(PROJECT_ID, REGION, CLUSTER_NAME)

ray.init('vertex_ray://{}'.format(CLUSTER_RESOURCE_NAME))

각 항목의 의미는 다음과 같습니다.

  • REGION: Vertex AI의 Ray 클러스터에 지정한 리전입니다.

  • PROJECT_ID: Google Cloud 프로젝트 ID Google Cloud 콘솔 시작 페이지에서 프로젝트 ID를 찾을 수 있습니다.

  • CLUSTER_NAME: 클러스터를 만들 때 지정한 Vertex AI의 Ray 클러스터 이름입니다.

출력은 다음과 비슷합니다.

Python version:  3.10.12
Ray version: 2.9
Vertex SDK version: 1.46.0
Dashboard: xxxx-dot-us-central1.aiplatform-training.googleusercontent.com

Dashboard URL을 사용하여 브라우저에서 Ray 대시보드에 액세스할 수 있습니다. URI 형식은 http://xxxx-dot-us-central1.aiplatform-training.googleusercontent.com/입니다. 대시보드에는 제출된 작업, 클러스터에 있는 각 머신의 GPU 또는 CPU 수와 디스크 공간이 표시됩니다.

Vertex AI의 Ray 클러스터에 연결되면 일반 OSS 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를 사용하여 Vertex AI의 Ray 클러스터에 Python 프로그램을 제출하는 방법을 설명합니다.

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 - 클러스터 리소스 이름

VPC 피어링된 네트워크 내에서 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",
            "xgboost",
            "ray==2.9.3", # pin the Ray version to prevent it from being overwritten
           ]
  }
)

# 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 클러스터 이름입니다.

Python - Ray 대시보드

공개 인터넷을 포함한 VPC 외부에서 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",
            "xgboost",
            "ray==2.9.3", # pin the Ray version to prevent it from being overwritten
           ]
  }
)
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

다음 단계