已合并
【feat】NPUAffinityController support multiple rangs of affinity cpu core #34959
zhaoyu65创建于 5月7日
【feat】NPUAffinityController support multiple rangs of affinity cpu core #34959
已合并
Pull Request已成功合入, 合并人@ascend-robot
(感谢 zhaoyu65 的贡献)5月7日 关联了issue:[Feature]: 【运行时】NPU亲和性绑核支持绑定多个CPU范围
zhaoyu65
5月7日 评论:
5月7日 评论:
compile


ascend-robot
5月7日 评论:
5月7日 评论:
ascend-robot
5月7日 评论:
5月7日 评论:
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/pytorch | ✅ liujunzhu, hbhu_bin (2/2) | ✅ liujunzhu (1/1) |
| test | ✅ liujunzhu, hbhu_bin (2/2) | ✅ liujunzhu (1/1) |
| torch_npu/utils | ✅ liujunzhu, hbhu_bin (2/2) | ✅ liujunzhu (1/1) |
💡 Tip:
- Committer can comment
/approveor/lgtm- Commenting
/approveimplies both code review (lgtm) and intent to merge (approve)
CLA Signature Pass
zhaoyu65, thanks for your pull request. All authors of the commits have signed the CLA. 👍


此处折叠了152条消息 查看更多
5月11日 添加了label:approved
5月11日 添加了label:lgtm
ascend-robot
5月11日 评论:
5月11日 评论:
5月11日 合入了pull request
【合入来源】
【修改方案】
1、修改内容总体
本次 commit 的核心目的是让 NPUAffinityController 支持绑定多个不连续的 CPU 核范围。同时修改了lintrunner工具扫描出来的代码规范问题。
主要技术变更:
CoreIdRange {start, end}改为CoreIdList = std::set<CoreId>,支持离散/多段 CPU 核集合,set容器符合语义,不存在重复值2、各文件修改的内容和目的
CoreIdRange结构体替换为CoreIdList = std::set<CoreId>,新增SetThreadAffinity(const CoreIdList)接口npu0:0-1,npu0:4-9多段格式;新增formatCoreRange()输出函数;重构parseDeviceCoreRange()解析多段范围parseAffinityCPU()改为parseAffinityCores(),支持逗号分隔的多段范围;重构GetExclusiveAffinityCPU()的核分配逻辑GetAffinityCores(int card_id)接口PyList参数的解析,支持python内部接口侧传入[0, 1, 4, 5]格式的核列表_set_thread_affinity()支持List[int]或List[List[int]]参数,如[[0,5], [8,10]]Union[List[int], List[List[int]], NoneType]3、修改后的影响
功能增强
CPU_AFFINITY_CONF='1,npu0:0-3,npu0:8-15',同一设备可绑定不连续的核,取并集set_thread_affinity()新增接受[[0,5], [8,10]]格式参数的功能行为变化
CPU_AFFINITY_CONF同一设备多次指定核范围会合并(如npu0:0-1,npu0:4-9绑定核 0,1,4,5,6,7,8,9)代码改进
formatCoreRange()函数统一输出格式(如0-3,8-15)parseNpuAffineMode()、parseLazyBindMode()、parseForceMode()、parseDeviceCoreRange()【资料变更】
在后续2.7.1分支的PR中修改
【接口变更】
CPU_AFFINITY_CONF环境变量和set_thread_affinity()接口变更方案已通过SEG例会评审,兼容旧版本;
【功能验证】
自测OK

验证环境变量CPU_AFFINITY_CONF
#!/bin/bash # 设置环境变量 export ASCEND_PROCESS_LOG_PATH=$(pwd) export ASCEND_GLOBAL_LOG_LEVEL=0 echo -e 'import torch\na=torch.tensor([1],device="npu")\nb=a+1\n' > add.py # 定义配置和期望值数组 configs=( "1,npu_affine:1,npu0:0-5" "1,npu_affine:1,npu0:0-5,npu1:6-7,npu2:8-8,npu2:10-11,npu0:12-19" "1,npu_affine:1,npu0:0-5,npu1:10-13,npu0:4-7" ) values=( "0-5" "0-5,12-19" "0-7" ) run_test() { local index=$1 local conf=$2 local expected_val=$3 export CPU_AFFINITY_CONF="$conf" # 清理旧日志,确保获取的是本次运行的日志 rm -rf debug/ # 执行测试脚本 python add.py # 检查 python 执行是否成功,可选 if [ $? -ne 0 ]; then echo "[ERROR] Index $index: python add.py failed." >> result.log return 1 fi # 提取 affinity 值 # 使用 find 避免 glob 匹配不到文件时的报错,或者保留原来的 glob 但确保目录存在 local log_file log_file=$(find debug/plog -name "*.log" 2>/dev/null | head -1) if [ -z "$log_file" ]; then echo "[ERROR] Index $index: No log file found in debug/plog/" >> result.log return 1 fi local affinity_line affinity_line=$(grep "Device 0 set acl_thread affinity to" "$log_file" 2>/dev/null | head -1) if [ -z "$affinity_line" ]; then echo "[ERROR] Index $index: Affinity line not found in log." >> result.log return 1 fi # 提取 "affinity to" 后面的具体值 local affinity_value # sed 提取最后一个非空字段,或者根据具体日志格式调整 affinity_value=$(echo "$affinity_line" | sed -n 's/.*affinity to \([^ ]*\).*/\1/p') # 去除可能的首尾空白字符 (trim) affinity_value=$(echo "$affinity_value" | xargs) # 比较实际值与期望值 if [ "$affinity_value" == "$expected_val" ]; then echo "测试环境变量 CPU_AFFINITY_CONF $conf: 的结果为:SUCCESS (Actual: ${affinity_value} == Expected: ${expected_val})" else echo "测试环境变量 CPU_AFFINITY_CONF $conf: 的结果为:FAIL (Actual: ${affinity_value} != Expected: ${expected_val})" fi } # 循环遍历配置 # 使用 C 风格的 for 循环以便同时获取索引 for ((i=0; i<${#configs[@]}; i++)); do run_test "$i" "${configs[$i]}" "${values[$i]}" done echo "All tests completed."测试结果为:所有用例都通过

【CheckList】