| 文件 | 最后提交记录 | 最后更新时间 |
|---|---|---|
| 2 年前 | ||
| 2 年前 | ||
| 2 年前 | ||
| 2 年前 | ||
| 2 年前 | ||
| 2 年前 | ||
| 2 年前 | ||
test(fx): add NPU tests for torch.fx graph APIs Co-authored-by: zhouzirui1234<2490136803@qq.com> # message auto-generated for no-merge-commit merge: !35514 merge intern/fx-graph-api-tests-v2-8-0 into v2.8.0 test(fx): add NPU tests for torch.fx graph APIs Created-by: zhouzirui-2026 Commit-by: zhouzirui1234 Merged-by: ascend-robot Description: # 【合入来源】 - [ ] 需求 - [ ] 问题单 - [x] issue/工单 - [ ] 重构优化 - [ ] 资料更新 关联 issue: - 任务 issue:https://gitcode.com/Ascend/pytorch/issues/1634 # 【修改方案】 本 PR 新增 test/fx/test_fx_graph_api.py,补充 torch.fx Graph 相关 API / 内部 codegen 链路在 NPU 环境下可直接运行的轻量验证用例。测试中涉及 Tensor 的输入均通过 .to(device_type) 放到当前 accelerator 上执行。 任务 API 功能及上游社区用例情况如下: | API / 内部链路 | 功能说明 | 上游社区用例情况 | 本 PR 处理 | | --- | --- | --- | --- | | torch.fx.Graph.inserting_after | 设置 FX Graph 插入点,使后续新节点插入到指定节点之后。 | PyTorch 上游 test/quantization/fx/test_model_report_fx.py::TestFxModelReportDetectDynamicStatic.test_nested_detection_case 中有使用,但该用例在当前 Kunpeng/ARM 环境会因 FBGEMM 条件被 skip,无法直接进入目标主体。 | 参考上游构图方式,新增轻量 NPU 测试直接验证插入顺序、节点参数传递和 NPU 执行结果。 | | torch.fx.Graph.inserting_before | 设置 FX Graph 插入点,使后续新节点插入到指定节点之前。 | PyTorch 上游 test/test_fx.py::TestFX.test_insertion_point 已有直接用例,但当前环境运行 test/test_fx.py 会受 libtorchbind_test.so 缺失影响。 | 新增独立轻量 NPU 测试,避免大文件环境依赖,同时验证插入顺序、参数重写和 NPU 执行结果。 | | torch.fx.Graph.lint | 校验 FX Graph 结构合法性,包括节点拓扑顺序、引用关系、owner、target 等约束。 | PyTorch 上游 test/test_fx.py 已有直接专项测试,例如 TestFX.test_wrong_topo、TestFX.test_copy_no_remap。 | 不重复新增 lint 专项测试;本 PR 仅在新增测试中调用 graph.lint() 校验所构造图的合法性。 | | torch.fx.graph.magic_methods.format | torch.fx.graph 内部 codegen 模板链路,用于生成 magic method 对应的 Python 表达式。 | PyTorch 上游 test/test_fx.py::TestFX.test_fx_shifts 通过 shift 表达式覆盖相关 codegen 链路。 | 新增 NPU 测试,通过 symbolic_trace 验证 Proxy 节点生成、节点参数、codegen 文本和 NPU 执行结果。 | | torch.fx.graph.inplace_methods.format | torch.fx.graph 内部 inplace codegen 模板链路,用于生成 inplace 操作对应的 Python 代码。 | PyTorch 上游 test/test_fx.py::TestFX.test_imul_code_print 通过 operator.imul 覆盖 inplace codegen。 | 新增 NPU 测试,手动构造 operator.imul 节点触发 a *= b codegen,并验证节点参数、输出结果和输入 Tensor 原地更新。 | 主要开发思路: 1. 先复查 PyTorch 上游社区用例,确认 5 个任务 API / 内部链路的已有覆盖情况。 2. 对 torch.fx.Graph.lint,上游已有直接专项测试,因此本 PR 不重复新增 lint 测试。 3. 对 torch.fx.Graph.inserting_after,上游 quantization 用例会因当前 Kunpeng/ARM 环境不支持 FBGEMM 被 skip,因此参考其插入点使用方式新增轻量测试。 4. 对 torch.fx.Graph.inserting_before、torch.fx.graph.magic_methods.format、torch.fx.graph.inplace_methods.format,上游相关用例集中在 test/test_fx.py,该文件在当前环境存在 libtorchbind_test.so 依赖问题,因此新增独立小文件验证核心行为。 5. 新增测试聚焦 FX 图构建、Proxy 生成、节点 target、节点参数传递、codegen 文本以及 NPU Tensor 执行结果,不修改 API 实现。 本 PR 覆盖以下 4 个新增测试目标项: | API / 内部链路 | 验证内容 | | --- | --- | | torch.fx.Graph.inserting_after | 验证 FX Graph 插入点设置后,新节点可插入到指定节点之后,并校验节点顺序、节点参数传递、节点 target 以及 NPU 执行结果。 | | torch.fx.Graph.inserting_before | 验证 FX Graph 插入点设置后,新节点可插入到指定节点之前,并校验节点顺序、节点参数传递、节点 target 以及 NPU 执行结果。 | | torch.fx.graph.magic_methods.format | 通过 symbolic_trace 触发 Proxy 生成链路,验证 operator.lshift / operator.rshift 节点生成、参数传递、生成代码格式以及 NPU 执行结果。 | | torch.fx.graph.inplace_methods.format | 手动构造 operator.imul FX 节点,验证 inplace codegen 生成 a *= b、节点参数传递、NPU 执行结果以及输入 Tensor 被原地更新。 | 本 PR 不新增 torch.fx.Graph.lint 专项测试,原因如下: 1. 复查 PyTorch 官方 test/test_fx.py,torch.fx.Graph.lint 已有直接专项测试覆盖,例如 TestFX.test_wrong_topo、TestFX.test_copy_no_remap 等。 2. 本 PR 新增的 4 个测试中也在构造或 trace 完 FX Graph 后调用 graph.lint(),用于校验测试图结构合法性,但不把它作为本 PR 的新增目标 API。 本 PR 不修改 API 实现,不修改 test/test_fx.py。新增独立测试文件 test/fx/test_fx_graph_api.py,避免受 test/test_fx.py 中历史环境依赖 libtorchbind_test.so 缺失问题影响。 新增测试文件: text test/fx/test_fx_graph_api.py 新增测试方法: text test_graph_inserting_after test_graph_inserting_before test_magic_methods_format_codegen test_inplace_methods_format_codegen 各测试核心验证点如下: | 测试方法 | 核心验证点 | | --- | --- | | test_graph_inserting_after | 构造 placeholder -> neg -> relu -> output;使用 graph.inserting_after(neg);断言 relu 紧跟 neg;断言 neg.args == (x,)、relu.args == (neg,)、relu.target == torch.relu;在 NPU Tensor 上执行 GraphModule 并校验结果。 | | test_graph_inserting_before | 先构造 placeholder -> relu -> output;使用 graph.inserting_before(relu) 插入 neg;断言 neg 位于 relu 前且相邻;断言 neg.args == (x,)、relu.args == (neg,)、neg.target == torch.neg;在 NPU Tensor 上执行 GraphModule 并校验结果。 | | test_magic_methods_format_codegen | 使用 symbolic_trace trace x << 3, x >> 3;断言 FX Graph 中生成 operator.lshift / operator.rshift 节点;断言节点参数为 (x_node, 3);断言 codegen 包含 x << 3、x >> 3;在 NPU LongTensor 上执行并校验结果。 | | test_inplace_methods_format_codegen | 手动构造 operator.imul(a, b) 节点;断言节点参数和 target;断言 codegen 包含 a *= b;在 NPU Tensor 上执行并校验输出结果,同时校验输入 Tensor 被原地更新。 | # 【资料变更】 当前不涉及资料补齐 PR。三个公开 torch.fx.Graph.* API 已在 docs/zh/native_apis 中记录;两个 torch.fx.graph.*.format 项为内部 codegen 字典模板链路,不宜按普通公开 API 强行补入 native API 表。 # 【接口变更】 不涉及。 # 【功能验证】 执行命令: bash cd /root python /workspace/pytorch/test/fx/test_fx_graph_api.py 执行结果: text .... ---------------------------------------------------------------------- Ran 4 tests in 7.673s OK 最终验证结论: 1. 本 PR 新增 4 个轻量 NPU 测试,覆盖 inserting_after、inserting_before、magic_methods.format、inplace_methods.format。 2. 新增测试不仅验证 API 调用不崩溃,也验证 FX 图构建、Proxy 生成、节点 target、节点参数传递、codegen 文本和 NPU Tensor 执行结果。 3. torch.fx.Graph.lint 已有 PyTorch 官方直接专项测试覆盖,因此本 PR 不重复新增 lint 专项测试。 4. 本 PR 不修改 API 实现,不涉及资料变更和接口变更。 # 【CheckList】 - [x] 代码注释完备,正确记录错误日志 - [x] 代码实现进行了返回值、空指针等校验 - [x] PR标题正确使用类型标签,如:feat、fix、refactor、docs、test等 - [ ] PR持续集成流水线(CI)执行通过,代码检查无异常 See merge request: Ascend/pytorch!35514 | 3 个月前 | |
test(fx): add test for lazy_format_graph_code Co-authored-by: Lane0218<laneljc@qq.com> # message auto-generated for no-merge-commit merge: !35080 merge task2-v2.8.0-test-fx-lazy-format into v2.8.0 test(fx): add test for lazy_format_graph_code Created-by: liujiacheng_2026 Commit-by: Lane0218 Merged-by: ascend-robot Description: # 【合入来源】 > [【开源实习】Torch-NPU API一致性对齐:补齐测试用例、API功能对齐、补齐文档(2)](https://gitcode.com/Ascend/pytorch/issues/1608) - [ ] 需求 - [ ] 问题单 - [X] issue/工单 - [ ] 重构优化 - [ ] 资料更新 本 PR 覆盖以下 API: - torch.fx._utils.lazy_format_graph_code # 【修改方案】 ## 一、API 功能说明 torch.fx._utils.lazy_format_graph_code 是 torch.fx 中用于延迟格式化 GraphModule 可读代码的 Python helper。 该 API 返回 torch._logging.LazyString,只有在日志系统或调用方真正需要字符串内容时,才调用内部格式化函数。字符串化时会调用 GraphModule.print_readable(print_output=False, **kwargs),并将结果包装成包含 TRACED GRAPH 标识和标题的可读文本。 该 API 主要用于 FX pass 的日志输出,例如 split_module、runtime_assert、_tensorify_python_scalars 等路径。它不涉及 NPU 算子分派,也不修改 graph 语义。 ## 二、用例完备性说明 经检索,PyTorch 社区测试中暂未发现直接覆盖 torch.fx._utils.lazy_format_graph_code 的专项测试。因此按任务要求在 Torch-NPU test 目录补充自写用例。 新增 TestFXNPU.test_lazy_format_graph_code,验证内容包括: - 构造最小 torch.nn.Module 并通过 symbolic_trace 得到 GraphModule。 - 调用 lazy_format_graph_code("fx lazy graph", gm, maybe_id=1)。 - 对返回的 LazyString 执行字符串化。 - 断言输出包含 TRACED GRAPH。 - 断言标题包含 ===== fx lazy graph 1 =====。 - 断言输出包含 def forward,证明 GraphModule.print_readable 的结果进入最终格式化文本。 该测试是功能类 API 的最小核心验证,不依赖真实 NPU 张量,不绑定具体算子节点文本,也不覆盖颜色输出等非核心格式细节,避免测试过度约束输出实现。 ## 三、NPU 适配说明 本次不修改 Torch-NPU 生产代码,仅新增测试用例。 lazy_format_graph_code 是 FX Python 层日志辅助函数,不涉及 NPU kernel、device dispatch 或 tensor 精度计算。NPU 适配目标是验证该 API 在 Torch-NPU 测试环境中可正常导入、调用和生成符合预期的 FX graph 可读输出。 为什么不需要修改 API: - Torch-NPU 未覆写该 API。 - 该 API 行为由 PyTorch torch.fx._utils 提供。 - 当前验证中 API 可正常运行,输出包含预期 graph 标识、标题和 forward 代码。 # 【资料变更】 不涉及。本 API 的资料补齐由单独文档 PR 提交到 v2.7.1。 # 【接口变更】 不涉及。 # 【功能验证】 执行命令: bash TORCH_DEVICE_BACKEND_AUTOLOAD=0 PYTHONPATH=<repo>/test python <repo>/test/test_fx.py TestFXNPU.test_lazy_format_graph_code 覆盖版本: - master - v2.7.1 - v2.8.0 - v2.9.0 - v2.10.0 - v2.11.0 - v2.12.0 执行结果: text ---------------------------------------------------------------------- Ran 1 test in 0.009s OK 额外检查: bash python -m py_compile <repo>/test/test_fx.py git diff --check 覆盖上述全部版本,结果均通过。 # 【CheckList】 - [x] 代码注释完备,正确记录错误日志 - [x] 代码实现进行了返回值、空指针等校验 - [x] PR标题正确使用类型标签,如:feat、fix、refactor、docs、test等 - [x] PR持续集成流水线(CI)执行通过,代码检查无异常 See merge request: Ascend/pytorch!35080 | 3 个月前 | |
| 2 年前 | ||
| 2 年前 | ||
test(fx): add test for Tracer.getattr Co-authored-by: m0_45651774<2759927519@qq.com> # message auto-generated for no-merge-commit merge: !35387 merge test-fx-Tracer-getattr-v2.8.0 into v2.8.0 test(fx): add test for Tracer.getattr Created-by: m0_45651774 Commit-by: m0_45651774 Merged-by: ascend-robot Description: <!-- PR描述模板更新日期:20260512 --> # 【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.weight 或 self.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 验证结果:  结果说明:测试用例执行通过。 注意:测试结果图中的test_fx_Tracer_getattr.py文件内容已添加到test/fx/test_fx_tracer_transformer_api.py文件。 # 【CheckList】 > PR提交人对以下CheckList自检项进行全量自检,自检通过或不涉及,均修改 [ ] 为 [x] - [x] 代码注释完备,正确记录错误日志 - [x] 代码实现进行了返回值、空指针等校验 - [x] PR标题正确使用类型标签,如:feat、fix、refactor、docs、test等 - [x] PR持续集成流水线(CI)执行通过,代码检查无异常 See merge request: Ascend/pytorch!35387 | 3 个月前 | |
| 2 年前 | ||
| 2 年前 | ||
| 2 年前 | ||
| 2 年前 | ||
| 2 年前 | ||
[test] add symbolic_shapes coverage under fx tests Co-authored-by: baoxuebin_2026<xuebin@isrc.iscas.ac.cn> # message auto-generated for no-merge-commit merge: !33902 merge codex/symbolic-shapes-tests-v280 into v2.8.0 [test] add symbolic_shapes coverage under fx tests Created-by: baoxuebin_2026 Commit-by: baoxuebin_2026 Merged-by: ascend-robot Description: # 【合入来源】 > 如有社区issue,请关联issue链接 - [ ] 需求 - [ ] 问题单 - [x] issue/工单 - [ ] 重构优化 - [ ] 资料更新 公开issue:暂无单独公开issue链接,本次提交对应 Torch-NPU API 补齐任务,涉及以下 5 个 API: - torch.fx.experimental.symbolic_shapes.is_accessor_node - torch.fx.experimental.symbolic_shapes.is_concrete_bool - torch.fx.experimental.symbolic_shapes.is_concrete_float - torch.fx.experimental.symbolic_shapes.is_concrete_int - torch.fx.experimental.symbolic_shapes.is_symbolic # 【修改方案】 1. 对目标 API 进行现状核查,确认在当前 PyTorch 2.9.0 + torch_npu 2.9.0rc1 环境中已实际支持,不需要补齐底层实现。 2. 补充独立测试文件 test/npu/test_symbolic_shapes.py,覆盖以下场景: - is_accessor_node 对 call_method / call_function 两类 accessor node 的识别 - is_concrete_int / is_concrete_float / is_concrete_bool 对字面量输入的判定 - ShapeEnv 生成的 SymInt / SymFloat / SymBool 的 symbolic 判定 - NPU tensor shape 场景下的实际验证 3. 采用最小化修改方案,不改动已有底层实现,仅补齐测试覆盖。 # 【资料变更】 不涉及。资料补齐已单独提交文档 PR。 # 【接口变更】 不涉及。未修改跨仓接口,也未修改客户面可见接口行为。 # 【功能验证】 测试场景: 1. symbolic_shapes 相关 API 的 accessor / concrete / symbolic 核心行为验证 2. NPU 设备下 tensor shape 场景验证 测试方法: ```bash cd /data TORCH_DEVICE_BACKEND_AUTOLOAD=1 /usr/local/python3.11.14/bin/python3 /data/pytorch/test/npu/test_symbolic_shapes.py -v See merge request: Ascend/pytorch!33902 | 3 个月前 | |
| 2 年前 |
| 文件 | 最后提交记录 | 最后更新时间 |
|---|---|---|
| 2 年前 | ||
| 2 年前 | ||
| 2 年前 | ||
| 2 年前 | ||
| 2 年前 | ||
| 2 年前 | ||
| 2 年前 | ||
| 3 个月前 | ||
| 3 个月前 | ||
| 2 年前 | ||
| 2 年前 | ||
| 3 个月前 | ||
| 2 年前 | ||
| 2 年前 | ||
| 2 年前 | ||
| 2 年前 | ||
| 2 年前 | ||
| 3 个月前 | ||
| 2 年前 |