Pull Request已成功合入, 合并人@ascend-robot
(感谢 huangyunlong 的贡献)变更摘要
此 PR 将 NPU 填充未初始化内存的控制方式从基于选项字符串(TORCH_NPU_FILL_UNINITIALIZED_MEMORY option)的间接机制,重构为通过 NpuContext 类和直接 Python 绑定(torch_npu._C._npu_set_fill_uninitialized_memory / _npu_get_fill_uninitialized_memory)进行管理。同时新增了全局单例 globalNpuContext() 来统一访问状态,并修复了测试中 only_for 参数的类型错误。
主要改动
- 新增
NpuContext类与全局单例:在EnvVariables.h/.cpp中引入NpuContext类,封装_npu_fill_uninitialized_memory布尔成员及其 getter/setter,并通过globalNpuContext()提供全局单例访问,替代原有的CheckFillUninitializedMemory()自由函数和REGISTER_OPTION_BOOL_FUNCTION宏注册方式。 - C++ 调用点统一替换:
ResizeNpu.cpp和TensorFactories.cpp中,原先调用at_npu::native::env::CheckFillUninitializedMemory()的位置全部改为at_npu::native::env::globalNpuContext().npuFillUninitializedMemory()。 - Python 层新增直接绑定:在
Module.cpp中注册了_npu_set_fill_uninitialized_memory和_npu_get_fill_uninitialized_memory两个 Python C 扩展方法,直接操作NpuContext状态,不再经过_npu_setOption的字符串键值对方式。 - Python 工具层适配:
_deterministic.py中的_add_deterministic_patch()从原先调用torch_npu._C._npu_setOption({"TORCH_NPU_FILL_UNINITIALIZED_MEMORY": "1"/"0"})改为直接调用torch_npu._C._npu_set_fill_uninitialized_memory(mode)。 - 测试参数修复:
test_torch_fill_uninitialized_memory.py中instantiate_device_type_tests的only_for参数从字符串'privateuse1'修正为元组('privateuse1',),以匹配正确的参数类型。


代码审查
审查总结
逐文件审查结果:
| 文件 | 审查结果 |
|---|---|
test/npu/test_torch_fill_uninitialized_memory.py |
✅ 无问题 — only_for 从字符串改为元组,两者均被 instantiate_device_type_tests 接受 |
torch_npu/csrc/aten/common/ResizeNpu.cpp |
✅ 无问题 — 新增 include,调用方式从 CheckFillUninitializedMemory() 改为 globalNpuContext().npuFillUninitializedMemory() |
torch_npu/csrc/aten/common/TensorFactories.cpp |
✅ 无问题 — 同上 |
torch_npu/csrc/framework/interface/EnvVariables.cpp |
✅ 无问题 — 移除旧的 option 注册代码,新增 NpuContext 实现和 globalNpuContext(),旧引用已全部清理 |
torch_npu/csrc/framework/interface/EnvVariables.h |
✅ 无问题 — 移除 CheckFillUninitializedMemory() 声明,新增 NpuContext 类和 globalNpuContext() 声明 |
torch_npu/csrc/npu/Module.cpp |
⚠️ 1 个问题(P3)— 新增的 getter 缺少 HANDLE_TH_ERRORS |
torch_npu/utils/_deterministic.py |
✅ 无问题 — 从基于字符串的环境变量方案简化为直接调用 boolean setter |
发现统计
- P0: 0
- P1: 0
- P2: 0
- P3: 1
整体风险评估
低风险。 核心改动(将 fill_uninitialized_memory 控制从环境变量 option 系统迁移到 NpuContext 单例)实现正确,所有旧引用已彻底清理,调用链完整。唯一发现的是新 getter 函数缺少异常保护宏,属于防御性编码一致性问题,不影响当前功能的正确运行。
| 类型 | 数量 |
|---|---|
| 🔴 阻塞 | 0 |
| 🟡 建议 | 1 |
💬 仅评论


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 | ✅ li_jing_hw, liangsongwei (2/2) | ✅ li_jing_hw, liangsongwei (2/1) |
| test | ✅ liangsongwei, li_jing_hw (2/2) | ✅ liangsongwei, li_jing_hw (2/1) |
| torch_npu/utils | ✅ liangsongwei, li_jing_hw (2/2) | ✅ liangsongwei, li_jing_hw (2/1) |
💡 Tip:
- Committer can comment
/approveor/lgtm- Commenting
/approveimplies both code review (lgtm) and intent to merge (approve)
CLA Signature Pass
huangyunlong2022, thanks for your pull request. All authors of the commits have signed the CLA. 👍


当前仓库存在以下 保护分支 :
| Protected Branch | Version | Release |
|---|---|---|
| master | ||
| v2.7.1 | ||
| v2.12.0 | ||
| v2.9.0 | ||
| v2.11.0 | ||
| v2.10.0 | ||
| v2.7.1-26.1.0 | ||
| v2.9.0-26.1.0 | ||
| v2.11.0-26.1.0 | ||
| v2.10.0-26.1.0 | ||
| v2.12.0-26.1.0 | ||
| ci-test |
评论 /sync <branch1> <branch2> ... 可将当前 PR 修改同步到其它分支(创建同步 PR):
a) 如果当前 PR 是 Open 状态,同步操作将延迟到 PR 被合并时执行
b) 如果当前 PR 已经 Merged,将立即执行同步操作
注意:
- /sync 命令可以指定同步到多个分支,仅最后一个 /sync 命令生效
- 如果创建的同步 PR 不正确,可通过向同步 PR 的源分支提交轻量级 PR 完善,或使用 /close 命令关闭


