Merged
npu-affinity-sched: add hbs_npu_affinity scheduling strategy #65
npu-affinity-sched: add hbs_npu_affinity scheduling strategy #65
Merged
wangjian161Add on Aug 11
wangjian161
Aug 11

Summary
对于A5场景,每个npu存在对应numa亲和的概念,npu访问亲和numa性能最高,因此需要将与npu绑定的进程调度到npu请和的numa上
Related issue: https://gitcode.com/Ascend/msboost/issues/5

changes
新增npu-affinity隔离策略,将acl等关键线程绑定到npu亲和的numa 。同时支持npu_process_out_cluster 进程/线程布局模式
"npu_process_out_cluster":acl等关键线程按cluster隔离绑定在npu亲核的numa上,npu进程范围绑定在npu亲核的numa中的其它cluster上

Test

(base) [root@localhost tests]# python3 test_hbs_npu_affinity.py 
.    NPU affinity NUMA[0] has no available cluster for processes, fallback to sibling NUMA[1] in socket[0]
..    NPU affinity NUMA[0] has no available cluster, fallback to sibling NUMA[1] in socket[0]
.............
----------------------------------------------------------------------
Ran 16 tests in 0.012s

OK

Log

The affinity info after schedule is as follows:
--------------------------- Current Affinity Status ---------------------------
TaskGroup[0]: 
  - PROCESS[3139234]: name=VLLM::EngineCore_DP0, priority=NORMAL, socket=[0], numa=[1], cluster=[10-11], cpu=[80-95]
  - PROCESS[3144034]: name=None, priority=NORMAL, socket=[0], numa=[1], cluster=[10-11], cpu=[80-95]
  - PROCESS[3144036]: name=None, priority=NORMAL, socket=[0], numa=[1], cluster=[10-11], cpu=[80-95]
  - PROCESS[3144041]: name=None, priority=NORMAL, socket=[0], numa=[1], cluster=[10-11], cpu=[80-95]
  - PROCESS[3144045]: name=None, priority=NORMAL, socket=[0], numa=[1], cluster=[10-11], cpu=[80-95]
  - NPU_A5[4]: 
    - Thread[3148284](acl_thread): numa=[1], cluster=[6], cpu=[48]
    - Thread[3148285](release_thread): numa=[1], cluster=[6], cpu=[49]
    - Thread[3148237](RT_RECYCLE): numa=[1], cluster=[6], cpu=[50]
  - NPU_A5[5]: 
    - Thread[3149140](acl_thread): numa=[1], cluster=[7], cpu=[56]
    - Thread[3149141](release_thread): numa=[1], cluster=[7], cpu=[57]
    - Thread[3149107](RT_RECYCLE): numa=[1], cluster=[7], cpu=[58]
  - NPU_A5[2]: 
    - Thread[3148297](acl_thread): numa=[1], cluster=[8], cpu=[64]
    - Thread[3148298](release_thread): numa=[1], cluster=[8], cpu=[65]
    - Thread[3148245](RT_RECYCLE): numa=[1], cluster=[8], cpu=[66]
  - NPU_A5[3]: 
    - Thread[3148483](acl_thread): numa=[1], cluster=[9], cpu=[72]
    - Thread[3148484](release_thread): numa=[1], cluster=[9], cpu=[73]
    - Thread[3148431](RT_RECYCLE): numa=[1], cluster=[9], cpu=[74]
TaskGroup[1]: 
  - PROCESS[3139235]: name=VLLM::EngineCore_DP1, priority=NORMAL, socket=[1], numa=[3], cluster=[22-23], cpu=[176-191]
  - PROCESS[3144035]: name=None, priority=NORMAL, socket=[1], numa=[3], cluster=[22-23], cpu=[176-191]
  - PROCESS[3144037]: name=None, priority=NORMAL, socket=[1], numa=[3], cluster=[22-23], cpu=[176-191]
  - PROCESS[3144039]: name=None, priority=NORMAL, socket=[1], numa=[3], cluster=[22-23], cpu=[176-191]
  - PROCESS[3144044]: name=None, priority=NORMAL, socket=[1], numa=[3], cluster=[22-23], cpu=[176-191]
  - NPU_A5[0]: 
    - Thread[3148280](acl_thread): numa=[3], cluster=[18], cpu=[144]
    - Thread[3148281](release_thread): numa=[3], cluster=[18], cpu=[145]
    - Thread[3148231](RT_RECYCLE): numa=[3], cluster=[18], cpu=[146]
  - NPU_A5[1]: 
    - Thread[3148381](acl_thread): numa=[3], cluster=[19], cpu=[152]
    - Thread[3148382](release_thread): numa=[3], cluster=[19], cpu=[153]
    - Thread[3148347](RT_RECYCLE): numa=[3], cluster=[19], cpu=[154]
  - NPU_A5[6]: 
    - Thread[3148240](acl_thread): numa=[3], cluster=[20], cpu=[160]
    - Thread[3148241](release_thread): numa=[3], cluster=[20], cpu=[161]
    - Thread[3148225](RT_RECYCLE): numa=[3], cluster=[20], cpu=[162]
  - NPU_A5[7]: 
    - Thread[3148351](acl_thread): numa=[3], cluster=[21], cpu=[168]
    - Thread[3148352](release_thread): numa=[3], cluster=[21], cpu=[169]
    - Thread[3148311](RT_RECYCLE): numa=[3], cluster=[21], cpu=[170]

