Ray Adapter调度策略详解:PlacementGroup与NodeAffinity实战
【免费下载链接】ray-adapterCompatible with the core interfaces of the open-source software Ray, it facilitates the seamless migration of workloads running on Ray (such as vllm/verl, etc.) to the Yuanrong cluster, while also enjoying the performance advantages brought by Yuanrong's deep optimization on Huawei Kunpeng and Ascend hardware.项目地址: https://gitcode.com/openeuler/ray-adapter
前往项目官网免费下载:https://ar.openeuler.org/ar/
Ray Adapter作为openEuler社区推出的高性能计算框架适配工具,通过兼容开源软件Ray的核心接口,帮助用户将vllm/verl等基于Ray的工作负载无缝迁移至元戎集群,并充分发挥华为鲲鹏与昇腾硬件的深度优化性能。本文将深入解析其两大核心调度策略——PlacementGroup与NodeAffinity,带您掌握任务调度的实战技巧。
一、PlacementGroup:资源打包的终极调度方案 📦
1.1 什么是PlacementGroup?
PlacementGroup是Ray Adapter中用于资源分配的高级抽象,通过将任务所需的计算资源(CPU、内存、NPU等)打包成"资源束(Bundle)",实现任务间的资源亲和性管理。其核心优势在于:
- 支持多种调度策略(PACK/SPREAD/STRICT_PACK/STRICT_SPREAD)
- 精确控制资源分配粒度
- 优化跨节点通信开销
1.2 四种调度策略深度对比
| 策略类型 | 适用场景 | 核心特点 | 实现代码 |
|---|---|---|---|
| PACK | 密集型任务 | 所有资源束集中在单个节点 | ray_adapter/util/placement_group.py#L28 |
| SPREAD | 容错性任务 | 资源束均匀分布在不同节点 | ray_adapter/util/placement_group.py#L29 |
| STRICT_PACK | 强资源亲和性 | 必须在单个节点满足所有资源 | ray_adapter/util/placement_group.py#L30 |
| STRICT_SPREAD | 强隔离需求 | 每个资源束必须在独立节点 | ray_adapter/util/placement_group.py#L31 |
1.3 实战:创建高性能PlacementGroup
创建PlacementGroup的标准流程包含资源定义、策略选择和实例化三个步骤:
from ray_adapter.util.placement_group import placement_group # 定义资源束:2个CPU束 + 1个NPU束 bundles = [ {"CPU": 2}, # 2核CPU {"CPU": 2}, # 2核CPU {"NPU": 1} # 1张NPU卡 ] # 创建PlacementGroup实例(采用PACK策略) pg = placement_group( bundles=bundles, strategy="PACK", name="npu_inference_group" ) # 等待资源分配完成 pg.wait(timeout_seconds=60)注意:资源单位转换已在底层自动处理,如CPU以毫核为单位(1 CPU = 1000毫核),NPU资源会自动转换为
NPU/.+/count格式。
1.4 高级操作:PlacementGroup管理
Ray Adapter提供完整的生命周期管理接口:
# 获取当前PlacementGroup current_pg = get_current_placement_group() # 查询PlacementGroup信息 pg_info = placement_group_table(current_pg) # 删除PlacementGroup remove_placement_group(pg)二、NodeAffinity:节点亲和性的精准控制 🎯
2.1 NodeAffinity的核心价值
NodeAffinity调度策略允许用户指定任务与特定节点的亲和性关系,适用于:
- 利用特定硬件特性(如昇腾NPU)
- 满足数据本地性需求
- 实现负载均衡或故障隔离
2.2 硬亲和与软亲和
NodeAffinity支持两种亲和性模式:
- 硬亲和(soft=False):任务必须调度到指定节点,否则失败
- 软亲和(soft=True):优先调度到指定节点,失败时可降级到其他节点
2.3 实战:指定NPU节点运行任务
from ray_adapter.util.scheduling_strategies import NodeAffinitySchedulingStrategy # 获取目标节点ID(可通过集群管理工具查询) target_node_id = "node-10-0-1-23" # 创建带节点亲和性的Actor actor = MyModelActor.options( scheduling_strategy=NodeAffinitySchedulingStrategy( node_id=target_node_id, soft=False # 硬亲和:必须在目标节点运行 ) ).remote()三、混合调度:PlacementGroup + NodeAffinity
在复杂场景下,可组合使用两种策略实现更精细的调度控制:
# 1. 创建带SPREAD策略的PlacementGroup pg = placement_group(bundles=[{"CPU": 4}, {"NPU": 1}], strategy="SPREAD") # 2. 在指定节点上使用该PlacementGroup actor = MyActor.options( scheduling_strategy=PlacementGroupSchedulingStrategy( placement_group=pg, placement_group_bundle_index=1 # 使用第2个资源束 ) ).remote()四、最佳实践与常见问题
4.1 性能优化建议
- 资源束设计:将通信密集型任务放入同一PlacementGroup的不同束
- 节点选择:通过NodeAffinity将大内存任务调度到高配置节点
- 策略组合:优先使用PlacementGroup控制资源分配,NodeAffinity处理特殊硬件需求
4.2 常见错误排查
- 资源分配失败:检查是否超过集群总资源,可尝试降低
CPU/NPU请求量 - 节点亲和性不生效:确认节点ID是否正确,硬亲和模式下检查目标节点状态
- 策略冲突:避免同时设置多个调度策略,优先级为:NodeAffinity > PlacementGroup
五、快速上手
通过以下步骤开始使用Ray Adapter调度策略:
- 克隆仓库:
git clone https://gitcode.com/openeuler/ray-adapter - 安装依赖:
pip install -r requirements.txt - 查看完整示例:ray_adapter/tests/test_placement_group.py
- API文档:ray_adapter/util/placement_group.py
通过灵活运用PlacementGroup和NodeAffinity调度策略,您可以充分发挥元戎集群的硬件优势,实现AI训练与推理任务的性能最大化。立即尝试,体验高效资源调度带来的计算加速吧!🚀
【免费下载链接】ray-adapterCompatible with the core interfaces of the open-source software Ray, it facilitates the seamless migration of workloads running on Ray (such as vllm/verl, etc.) to the Yuanrong cluster, while also enjoying the performance advantages brought by Yuanrong's deep optimization on Huawei Kunpeng and Ascend hardware.项目地址: https://gitcode.com/openeuler/ray-adapter
创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考