Linking Issue Notice
@huangyunlong2022 , the pull request must be linked to at least one issue.
If an issue has already been linked, but the needs-issue label remains, you can remove the label by commenting /check-issue .


ascend docs pipeline is running...


✅ 跳过 docs ci 检查,没有需要检查的文档文件


🟡 Medium Priority
变更行:Module.cpp 第 2540–2547 行新增的 THNPModule_npuFillUninitializedMemory 函数( _npu_get_fill_uninitialized_memory 的 C 实现)没有 HANDLE_TH_ERRORS / END_HANDLE_TH_ERRORS 宏包裹。
受影响的调用链:Python 调用 torch_npu._C._npu_get_fill_uninitialized_memory() → THNPModule_npuFillUninitializedMemory → globalNpuContext().npuFillUninitializedMemory()。
失败模式:该文件内所有其他 Python 可调用函数(包括其兄弟 setter THNPModule_setNpuFillUninitializedMemory、THNPModule_get_deterministic_level 等)均使用 HANDLE_TH_ERRORS / END_HANDLE_TH_ERRORS 将 C++ 异常转换为 Python 异常。缺少该保护时,若内部代码(如 globalNpuContext() 的初始化、或未来对 NpuContext 的扩展)抛出 C++ 异常,异常将直接穿透 Python 解释器边界导致进程崩溃(UB)。
虽然当前 NpuContext::npuFillUninitializedMemory() 仅返回一个 bool、几乎不可能抛出异常,但这是一个一致性/维护性缺陷:后续任何对 NpuContext 的修改都可能无意中引入异常风险,而此处缺乏保护不会被编译器或审查人注意到。
建议:为 THNPModule_npuFillUninitializedMemory 添加 HANDLE_TH_ERRORS / END_HANDLE_TH_ERRORS 宏包裹,与文件中所有其他 Python C API 函数保持一致。
|
2547 | + static PyObject* THNPModule_npuFillUninitializedMemory( |
|
2548 | + PyObject* _unused, |
|
2549 | + PyObject* noargs) { |
|
2550 | + HANDLE_TH_ERRORS |
|
2551 | + if (at_npu::native::env::globalNpuContext().npuFillUninitializedMemory()) |
|
2552 | + Py_RETURN_TRUE; |
|
2553 | + else |
|
2554 | + Py_RETURN_FALSE; |
|
2555 | + END_HANDLE_TH_ERRORS |
| 2547
2556 | } |


| 阶段 | 任务名 | 状态 | 详情 |
|---|---|---|---|
| 编译构建 | Build_X86 | ✅ | >>> |
| Build_ARM | ✅ | >>> | |
| Build_LibTorch_x86 | ✅ | >>> | |
| Build_LibTorch_ARM | ✅ | >>> | |
| Build_X86_torchair | 🛑 | >>> | |
| Build_ARM_torchair | 🛑 | >>> | |
| patch_test | 🛑 | >>> | |
| 恶意代码检查 | Antipoison | ✅ | >>> |
| 编码安全与规范检查 | codecheck_pre-commit | ✅ | >>> |
| check_error | ✅ | >>> | |
| lintrunner | ✅ | >>> | |
| 开源片段检查 | SCA | ✅ | >>> |
| 开发者测试 | UT_ARM_A3_Part_01 | 🛑 | >>> |
| UT_ARM_A3_Part_02 | 🛑 | >>> | |
| UT_ARM_A2_Part_01 | ✅ | >>> | |
| UT_ARM_A2_Part_02 | ✅ | >>> | |
| UT_ARM_A2_Part_03 | ✅ | >>> | |
| UT_inductor_Part_01 | 🛑 | >>> | |
| UT_inductor_Part_02 | 🛑 | >>> | |
| UT_inductor_Part_03 | 🛑 | >>> | |
| UT_inductor_Part_04 | 🛑 | >>> | |
| UT_DIST_ARM_Part_01 | 🛑 | >>> | |
| UT_DIST_ARM_Part_02 | 🛑 | >>> | |
| UT_DIST_ARM_Part_03 | 🛑 | >>> | |
| UT_DIST_ARM_Part_04 | 🛑 | >>> | |
| UT_ARM_A2_Select_Part_01 | ✅ | >>> | |
| UT_ARM_A2_Select_Part_02 | ✅ | >>> | |
| 流水线 | PR-pipeline_pytorch | ✅ | >>> |
- compile、compile_inductor、compile_torchair : 运行流水线
- retry : 重试流水线所有失败子任务
- retry <任务名> : 仅重试指定失败子任务
- stop : 停止流水线


/approve


/approve


Pull Request 已合并或已关闭。
If you want to solve this problem, you can click here to do it in the FAQs.




【合入来源】
【修改方案】
使用torch_npu._C接口替换option接口
【资料变更】
【接口变更】
【功能验证】
【CheckList】