已合并
[sync] PR-37970: update VariableType to support DEBUG compile #38850
[sync] PR-37970: update VariableType to support DEBUG compile #38850
已合并
ascend-robot创建于 6月18日
ascend-robot
ascend-robot成员
6月18日

1. Origin pull request:

https://gitcode.com/Ascend/pytorch/merge_requests/37970

https://gitcode.com/Ascend/pytorch/issues/2326

Sha Datetime Message
e81fe181 2026-06-09 16:29:46 +0800 CST update VariableType to support DEBUG compile
likedislike
Pull Request已成功合入, 合并人@ascend-robot
(感谢 ascend-robot 的贡献)
ascend-robotascend-robot成员
6月18日 创建了 pull request,commit b3859e64
ascend-robotascend-robot成员
6月18日 关联了issue:[Bug]: cannot compile a debug package over 2.10.0
ascend-robotascend-robot成员
6月18日 添加了label:ascend-cla/yes
ascend-robot
ascend-robot成员
6月18日 评论:

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 /approve or /lgtm
  • Commenting /approve implies 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. 👍

likedislike
ascend-robot
ascend-robot成员
6月18日 评论:

Ascend docs pipeline is running...

likedislike
ascend-robotascend-robot成员
6月18日 添加了label:docs-ci-pipeline-running
ascend-robot
ascend-robot成员
6月18日 评论:

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

likedislike
ascend-robotascend-robot成员
6月18日 删除了label:docs-ci-pipeline-running
ascend-robotascend-robot成员
6月18日 添加了label:docs-ci-pipeline-success
SCh_zx成员
6月18日 评论:

compile

likedislike
ascend-robotascend-robot成员
6月18日 添加了label:ci-pipeline-running
ascend-robot
ascend-robot成员
6月18日 评论:

Ascend docs pipeline is running...

likedislike
ascend-robotascend-robot成员
6月18日 删除了label:docs-ci-pipeline-success
ascend-robotascend-robot成员
6月18日 添加了label:docs-ci-pipeline-running
ascend-robot
ascend-robot成员
6月18日 评论:

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

likedislike
ascend-robotascend-robot成员
6月18日 删除了label:docs-ci-pipeline-running
ascend-robotascend-robot成员
6月18日 添加了label:docs-ci-pipeline-success
atomgit-bot
atomgit-bot
6月18日 评论:

代码审查

✅ 未发现问题

likedislike
ascend-robotascend-robot成员
6月18日 删除了label:ci-pipeline-running
ascend-robotascend-robot成员
6月18日 添加了label:ci-pipeline-failed
atomgit-bot
atomgit-bot
6月18日 评论:

我已仔细审查了该文件的所有变更。以下是我的分析结论:

文件 torchnpugen/autograd/templates/VariableType.cpp

变更内容:

  1. C10_UNUSED 宏替换为 C++17 标准属性 [[maybe_unused]],以确保在 DEBUG 编译模式下正确抑制未使用警告。
  2. 新增辅助函数 expected_fresh_use_count,用于在 DEBUG 编译中验证 Tensor 的预期引用计数。
  3. 微调引用类型声明的格式(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 空格风格。
likedislike
梁松伟
梁松伟成员
6月18日 评论:

/approve

likedislike
ascend-robotascend-robot成员
6月18日 添加了label:approved
Jingwei Huang
Jingwei Huang成员
6月18日 评论:

/lgtm

likedislike
ascend-robotascend-robot成员
6月18日 添加了label:lgtm
ascend-robotascend-robot成员
6月22日 删除了label:ci-pipeline-failed
ascend-robotascend-robot成员
6月22日 添加了label:ci-pipeline-running
ascend-robotascend-robot成员
6月22日 删除了label:ci-pipeline-running
ascend-robotascend-robot成员
6月22日 添加了label:ci-pipeline-passed
ascend-robot
ascend-robot成员
6月22日 评论:
流水线 PR-pipeline_pytorch#33468 (重试第1次)已完成
阶段 任务名 状态 详情
编译构建 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 >>>
likedislike
ascend-robotascend-robot成员
6月22日 合入了pull request
ascend-robot
ascend-robot成员
6月22日 评论:
流水线 pytorch_gitcode_PR_multiVersion#11022 [ commitID:acbb2ad2 ] 已完成
likedislike