已合并
test(fx): add test for Tracer.getattr #35387
test(fx): add test for Tracer.getattr #35387
已合并
m0_45651774创建于 5月12日
m0_45651774
m0_45651774
5月12日

【API功能】

API:
torch.fx.Tracer.getattr

功能:
Tracer.getattr 是 PyTorch FX 符号追踪(Symbolic Tracing)过程中的核心钩子函数之一,它充当了 Python 对象属性访问与 FX 计算图节点之间的桥梁。torch.fx.Tracer.getattr 的主要职责是在追踪(Trace)阶段,拦截对 nn.Module 实例属性的访问操作,并将其转化为计算图(Graph)中的一个 get_attr 节点。在 PyTorch FX 的设计中,Tracer 通过重写 Python 的属性访问机制,确保当代码尝试读取模型的权重(如 self.linear.weight)、偏置或子模块时,不会直接返回底层的 Tensor 或 Module 对象,而是返回一个代表该值的符号代理(Proxy)。

【用例完整性】

经 PyTorch 社区代码检索,v2.7.1/v2.8.0/v2.9.0/v2.10.0/v2.11.0均无torch.fx.Tracer.getattr 测试用例覆盖,因此需要新增 torch-npu 自有用例。

【修改方案】

对目标 API 进行现状核查,确认在当前v2.7.1/v2.8.0/v2.9.0/v2.10.0/v2.11.0已实际支持,不需要补齐底层实现,本 PR 仅补充torch.fx.Tracer.getatt的测试用例至test/fx/test_fx_tracer_transformer_api.py。

【版本说明】

经 PyTorch 社区代码检索,v2.7.1/v2.8.0/v2.9.0/v2.10.0/v2.11.0均存在该API

【NPU适配分析】

  • 职责:在符号追踪过程中,处理模型代码对属性(如 self.submodule.weightself.layers.__len__)的访问。决定该属性是直接返回原始值(视为常量),还是记录为图中的 get_attr 节点。
  • 实现层次:该方法属于 Tracer 类的 Python 成员函数,其逻辑由 PyTorch 框架在 Python 层完成(例如通过 super().getattr() 调用默认行为或用户自定义的 is_leaf_module 判断)。
  • 设备无关性getattr 操作的对象是 torch.nn.Module 或 Python 对象,并不直接接触 torch.Tensor 的底层设备内存。即使访问的张量位于 NPU 上,getattr 也只是获取该张量的属性元信息(如 .shape, .dtype)或子模块引用,不涉及 NPU 侧的 kernel 执行或内存传输。

torch.fx.Tracer.getattr属于纯 Python 框架层面的操作,不需要进行 NPU 适配。

【资料变更】

不涉及

【接口变更】

不涉及

【功能验证】

torch.fx.Tracer.getattr测试用例路径:test/fx/test_fx_tracer_transformer_api.py

验证环境

  • 对应版本:torch-npu 2.8.0
  • 操作系统:openEuler22.03
  • CANN 软件版本:8.5.0
  • 安装的软件版本:torch、torch-npu 2.8.0

验证结果:
ScreenShot_280.JPG

结果说明:测试用例执行通过。
注意:测试结果图中的test_fx_Tracer_getattr.py文件内容已添加到test/fx/test_fx_tracer_transformer_api.py文件。

【CheckList】

PR提交人对以下CheckList自检项进行全量自检,自检通过或不涉及,均修改 [ ] 为 [x]

likedislike
Pull Request已成功合入, 合并人@ascend-robot
(感谢 m0_45651774 的贡献)
m0_45651774m0_45651774
5月12日 创建了 pull request,commit 6826bab1
ascend-robot
ascend-robot成员
5月12日 评论:

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
test 李伟, sunyu-xuan (2/2) 李伟 (1/1)

💡 Tip:

  • Committer can comment /approve or /lgtm
  • Commenting /approve implies both code review (lgtm) and intent to merge (approve)

CLA Signature Pass

m0_45651774, thanks for your pull request. All authors of the commits have signed the CLA. 👍

likedislike
ascend-robotascend-robot成员
5月12日 添加了label:ascend-cla/yes
ascend-robot
ascend-robot成员
5月12日 评论:

当前仓库存在以下 保护分支

Protected Branch Version Release
master
v2.7.1
v2.9.0
v2.11.0
v2.10.0
v2.12.0

评论 /sync <branch1> <branch2> ... 可将当前 PR 修改同步到其它分支(创建同步 PR):
a) 如果当前 PR 是 Open 状态,同步操作将延迟到 PR 被合并时执行
b) 如果当前 PR 已经 Merged,将立即执行同步操作

注意:

  1. /sync 命令可以指定同步到多个分支,仅最后一个 /sync 命令生效
  2. 如果创建的同步 PR 不正确,可通过向同步 PR 的源分支提交轻量级 PR 完善,或使用 /close 命令关闭
likedislike
此处折叠了58条消息 查看更多
liwei386成员
5月14日 评论:

/approve

likedislike
ascend-robotascend-robot成员
5月14日 添加了label:approvedlgtm
ascend-robot
ascend-robot成员
5月14日 评论:

Review Guide

This pull-request passes review.
Committers who wrote a comment of /approve are: 李伟.
Reviewers who wrote a comment of /lgtm are: sunyu-xuan, 李伟.

likedislike
ascend-robotascend-robot成员
5月14日 合入了pull request
ascend-robot
ascend-robot成员
5月15日 评论:
流水线 pytorch_gitcode_PR_multiVersion#9142 [ commitID:5262c850 ] 已完成
likedislike