Ray Train XGBoostTrainer 在虚拟机上运行#

注意

要学习 Ray 在虚拟机上的基础知识,我们建议您先查看入门指南

在本指南中,我们将向您展示如何在 AWS 上运行一个 Ray 机器学习示例工作负载。类似步骤也适用于 GCP 或 Azure。

我们将使用 100GB 的训练集运行 Ray 的XGBoost 训练基准测试。要了解更多关于使用 Ray 的 XGBoostTrainer 的信息,请查看XGBoostTrainer 文档

虚拟机集群设置#

对于本指南中的工作负载,建议使用以下设置

  • 总共 10 个节点

  • 每个节点 16 个 CPU 和 64 Gi 内存容量。对于主要的云提供商,适用的实例类型包括

    • m5.4xlarge (亚马逊云服务)

    • Standard_D5_v2 (Azure)

    • e2-standard-16 (谷歌云)

  • 每个节点应配置 1000GB 的磁盘空间(用于存储训练集)。

相应的集群配置文件如下

# This is a Ray cluster configuration for exploration of the 100Gi Ray XGBoostTrainer benchmark.

# The configuration includes 1 Ray head node and 9 worker nodes.

cluster_name: ray-cluster-xgboost-benchmark

# The maximum number of worker nodes to launch in addition to the head
# node.
max_workers: 9

docker:
  image: "rayproject/ray-ml:2.0.0"
  container_name: "ray_container"

provider:
  type: aws
  region: us-west-2
  availability_zone: us-west-2a

auth:
  ssh_user: ubuntu

available_node_types:
  # Configurations for the head node.
  head:
    node_config:
      InstanceType: m5.4xlarge
      ImageId: latest_dlami
      BlockDeviceMappings:
        - DeviceName: /dev/sda1
          Ebs:
            VolumeSize: 1000

  # Configurations for the worker nodes.
  worker:
    # To experiment with autoscaling, set min_workers to 0.
    # min_workers: 0
    min_workers: 9
    max_workers: 9
    node_config:
      InstanceType: m5.4xlarge
      ImageId: latest_dlami
      BlockDeviceMappings:
        - DeviceName: /dev/sda1
          Ebs:
            VolumeSize: 1000

head_node_type: head

可选:设置自动扩缩容集群

如果您想尝试启用自动扩缩容来运行工作负载,请将 worker 节点的 min_workers 设置为 0。工作负载提交后,9 个 worker 节点将向上扩展以适应工作负载。工作负载完成后,这些节点将缩减回来。

部署 Ray 集群#

现在我们准备好部署具有上述配置的 Ray 集群。在运行命令之前,请确保您的 AWS 凭证已正确配置。

ray up -y cluster.yaml

将创建一个 Ray head 节点和 9 个 Ray worker 节点。

运行工作负载#

我们将使用Ray Job Submission 来启动工作负载。

连接到集群#

首先,我们连接到 Job 服务器。在一个单独的 shell 中运行以下阻塞命令。

ray dashboard cluster.yaml

这将把远程端口 8265 转发到本地主机的 8265 端口。

提交工作负载#

我们将使用Ray Job Python SDK 提交 XGBoost 工作负载。

from ray.job_submission import JobSubmissionClient

client = JobSubmissionClient("http://127.0.0.1:8265")

kick_off_xgboost_benchmark = (
    # Clone ray. If ray is already present, don't clone again.
    "git clone https://github.com/ray-project/ray || true; "
    # Run the benchmark.
    "python ray/release/train_tests/xgboost_lightgbm/train_batch_inference_benchmark.py"
    " xgboost --size=100G --disable-check"
)


submission_id = client.submit_job(
    entrypoint=kick_off_xgboost_benchmark,
)

print("Use the following command to follow this Job's logs:")
print(f"ray job logs '{submission_id}' --follow")

要提交工作负载,请运行上述 Python 脚本。该脚本可在Ray 仓库中找到。

# Download the above script.
curl https://raw.githubusercontent.com/ray-project/ray/releases/2.0.0/doc/source/cluster/doc_code/xgboost_submit.py -o xgboost_submit.py
# Run the script.
python xgboost_submit.py

观察进度#

基准测试可能需要长达 30 分钟才能运行。使用以下工具观察其进度。

作业日志#

要查看作业日志,请使用上述提交脚本打印的命令。

# Substitute the Ray Job's submission id.
ray job logs 'raysubmit_xxxxxxxxxxxxxxxx' --address="http://localhost:8265" --follow

Ray Dashboard#

在浏览器中访问 localhost:8265 以访问 Ray Dashboard。

Ray 状态#

使用以下命令观察自动扩缩容状态和 Ray 资源使用情况

ray exec cluster.yaml 'ray status'

作业完成#

基准测试结果#

基准测试完成后,作业日志将显示结果

Results: {'training_time': 1338.488839321999, 'prediction_time': 403.36653568099973}

基准测试的性能对底层云基础设施很敏感——您可能无法匹配基准测试文档中引用的数字

模型参数#

Ray head 节点中的文件 model.json 包含训练模型的参数。其他结果数据将在 head 节点中的 ray_results 目录中提供。详情请参阅XGBoostTrainer 文档

缩减

如果启用了自动扩缩容,Ray worker 节点将在指定的空闲超时后缩减。

清理#

使用以下命令删除您的 Ray 集群

ray down -y cluster.yaml