Affinity schedule SUCCESS!
---------------------------- Affinity Schedule End ----------------------------
likedislike
Pull Request has been successfully merged by ascend-robot.
(Thank you for wangjian161's contribution.)
wangjian161wangjian161
Aug 11 Associated issue: [Feature]: 支持npu亲和调度
atomgit-bot
atomgit-bot
Aug 11 Comments:

变更摘要

此 PR 新增了 npu_affinity NPU 亲和调度策略,面向 A5 场景:将 ACL 等关键线程按 cluster 隔离绑定到 NPU 亲和的 NUMA 上,同时将 NPU 进程及其他线程范围绑定到同 NUMA 的剩余 cluster。核心实现为新增的 HbsNpuAffinity 调度器类,通过 npu-smi info -t topo 和 /sys/devices/system/node/ 获取 NPU→NUMA 亲和拓扑,支持 npu_process_out_cluster 布局模式及 cluster 分配降级(亲和 NUMA 无可用 cluster 时回退到同 socket 兄弟 NUMA)。同时扩展了配置、API、调度器工厂和工具函数以支持该策略。

主要改动

  • 新增 HbsNpuAffinity 调度器:在 hbs_npu_affinity.py 中实现完整的 NPU 亲和调度逻辑,包括 NPU 拓扑初始化、亲和组约束校验、背景任务 cluster 预留(每 socket 取非 NPU 亲和 NUMA 的 20% cluster)、以及按 npu_process_out_cluster 布局为高优先级线程分配独占 cluster、为进程/线程分配共享 cluster(含降级策略)。
  • 配置与 API 扩展:AffinityConfig 新增 layout 字段和 set_isolate_strategy 方法;api.py 的 set_isolate_strategy 接口新增 layout 参数,支持 npu_affinity 隔离策略和 npu_process_out_cluster 布局策略的传递。
  • 调度器工厂新增 NPU 亲和决策:scheduler_factory.py 新增 decide_on_npu_affinity 函数,仅在 A5 设备上返回 HBS_NPU_AFFINITY 调度器;decide_scheduler 中 NPU 亲和策略优先级高于 cluster/numa/auto。
  • 新增 get_npu_topo_numa_affinity 工具函数:在 utils.py 中通过 get_npu_topo_cpu_affinity 获取 NPU 亲和 CPU 列表,再结合 /sys/devices/system/node/*/cpulist 建立 NPU→NUMA 映射,要求每个 NPU 恰好亲和到一个 NUMA。
  • 新增单元测试:test_hbs_npu_affinity.py 覆盖配置行为、调度器决策、cluster 降级分配、以及 _assign_clusters_cpus_to_tasks 和 _assign_clusters_for_task_group 在降级场景下 task/group 的 NUMA 元数据正确性。
likedislike
Incorrect?
atomgit-bot
atomgit-bot
Aug 11 Comments:

代码审查

我的审查完成。以下是各文件的审查结论汇总:


审查结果汇总

优先级 数量
P2 1
P3 2

各文件审查结论:

  • affinity-sched/affinity_sched/api.py — P3:layout 默认值使用硬编码字符串,与 config.py 常量存在分歧风险
  • affinity-sched/affinity_sched/config.py — 无问题
  • affinity-sched/affinity_sched/strategy/__init__.py — 无问题(仅新增 import 和 __all__ 条目)
  • affinity-sched/affinity_sched/strategy/hbs_npu_affinity.py — P3:_get_numa_of_cluster 返回 -1 可静默传播到 numa 数组
  • affinity-sched/affinity_sched/strategy/scheduler_factory.py — 无问题
  • affinity-sched/affinity_sched/utils.py — P2:单个 NUMA 节点 cpulist 读取异常被静默吞掉
  • affinity-sched/tests/test_hbs_npu_affinity.py — 无问题

整体风险判断:低。 核心调度逻辑正确,降级策略、NUMA 元数据一致性、约束校验等关键路径均经过仔细测试覆盖。发现的问题均为防御性编程缺口(静默吞异常、哨兵值传播)和可维护性细节(硬编码字符串),不影响正常功能流程。

类型 数量
🔴 阻塞 0
🟡 建议 1

💬 仅评论

likedislike
Incorrect?
ascend-robotascend-robotCollaborators
Aug 11 Added label: ascend-cla/yes
ascend-robot
ascend-robotCollaborators
Aug 11 Comments:

Thanks for your pull-request.
The full list of commands accepted by me can be found at here。
You can get sig-info at here


PR Approval Progress

✅ Congratulations! All modules have met the lgtm and approve requirements.

Module Approval Details

module lgtm status approve status
repo-Ascend/msboost ✅ baij77, xnm-s83u (2/2) ✅ baij77 (1/1)

💡 Tip:

  • Committer can comment /approve or /lgtm
  • Commenting /approve implies both code review (lgtm) and intent to merge (approve)

CLA Signature Pass

wangjian161, thanks for your pull request. All authors of the commits have signed the CLA. 👍

likedislike
234 messages are collapsed here。 See more
ascend-robot
ascend-robotCollaborators
Aug 17 Comments:

The MR can not be merged, because of CodeReview discussion not resolved

If you want to solve this problem, you can click here to do it in the FAQs.

likedislike
baijbaijCollaborators
Aug 17 Resolved final issue
ascend-robotascend-robotCollaborators
Aug 17 Closed associated issue
ascend-robotascend-robotCollaborators
Aug 17 Merged pull request, merge node SHA: c184c0fb010c1b9c1bda2660c11e40f00c774232
ascend-robot
ascend-robotCollaborators
Aug 17 Comments:

Something went wrong during merge: Only fast-forward merge is allowed for your project. Please update your source branch

If you want to solve this problem, you can click here to do it in the FAQs.

likedislike