Closed
[Feature]: Ascend950 API “hasPrimaryContext”对齐CUDA #2073
wuyouqi1Add on May 22Closed at Jun 1
May 22 Added label: feature
May 22 Assign wuyouqi1 as responsible person
May 22 changed custom category from Bug-Report to 需求
May 22 Change title to "[Feature]: Ascend950 API", originally titled "[Feature]: API".
May 22 Change title to "[Feature]: Ascend950 API “hasPrimaryContext”对齐CUDA", originally titled "[Feature]: Ascend950 API".
May 22 Change title to "[Feature]: Ascend950 API “hasPrimaryContext”对齐CUDA", originally titled "[Feature]: Ascend950 API".
May 22 Modify description of issue
May 23 Modify description of issue
May 23 Modify description of issue
May 27 Associated pull request
May 27 Associated pull request
May 27 Associated pull request
May 27 Associated pull request
May 27 Associated pull request
Jun 1 changed custom state from TODO to DONE
Jun 1 Close issue
💻 需求背景、当前现状、期望实现的功能内容、具体的设计方案、以及测试方案
需求背景
A5(Ascend950)关键 API 需与 GPU(CUDA)行为对齐。
hasPrimaryContext是其中之一。当前现状
c10_npu::hasPrimaryContext(自由函数)acl::AclrtGetPrimaryCtxState直调NPUHooksInterface::hasPrimaryContext(hooks override)c10_npu::isDeviceCtxActive本地缓存查询c10::cuda::hasPrimaryContext此外,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()— 对齐 CUDAc10::cuda::getDeviceIndexWithPrimaryContext()CachingHostAllocator—allocate_host_memory和CachingHostAllocator_isPinned加CheckCompatibleImpl()分支,Compatible=1 走getDeviceIndexWithPrimaryContext() → OptionalDeviceGuard,Compatible=0 保留原始GetLocalDevice() < 0 → SetCurrentDevice()具体对照:
MaybeSetDevice: hasPrimaryContext(device)isDeviceCtxActive(device)isDeviceCtxActive加门控MaybeExchangeDevice: hasPrimaryContext(to_device)isDeviceCtxActive(to_device)currentStreamCaptureStatus: hasPrimaryContext(current_device())IsContextInitialized()isDeviceCtxActive, Compatible=0:IsContextInitializedgetDeviceIndexWithPrimaryContext()CachingHostAllocator: getDeviceIndexWithPrimaryContext()GetLocalDevice() < 0 → SetCurrentDevice()getDeviceIndexWithPrimaryContext, Compatible=0:GetLocalDeviceisPinnedPtr: getDeviceIndexWithPrimaryContext()GetLocalDevice() < 0 → SetCurrentDevice()修改文件:
torch_npu/csrc/core/npu/NPUHooksInterface.cpp— hooks 路径简化为直接调用isDeviceCtxActivetorch_npu/csrc/npu/Module.cpp— Python 绑定同上简化torch_npu/csrc/core/npu/NPUFunctions.cpp—isDeviceCtxActive门控 + 新增getDeviceIndexWithPrimaryContexttorch_npu/csrc/core/npu/NPUFunctions.h— 新增声明torch_npu/csrc/core/npu/NPUGraphsUtils.h—currentStreamCaptureStatus加 CheckCompatibleImpl 分支,兼容态走 IsContextInitializedtorch_npu/csrc/core/npu/CachingHostAllocator.cpp—allocate_host_memory加 CheckCompatibleImpl 分支,兼容态走 GetLocalDevicetorch_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(避免 NPUTestCase.setUpClass调set_npu_device()预创建 context)setUp遍历全部 device,断言无预创建 primary contexttest_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.mdfeat/hasPrimaryContext-api-consistency,目标分支v2.7.1