Ray Train 在虚拟机上使用 XGBoostTrainer#

注意

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

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

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

虚拟机集群设置#

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

  • 总共 10 个节点

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

    • m5.4xlarge (Amazon Web Services)

    • Standard_D5_v2 (Azure)

    • e2-standard-16 (Google Cloud)

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

相应的集群配置文件如下:

# 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 nodes)的 `min_workers` 设置为 0。工作负载提交后,将有 9 个工作节点自动扩容以适应工作负载。工作负载完成后,这些节点将自动缩减。

部署 Ray 集群#

现在我们可以使用上面定义的配置来部署 Ray 集群了。在运行命令之前,请确保您的 AWS 凭证已正确配置。

ray up -y cluster.yaml

将创建一个 Ray 头节点和 9 个 Ray 工作节点。

运行工作负载#

我们将使用Ray 作业提交来启动工作负载。

连接到集群#

首先,我们连接到作业服务器(Job server)。在单独的 shell 中运行以下阻塞命令:

ray dashboard cluster.yaml

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

提交工作负载#

我们将使用Ray 作业 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="https://: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 头节点中的 `model.json` 文件包含训练模型的参数。其他结果数据将在头节点的 `ray_results` 目录中提供。有关详细信息,请参阅XGBoostTrainer 文档

缩减

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

清理#

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

ray down -y cluster.yaml