Pull Request已成功合入, 合并人@ascend-robot
(感谢 ascend-robot 的贡献)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 | ✅ liangsongwei, huangjingwei (2/2) | ✅ liangsongwei (1/1) |
💡 Tip:
- Committer can comment
/approveor/lgtm- Commenting
/approveimplies both code review (lgtm) and intent to merge (approve)
CLA Signature Pass
ascend-ds-bot, thanks for your pull request. All authors of the commits have signed the CLA. 👍


Ascend docs pipeline is running...


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


Ascend docs pipeline is running...


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


我已仔细审查了该文件的所有变更。以下是我的分析结论:
文件 torchnpugen/autograd/templates/VariableType.cpp:
变更内容:
- 将
C10_UNUSED宏替换为 C++17 标准属性[[maybe_unused]],以确保在 DEBUG 编译模式下正确抑制未使用警告。 - 新增辅助函数
expected_fresh_use_count,用于在 DEBUG 编译中验证 Tensor 的预期引用计数。 - 微调引用类型声明的格式(
Variable &→Variable&)。
经过逐行审查:
reset_grad_accumulator函数:对meta做了正确的空指针检查,逻辑无问题。expected_fresh_use_count函数:- 先通过
self.defined()检查 Tensor 是否有效,再调用unsafeGetTensorImpl(),避免了空指针解引用。 pyobj_slot()在 PyTorch 的TensorImpl中返回成员地址,始终非空,后续load_pyobj()调用安全。- 函数标记为
[[maybe_unused]],因为仅在 DEBUG 模式下可能被生成的代码引用。
- 先通过
- 匿名命名空间的闭合括号正确从原
reset_grad_accumulator之后移动到新函数之后,作用域正确。
未发现任何代码缺陷。 变更属于标准属性迁移和 DEBUG 编译支持的前置准备,逻辑正确、无内存安全问题、无未定义行为。
变更摘要
此 PR 主要对 torchnpugen/autograd/templates/VariableType.cpp 进行修改,以支持 DEBUG 编译模式。核心改动是将原有的 C10_UNUSED 宏替换为 C++17 标准的 [[maybe_unused]] 属性,并新增一个辅助函数 expected_fresh_use_count,用于在 DEBUG 编译中验证 Tensor 的预期引用计数。
主要改动
C10_UNUSED替换为[[maybe_unused]]:将reset_grad_accumulator函数前的C10_UNUSED宏替换为 C++17 标准属性[[maybe_unused]],确保在 DEBUG 编译模式下也能正确抑制"未使用函数"警告。- 新增
expected_fresh_use_count函数:在匿名命名空间中新增一个[[maybe_unused]]标记的辅助函数,根据 Tensor 的不同状态(未定义 / 持有 Python 对象 / 普通 TensorImpl)返回预期的引用计数值(0 / 2 / 1),用于 DEBUG 断言或验证逻辑。 - 代码格式微调:将引用类型声明从
Variable & self调整为Variable& self,并将reset_grad_accumulator函数体缩进统一为 2 空格风格。


/approve


/lgtm


| 阶段 | 任务名 | 状态 | 详情 |
|---|---|---|---|
| 编译构建 | Build_X86 | ✅ | >>> |
| Build_ARM | ✅ | >>> | |
| Build_LibTorch_x86 | ✅ | >>> | |
| Build_LibTorch_ARM | ✅ | >>> | |
| Build_X86_torchair | 🛑 | >>> | |
| Build_ARM_torchair | 🛑 | >>> | |
| patch_test | 🛑 | >>> | |
| 恶意代码检查 | Antipoison | ✅ | >>> |
| 编码安全与规范检查 | CodeCheck | ✅ | >>> |
| check_error | ✅ | >>> | |
| CodeCheck_lintrunner | ✅ | >>> | |
| 开源片段检查 | SCA | ✅ | >>> |
| 开发者测试 | UT_X86_Part_01 | 🛑 | >>> |
| UT_X86_Part_02 | 🛑 | >>> | |
| 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 | ✅ | >>> |




1. Origin pull request:
https://gitcode.com/Ascend/pytorch/merge_requests/37970
2. Original pull request related issue(s):
https://gitcode.com/Ascend/pytorch/issues/2326
3. Original pull request related commit(s):