Closed
[Feature]: Ascend950 API “hasPrimaryContext”对齐CUDA #2073
wuyouqi1Add on  May 22Closed at  Jun 1
wuyouqi1
wuyouqi1Collaborators
May 22 Create

💻 需求背景、当前现状、期望实现的功能内容、具体的设计方案、以及测试方案

需求背景

A5(Ascend950)关键 API 需与 GPU(CUDA)行为对齐。hasPrimaryContext 是其中之一。

当前现状

路径 实现 问题
c10_npu::hasPrimaryContext(自由函数) acl::AclrtGetPrimaryCtxState 直调 性能开销大
NPUHooksInterface::hasPrimaryContext(hooks override) c10_npu::isDeviceCtxActive 本地缓存查询 仅感知本进程创建的 context,与 GPU 语义不一致
CUDA c10::cuda::hasPrimaryContext driver API 查询真实状态 准确

此外,CUDA 内部函数(MaybeSetDevice、MaybeExchangeDevice、currentStreamCaptureStatus、CachingHostAllocator)均直接使用 hasPrimaryContext,而 NPU 侧对应位置使用了 isDeviceCtxActive(缓存)、IsContextInitialized(thread-local)、GetLocalDevice() + SetCurrentDevice() 等替代方案,均未接入 CheckCompatibleImpl() 门控,导致 Compatible 模式下行为不一致。

设计方案

引入 TORCH_NPU_USE_COMPATIBLE_IMPL 环境变量门控,覆盖所有 hasPrimaryContext 相关路径:

  • isDeviceCtxActive() — 内部加 CheckCompatibleImpl() 门控,Compatible=1 时走 hasPrimaryContext(ACL)
  • NPUGraphsUtils.h:currentStreamCaptureStatus() — 加 CheckCompatibleImpl() 分支,Compatible=1 走 isDeviceCtxActive(current_device()),Compatible=0 保留原始 IsContextInitialized()
  • 新增 c10_npu::getDeviceIndexWithPrimaryContext() — 对齐 CUDA c10::cuda::getDeviceIndexWithPrimaryContext()
  • CachingHostAllocator — allocate_host_memory 和 CachingHostAllocator_isPinned 加 CheckCompatibleImpl() 分支,Compatible=1 走 getDeviceIndexWithPrimaryContext() → OptionalDeviceGuard,Compatible=0 保留原始 GetLocalDevice() < 0 → SetCurrentDevice()

具体对照:

CUDA (pytorch) NPU (修改前) NPU (修改后)
MaybeSetDevice: hasPrimaryContext(device) isDeviceCtxActive(device) isDeviceCtxActive 加门控
MaybeExchangeDevice: hasPrimaryContext(to_device) isDeviceCtxActive(to_device) 同上(连锁受益)
currentStreamCaptureStatus: hasPrimaryContext(current_device()) IsContextInitialized() Compatible=1: isDeviceCtxActive, Compatible=0: IsContextInitialized
getDeviceIndexWithPrimaryContext() 缺失 新增
CachingHostAllocator: getDeviceIndexWithPrimaryContext() GetLocalDevice() < 0 → SetCurrentDevice() Compatible=1: getDeviceIndexWithPrimaryContext, Compatible=0: GetLocalDevice
isPinnedPtr: getDeviceIndexWithPrimaryContext() GetLocalDevice() < 0 → SetCurrentDevice() 同上

修改文件:

  • torch_npu/csrc/core/npu/NPUHooksInterface.cpp — hooks 路径简化为直接调用 isDeviceCtxActive
  • torch_npu/csrc/npu/Module.cpp — Python 绑定同上简化
  • torch_npu/csrc/core/npu/NPUFunctions.cpp — isDeviceCtxActive 门控 + 新增 getDeviceIndexWithPrimaryContext
  • torch_npu/csrc/core/npu/NPUFunctions.h — 新增声明
  • torch_npu/csrc/core/npu/NPUGraphsUtils.h — currentStreamCaptureStatus 加 CheckCompatibleImpl 分支,兼容态走 IsContextInitialized
  • torch_npu/csrc/core/npu/CachingHostAllocator.cpp — allocate_host_memory 加 CheckCompatibleImpl 分支,兼容态走 GetLocalDevice
  • torch_npu/csrc/core/npu/CachingHostAllocator.h — CachingHostAllocator_isPinned 同上
  • test/distributed/test_npu_primary_ctx.py(新增)— 测试用例

测试方案

test/distributed/test_npu_primary_ctx.py 参照 test_cuda_primary_ctx.py 逐行对齐:

  • 基类用 unittest.TestCase(避免 NPU TestCase.setUpClass 调 set_npu_device() 预创建 context)
  • setUp 遍历全部 device,断言无预创建 primary context
  • test_str_repr / test_copy / test_pin_memory(含 6 个子场景)
  • 需全新进程隔离(--subprocess)
  • TORCH_NPU_USE_COMPATIBLE_IMPL=1 下相同 3 用例验证 ACL 路径

替代方案

曾考虑纯 SoC 版本判断(GetSocVersion() >= Ascend950),不采用原因:无法通过环境变量关闭回退,线上出问题难以快速恢复。

补充说明

  • 详细设计见 HPC/A5_API_consistency_hasPrimaryContext.md
  • 分支:feat/hasPrimaryContext-api-consistency,目标分支 v2.7.1
likedislike
wuyouqi1wuyouqi1Collaborators
May 22 Added label: feature
wuyouqi1wuyouqi1Collaborators
May 22 Assign wuyouqi1 as responsible person
wuyouqi1wuyouqi1Collaborators
May 22 changed custom category from Bug-Report to 需求
wuyouqi1wuyouqi1Collaborators
May 22 Change title to "[Feature]: Ascend950 API", originally titled "[Feature]: API".
wuyouqi1wuyouqi1Collaborators
May 22 Change title to "[Feature]: Ascend950 API “hasPrimaryContext”对齐CUDA", originally titled "[Feature]: Ascend950 API".
wuyouqi1wuyouqi1Collaborators
May 22 Modify description of issue
wuyouqi1wuyouqi1Collaborators
May 23 Modify description of issue
wuyouqi1wuyouqi1Collaborators
May 23 Modify description of issue
wuyouqi1wuyouqi1Collaborators
May 27 Associated pull request
wuyouqi1wuyouqi1Collaborators
May 27 Associated pull request
wuyouqi1wuyouqi1Collaborators
May 27 Associated pull request
wuyouqi1wuyouqi1Collaborators
May 27 Associated pull request
wuyouqi1wuyouqi1Collaborators
May 27 Associated pull request
wuyouqi1wuyouqi1Collaborators
Jun 1 changed custom state from TODO to DONE
wuyouqi1wuyouqi1Collaborators
Jun 1 Close issue