ARTICLE DETAIL

资讯详情

深耕网站视觉设计与运营推广的一线实战洞察。

Ray Tune 分布式超参数调优实战:多节点集群搭建、存储配置、Spot 实例与故障恢复

Ray Tune 分布式超参数调优实战:多节点集群搭建、存储配置、Spot 实例与故障恢复 人工智能分布式训练强化学习任务调度模型推理服务【免费下载链接】rayRay is an AI compute engine. Ray consists of a core distributed runtime and a set of AI Libraries for accelerating ML workloads.项目地址https://gitcode.com/gh_mirrors/ra/ray点击查看免费下载Ray Tune 是 Ray 生态中面向大规模分布式超参数优化的核心库在训练脚本中只需在Tuner.fit()之前调用ray.init(address...)连接集群Tune 便会自动把每个 trial一次超参数组合的训练调度到集群各节点的 Ray Actor 上并行执行并自动感知各节点可用的 CPU/GPU 资源。本文基于 tune-distributed.rst 展开完整介绍如何从零搭建多节点集群、提交分布式 Tune 实验、配置持久化存储、在 Spot抢占式实例上低成本运行以及利用 checkpoint 与Tuner.restore实现 trial 级与实验级的故障容错。读完本文你将掌握一套可直接落地的分布式调参工作流。分布式实验概览三步完成一次多节点调参运行分布式 Tune 实验的整体流程非常简洁只需两步先启动 Ray 集群若尚未启动。集群搭建方式参见 Ray 集群入门既可以在多台本地机器上手动启动也可以借助ray up在云上拉起节点在 head 节点上运行调参脚本提交方式有三种可选直接在 head 节点上执行python tune_script.py使用ray submit将本地脚本上传到集群并远程执行使用 Ray Job Submission 提交作业。从 Tune 的内部实现看Tuner.fit()所在的 Python 进程是 driver 进程负责编排调度每个 trial 则由 driver 通过 Ray Actor 机制分发到集群中任意节点的独立进程执行参见 Tune 工作原理。因此分布式运行不需要修改任何训练逻辑唯一的关键区别是连接集群的方式。示例在 AWS VM 上启动分布式 Tune下面的配置演示了如何在 AWS 上使用 Deep Learning AMI 拉起一个 1 head 3 worker 的集群。保存为tune-default.yaml完整文件见 python/ray/tune/examples/tune-default.yamlcluster_name: tune-default provider: {type: aws, region: us-west-2} auth: {ssh_user: ubuntu} min_workers: 3 max_workers: 3 # Deep Learning AMI (Ubuntu) Version 21.0 available_node_types: head_node: node_config: {InstanceType: c5.xlarge, ImageId: ami-0b294f219d14e6a82} worker_nodes: node_config: {InstanceType: c5.xlarge, ImageId: ami-0b294f219d14e6a82} head_node_type: head_node setup_commands: # Set up each node. - pip install ray torch torchvision tensorboard关键配置项说明provider声明云提供商AWS与区域Ray Autoscaler 据此创建、管理实例min_workers/max_workers固定 worker 数量的上下界这里固定为 3 台 workeravailable_node_types定义节点类型head_node与worker_nodes可分别指定实例规格这里都用c5.xlarge与 AMI 镜像setup_commands每台节点启动时执行的初始化命令此处安装了ray、torch、torchvision与tensorboard保证集群环境一致。拉起集群并提交脚本ray up会根据该 YAML 在 AWS 上启动并初始化集群ray up tune-default.yamlray submit --start则更进一步按配置启动集群、把本地的tune_script.py上传到 head 节点并在远程执行python tune_script.py [args]ray submit tune-default.yaml tune_script.py --start -- --ray-addresslocalhost:6379上图展示了ray submit的完整数据流本地机器的tune_script.py被上传至 head 节点head 节点再将各 trial 的 Trainable 分发到 worker 节点上的 Ray Actor 中并行执行。用 TensorBoard 远程可视化结果实验运行后在本地机器上通过端口转发访问 head 节点上的 TensorBoard# Go to http://localhost:6006 to access TensorBoard. ray exec tune-default.yaml tensorboard --logdir~/ray_results/ --port 6006 --port-forward 6006ray exec会在 head 节点上执行给定的命令--port-forward把 head 节点的 6006 端口转发到本地浏览器访问http://localhost:6006即可看到各 trial 的指标曲线。结果默认写入~/ray_results目录你也可以通过Tuner的RunConfig(storage_path...)自定义结果目录并把 TensorBoard 指向该目录进行可视化。运行分布式 Tune 实验连接集群的关键代码运行分布式多节点实验的前提是 Ray 已经启动无论本地机器还是云端集群均可。跨机器运行时Tune 会自动检测每台机器上的 GPU 与 CPU 数量无需手动管理CUDA_VISIBLE_DEVICES。执行分布式实验的要点在Tuner.fit()之前调用ray.init(addressXXX)其中XXX是 Ray 的地址通常为localhost:6379。Tune 脚本只需在集群的 head 节点上执行。一个常见的做法是给脚本增加argparse参数让脚本在分布式与单机模式之间无缝切换import ray import argparse parser argparse.ArgumentParser() parser.add_argument(--address) args parser.parse_args() ray.init(addressargs.address) tuner tune.Tuner(...) tuner.fit()执行时在 head 节点上连接已有集群# On the head node, connect to an existing ray cluster $ python tune_script.py --ray-addresslocalhost:XXXX如果集群是通过ray up或ray submit --start启动的则用ray submit tune-default.yaml tune_script.py -- --ray-addresslocalhost:6379两个实用提示示例中常用的 Ray 地址是localhost:6379head 节点上的 GCS/Raylet 地址如果集群已经启动worker 节点上不需要运行任何额外命令脚本只在 head 节点执行即可。分布式运行中的存储选项在多节点实验里应优先使用cloud checkpointing云端检查点来降低同步开销只需在RunConfig中指定远程storage_path即可。其中my_trainable是用户自定义的 Tune Trainable即实现setup/step/save_checkpoint/load_checkpoint的对象或函数式 trainablefrom ray import tune from my_module import my_trainable tuner tune.Tuner( my_trainable, run_configtune.RunConfig( nameexperiment_name, storage_paths3://bucket-name/sub-path/, ) ) tuner.fit()所有实验产物会写入共享存储s3://bucket-name/sub-path/experiment_name。关于存储的更完整讲解参见 如何在 Ray Tune 中配置持久化存储要点如下云存储AWS S3 / GCS所有节点均可访问云端 bucket 时把结果与 checkpoint 直接上传到远端实例销毁后数据依然保留且 head 节点本地不保留全部结果若需处理最佳 checkpoint需先从云端拉取网络文件系统NFS如 AWS EFS / GCS Filestore把共享目录设为storage_path各节点直接写入同一目录单机本地文件系统默认写入~/ray_results下的自动命名子目录可用storage_path与name自定义多节点 仅本地文件系统已被弃用在多节点集群上若未配置 NFS 或云存储Tune 默认会报错避免 checkpoint 分散在不可共享的本地磁盘导致 trial 恢复失败。在 Spot抢占式实例上运行 Tune使用 Spot或抢占式实例可以显著降低实验成本。AWS 上的配置方式是在worker_nodes中增加InstanceMarketOptions# Provider-specific config for worker nodes, e.g. instance type. worker_nodes: InstanceType: m5.large ImageId: ami-0b294f219d14e6a82 # Deep Learning AMI (Ubuntu) Version 21.0 # Run workers on spot by default. Comment this out to use on-demand. InstanceMarketOptions: MarketType: spot SpotOptions: MaxPrice: 1.0 # Max Hourly PriceGCP 上的对应配置是scheduling.preemptibleworker_nodes: machineType: n1-standard-2 disks: - boot: true autoDelete: true type: PERSISTENT initializeParams: diskSizeGb: 50 # See https://cloud.google.com/compute/docs/images for more images sourceImage: projects/deeplearning-platform-release/global/images/family/tf-1-13-cpu # Run workers on preemtible instances. scheduling: - preemptible: true用 checkpoint 对抗节点抢占Spot 实例可能在 trial 运行中途被突然回收。Tune 缓解这一影响的核心手段是checkpointing只要 Trainable 实现了保存/加载 checkpointtrial 就能在节点被抢占后从最新检查点恢复训练。官方教程 python/ray/tune/tests/tutorial.py 中展示了一个带 checkpoint 的 Trainable 的推荐写法__trainable_run_begin__与__trainable_run_end__之间的代码search_space { lr: tune.sample_from(lambda spec: 10 ** (-10 * np.random.rand())), momentum: tune.uniform(0.1, 0.9), } tuner tune.Tuner( TrainMNIST, run_configtune.RunConfig(stop{training_iteration: 10}), param_spacesearch_space, ) results tuner.fit()其中TrainMNIST的完整实现见 python/ray/tune/examples/mnist_pytorch_trainable.py它继承tune.Trainable并实现了四个关键方法setup(config)初始化设备、数据加载器、模型与优化器学习率、动量从config中读取step()执行一个训练迭代并返回{mean_accuracy: acc}作为上报指标save_checkpoint(checkpoint_dir)把model.state_dict()保存为model.pthload_checkpoint(checkpoint_dir)从model.pth恢复模型权重。正是save_checkpoint/load_checkpoint的实现让 Tune 在节点故障后能够把 checkpoint 迁移到新节点并恢复训练。完整实操在 AWS Spot 实例上跑 MNIST假设 AWS 凭证已配置aws configure按以下步骤运行下载完整示例脚本 mnist_pytorch_trainable.py包含带 checkpoint 的 Trainable并安装依赖$ pip install ray torch torchvision filelock下载示例集群配置 tune-default.yaml用ray submit跨节点运行 Tune。集群未启动时追加--start希望运行结束后自动关停节点则追加--stopray submit tune-default.yaml mnist_pytorch_trainable.py --start -- --ray-addresslocalhost:6379可选在 AWS/GCP 上测试时等所有 worker 节点就绪后用下面的命令随机硬杀一个 worker 节点验证故障恢复$ ray kill-random-node tune-default.yaml --hard汇总成完整命令序列为wget https://raw.githubusercontent.com/ray-project/ray/master/python/ray/tune/examples/mnist_pytorch_trainable.py wget https://raw.githubusercontent.com/ray-project/ray/master/python/ray/tune/tune-default.yaml ray submit tune-default.yaml mnist_pytorch_trainable.py --start -- --ray-addresslocalhost:6379 # wait a while until after all nodes have started ray kill-random-node tune-default.yaml --hard注wget需联网从上游仓库获取文件若已在本仓库内可直接使用 python/ray/tune/examples/mnist_pytorch_trainable.py 与 python/ray/tune/examples/tune-default.yaml。你会观察到节点被硬杀后Tune 会在另一台 worker 节点上继续执行被中断的 trial。同时通过在RunConfig中指定storage_path...把结果上传到 S3 等云存储可以在集群自动启停后依然持久保留实验结果。Tune 运行的故障容错Trial 级恢复自动重启失败的 trial只要max_failures ! 0Tune 就会在 trial 失败时自动重启它单机与分布式场景均适用。具体行为如下假设某个节点被抢占或崩溃时其上的 trial 仍在执行若该 trial 存在 checkpoint分布式场景下需要配置了持久化存储以便访问 checkpointTune 会等待可用资源出现然后从 checkpoint 处继续执行若找不到 checkpointtrial 将从头开始训练若 trial 被调度到另一台节点Tune 会自动把之前的 checkpoint 文件推送到新节点并恢复 trial 的 Actor 状态从而从最新 checkpoint 无缝续跑。checkpoint 的详细机制可参考 Tune 的 trial checkpoint 指南 与 故障容错文档。实验级恢复Tuner.restore续跑整个实验Tune 会自动持久化整个实验一次Tuner.fit()会话的进度因此实验崩溃或被取消后可通过Tuner.restore恢复。结合云端存储的恢复示例出自 tune-storage.rstfrom ray import tune tuner tune.Tuner.restore( s3://my-checkpoints-bucket/path/my-tune-exp, trainablemy_trainable, resume_erroredTrue, ) tuner.fit()Tuner.restore提供resume_unfinished、resume_errored、restart_errored等恢复选项分别对应“仅续跑未完成 trial”“续跑含报错 trial”“重跑报错 trial”等语义。注意恢复时应使用云端存储中的实验目录 URI而非 head 节点的本地目录。常用 Tune 集群命令速查以下是分布式调参中最常用的命令完整命令列表见 Ray 集群 CLI 文档# Upload tune_experiment.py from your local machine onto the cluster. Then, # run python tune_experiment.py --addresslocalhost:6379 on the remote machine. $ ray submit CLUSTER.YAML tune_experiment.py -- --addresslocalhost:6379 # Start a cluster and run an experiment in a detached tmux session, # and shut down the cluster as soon as the experiment completes. # In tune_experiment.py, set RunConfig(storage_paths3://...) # to persist results $ ray submit CLUSTER.YAML --tmux --start --stop tune_experiment.py -- --addresslocalhost:6379 # To start or update your cluster: $ ray up CLUSTER.YAML [-y] # Shut-down all instances of your cluster: $ ray down CLUSTER.YAML [-y] # Run TensorBoard and forward the port to your own machine. $ ray exec CLUSTER.YAML tensorboard --logdir ~/ray_results/ --port 6006 --port-forward 6006 # Run Jupyter Lab and forward the port to your own machine. $ ray exec CLUSTER.YAML jupyter lab --port 6006 --port-forward 6006 # Get a summary of all the experiments and trials that have executed so far. $ ray exec CLUSTER.YAML tune ls ~/ray_results # Upload and sync file_mounts up to the cluster with this command. $ ray rsync-up CLUSTER.YAML # Download the results directory from your cluster head node to your local machine on ~/cluster_results. $ ray rsync-down CLUSTER.YAML ~/ray_results ~/cluster_results # Launching multiple clusters using the same configuration. $ ray up CLUSTER.YAML -ncluster1 $ ray up CLUSTER.YAML -ncluster2 $ ray up CLUSTER.YAML -ncluster3命令要点归纳ray submit上传脚本并远程执行--tmux让任务跑在分离的 tmux 会话中不受 SSH 断连影响--start/--stop控制集群启停ray up/ray down启动/更新与关停集群-y跳过确认-nname用同一配置启动多个命名集群ray exec ... --port-forward远程执行命令并做端口转发适合可视化场景ray rsync-up/rsync-down把本地文件含file_mounts配置的挂载同步到集群或把结果目录拉回本地tune ls快速列出~/ray_results下所有已执行实验与 trial 的摘要。故障排查程序卡住时如何快速恢复当你的程序偶尔出现卡死freeze时可以用以下命令重启 Ray 集群且不会重新执行任何安装命令$ ray up CLUSTER.YAML --restart-only--restart-only只重启集群中的 Ray 进程跳过setup_commands等安装步骤是排障时最快速的恢复手段配合前文介绍的持久化存储与Tuner.restore即使重启集群也能从云端 checkpoint 无缝续跑实验。小结把 Tune 从单机扩展到多节点集群核心改动其实只有一行ray.init(address...)集群由ray up拉起脚本由ray submit提交资源感知、trial 调度与失败重试全部由 Tune 自动完成。在此基础上用云存储或 NFS 统一 checkpoint 位置、用save_checkpoint/load_checkpoint抵御 Spot 实例抢占、用Tuner.restore兜底整个实验即可构建一套低成本、高可用、可随时中断续跑的分布式超参数调优流水线。赞分享人工智能分布式训练强化学习任务调度模型推理服务【免费下载链接】rayRay is an AI compute engine. Ray consists of a core distributed runtime and a set of AI Libraries for accelerating ML workloads.项目地址https://gitcode.com/gh_mirrors/ra/ray点击查看免费下载相关推荐攻克JAX分布式集群痛点多节点通信配置与故障恢复全指南攻克JAX分布式集群痛点多节点通信配置与故障恢复全指南 JAX作为一款强大的PythonNumPy程序转换工具支持自动微分、向量化和JIT编译到GPU/T机器学习深度学习Ray Train V2 与 Ray Tune 联合调优分布式超参数搜索的完整实战指南Ray Train V2 与 Ray Tune 联合调优分布式超参数搜索的完整实战指南 导读 本文基于 Ray 官方文档 hyperparameter opt人工智能分布式训练强化学习任务调度模型推理服务vLLM 多节点分布式推理故障排查跨节点 GPU 通信验证与 Ray 集群 IP 配置vLLM 多节点分布式推理故障排查跨节点 GPU 通信验证与 Ray 集群 IP 配置 本篇指南聚焦 vLLM 基于 Ray 的多节点multi node人工智能大模型模型推理服务推理引擎创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
返回列表