| 文件 | 最后提交记录 | 最后更新时间 |
|---|---|---|
test(fx): add ShapeEnv core methods tests for v2.10.0 Co-authored-by: chenlan114514<3470991357@qq.com> # message auto-generated for no-merge-commit merge: !37573 merge test-shapeenv-apis-v2.10.0 into v2.10.0 test(fx): add tests for symbolic shapes APIs Created-by: zhufu-2026 Commit-by: chenlan114514 Merged-by: ascend-robot Description: # 【合入来源】 任务:[【开源实习】Torch-NPU API一致性对齐:补齐测试用例、API功能对齐、补齐文档(15) #1621 ](https://gitcode.com/Ascend/pytorch/issues/1621) 关联issue:[API一致性说明:torch.fx.experimental.symbolic_shapes.ShapeEnv 系列方法在 NPU 环境无需适配](https://gitcode.com/Ascend/pytorch/issues/2226) > <font color="red">**请勿携带内部流程信息(需求链接、问题单、内部issue等)**</font> - [ ] 需求 - [ ] 问题单 - [x] issue/工单 - [ ] 重构优化 - [ ] 资料更新 本次提交对应 Torch-NPU API 补齐任务,涉及以下 5 个 API: - torch.fx.experimental.symbolic_shapes.ShapeEnv.create_symbol - torch.fx.experimental.symbolic_shapes.ShapeEnv.bound_sympy - torch.fx.experimental.symbolic_shapes.ShapeEnv.check_equal - torch.fx.experimental.symbolic_shapes.ShapeEnv.cleanup - torch.fx.experimental.symbolic_shapes.ShapeEnv.bind_symbols ## 【API 功能介绍】 - create_symbol:在ShapeEnv中创建一个带初始值的符号变量,返回sympy.Symbol对象。属于符号形状管理基础操作。 - bound_sympy:计算符号表达式的上下界,返回包含lower和upper属性的边界对象。属于符号表达式分析操作。 - check_equal:校验两个ShapeEnv实例的符号状态是否完全一致。属于符号环境对比操作。 - cleanup:清理ShapeEnv中未使用的符号变量,释放内存。属于符号环境管理操作。 - bind_symbols:将符号变量与具体值绑定,返回绑定后的映射字典。属于符号变量绑定操作。 ## 【社区用例分析】 所有5个API均属于**1.3类API(上游无专门测试用例,需自行编写并提交PR到test目录)**: 1. 上游PyTorch仓库中,test/fx/目录下无任何针对ShapeEnv核心方法的独立测试文件 2. 虽然在test/dynamo/、test/export/等目录中有间接使用ShapeEnv的代码,但都是作为其他功能的依赖组件调用 3. 没有针对上述5个API的参数合法性、返回值正确性、异常处理等核心行为进行独立验证 4. 因此需要新增专门的测试文件,覆盖所有API的基础功能场景 # 【修改方案】 1. 新增测试文件test/fx/test_shape_env_methods.py,为每个API编写独立的测试用例 2. 测试用例严格遵循PyTorch单元测试规范,使用torch.testing._internal.common_utils.TestCase基类 3. 每个测试用例聚焦核心功能,无冗余逻辑: - test_create_symbol:验证符号变量的类型和命名规则 - test_bound_sympy:验证简单符号表达式的边界计算正确性 - test_check_equal:验证空ShapeEnv实例的相等性校验 - test_cleanup:验证环境清理后可以正常创建新的符号变量 - test_bind_symbols:验证空符号列表的绑定行为 4. 测试用例无硬件强相关代码,同时兼容昇腾NPU环境与CPU环境 # 【资料变更】 不涉及。本PR仅补充测试用例,不包含文档修改。 # 【接口变更】 不涉及。未修改任何跨仓接口,也未改变用户可见的API行为。 # 【功能验证】 ## 测试环境 - 硬件:昇腾910B NPU - 软件:CANN 8.5.0 + torch 2.10.0 + torch-npu 2.10.0 - 测试平台:HiDevLab在线开发环境 ## 测试场景1:新增测试用例验证 执行命令: bash python -m pytest test/fx/experimental/test_symbolic_shapes.py::TestShapeEnvCoreMethods -v 测试日志: bash =========================================================================== test session starts =========================================================================== platform linux -- Python 3.11.13, pytest-9.0.2, pluggy-1.6.0 -- /usr/local/bin/python cachedir: .pytest_cache rootdir: /workspace/pytorch_2373 configfile: pyproject.toml collected 5 items test/fx/experimental/test_symbolic_shapes.py::TestShapeEnvCoreMethods::test_bind_symbols PASSED [ 20%] test/fx/experimental/test_symbolic_shapes.py::TestShapeEnvCoreMethods::test_bound_sympy PASSED [ 40%] test/fx/experimental/test_symbolic_shapes.py::TestShapeEnvCoreMethods::test_check_equal PASSED [ 60%] test/fx/experimental/test_symbolic_shapes.py::TestShapeEnvCoreMethods::test_cleanup PASSED [ 80%] test/fx/experimental/test_symbolic_shapes.py::TestShapeEnvCoreMethods::test_create_symbol PASSED [100%] ============================================================================ 5 passed in 4.22s ============================================================================  # 【CheckList】 - [x] 代码注释完备,正确记录错误日志 - [x] 代码实现进行了返回值、空指针等校验 - [x] PR标题正确使用类型标签,如:feat、fix、refactor、docs、test等 - [x] PR持续集成流水线(CI)执行通过,代码检查无异常 See merge request: Ascend/pytorch!37573 | 1 个月前 | |
| 2 年前 | ||
| 2 年前 | ||
| 2 年前 | ||
| 2 年前 | ||
| 2 年前 | ||
| 2 年前 | ||
test(fx): add tests for graph codegen api Co-authored-by: nannan-2026<1794949109@qq.com> # message auto-generated for no-merge-commit merge: !34462 merge test-fx-codegen-api-v2.10.0 into v2.10.0 test(fx): add tests for graph codegen api Created-by: nannan-2026 Commit-by: nannan-2026 Merged-by: ascend-robot Description: # 【合入来源】 - [ ] 需求 - [ ] 问题单 - [x] issue/工单 - [ ] 重构优化 - [ ] 资料更新 关联 issue:https://gitcode.com/Ascend/pytorch/issues/1637 # 【修改方案】 本 PR 新增 torch.fx 代码生成相关 API 的单元测试,覆盖如下 API: - torch.fx.Graph.python_code - torch.fx.graph.PythonCode - torch.fx.Graph.set_codegen 具体修改内容: 1. 新增 test/test_fx_codegen_api.py 测试文件。 2. 增加 test_graph_python_code_returns_python_code 用例,验证 Graph.python_code("self") 返回对象类型为 PythonCode,并校验返回对象包含 src、globals 字段。 3. 增加 test_graph_set_codegen 用例,验证 Graph.set_codegen 设置自定义 CodeGen 后,GraphModule 可重新编译并正常执行。 4. 该组 API 属于 torch.fx Python 层代码生成接口,不涉及新增 NPU 算子实现。 # 【资料变更】 不涉及。 本 PR 仅新增测试用例,不修改资料文档。如后续确认 torch.fx.graph.PythonCode 在 native_apis 文档中缺失,将单独提交 docs PR 补充。 # 【接口变更】 不涉及。 本 PR 不修改客户可见接口,不修改 API 行为,仅新增测试用例验证现有 API 兼容性。 # 【功能验证】 测试场景: 1. 验证 torch.fx.Graph.python_code 可正常生成 PythonCode。 2. 验证 torch.fx.graph.PythonCode 对象包含 src 和 globals 字段。 3. 验证 torch.fx.Graph.set_codegen 设置自定义 CodeGen 后,GraphModule 可重新编译并正常执行。 测试命令: TORCH_DEVICE_BACKEND_AUTOLOAD=0 python test/test_fx_codegen_api.py -v 测试结果: test_graph_python_code_returns_python_code (__main__.TestFXCodegenAPI.test_graph_python_code_returns_python_code) ... ok test_graph_set_codegen (__main__.TestFXCodegenAPI.test_graph_set_codegen) ... ok Ran 2 tests in 0.168s OK 说明: 该 API 为 torch.fx Python 层代码生成接口,不涉及 NPU 算子计算。本次测试主要验证 API 功能行为与 PyTorch 原生保持一致。 # 【CheckList】 - [x] 代码注释完备,正确记录错误日志 - [x] 代码实现进行了返回值、空指针等校验 - [x] PR标题正确使用类型标签,如:feat、fix、refactor、docs、test等 - [x] PR持续集成流水线(CI)执行通过,代码检查无异常 See merge request: Ascend/pytorch!34462 | 3 个月前 | |
| 2 年前 | ||
docs(torch.fx): 完善 experimental.symbolic_shapes 文档与测试用例 Co-authored-by: yuhongming-2026<moshui_1@qq.com> # message auto-generated for no-merge-commit merge: !35209 merge add-fx-experimental-symbolic-api-test-v2100 into v2.10.0 test: 新增torch.fx.experimental.symbolic_shapes NPU 适配验证与统一运行脚本 Created-by: yuhongming-2026 Commit-by: yuhongming-2026 Merged-by: ascend-robot Description: <!-- PR描述模板更新日期:20260203 --> # 【合入来源】 > <font color="red">**https://gitcode.com/Ascend/pytorch/issues/1611**</font>\ > <font color="red">**请勿携带内部流程信息(需求链接、问题单、内部issue等)**</font> - [ ] 需求 - [ ] 问题单 - [x] issue/工单 - [ ] 重构优化 - [ ] 资料更新 --- ## 【背景与目标】 完善 Torch-NPU 对 PyTorch 原生 FX symbolic shapes 相关 API 的兼容性验证,覆盖以下 5 个 API 在 NPU 环境下的可用性,提升昇腾 NPU 上 PyTorch 框架的易用性与模型适配覆盖率。 | API | 说明 | | --- | --- | | torch.fx.experimental.symbolic_shapes.compute_unbacked_bindings | 计算 unbacked symint 绑定关系 | | torch.fx.experimental.symbolic_shapes.constrain_range | 对符号量施加范围约束 | | torch.fx.experimental.symbolic_shapes.constrain_unify | 统一两个符号量约束 | | torch.fx.experimental.symbolic_shapes.ConvertIntKey | Dynamo 图转换中的 int key 转换器 | | torch.fx.experimental.symbolic_shapes.ConvertIntKey.get | 根据 bool 条件获取对应 int key | --- ## 【上游社区支持情况】 | 项目 | 结论 | | --- | --- | | 是否属于上游 PyTorch API | 是,均位于 torch.fx.experimental.symbolic_shapes,为纯 Python 实现,不依赖 CUDA 专用算子 | | 上游是否提供公开 API | 是,constrain_range / constrain_unify 等已列入公开 API allowlist(如 test/allowlist_for_publicAPI.json) | | 上游是否有相关用例 | **部分有**。上游在 test/dynamo/test_misc.py 等文件中调用 constrain_range、constrain_unify;compute_unbacked_bindings、ConvertIntKey、ConvertIntKey.get 在基线 test/ 中缺少直接用例 | | 上游用例能否直接证明 NPU 支持 | 不能单独证明。上游现有调用主要覆盖 CPU/CUDA 或通用语义,未覆盖 NPU tensor 输入、torch.npu.device 上下文、由 NPU tensor 比较派生 bool 路径 | | 本 PR 的补充价值 | 在 Ascend NPU 上新增专项兼容性测试,补齐上游“部分覆盖/场景不足”导致的 NPU 侧验证缺口 | **结论:** 5 个 API 属于上游能力范围,但测试覆盖对 NPU 场景不完整(部分 API 无直接用例、部分 API 缺少 NPU关键路径)。本 PR 新增用例用于补齐 Torch-NPU 侧可验证性。**本 MR 不修改文档资料。** --- ## 【修改方案】 ### 1. API功能 1. torch.fx.experimental.symbolic_shapes.compute_unbacked_bindings 计算 unbacked SymInt 的绑定关系。本 PR 验证该接口在 NPU tensor 输入、NPU 上下文与 .to("npu") 迁移路径下可稳定调用。 2. torch.fx.experimental.symbolic_shapes.constrain_range 为符号值施加范围约束。本 PR 验证正常分支、异常分支以及 NPU 上下文调用行为。 3. torch.fx.experimental.symbolic_shapes.constrain_unify 对两个符号值进行约束统一。本 PR 验证正常/异常分支及 NPU 上下文下的可用性。 4. torch.fx.experimental.symbolic_shapes.ConvertIntKey 将布尔条件映射为整型 key 的转换器。本 PR 验证实例化与基础映射能力。 5. torch.fx.experimental.symbolic_shapes.ConvertIntKey.get 根据 bool 条件返回对应 int key(True->1,False->0)。本 PR 重点验证字面量 bool 和 NPU tensor 比较派生 bool 两类输入。 ### 2. 新增 NPU 兼容性测试 新增 test/fx/test_fx_experimental_symbolic.py,写法与同目录 FX 测试一致(TestCase + run_tests,默认 NPU 环境可用)。 | 用例 | 覆盖 API / 场景 | | --- | --- | | test_compute_unbacked_bindings | compute_unbacked_bindings + NPU tensor 输入 | | test_compute_unbacked_bindings_npu_context | compute_unbacked_bindings + torch.npu.device(0) 上下文 | | test_compute_unbacked_bindings_to_npu | compute_unbacked_bindings + .to("npu") 路径 | | test_constrain_range | constrain_range 基本调用与异常分支 | | test_constrain_range_npu_context | constrain_range + NPU device 上下文 | | test_constrain_unify | constrain_unify 基本调用与异常分支 | | test_constrain_unify_npu_context | constrain_unify + NPU device 上下文 | | test_convert_int_key_singleton | ConvertIntKey 实例化 | | test_convert_int_key_get_bool | ConvertIntKey.get(True/False) | | test_convert_int_key_get_npu_derived | ConvertIntKey.get + 由 NPU tensor 比较得到的 bool | | test_convert_int_key_npu_context | ConvertIntKey.get + NPU device 上下文 | **场景覆盖说明:** - compute_unbacked_bindings:NPU 张量、device 上下文、.to("npu") 迁移路径(3 类主路径) - constrain_range / constrain_unify:正常调用、异常分支、NPU 上下文(与上游 dynamo 通用语义用例互补) - ConvertIntKey / .get:实例化、字面量 bool、NPU 派生 bool、device 上下文(4 类主路径) ### 2.1 新增用例必要性与完整性分析 1. 针对 compute_unbacked_bindings: 上游基线 test/ 缺少面向 NPU tensor 的直接用例;本 PR 覆盖 device="npu"、.to("npu")、torch.npu.device(0) 三类主路径,补齐验证闭环。 2. 针对 constrain_range / constrain_unify: 上游 test/dynamo/test_misc.py 已有调用,但主要是 Python int + fallback 语义验证;本 PR 增加 NPU 上下文与异常分支验证,属于互补增强,不与上游重复。 3. 针对 ConvertIntKey / ConvertIntKey.get: 上游基线 test/ 缺少这组 API 的直接测试,尤其缺失“由 NPU tensor 比较结果派生 bool 后调用 get()”路径;本 PR 新增专项用例为必要补齐。 4. 结论: 本 PR 的新增测试属于“对上游部分覆盖场景做 NPU 侧补齐 + 对上游缺失场景补空白”,不是重复建设。 ### 3. 多版本分支合入(如适用) 按 Ascend PyTorch 版本线分别提 MR,每个 MR 仅 1 个 squash 提交: | 目标分支 | MR | | --- | --- | | v2.7.1 | https://gitcode.com/Ascend/pytorch/pull/35207 | | v2.9.0 | https://gitcode.com/Ascend/pytorch/pull/35208 | | v2.10.0 | https://gitcode.com/Ascend/pytorch/pull/35209 | | v2.11.0 | https://gitcode.com/Ascend/pytorch/pull/35210 | | v2.12.0 | https://gitcode.com/Ascend/pytorch/pull/35552 | | master | https://gitcode.com/Ascend/pytorch/pull/35211 | > 若当前 MR 仅针对单一版本分支,可删除上表中与本 MR 无关的行。 --- ## 【资料变更】 不涉及,测试 PR 本身不直接修改资料文件;资料补齐由配套资料 PR 单独提交。 本 PR 仅负责测试补齐与验证,确保测试范围与当前版本实际能力一致。 --- ## 【接口变更】 不涉及(无新增/修改对外 C++ 或 Python 接口,仅新增测试) --- ## 【功能验证】 ### 1. 测试环境 - **设备:** Ascend NPU(如 Ascend910B4) - **框架:** 对应分支 Torch-NPU + PyTorch - **说明:** 测试默认 NPU 可用,不额外做 torch.npu.is_available() 跳过逻辑 ### 2. 测试命令 bash cd <pytorch-repo-root> python test/fx/test_fx_experimental_symbolic.py ### 3. 测试结果  ### 4. 与上游用例的关系 | 验证维度 | 上游现有用例 | 本 PR 新增用例 | | --- | --- | --- | | constrain_range / constrain_unify 调用 | test/dynamo/test_misc.py 等(通用语义) | 增加 NPU 上下文与异常分支验证 | | NPU tensor + compute_unbacked_bindings | 无直接用例 | 已覆盖(NPU输入/上下文/迁移路径) | | NPU 上 ConvertIntKey.get(含 NPU 派生 bool) | 无直接用例 | 已覆盖 | | NPU 侧验证闭环 | 不完整 | 本 PR 补齐 | --- ## 【CheckList】 - [x] 代码注释完备,正确记录错误日志 - [x] 代码实现进行了返回值、空指针等校验 - [x] PR标题正确使用类型标签,如:feat、fix、refactor、docs、test等 - [ ] PR持续集成流水线(CI)执行通过,代码检查无异常 See merge request: Ascend/pytorch!35209 | 2 个月前 | |
[test] add tests for torch.fx Graph APIs Co-authored-by: lihaokun-2026<851563813@qq.com> # message auto-generated for no-merge-commit merge: !34401 merge add-fx-graph-api-test-v2100 into v2.10.0 [test] add tests for torch.fx Graph APIs Created-by: lihaokun-2026 Commit-by: lihaokun-2026 Merged-by: ascend-robot Description: <!-- PR描述模板更新日期:20260203 --> # 【合入来源】 > <font color="red">**如有社区issue,请关联issue链接**</font>\ > [【开源实习】Torch-NPU API一致性对齐:补齐测试用例、API功能对齐、补齐文档(30) #1636](https://gitcode.com/Ascend/pytorch/issues/1636) > <font color="red">**请勿携带内部流程信息(需求链接、问题单、内部issue等)**</font> - [ ] 需求 - [ ] 问题单 - [x] issue/工单 - [ ] 重构优化 - [ ] 资料更新 本次提交对应 Torch-NPU API 补齐任务,涉及以下 5 个 API: - torch.fx.Graph.output_node - torch.fx.Graph.placeholder - torch.fx.Graph.print_tabular - torch.fx.Graph.process_inputs - torch.fx.Graph.process_outputs # 【修改方案】 1. 对目标 API 进行现状核查,确认在当前Pytorch环境中已实际支持,不需要补齐底层实现。 2. 补充独立测试文件test/fx/test_fx_graph.py文件。 3. 采用最小化修改方案,不改动已有底层实现,仅补齐测试覆盖。 # 【资料变更】 不涉及 # 【接口变更】 不涉及 # 【功能验证】 测试场景: 1. torch.fx.Graph的5个API进行验证 2. NPU 设备下 tensor shape 场景验证 测试方法: cd /data TORCH_DEVICE_BACKEND_AUTOLOAD=1 /usr/local/python3.11.14/bin/python3 /data/pytorch/test/fx/test_fx_graph.py -v # 【CheckList】 > PR提交人对以下CheckList自检项进行全量自检,自检通过或不涉及,均修改 [ ] 为 [x] - [x] 代码注释完备,正确记录错误日志 - [x] 代码实现进行了返回值、空指针等校验 - [x] PR标题正确使用类型标签,如:feat、fix、refactor、docs、test等 - [ ] PR持续集成流水线(CI)执行通过,代码检查无异常 【开源实习】Torch-NPU API一致性对齐:补齐测试用例、API功能对齐、补齐文档(30) [#1636](https://gitcode.com/Ascend/pytorch/issues/1636) See merge request: Ascend/pytorch!34401 | 3 个月前 | |
[test] add fx graph api coverage for find_nodes and graph_copy Co-authored-by: anning-2026<870721918@qq.com> # message auto-generated for no-merge-commit merge: !36537 merge fx-graph-apis-v2.10.0 into v2.10.0 [test] add fx graph api coverage for find_nodes and graph_copy Created-by: anning-2026 Commit-by: anning-2026 Merged-by: ascend-robot Description: # 【合入来源】 > 如有社区issue,请关联issue链接 - [ ] 需求 - [ ] 问题单 - [x] issue/工单 - [ ] 重构优化 - [ ] 资料更新 公开issue:https://gitcode.com/Ascend/pytorch/issues/1633 本次提交对应 Torch-NPU API 补齐任务,涉及以下 5 个 API: - torch.fx.Graph.find_nodes - torch.fx.Graph.graph_copy - torch.fx.Graph.eliminate_dead_code - torch.fx.Graph.erase_node - torch.fx.Graph.get_attr # 【API 功能介绍】 - torch.fx.Graph.find_nodes:根据 op 类型和 target 快速查询图中节点,支持 sort 参数控制返回顺序。属于图结构查询操作。 - torch.fx.Graph.graph_copy:将源图的所有节点复制到目标图中,通过 val_map 映射已有节点,支持节点复用。属于图结构复制操作。 - torch.fx.Graph.eliminate_dead_code:删除图中无用户的死节点,保留有副作用的节点。属于图结构优化操作。 - torch.fx.Graph.erase_node:从图中删除指定节点。属于图结构操作。 - torch.fx.Graph.get_attr:在图中创建一个 get_attr 节点,用于获取模块属性。属于图节点创建操作。 # 【社区用例分析】 ## 1.3 类 API(上游无专门用例,自行编写并提交 PR) torch.fx.Graph.find_nodes 上游无专门测试。虽然在 test/fx/test_fx_node_hook.py 和 test/export/test_export.py 中有间接使用,但都是作为辅助手段调用,没有针对 find_nodes 本身的参数(op、target、sort)和返回值进行验证。因此需要新增用例。 torch.fx.Graph.graph_copy 上游无专门测试。虽然在 test/test_fx.py 中有间接使用,但没有针对 graph_copy 的 val_map 填充、节点复制正确性、已有节点复用等核心行为进行独立验证。因此需要新增用例。 torch.fx.Graph.erase_node 上游在 test/fx/test_fx_node_hook.py 的 test_hooks_for_node_update 中有间接调用,但该测试核心目的是验证 Node Hook 机制,erase_node 仅作为清理手段,未对节点删除后的图结构完整性和计算正确性进行独立验证。因此需要新增用例。 torch.fx.Graph.get_attr 上游在 test/test_fx.py 的多个测试函数中有间接使用 get_attr 节点,但都是在验证其他功能时作为图的组成部分出现,没有针对 get_attr 节点的创建、多属性获取等核心行为进行独立验证。因此需要新增用例。 ## 1.2 类 API(NPU 无需任何修改) torch.fx.Graph.eliminate_dead_code 上游用例文件:test/fx/test_dce_pass.py,共 7 个测试函数(test_simple、test_dead_chain、test_dead_getattr、test_dead_placeholder、test_dead_placeholder_with_user、test_keep_module_with_side_effects、test_keep_torch_assert)。 源文件已自带 import torch_npu 和 import torch_npu.testing,无需 patch,直接运行全部通过。该 API 为纯图结构操作,NPU 无需任何修改。 # 【修改方案】 其中 eliminate_dead_code 源文件已自带 NPU 支持,无需修改。 对 find_nodes、graph_copy、erase_node、get_attr 各补充 1 个测试用例到 test/fx/test_fx_graph_api.py,在功能验证充分的前提下尽量简化,每个 API 聚焦一个用例覆盖核心功能: 1. torch.fx.Graph.find_nodes:在一个包含 call_function、call_module、get_attr 的图中验证 op/target 查询、sort 参数和空结果场景 2. torch.fx.Graph.graph_copy:验证基本复制与 val_map 填充、val_map 复用已有节点、return_output_node 参数 3. torch.fx.Graph.erase_node:删除中间节点并重接依赖,验证图结构和 NPU 计算正确性 4. torch.fx.Graph.get_attr:通过 symbolic_trace 产生多属性 get_attr 节点,验证节点正确性和 NPU 计算结果 # 【资料变更】 不涉及。本 PR 仅补测试,不包含资料修改。 # 【接口变更】 不涉及。未修改跨仓接口,也未修改用户可见接口行为。 # 【功能验证】 测试场景1:torch.fx.Graph.find_nodes、graph_copy、erase_node、get_attr 的核心行为验证。 python test/fx/test_fx_graph_api.py 测试日志: test_erase_node ... ok test_find_nodes ... ok test_get_attr ... ok test_graph_copy ... ok test_graph_inserting_after ... ok test_graph_inserting_before ... ok test_inplace_methods_format_codegen ... ok test_magic_methods_format_codegen ... ok ---------------------------------------------------------------------- Ran 8 tests in 12.657s OK 测试场景2:torch.fx.Graph.eliminate_dead_code 上游用例验证(1.2 类,源文件已自带 NPU 支持)。 python test/fx/test_dce_pass.py 测试日志:exit code 0,7/7 tests passed。 [#1633](https://gitcode.com/Ascend/pytorch/issues/1633) See merge request: Ascend/pytorch!36537 | 2 个月前 | |
[test] Add fx graph internal API verify tests Co-authored-by: lihaokun-2026<851563813@qq.com> # message auto-generated for no-merge-commit merge: !35604 merge add-fx-graph-api-verify-v2100 into v2.10.0 [test] Add fx graph internal API verify tests Created-by: lihaokun-2026 Commit-by: lihaokun-2026 Merged-by: ascend-robot Description: <!-- PR描述模板更新日期:20260203 --> # 【合入来源】 > <font color="red">**如有社区issue,请关联issue链接**</font>\ > [【开源实习】Torch-NPU API一致性对齐:补齐测试用例、API功能对齐、补齐文档(25) #1631](https://gitcode.com/Ascend/pytorch/issues/1631) > <font color="red">**请勿携带内部流程信息(需求链接、问题单、内部issue等)**</font> - [ ] 需求 - [ ] 问题单 - [x] issue/工单 - [ ] 重构优化 - [ ] 资料更新 # 【修改背景】 本次任务聚焦 torch.fx.graph 相关内部能力的测试补齐与行为验证,主要覆盖 FX 图代码生成过程中涉及的内部辅助接口及状态维护逻辑。 结合目标分支实际源码核查后发现,torch.fx.graph.py 中相关能力的暴露形式并不完全一致: - torch.fx.graph._format_target、torch.fx.graph._is_from_torch、torch.fx.graph._origin_type_map、torch.fx.graph._register_custom_builtin 为 torch.fx.graph 模块级内部对象,可直接进行 API 级测试; - torch.fx.graph._format_args 在当前目标分支中并非 torch.fx.graph 模块级函数,也不是 CodeGen 类方法,而是定义在 CodeGen._gen_python_code() 内部的局部 helper 函数,无法通过 torch.fx.graph._format_args 直接访问。 因此,本次 PR 对 torch.fx.graph._format_args 不进行测试验证。 本次任务范围为 test/ 目录下测试用例开发与验证,不涉及 torch_npu/ 目录实现逻辑修改。 # 【资料支持情况核查】 核查结论: 1. torch.fx.graph._format_target 为模块级内部函数,可直接验证目标路径格式化行为; 2. torch.fx.graph._is_from_torch 为模块级内部函数,可直接验证 torch 对象识别行为; 3. torch.fx.graph._origin_type_map 为模块级内部映射表,可直接验证内置容器类型映射行为; 4. torch.fx.graph._register_custom_builtin 为模块级内部函数,可直接验证自定义 builtin 注册及内部状态更新行为; 5. torch.fx.graph._format_args 当前不是模块级 API,而是 CodeGen._gen_python_code() 内部局部函数,因此测试中不进行测试验证。 同时在torch官方社区中并没有针对这5个api的测试方法,同时由于torch.fx.graph._format_args 当前不是模块级 API,而是 CodeGen._gen_python_code() 内部局部函数,因此测试中不进行测试验证,因此需要在本pr中新增对4个api的测试方法,测试范围已与当前目标分支实际源码保持一致,避免将局部 helper 错误声明为模块级 API。 本次提交对应 Torch-NPU API 补齐任务,涉及以下 4 个 API: | API / 行为 | 作用说明 | 备注 | |---|---|---| | torch.fx.graph._format_target | 将 FX Node 的 target 路径格式化为合法的 Python 属性访问表达式。对于非法 Python 标识符,会转换为 getattr(...) 形式。 | 主要用于 call_method、call_module、get_attr 等节点的代码生成。 | | torch.fx.graph._is_from_torch | 判断一个对象是否来自 torch 命名空间,用于区分 torch 原生对象和用户自定义对象。 | FX codegen 可据此决定是否直接生成类似 torch.add(...) 的调用形式。 | | torch.fx.graph._origin_type_map.get | 查询内置容器类型到 typing 类型的映射关系,例如 list -> typing.List、dict -> typing.Dict。 | 主要用于 FX 代码生成过程中的类型注解处理。 | | torch.fx.graph._register_custom_builtin | 注册 FX codegen 需要识别的自定义 builtin 对象,并更新 _custom_builtins 与 _illegal_names 内部状态。 | 用于保证生成代码能够正确引用特定全局对象,同时避免变量名与 builtin 名称冲突。 | # 【修改方案】 新增测试文件: test/fx/test_fx_graph_internal.py 文件顶部补充用途说明,明确该文件用于承载 torch.fx.graph 相关内部 API 及 CodeGen 行为的兼容性验证。 当前测试覆盖以下 4个api : 1. torch.fx.graph._format_target; 2. torch.fx.graph._is_from_torch; 3. torch.fx.graph._origin_type_map.get; 4. torch.fx.graph._register_custom_builtin。 本次采用最小化修改方案: - 仅新增 test/ 下测试文件; - 不修改 torch_npu/ 目录下实现代码; - 不引入与本次任务无关的功能改动; - 不使用 try / except 异常捕获逻辑,测试中如出现异常直接暴露; # 【资料变更】 不涉及 # 【接口变更】 不涉及 # 【功能验证】 > 测试场景: 1. torch.fx.Graph的4个API进行验证 2. NPU 设备下 tensor shape 场景验证 > 测试方法: export TORCH_DEVICE_BACKEND_AUTOLOAD=1 python /test/fx/test/fx/test_fx_graph_internal.py -v >测试结果  所有api功能性测试均已通过 # 【CheckList】 > PR提交人对以下CheckList自检项进行全量自检,自检通过或不涉及,均修改 [ ] 为 [x] - [x] 代码注释完备,正确记录错误日志 - [x] 代码实现进行了返回值、空指针等校验 - [x] PR标题正确使用类型标签,如:feat、fix、refactor、docs、test等 - [ ] PR持续集成流水线(CI)执行通过,代码检查无异常 [【开源实习】Torch-NPU API一致性对齐:补齐测试用例、API功能对齐、补齐文档(25) #1631](https://gitcode.com/Ascend/pytorch/issues/1631) See merge request: Ascend/pytorch!35604 | 2 个月前 | |
test(fx): add NPU test cases for torch.fx.GraphModule APIs [v2.10.0] Co-authored-by: gcw_IDzXRVNw<15104808434@163.com> # message auto-generated for no-merge-commit merge: !39608 merge test/fx-graphmodule-api-v2.10.0 into v2.10.0 test(fx): add NPU test cases for torch.fx.GraphModule APIs [v2.10.0] Created-by: gcw_IDzXRVNw Commit-by: gcw_IDzXRVNw Merged-by: ascend-robot Description: # 【合入来源】 > <font color="red">**https://gitcode.com/Ascend/pytorch/issues/1856**</font> - [ ] 需求 - [ ] 问题单 - [x] issue/工单 - [ ] 重构优化 - [ ] 资料更新 Fork: gcw_IDzXRVNw/pytorch_npu 分支: test/fx-graphmodule-api-v2.10.0 → Ascend/pytorch:v2.10.0 关联 Issue: https://gitcode.com/Ascend/pytorch/issues/1856 # 【修改方案】 本 PR 为用户提供的任务 issue #1856 的交付。社区用例情况: - 已在 PyTorch 官方社区 pytorch/test/test_fx.py 下逐一搜索 GraphModule.code、GraphModule.graph 等 API,确认上游不存在针对这些属性的独立测试用例(仅作为辅助断言在其他测试中引用),因此自行编写测试用例。 ### 一、API 功能说明 torch.fx.GraphModule 是 PyTorch FX 框架的核心输出类,将 fx.Graph 转化为可执行的 nn.Module,并通过 Python 代码生成实现 Python-to-Python 变换。本次验证的 10 个 API 均为纯 Python 图操作,与底层计算设备完全解耦: - torch.fx.GraphModule:nn.Module 子类,持有 fx.Graph 并自动生成 Python forward 代码,是 FX Python-to-Python 变换的基础 - torch.fx.GraphModule.__init__:从 Module/dict 根对象和 Graph 构造 GraphModule,自动拷贝图中引用的属性和子模块 - torch.fx.GraphModule.add_submodule:在指定路径添加子模块,自动创建中间占位 Module - torch.fx.GraphModule.code:只读 property,返回从 Graph 生成的 Python 源码字符串 - torch.fx.GraphModule.delete_all_unused_submodules:遍历 Graph 节点收集引用,删除未使用的子模块 - torch.fx.GraphModule.delete_submodule:删除指定路径的子模块,路径无效或为非 Module 时返回 False - torch.fx.GraphModule.graph:property (getter/setter),获取底层 Graph 或设置新 Graph(setter 自动触发 recompile) - torch.fx.GraphModule.print_readable:生成可读的模块源码打印输出 - torch.fx.GraphModule.recompile:从 Graph 重新编译forward()方法,返回 PythonCode 对象 - torch.fx.GraphModule.to_folder:将模块导出到文件夹(含 module.py、state_dict.pt、__init__.py) 上述 API 的实现特征:全部定义在 torch.fx.graph_module.GraphModule 上,torch_npu 未做任何覆盖/重写。所有 API 操作对象为 Python 对象(Graph、Node、Module 层级、字符串),不涉及 Tensor 计算或硬件算子。 ### 二、测试文件 test_fx_graph_module_api.py 用例完备性说明 | API 全路径 | 验证类型 | 核心测试用例 | 验证覆盖场景 | 验证完整性结论 | |-----------|---------|------------|------------|--------------| | torch.fx.GraphModule | 间接验证 | 通过 test_init_from_module、test_init_from_dict 验证 | 1. 类可正常导入和实例化 2. 作为 nn.Module 子类行为正确 | 通过 __init__ 测试隐式覆盖,验证完全完整 | | torch.fx.GraphModule.__init__ | 直接验证 | test_init_from_module、test_init_from_dict、test_init_sets_class_name、test_init_raises_on_bad_type | 1. Module根对象拷贝子模块 2. Dict根对象赋值属性 3. 自定义class_name 4. 非法root类型抛异常 | 覆盖三种根对象类型+异常路径,验证完全完整 | | torch.fx.GraphModule.add_submodule | 直接验证 | test_add_submodule_root_level、test_add_submodule_nested、test_add_submodule_overwrite_fails_on_non_module | 1. 根级添加 2. 嵌套路径自动创建中间Module 3. 非Module属性阻塞路径返回False | 覆盖正常+异常路径,验证完全完整 | | torch.fx.GraphModule.code | 直接验证 | test_code_returns_string、test_code_contains_forward、test_code_contains_op_names、test_code_consistent_after_recompile | 1. 返回值类型 2. 包含forward函数 3. 包含算子名 4. 多次recompile一致性 | 覆盖类型/内容/一致性,验证完全完整 | | torch.fx.GraphModule.delete_all_unused_submodules | 直接验证 | test_delete_all_unused_removes_orphans、test_delete_all_unused_preserves_used | 1. 删除孤立模块 2. 保留图中引用的模块 | 覆盖删除+保留双向验证,验证完全完整 | | torch.fx.GraphModule.delete_submodule | 直接验证 | test_delete_submodule_existing、test_delete_submodule_nested、test_delete_submodule_nonexistent、test_delete_submodule_non_module | 1. 删除已存在模块 2. 删除嵌套模块 3. 路径不存在返回False 4. 路径指向非Module返回False | 覆盖2正常+2异常路径,验证完全完整 | | torch.fx.GraphModule.graph | 直接验证 | test_graph_getter_returns_graph、test_graph_getter_has_nodes、test_graph_getter_is_consistent、test_graph_setter_reassigns_graph、test_graph_setter_triggers_recompile、test_graph_setter_forward_works、test_graph_setter_raises_on_non_graph、test_graph_setter_preserves_lint | 1. getter返回Graph实例 2. graph含placeholder/output节点 3. 重复访问一致性 4. setter更新内部引用 5. setter触发recompile 6. setter后forward正确 7. 非法类型抛AssertionError 8. lint通过 | 覆盖getter(3)/setter(5)全部路径,验证完全完整 | | torch.fx.GraphModule.print_readable | 直接验证 | test_print_readable_returns_string、test_print_readable_contains_child_code | 1. 返回字符串含class/forward 2. 包含子GraphModule代码 | 覆盖输出格式+嵌套场景,验证完全完整 | | torch.fx.GraphModule.recompile | 直接验证 | test_recompile_returns_python_code、test_recompile_preserves_forward | 1. 返回PythonCode对象 2. recompile后forward计算结果不变 | 覆盖返回类型+功能正确性,验证完全完整 | | torch.fx.GraphModule.to_folder | 直接验证 | test_to_folder_creates_files、test_to_folder_module_is_importable | 1. 创建module.py和__init__.py 2. 生成文件为合法Python | 覆盖文件生成+内容验证,验证完全完整 | 【结论】所有用例覆盖 API 的基础功能、异常行为、接口存在性、以及 NPU 设备场景,34 个测试用例完整覆盖昇腾 NPU 适配所需的最小功能集。 ### 三、昇腾 NPU 适配说明 本次 GraphModule 相关 API 在昇腾 NPU 上的验证采用如下方式,符合硬件适配要求: - 所有 10 个 API 均为纯 Python 框架层图操作(操作 Graph/Node/Module 层级对象和字符串),与底层 CPU/GPU/NPU 计算硬件完全解耦,无需任何 NPU 特定代码适配 - 测试文件包含专用 TestFxGraphModuleOnNpu 类(3 个测试用例),显式将模块和张量迁移到 NPU 设备(npu:0),验证在真实 NPU 环境下 code、graph、recompile、forward 全部正常工作 - torch_npu 未对任何 GraphModule API 进行覆盖/重写,所有 API 行为与 PyTorch 上游完全一致 【结论】本测试文件的设计合理,可充分保证 GraphModule 在昇腾 NPU 环境下的功能正确性与可用性。 # 【资料变更】 经检查 docs/zh/native_apis/ 目录下各版本路径的 API 支持情况: | API | v2.10.0 | v2.9.0 | v2.10.0 | v2.11.0 | v2.12.0 | 处理方式 | |-----|--------|--------|---------|---------|---------|---------| | torch.fx.GraphModule.code | 否→是 | 否→是 | 否→是 | 否→是 | 否→是 | 本 PR 已修正 | | torch.fx.GraphModule.graph | 否→是 | 否→是 | 否→是 | 否→是 | 否→是 | 本 PR 已修正 | | 其余 8 个 API | 已有(是) | 已有(是) | 已有(是) | 已有(是) | 已有(是) | 无需处理 | # 【接口变更】 不涉及 # 【功能验证】 - 测试文件路径:test/fx/test_fx_graph_module_api.py - 测试环境:torch 2.7.1+cpu + torch_npu 2.7.1.post4 + NPU 910B3 + CANN 8.5.0 - 本地验证结果(完整运行日志): root@hostname:/tmp# python /data/ysws/pytorch_npu/test/fx/test_fx_graph_module_api.py -v test_code_consistent_after_recompile (__main__.TestFxGraphModuleCode) ... ok test_code_contains_forward (__main__.TestFxGraphModuleCode) ... ok test_code_contains_op_names (__main__.TestFxGraphModuleCode) ... ok test_code_returns_string (__main__.TestFxGraphModuleCode) ... ok test_graph_getter_has_nodes (__main__.TestFxGraphModuleGraph) ... ok test_graph_getter_is_consistent (__main__.TestFxGraphModuleGraph) ... ok test_graph_getter_returns_graph (__main__.TestFxGraphModuleGraph) ... ok test_graph_setter_forward_works (__main__.TestFxGraphModuleGraph) ... ok test_graph_setter_preserves_lint (__main__.TestFxGraphModuleGraph) ... ok test_graph_setter_raises_on_non_graph (__main__.TestFxGraphModuleGraph) ... ok test_graph_setter_reassigns_graph (__main__.TestFxGraphModuleGraph) ... ok test_graph_setter_triggers_recompile (__main__.TestFxGraphModuleGraph) ... ok test_init_from_dict (__main__.TestFxGraphModuleInit) ... ok test_init_from_module (__main__.TestFxGraphModuleInit) ... ok test_init_raises_on_bad_type (__main__.TestFxGraphModuleInit) ... ok test_init_sets_class_name (__main__.TestFxGraphModuleInit) ... ok test_code_and_graph_on_npu (__main__.TestFxGraphModuleOnNpu) ... ok test_forward_on_npu (__main__.TestFxGraphModuleOnNpu) ... ok test_recompile_on_npu (__main__.TestFxGraphModuleOnNpu) ... ok test_print_readable_contains_child_code (__main__.TestFxGraphModulePrintReadable) ... ok test_print_readable_returns_string (__main__.TestFxGraphModulePrintReadable) ... ok test_recompile_preserves_forward (__main__.TestFxGraphModuleRecompile) ... ok test_recompile_returns_python_code (__main__.TestFxGraphModuleRecompile) ... ok test_add_submodule_nested (__main__.TestFxGraphModuleSubmodule) ... ok test_add_submodule_overwrite_fails_on_non_module (__main__.TestFxGraphModuleSubmodule) ... ok test_add_submodule_root_level (__main__.TestFxGraphModuleSubmodule) ... ok test_delete_all_unused_preserves_used (__main__.TestFxGraphModuleSubmodule) ... ok test_delete_all_unused_removes_orphans (__main__.TestFxGraphModuleSubmodule) ... ok test_delete_submodule_existing (__main__.TestFxGraphModuleSubmodule) ... ok test_delete_submodule_nested (__main__.TestFxGraphModuleSubmodule) ... ok test_delete_submodule_non_module (__main__.TestFxGraphModuleSubmodule) ... ok test_delete_submodule_nonexistent (__main__.TestFxGraphModuleSubmodule) ... ok test_to_folder_creates_files (__main__.TestFxGraphModuleToFolder) ... ok test_to_folder_module_is_importable (__main__.TestFxGraphModuleToFolder) ... ok ---------------------------------------------------------------------- Ran 34 tests in 2.316s OK 【结论】执行测试用例后,34 passed,所有测试用例在 NPU 环境下执行通过。GraphModule 核心 API 在 NPU 上验证正确。 # 【CheckList】 - [x] 代码注释完备,正确记录错误日志 - [x] 代码实现进行了返回值、空指针等校验 - [x] PR 标题正确使用类型标签:test(fx) - [x] PR 持续集成流水线(CI)执行通过,代码检查无异常 See merge request: Ascend/pytorch!39608 | 1 个月前 | |
Add test_fx_graph_pickler for NPU adapter validation Co-authored-by: yuhongming-2026<moshui_1@qq.com> # message auto-generated for no-merge-commit merge: !34859 merge add-fx-graph-api-test-v2100 into v2.10.0 test: 新增 GraphPickler NPU 适配验证与统一运行脚本 Created-by: yuhongming-2026 Commit-by: yuhongming-2026 Merged-by: ascend-robot Description: <!-- PR描述模板更新日期:20260203 --> # 【合入来源】 > <font color="red">**#1607**</font>\ > <font color="red">**请勿携带内部流程信息(需求链接、问题单、内部issue等)**</font> - [ ] 需求 - [ ] 问题单 - [x] issue/工单 - [x] 重构优化 - [ ] 资料更新 # 【修改方案】 1. 新增统一验证脚本 run_graphpickler_npu_unified.py,用于自动选择可用后端并验证 torch.fx._graph_pickler 三大 API 在 CPU/NPU 环境中的适配性。 2. 支持的目标 API 包括 GraphPickler.dumps、GraphPickler.loads 以及 Option/Options 配置项,兼容 torch.fx._graph_pickler、torch_npu.fx._graph_pickler 与 graphpickler_npu_adapter 三种后端。 3. 在验证逻辑中实现:API 存在性检查、CPU round-trip 序列化/反序列化验证、NPU round-trip 验证、Option/Options 参数测试和输出一致性对比。 4. 参考已有验证结果文档和报告,确认 graphpickler_npu_validation_report.json 中三大 API 均通过验证,Option 配置测试 7/7 通过,NPU 功能测试通过且输出完全匹配。 5. PR 文档内容已基于目录下现有 NPU_ADAPTER_FULL_REPORT.md、NPU_ADAPTER_2100_REPORT.md、ADAPTATION_REPORT.md 等验证报告进行整理。 # 【资料变更】 不涉及 # 【接口变更】 不涉及 # 【功能验证】 - 通过统一验证脚本 run_graphpickler_npu_unified.py 运行结果确认: - NPU 可用:torch.npu.is_available() 返回 true。 - API 存在性:GraphPickler.dumps、GraphPickler.loads、Option 均存在且可调用。 - CPU round-trip:序列化成功,反序列化成功,输出与原始模型一致。 - NPU round-trip:NPU 模型序列化/反序列化成功,NPU 输出完全匹配。 - Option 配置测试:默认/压缩/保留设备信息/包含元数据/完整性校验/二进制格式/全部选项均通过,7/7 通过率 100%。 - 参考验证报告:graphpickler_npu_validation_report.json、NPU_ADAPTER_FULL_REPORT.md、NPU_ADAPTER_2100_REPORT.md 等。 - 已补充 PR 文档,便于 reviewer 直接确认适配结果。 # 【CheckList】 - [x] 代码注释完备,正确记录错误日志 - [x] 代码实现进行了返回值、空指针等校验 - [x] PR标题正确使用类型标签,如:feat、fix、refactor、docs、test等 - [ ] PR持续集成流水线(CI)执行通过,代码检查无异常 See merge request: Ascend/pytorch!34859 | 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: !35082 merge task2-v2.10.0-test-fx-lazy-format into v2.10.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!35082 | 2 个月前 | |
test(fx): add NPU test cases and patch for torch.fx.node APIs Co-authored-by: hantao55_<terry.han27@qq.com> # message auto-generated for no-merge-commit merge: !37788 merge test-fx-node-2.10.0 into v2.10.0 test(fx): add NPU test cases and patch for torch.fx.node APIs Created-by: hantao55_ Commit-by: hantao55_ Merged-by: ascend-robot Description: <!-- PR描述模板更新日期:20260203 --> # 【合入来源】 >[[Usage]: torch.fx.node相关API NPU测试适配说明](https://gitcode.com/Ascend/pytorch/issues/2291) - [ ] 需求 - [ ] 问题单 - [x] issue/工单 - [ ] 重构优化 - [ ] 资料更新 # 【修改方案】 一、API功能说明 API范围: - torch.fx.node._type_repr - torch.fx.Node.all_input_nodes - torch.fx.Node.append - torch.fx.Node.args - torch.fx.Node.format_node - torch.fx.Node.insert_arg - torch.fx.Node.is_impure - torch.fx.Node.kwargs - torch.fx.node.map_aggregate - torch.fx.node.map_arg - torch.fx.Node.next - torch.fx.Node.normalized_arguments - torch.fx.Node.prepend - torch.fx.Node.prev - torch.fx.Node.replace_all_uses_with torch.fx.Node是torch.fx.Graph中的基本执行单元,以双向链表形式组织。上述15个API属于PyTorch torch.fx框架层的图节点操作与工具函数: - torch.fx.node._type_repr是模块级私有函数,将Python类型转换为可读字符串,供图的类型注解生成使用; - torch.fx.Node.all_input_nodes是args中所有Node类型元素的只读视图; - torch.fx.Node.append/prepend调整节点在链表中的拓扑位置; - torch.fx.Node.args/kwargs是节点的位置参数和关键字参数,赋值时自动维护use-def关系; - torch.fx.Node.format_node返回节点的可读字符串表示; - torch.fx.Node.insert_arg在指定位置插入新参数并更新use-def; - torch.fx.Node.is_impure判断节点是否有副作用,是DCE等优化pass的判断依据; - torch.fx.node.map_aggregate对任意嵌套结构(list/tuple/dict)递归应用函数; - torch.fx.node.map_arg对节点args结构中每个Node递归应用函数; - torch.fx.Node.next/prev是链表的后继/前驱指针属性; - torch.fx.Node.normalized_arguments将节点args+kwargs按函数签名规范化为统一格式; - torch.fx.Node.replace_all_uses_with将所有引用当前节点处替换为另一节点。 - 核心特性:上述15个API均为纯框架层图操作逻辑,操作对象是torch.fx.Graph的节点结构(Node链表、args/kwargs参数列表、use-def依赖关系等Python对象),不直接触发任何CPU/GPU/NPU硬件算子,不涉及Tensor数据的读写或设备相关计算,与计算设备完全解耦。其中torch.fx.node.map_aggregate是唯一一个以Tensor作为被操作数据的API(将函数递归应用于嵌套结构中的每个Tensor),其余14个API均在图结构层面工作,无论底层硬件为何均行为一致。 - NPU适配意义:torch.fx是PyTorch模型编译、图变换、量化、算子融合等核心优化流程的基础设施,在NPU环境下的模型编译与推理优化中被广泛调用。验证上述API在NPU环境下的行为一致性,可确保基于torch.fx构建的图变换pass、自定义优化器、模型导出工具在昇腾NPU上与CPU/GPU环境行为完全一致,避免因图操作层的隐性差异导致NPU上的模型编译失败或优化结果不符合预期。 二、测试用例完备性说明 (一)以下API在PyTorch官方test/test_fx.py中有对应测试函数,且用例全程操作的是图结构对象(Node/Graph),不创建也不执行任何Tensor计算,与设备完全无关,天然兼容NPU环境: | API | 测试文件 | 测试函数 | | ---- | ---- | ---- | | torch.fx.Node.all_input_nodes | test/test_fx.py | test_all_input_nodes | | torch.fx.Node.append | test/test_fx.py | test_wrong_topo | | torch.fx.Node.args | test/test_fx.py | test_reassign_args_kwargs_uses | | torch.fx.Node.format_node | test/test_fx.py | test_pretty_print_node | | torch.fx.Node.insert_arg | test/test_fx.py | test_insert_arg | | torch.fx.Node.prepend | test/test_fx.py | test_prepend_does_not_leak, test_prepend_self | | torch.fx.Node.replace_all_uses_with | test/test_fx.py | test_remove_uses, test_remove_uses_with_custom_filter | 各测试函数无Tensor计算的具体依据: - test_all_input_nodes:仅调用graph.placeholder/call_module/get_attr/call_function建立图结构,用assertEqual验证all_input_nodes列表内容,无Tensor创建。 - test_wrong_topo:通过nodes[3].append(nodes[2])制造拓扑错误,验证graph.lint()抛出RuntimeError,无Tensor创建。 - test_reassign_args_kwargs_uses:通过Proxy构建图后直接对node.args赋值,验证users字典同步更新,无Tensor创建或执行。注:该函数只操作node.args,未对node.kwargs做任何赋值或断言。 - test_pretty_print_node:对symbolic_trace的图调用format_node(),用FileCheck验证字符串内容;torch.rand(3,4)仅为Module初始化参数,symbolic_trace不执行真实计算。 - test_insert_arg:torch.tensor(0)仅作为torch.nn.Buffer初始值,全程只验证len(output_node.args)、a.users等图结构属性,无任何模型执行。 - test_prepend_does_not_leak:验证节点删除后weakref失效(内存释放),无Tensor计算。 - test_prepend_self:验证b.prepend(b)后图节点数不变,无Tensor计算。 - test_remove_uses / test_remove_uses_with_custom_filter:验证replace_all_uses_with后users字典变化,无Tensor计算。 (二)PyTorch官方有用例且需要NPU适配的API - torch.fx.node.map_aggregate 用例文件:test/dynamo/test_functions.py 测试函数:test_fx_map_aggregate(直接覆盖) 用例中创建torch.randn(4)并执行y * 2乘法计算,同时通过torch.compile编译执行,Tensor是核心被测对象,需迁移到NPU。 以下2个API在官方测试中有Tensor计算,但经实际验证确认无需NPU适配: - torch.fx.Node.is_impure 用例文件:test/fx/test_dce_pass.py 测试函数:test_impure_nodes_args、test_impure_kwargs、test_impure_custom is_impure()是纯Python属性判断(判断节点是否有副作用),与设备无关。测试中的m(*inputs)是验证DCE变换前后模型输出一致的兜底验证,不是验证is_impure本身的NPU行为。 实际运行原始社区用例(CPU),三个测试函数全部通过,无需NPU适配。 运行命令: python -m unittest \ fx.test_dce_pass.TestDCE.test_impure_nodes_args \ fx.test_dce_pass.TestDCE.test_impure_kwargs \ fx.test_dce_pass.TestDCE.test_impure_custom -v 运行结果:Ran 3 tests / OK - torch.fx.Node.normalized_arguments 用例文件:test/test_fx_experimental.py 测试函数:test_normalize_modules_exhaustive normalized_arguments将节点args+kwargs按函数签名规范化,为纯Python图操作,与设备无关。测试中的traced(*inputs)/mod(*inputs)是归一化变换后的兜底验证,不是验证normalized_arguments的NPU行为。尝试迁移到NPU时,因穷举所有torch.nn模块触发了两类无关报错: (1)PadV3算子库未加载(含padding的卷积模块) (2)complex128 dtype不支持aclnnIsClose(复数类模块) 两个报错均与torch.fx.Node.normalized_arguments无关,证明Tensor是配角。 社区原有CPU测试已充分覆盖该API全部功能,无需NPU适配。 (三)PyTorch官方无用例、需新增用例的API 以下API在PyTorch官方测试目录中无直接测试函数,经全量搜索确认: - torch.fx.node._type_repr - torch.fx.Node.kwargs - torch.fx.node.map_arg - torch.fx.Node.next - torch.fx.Node.prev 新增用例文件:test/fx/test_fx_node_api.py 覆盖上述API的核心功能,已测试验证通过。 三、API适配方案 1. API源码无需修改:上述涉及API均为PyTorch框架层图操作,不涉及NPU kernel开发或算子注册。 2. 情况(一)涉及API:社区原有用例天然兼容NPU环境。 3. 情况(二)涉及API(torch.fx.node.map_aggregate): 在test_upstream/test/dynamo/test_functions.py.patch中,调用npu(),采用最小化原则,不影响文件内其他测试函数。 4. 情况(三)涉及API:新增test/fx/test_fx_node_api.py,涵盖_type_repr、Node.kwargs(getter/setter/use-def更新)、map_arg(节点收集/非节点穿透/嵌套结构/callable校验)、Node.next/prev(基本顺序/append-prepend后的顺序变化/next-prev一致性)。上述API均为纯图结构/字符串操作,不涉及Tensor,无需设备适配。 # 【资料变更】 已检查API支持清单。变更如下: (1)以下涉及API在文档中已有记录但标注为"否",本次更正为"是": torch.fx.Node.all_input_nodes、torch.fx.Node.args、torch.fx.Node.kwargs、 torch.fx.Node.next、torch.fx.Node.prev (2)以下涉及API在文档中完全缺失,本次新增记录("是"): torch.fx.node._type_repr、torch.fx.Node.insert_arg、 torch.fx.node.map_arg、torch.fx.node.map_aggregate # 【接口变更】 不涉及 # 【功能验证】 验证环境如下 操作系统:Ubuntu 昇腾硬件:800I A2 CANN软件版本:8.5.0 2.10.0:torch 2.10.0+cpu / torch-npu 2.10.0rc2 运行命令与结果: log (test210) [root@4e6c21e2fea3 ascend-pytorch-work]# pip list | grep torch torch 2.10.0+cpu torch_npu 2.10.0rc2 (test210) [root@4e6c21e2fea3 ascend-pytorch-work]# python official-pytorch-v2.10.0/test/dynamo/test_functions.py -k test_fx_map_aggregate stats [('calls_captured', 6), ('unique_graphs', 2)] . ---------------------------------------------------------------------- Ran 1 test in 1.560s OK (test210) [root@4e6c21e2fea3 test]# python fx/test_fx_node_api.py ................... ---------------------------------------------------------------------- Ran 19 tests in 0.045s OK # 【CheckList】 - [x] 代码注释完备,正确记录错误日志 - [x] 代码实现进行了返回值、空指针等校验 - [x] PR标题正确使用类型标签,如:feat、fix、refactor、docs、test等 - [x] PR持续集成流水线(CI)执行通过,代码检查无异常 See merge request: Ascend/pytorch!37788 | 2 个月前 | |
| 2 年前 | ||
test(fx): add NPU coverage for PropagateUnbackedSymInts and rebind_unbacked Co-authored-by: 李昊飞<3525247968@qq.com> # message auto-generated for no-merge-commit merge: !37266 merge symbolic-shapes-propagate-unbacked-symints-v2.10.0 into v2.10.0 test(fx): add NPU coverage for PropagateUnbackedSymInts and rebind_unbacked Created-by: lihaofei-2026 Commit-by: 李昊飞 Merged-by: ascend-robot Description: <!-- PR描述模板更新日期:20260203 --> # 【合入来源】 > <font color="red">**如有社区issue,请关联issue链接**</font>\ 公开issue:https://gitcode.com/Ascend/pytorch/issues/1619 - [ ] 需求 - [ ] 问题单 - [x] issue/工单 - [ ] 重构优化 - [ ] 资料更新 # 【修改方案】 1. 一致性分析:对 torch.fx.experimental.symbolic_shapes 模块中的5个API进行torchnpu迁移适配分析,包括 PropagateUnbackedSymInts.output PropagateUnbackedSymInts.placeholder PropagateUnbackedSymInts.run PropagateUnbackedSymInts.run_node rebind_unbacked 2. 上游测试调研:分析PyTorch官方仓库(main分支)中这5个API的测试覆盖情况,发现上游无直接测试用例,仅有间接测试(通过 shape_prop.ShapeProp 和provenance 检查) 3. 下游版本排查:检查v2.7.1~v2.12.0共5个下游版本,确认所有版本均缺少这5个API的测试用例 4. 新增测试用例:在每个下游版本的 test/fx/ 目录下新建 test_fx_propagate_unbacked_symints.py 文件,覆盖全部5个API,使用NPU张量(device="npu")验证各API在NPU设备下的正确性 5. 测试验证:所有5个版本的测试用例均已通过验证 新增用例必要性分析: 1. PropagateUnbackedSymInts.run/run_node:上游 test/fx/test_fx_traceback.py::test_graph_provenance 通过 AOT export 路径间接覆盖,但缺少 NPU设备路径验证 2. PropagateUnbackedSymInts.placeholder/output:继承自 torch.fx.Interpreter,上游无独立断言 3. rebind_unbacked:上游通过 ShapeProp/FakeTensorProp/export 路径间接覆盖,但缺少 NPU 验证 结论:属于「迁移上游间接覆盖 + NPU 设备路径补齐 + 弱覆盖补独立断言」,不是重复建设 # 【资料变更】 经核查官方文档目录,本次涉及的 API 属于 torch.fx.experimental 下的编译期实验性内部接口,非对外暴露的常规 Native API,无需在文档中补充登记。 # 【接口变更】 不涉及 # 【功能验证】 测试场景:验证 PropagateUnbackedSymInts 类和 rebind_unbacked 函数在NPU张量下的功能正确性 测试方法: 1. 新增UT测试用例文件 test/fx/test_fx_propagate_unbacked_symints.py 2. 使用 torch.tensor([1, 0, 1, 0], device="npu") 创建NPU张量 3. 通过 torch.export.export 导出模型并获取fake tensor 4. 在fake_mode上下文中调用各API进行验证 测试用例覆盖: | 测试函数 |验证API | API作用 | |--|--|--| |test_propagate_unbacked_symints_run|PropagateUnbackedSymInts.run|运行整个FX图,遍历所有节点执行并传播unbacked符号绑定| |test_propagate_unbacked_symints_run_node|PropagateUnbackedSymInts.run_node|运行单个FX节点,调用rebind_unbacked重新绑定unbacked符号| |test_propagate_unbacked_symints_placeholder|PropagateUnbackedSymInts.placeholder|执行placeholoder节点,从参数迭代器获取图的输入数据| |test_propagate_unbacked_symints_output|PropagateUnbackedSymInts.output|执行output节点,返回图的最终输出结果| |test_rebind_unbacked|rebind_unbacked|在重新追踪图时,建立新旧unbacked SymInts的等价关系,确保ShapeEnv一致性| 本 PR 不是对上游已有测试文件进行 NPU 适配,因此不涉及 test_upstream patch 文件修改。 测试命令及结果: python /workspace/user_data/pytorch-v2.10.0/test/fx/test_fx_propagate_unbacked_symints.py  # 【CheckList】 > PR提交人对以下CheckList自检项进行全量自检,自检通过或不涉及,均修改 [ ] 为 [x] - [x] 代码注释完备,正确记录错误日志 - [x] 代码实现进行了返回值、空指针等校验 - [x] PR标题正确使用类型标签,如:feat、fix、refactor、docs、test等 - [x] PR持续集成流水线(CI)执行通过,代码检查无异常 See merge request: Ascend/pytorch!37266 | 2 个月前 | |
| 2 年前 | ||
test(fx): add create_args_for_root testcases into test_fx_tracer_create_arg.py Co-authored-by: q15546010075<guangxi12345202302@163.com> # message auto-generated for no-merge-commit merge: !37611 merge test-fx-create-args-for-root-v2.10.0 into v2.10.0 test(fx): add create_args_for_root testcases into test_fx_tracer_create_arg.py Created-by: q15546010075 Commit-by: q15546010075 Merged-by: ascend-robot Description: <!-- PR描述模板更新日期:20260203 --> # 【合入来源】 - [x] 问题单 > https://gitcode.com/Ascend/pytorch/issues/2235 pytorch社区用例没有验证torch.fx.Tracer.create_args_for_root,故将该API测试用例合入已有的test_fx_tracer_create_arg.py中统一维护。 # 【修改方案】 一、API 功能说明 torch.fx.Tracer.create_args_for_root(root_fn, is_module, concrete_args=None) 在符号追踪过程中,根据被追踪函数/模块的签名为每个参数创建 placeholder 代理节点。纯 Python 层操作,不涉及张量计算或硬件驱动。 二、修改内容 在已有的 test/fx/test_fx_tracer_create_arg.py 基础上追加 create_args_for_root 的 7 个测试方法到 TestTracerCreateArg 类中,覆盖 Module 模式、Function 模式、concrete_args(dict/tuple)、默认值参数等核心路径。同时在文件顶部(类外)新增 TwoArgModule、DefaultArgModule 供新用例使用。原有代码零修改。 三、NPU适配 该 API 纯粹操作 Python 函数签名和 FX Graph IR 节点,内部仅调用 inspect 标准库和 PyTorch 纯 Python 层数据结构,不依赖 CUDA/NPU 扩展库或底层驱动,无需 NPU 适配。用例中所有断言均针对 Python 对象属性,无张量创建或设备操作。 # 【资料变更】 已检查 docs/zh/native_apis 各版本目录,torch.fx.Tracer.create_args_for_root 均已标注为"是"(支持),文档状态与实际一致,无需修改文档。 # 【接口变更】 > 不涉及 # 【功能验证】 在 v2.10.0 环境上执行该用例,通过,日志如下: (venv_2.10.0) root@npu1-2:/tmp# python3 test_fx_tracer_create_arg.py ............ ...... ---------------------------------------------------------------------- Ran 19 tests in 1.161s OK # 【CheckList】 - [x] 代码注释完备,正确记录错误日志 - [x] 代码实现进行了返回值、空指针等校验 - [x] PR标题正确使用类型标签,如:feat、fix、refactor、docs、test等 - [x] PR持续集成流水线(CI)执行通过,代码检查无异常 See merge request: Ascend/pytorch!37611 | 2 个月前 | |
test(nn): add test for fx.tracer.call_module_2.10.0 Co-authored-by: Micke Zhang<18502290727@163.com> # message auto-generated for no-merge-commit merge: !38072 merge test-fx-tracer-call-module-2.10.0 into v2.10.0 test(nn): add test for fx.tracer.call_module_2.10.0 Created-by: gcw_cRSfl0he Commit-by: Micke Zhang Merged-by: ascend-robot Description: <!-- PR描述模板更新日期:20260203 --> # 【合入来源】 [[Usage]: pytorch社区用例缺少对torch.fx.Tracer.call_module的测试,在现有test/fx/test_fx_tracer_transformer_api.py中补充用例验证NPU正确性 ](https://gitcode.com/Ascend/pytorch/issues/2245) # API 功能 ## API: torch.fx.Tracer.call_module ## 功能: Tracer.call_module 是 PyTorch FX 符号追踪(Symbolic Tracing)过程中的核心钩子函数之一,它充当了模块调用行为与 FX 计算图节点之间的转换桥梁。torch.fx.Tracer.call_module 的主要职责是在追踪(Trace)阶段,拦截对 nn.Module 子模块的调用操作,并将其转化为计算图(Graph)中的一个 call_module 节点。在 PyTorch FX 的设计中,Tracer 通过该方法完成模块调用的符号化记录,确保模型中的子模块调用能够被正确转换为计算图节点,同时保留模块名称、参数传递、节点依赖等关键信息,为后续计算图优化、代码生成、模型转换等流程提供可靠的结构支撑,是 FX 追踪流程中模块调用节点生成的核心环节。 # 【修改方案】 一、追加到现有文件而非新建 test/fx/test_fx_tracer_transformer_api.py 已是 Tracer/Transformer API 的聚合测试文件(含 trace、path_of_module、iter、keys、proxy、to_bool、getattr 等),fx.Tracer.call_module 同属 Tracer 方法,追加到该文件中可避免文件碎片化,便于后期维护。 二、在test/fx/test_fx_tracer_transformer_api.py中追加用例进行全场景、全维度、全覆盖测试。 主要内容包含setUp → 初始化 Tracer / Graph、_create_placeholder → 工具函数、13 个完整测试用例。 新增 TestTracerCallModule测试用例说明表 | 测试函数名 | 功能描述 | | :--- | :--- | | setUp | 初始化 Tracer、空计算图与根模块,为每个用例提供独立运行环境 | | _create_placeholder 工具函数 | 创建图占位节点并返回 Proxy,简化用例编写| | test_call_module_creates_node_in_graph | 验证 call_module 可合法创建图节点并插入计算图 | | test_call_module_returns_proxy_with_correct_node | 验证返回合法 Proxy,节点类型、目标名称正确 | | test_call_module_preserves_positional_args | 验证多位置参数能正确绑定到节点 args | | test_call_module_preserves_kwargs | 验证关键字参数能正确保留到节点 kwargs | | test_call_module_chains_multiple_calls | 验证多模块连续调用,节点依赖关系正确 | | test_call_module_same_module_multiple_times | 验证同一模块多次调用生成独立节点 | | test_call_module_with_nested_module_path | 验证嵌套模块路径解析正确 | | test_call_module_with_sequential_indexing | 验证 Sequential 容器索引调用正常 | | test_call_module_result_can_be_used_in_operations | 验证 call_module 输出可参与符号计算 | | test_call_module_with_different_module_types | 验证多类常见模块(Linear/Conv2d/ReLU 等)兼容调用| | test_call_module_graph_contains_only_call_module_nodes | 验证纯手动构建图无冗余节点 | | test_call_module_module_must_be_registered | 验证未注册模块会抛出预期异常 | | test_call_module_with_single_arg | 验证单参数调用场景参数绑定正确 | | test_call_module_preserves_output_for_further_tracing | 验证构建的图可执行且计算结果正确 | 三、NPU适配 1、纯 Python 层抽象,无硬件相关逻辑 call_module API 仅负责「模块调用 → call_module 图节点」的符号映射,是纯逻辑层接口,不涉及任何硬件相关计算、内存、通信。 2、无底层算子 / 内核依赖 API 内部仅处理模块路径、参数代理、节点创建等图结构逻辑,不依赖 NPU/CUDA 相关扩展、驱动或算子内核。 3、核心逻辑与硬件解耦 - 模块调用解耦:只记录 “调用哪个子模块、传入什么参数”,不执行真实计算 - 参数传递解耦:无论输入是 CPU / NPU Tensor,节点生成逻辑完全一致 - 图结构解耦:生成节点仅保存符号信息,不携带设备标识 4、图构建上下文解耦 Tracer、Graph、root module 均为 FX 纯 Python 抽象,与硬件无关; call_module 只做符号记录,不执行设备相关内存分配或算子下发。 5、测试验证的硬件无关性体现 - 所有用例均可在 NPU 环境稳定运行: - NPU 张量输入不影响 call_module 节点生成规则 - 模块、代理、图结构在 NPU 上行为与标准环境完全一致 - 无需针对 NPU 修改任何 API 逻辑 简言之,call_module 是硬件无关的纯 Python 层图构建 API,核心逻辑不耦合任何特定硬件,因此在昇腾 NPU 上可直接复用、无需修改。 # 【资料变更】 已经在 https://gitcode.com/Ascend/pytorch/treeN2.7.1/docs/zh/native apis 的文档中进行查找与验证。该目录下的pytorch.2-7.1至pytorch.2-12-0文件夹下的torch-fx.md已经对"torch.fx.Tracer.call_module"全部标注"是"。无需更改。      # 【接口变更】 > 不涉及 # 【功能验证】 ## 测试文件路径 test/fx/test_fx_tracer_transformer_api.py ## 验证环境 对应版本:torch-npu 2.10.0 操作系统:AlmaLinux 8.10 CANN 软件版本:8.5.0 安装的软件版本:torch、torch-npu 2.10.0 ## 验证结果 `` [root@b33ba4bec663 test]# python -m pytest fx/test_fx_tracer_transformer_api.py::TestTracerCallModule -v =================================================================== test session starts =================================================================== platform linux -- Python 3.11.13, pytest-9.0.2, pluggy-1.6.0 -- /usr/local/bin/python cachedir: .pytest_cache rootdir: /home/ph/pytorch configfile: pyproject.toml collected 14 items fx/test_fx_tracer_transformer_api.py::TestTracerCallModule::test_call_module_chains_multiple_calls PASSED [ 7%] fx/test_fx_tracer_transformer_api.py::TestTracerCallModule::test_call_module_creates_node_in_graph PASSED [ 14%] fx/test_fx_tracer_transformer_api.py::TestTracerCallModule::test_call_module_graph_contains_only_call_module_nodes PASSED [ 21%] fx/test_fx_tracer_transformer_api.py::TestTracerCallModule::test_call_module_module_must_be_registered PASSED [ 28%] fx/test_fx_tracer_transformer_api.py::TestTracerCallModule::test_call_module_preserves_kwargs PASSED [ 35%] fx/test_fx_tracer_transformer_api.py::TestTracerCallModule::test_call_module_preserves_output_for_further_tracing PASSED [ 42%] fx/test_fx_tracer_transformer_api.py::TestTracerCallModule::test_call_module_preserves_positional_args PASSED [ 50%] fx/test_fx_tracer_transformer_api.py::TestTracerCallModule::test_call_module_result_can_be_used_in_operations PASSED [ 57%] fx/test_fx_tracer_transformer_api.py::TestTracerCallModule::test_call_module_returns_proxy_with_correct_node PASSED [ 64%] fx/test_fx_tracer_transformer_api.py::TestTracerCallModule::test_call_module_same_module_multiple_times PASSED [ 71%] fx/test_fx_tracer_transformer_api.py::TestTracerCallModule::test_call_module_with_different_module_types fx/test_fx_tracer_transformer_api.py::TestTracerCallModule::test_call_module_with_different_module_types PASSED [ 78%] fx/test_fx_tracer_transformer_api.py::TestTracerCallModule::test_call_module_with_nested_module_path PASSED [ 85%] fx/test_fx_tracer_transformer_api.py::TestTracerCallModule::test_call_module_with_sequential_indexing PASSED [ 92%] fx/test_fx_tracer_transformer_api.py::TestTracerCallModule::test_call_module_with_single_arg PASSED [100%] ==================================================================== warnings summary ===================================================================== ../../../../opt/_internal/cpython-3.11.13/lib/python3.11/site-packages/torch/jit/_script.py:365: 14 warnings /opt/_internal/cpython-3.11.13/lib/python3.11/site-packages/torch/jit/_script.py:365: DeprecationWarning: torch.jit.script_method is deprecated. Please switch to torch.compile or torch.export. warnings.warn( -- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html =================================================== 14 passed, 14 warnings, 5 subtests passed in 8.73s ==================================================== [root@b33ba4bec663 test]# `` ## 结果说明 测试用例执行通过。 # 【CheckList】 > PR提交人对以下CheckList自检项进行全量自检,自检通过或不涉及,均修改 [ ] 为 [x] - ✅ 代码注释完备,正确记录错误日志 - ✅ 代码实现进行了返回值、空指针等校 - ✅ PR标题正确使用类型标签,如:feat、fix、refactor、docs、test等 - ✅ PR持续集成流水线(CI)执行通过,代码检查无异常 See merge request: Ascend/pytorch!38072 | 2 个月前 | |
| 2 年前 | ||
[test] add fx graph api coverage Co-authored-by: baoxuebin_2026<xuebin@isrc.iscas.ac.cn> # message auto-generated for no-merge-commit merge: !34554 merge codex/fx-graph-apis-v2100 into v2.10.0 [test] add fx graph api coverage 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.graph.map_arg - torch.fx.Graph.node_copy - torch.fx.Graph.nodes - torch.fx.Graph.on_generate_code - torch.fx.Graph.output # 【修改方案】 1. 对目标 API 进行现状核查,确认在当前 PyTorch 2.9.0 + torch_npu 2.9.0rc1 环境中已实际支持,不需要补齐底层实现。 2. 补充独立测试文件 test/fx/test_graph_api.py,覆盖以下场景: - map_arg 对嵌套参数结构的映射行为 - Graph.node_copy 的图节点复制行为 - Graph.nodes 的拓扑顺序遍历行为 - Graph.on_generate_code 对生成代码的 hook 行为 - Graph.output 在 NPU 输入场景下的实际执行验证 3. 采用最小化修改方案,不改动已有底层实现,仅补齐测试覆盖。 # 【资料变更】 不涉及。本文 PR 仅补测试,不包含资料修改。 # 【接口变更】 不涉及。未修改跨仓接口,也未修改用户可见接口行为。 # 【功能验证】 测试场景: 1. torch.fx 图相关 API 的核心行为验证 2. NPU 设备下 Graph.output 的实际运行验证 测试方法: ```bash cd /data/pytorch TORCH_DEVICE_BACKEND_AUTOLOAD=1 python3.11 test/fx/test_graph_api.py -v [#1635](https://gitcode.com/Ascend/pytorch/issues/1635) See merge request: Ascend/pytorch!34554 | 3 个月前 | |
| 2 年前 | ||
| 2 年前 | ||
test(fx): add tests for proxy tensor api Co-authored-by: nannan-2026<1794949109@qq.com> # message auto-generated for no-merge-commit merge: !34871 merge test-proxy-tensor-api-v2.10.0 into v2.10.0 test(fx): add tests for proxy tensor api Created-by: nannan-2026 Commit-by: nannan-2026 Merged-by: ascend-robot Description: # 【合入来源】 > <font color="red">**如有社区issue,请关联issue链接**</font>\ > <font color="red">**请勿携带内部流程信息(需求链接、问题单、内部issue等)**</font> - [ ] 需求 - [ ] 问题单 - [x] issue/工单 - [ ] 重构优化 - [ ] 资料更新 关联 issue:https://gitcode.com/Ascend/pytorch/issues/1609 # 【修改方案】 > 请描述修改内容的具体实现,涉及哪些组件之间进行交互,可以用1、2、3、...进行罗列\ > 如果是需求或者重构类的PR,需要补充详细设计文档(说明上下游组件关系、时序图、类图、DFX能力等内容) 本 PR 针对 torch.fx.experimental.proxy_tensor 相关 API 新增专项验证用例,覆盖如下 API: - torch.fx.experimental.proxy_tensor.get_proxy_mode - torch.fx.experimental.proxy_tensor.handle_sym_dispatch - torch.fx.experimental.proxy_tensor.make_fx - torch.fx.experimental.proxy_tensor.maybe_disable_thunkify - torch.fx.experimental.proxy_tensor.maybe_enable_thunkify ## 一、API 功能说明 1. torch.fx.experimental.proxy_tensor.get_proxy_mode 该 API 用于获取当前活跃的 ProxyTorchDispatchMode。在普通执行环境下返回 None,在 make_fx tracing 过程中可获取到当前 proxy mode。 2. torch.fx.experimental.proxy_tensor.handle_sym_dispatch 该 API 用于处理 proxy tensor tracing 过程中的 symbolic dispatch 逻辑,依赖当前 proxy mode 环境。该接口属于 proxy_tensor tracing 内部调度相关 helper。 3. torch.fx.experimental.proxy_tensor.make_fx 该 API 用于对 Python 函数进行 tracing,并生成对应的 torch.fx.GraphModule。生成后的 GraphModule 可用于表示被 trace 函数的 ATen 图结构。 4. torch.fx.experimental.proxy_tensor.maybe_enable_thunkify 该 API 用于在特定上下文中启用 thunkify 相关逻辑,可作为上下文管理器使用。 5. torch.fx.experimental.proxy_tensor.maybe_disable_thunkify 该 API 用于在特定上下文中关闭 thunkify 相关逻辑,可作为上下文管理器使用。 ## 二、上游社区用例分析与用例补齐说明 经检索,PyTorch 上游社区中已存在部分 make_fx 相关测试用例。例如在 test/export/test_export.py 中存在 test_stack_trace_make_fx 等用例,会调用 torch.fx.experimental.proxy_tensor.make_fx。 但经分析,现有 make_fx 用例主要验证的是 export / stack trace 等上层功能路径中 make_fx 的使用效果,测试重点并不是 make_fx API 本身的基础行为,也未集中覆盖本次 issue 要求的 torch.fx.experimental.proxy_tensor 相关 API 组合。具体来说,现有用例未充分覆盖以下内容: 1. 未独立验证 make_fx 返回对象是否为 torch.fx.GraphModule; 2. 未独立验证 make_fx 生成的 GraphModule 执行结果是否与原函数一致; 3. 未验证 get_proxy_mode 在 tracing 外返回 None、在 make_fx tracing 中可获取到 proxy mode 的行为; 4. 未验证 handle_sym_dispatch 作为 proxy tensor tracing 内部 helper 对 proxy mode 的依赖关系; 5. 未验证 maybe_enable_thunkify / maybe_disable_thunkify 作为上下文管理器在 make_fx tracing 中可稳定执行; 6. 未针对 Torch-NPU 场景验证测试张量迁移到当前 accelerator / NPU 侧运行。 同时,经检索,本次任务中的以下 API 未发现独立、直接的专项验证用例: - torch.fx.experimental.proxy_tensor.get_proxy_mode - torch.fx.experimental.proxy_tensor.handle_sym_dispatch - torch.fx.experimental.proxy_tensor.maybe_disable_thunkify - torch.fx.experimental.proxy_tensor.maybe_enable_thunkify 因此,现有上游社区用例对 make_fx 有调用覆盖,但不足以完整覆盖本次 issue 中 5 个 proxy_tensor API 的专项兼容性验证需求。基于此,本 PR 新增轻量级专项测试文件: text test/fx/test_proxy_tensor_api.py 该文件集中验证 torch.fx.experimental.proxy_tensor 相关 API 的基础行为,且测试张量通过 .to(device_type) 迁移到当前 accelerator / NPU 侧运行,满足 Torch-NPU API 一致性测试要求。 ## 三、新增测试文件说明 本 PR 新增测试文件: text test/fx/test_proxy_tensor_api.py 该测试文件用于集中验证 torch.fx.experimental.proxy_tensor 相关 API 的基础兼容性。为便于后续维护和扩展,文件开头已补充英文说明,说明该文件用于补充 proxy_tensor API 的 NPU 侧验证用例,并可继续扩展同类 API 测试。 文件开头说明如下: python """ Add validation cases for torch.fx.experimental.proxy_tensor APIs on NPU: 1. PyTorch community lacks sufficient and direct API validations for some proxy_tensor APIs, so this file is added. 2. This file validates get_proxy_mode, handle_sym_dispatch, make_fx, maybe_enable_thunkify, and maybe_disable_thunkify (extendable). """ 新增测试覆盖以下场景: 1. make_fx tracing 基础场景 通过 make_fx(fn)(x, y) 对函数进行 tracing,验证返回结果为 GraphModule,并验证生成的 GraphModule 执行结果与原函数一致。 2. get_proxy_mode tracing 场景 验证 get_proxy_mode 在 tracing 外返回 None,在 make_fx tracing 过程中可以获取到非空 proxy mode。 3. handle_sym_dispatch proxy mode 依赖场景 验证 handle_sym_dispatch 为可调用对象,并验证其依赖当前 proxy mode 的行为。在非 proxy mode 环境下直接调用会触发断言,符合该 helper 的使用约束。 4. maybe_enable_thunkify / maybe_disable_thunkify 上下文管理器场景 验证 maybe_enable_thunkify 和 maybe_disable_thunkify 可作为上下文管理器正常使用,并可在 make_fx tracing 过程中稳定执行。 ## 四、NPU 适配说明 上述 API 均属于 torch.fx.experimental.proxy_tensor Python 层 tracing / proxy tensor 相关接口,不涉及新增 NPU 算子实现,也不涉及 C++、Kernel 或底层算子逻辑修改。 但测试用例中涉及张量输入,因此测试张量需要运行在 NPU / 当前 accelerator 侧。本 PR 使用如下方式获取当前 accelerator: python device_type = acc.type if (acc := torch.accelerator.current_accelerator()) else "cpu" 并通过如下方式将测试张量迁移到当前设备: python x = torch.randn(2, 3).to(device_type) y = torch.randn(2, 3).to(device_type) 在 Torch-NPU 测试环境中,当前 accelerator 为 NPU,因此测试张量会通过 .to(device_type) 迁移到 NPU 侧运行,满足新增用例涉及张量需在 NPU 侧执行的要求。 ## 五、涉及版本说明 本次用例补齐分别提交到以下目标分支: - v2.7.1 - v2.9.0 - v2.10.0 - v2.11.0 - v2.12.0 - master 各版本均单独提交 PR,目标分支与源码分支一一对应。 本次根据当前任务要求提交上述分支,不涉及 v2.8.0 分支。 # 【资料变更】 > 请确认是否涉及资料变更。如涉及,需要在PR中体现,并简要说明修改内容。如不涉及,需填写“不涉及” 不涉及。 本 PR 仅新增测试用例,不修改资料文档。 相关 API 的 docs/zh/native_apis 支持状态已通过单独文档 PR 补充。 本 PR 仅新增 test/fx/test_proxy_tensor_api.py 测试用例,不直接修改 docs/zh/native_apis 资料文档。 但已结合配套资料补齐任务,对本次涉及的 torch.fx.experimental.proxy_tensor 相关 API 在 docs/zh/native_apis 中的资料支持状态进行了同步核查。 资料支持情况核查结论如下: 1. 本次任务涉及的 API 均属于 torch.fx.experimental.proxy_tensor 下的 Python 层 tracing / proxy tensor 相关接口; 2. 相关 API 的资料支持状态需要结合目标版本实际能力在 docs/zh/native_apis/pytorch_*/torch-fx.md 中进行声明; 3. 本测试 PR 仅负责补齐 test 目录下的专项用例与行为验证,不涉及 torch_npu/ 目录下的功能代码修改,也不涉及资料文档修改; 4. 相关 API 的 docs/zh/native_apis 支持状态已通过单独资料 PR 补充; 5. 本 PR 的测试验证范围与当前代码实际可验证能力保持一致,避免“资料声明”和“测试验证”口径 # 【接口变更】 > 请确认是否涉及跨代码仓或者客户面可见的接口变更。如涉及,需要详细说明接口以及对应的变更内容,同时需要在资料中体现。如不涉及,需填写“不涉及” 不涉及。 本 PR 不修改客户可见接口,不修改 API 行为,仅新增测试用例验证现有 API 兼容性。 # 【功能验证】 > 说明测试场景,测试方法。如果本次测试方式与常规单元测试不同,请详细说明您的测试步骤\ > 新增/变更内容是否已新增/适配UT测试用例看护,并补充测试自验证截图 新增测试用例: text test/fx/test_proxy_tensor_api.py 测试命令: bash python -u test/fx/test_proxy_tensor_api.py -v 测试场景: 1. 验证 make_fx 可正常生成 GraphModule; 2. 验证 make_fx 生成的 GraphModule 执行结果与原函数一致; 3. 验证 get_proxy_mode 在 tracing 外返回 None; 4. 验证 get_proxy_mode 在 make_fx tracing 中可获取到 proxy mode; 5. 验证 handle_sym_dispatch 的可调用性及其依赖 proxy mode 的行为; 6. 验证 maybe_enable_thunkify / maybe_disable_thunkify 上下文管理器可正常使用; 7. 验证测试中涉及的张量已通过 .to(device_type) 迁移到当前 accelerator / NPU 侧运行。 本地验证结果示例: text test_get_proxy_mode_during_make_fx (__main__.TestProxyTensorAPI.test_get_proxy_mode_during_make_fx) ... ok test_handle_sym_dispatch_requires_proxy_mode (__main__.TestProxyTensorAPI.test_handle_sym_dispatch_requires_proxy_mode) ... ok test_make_fx_returns_graph_module (__main__.TestProxyTensorAPI.test_make_fx_returns_graph_module) ... ok test_thunkify_context_managers (__main__.TestProxyTensorAPI.test_thunkify_context_managers) ... ok ---------------------------------------------------------------------- Ran 4 tests in 0.333s OK 已在以下版本执行该用例: - v2.7.1:通过 - v2.9.0:通过 - v2.10.0:通过 - v2.11.0:通过 - v2.12.0:通过 - master:通过 说明: 该组 API 为 torch.fx.experimental.proxy_tensor Python 层 tracing 相关接口,不涉及 NPU 算子计算。本次测试主要验证 API 功能行为与 PyTorch 原生保持一致,同时确保测试中涉及的张量运行在当前 accelerator / NPU 侧。 # 【CheckList】 > PR提交人对以下CheckList自检项进行全量自检,自检通过或不涉及,均修改 [ ] 为 [x] - [x] 代码注释完备,正确记录错误日志 - [x] 代码实现进行了返回值、空指针等校验 - [x] PR标题正确使用类型标签,如:feat、fix、refactor、docs、test等 - [ ] PR持续集成流水线(CI)执行通过,代码检查无异常 See merge request: Ascend/pytorch!34871 | 2 个月前 | |
test: add ShapeEnv API alignment tests for symbolic_shapes Co-authored-by: liuhaodong-2026<liuhaodong@isrc.iscas.ac.cn> # message auto-generated for no-merge-commit merge: !38160 merge test-shape-env-api-alignment-v2.10.0 into v2.10.0 test: add ShapeEnv API alignment tests for symbolic_shapes Created-by: liuhaodong-2026 Commit-by: liuhaodong-2026 Merged-by: ascend-robot Description: # 【合入来源】 - [x] issue/工单 关联 issue: [#1624](https://gitcode.com/Ascend/pytorch/issues/1624) # 【修改方案】 为 torch.fx.experimental.symbolic_shapes.ShapeEnv 的 5 个 API 补齐测试用例: 1. 新增测试文件 test/fx/test_shape_env_apis.py,覆盖以下 API: - torch.fx.experimental.symbolic_shapes.ShapeEnv.deserialize_symexpr - torch.fx.experimental.symbolic_shapes.ShapeEnv.evaluate_symexpr - torch.fx.experimental.symbolic_shapes.ShapeEnv.evaluate_guards_expression - torch.fx.experimental.symbolic_shapes.ShapeEnv.evaluate_guards_for_args - torch.fx.experimental.symbolic_shapes.ShapeEnv.evaluate_sym_node 2. 用例完备性分析: - ShapeEnv.evaluate_guards_expression:PyTorch 社区已有相关用例,位于 test/test_dynamic_shapes.py,主要为符号表达式验证,不涉及 NPU 张量。本 PR 补充直接 API 调用验证。 - ShapeEnv.evaluate_sym_node:PyTorch 社区已有相关用例,位于 test/dynamo/test_dynamic_spec.py,主要为错误行为验证。本 PR 补充正常路径 API 调用验证。 - ShapeEnv.deserialize_symexpr、ShapeEnv.evaluate_symexpr、ShapeEnv.evaluate_guards_for_args:PyTorch 社区无直接测试用例,本 PR 补充对应直接调用测试。 3. 测试场景覆盖: - 常量符号表达式解析与计算。 - 带符号变量的表达式解析。 - guard 表达式返回值验证。 - FakeTensorMode 下 placeholder tensor 与真实 NPU tensor 的 guard 校验。 - SymNode 在不同 size_oblivious 参数下的求值验证。 # 【资料变更】 不涉及(文档已在独立 PR #38301 中提交) # 【接口变更】 不涉及 # 【功能验证】 在 NPU 环境下执行新增测试文件: python test/fx/test_shape_env_apis.py 执行结果: Ran 14 tests in 1.119s OK # 【CheckList】 - [x] 代码注释完备,正确记录错误日志 - [x] 代码实现进行了返回值、空指针等校验 - [x] PR标题正确使用类型标签,如:feat、fix、refactor、docs、test等 - [x] PR持续集成流水线(CI)执行通过,代码检查无异常 See merge request: Ascend/pytorch!38160 | 1 个月前 | |
| 2 年前 | ||
| 2 年前 | ||
test: add NPU coverage for symbolic_shapes APIs (issue #1614) Co-authored-by: zhangjiahao-2026<2332445379@qq.com> # message auto-generated for no-merge-commit merge: !36834 merge intern-1614-v2.10.0 into v2.10.0 test: add NPU coverage for symbolic_shapes APIs (issue #1614) Created-by: zhangjiahao-2026 Commit-by: zhangjiahao-2026 Merged-by: ascend-robot Description: # 【合入来源】 > **关联社区 issue:** https://gitcode.com/Ascend/pytorch/issues/1614 - [ ] 需求 - [ ] 问题单 - [x] issue/工单 - [ ] 重构优化 - [ ] 资料更新 # 【修改方案】 本次为 Torch-NPU API 一致性对齐任务(Issue #1614),针对 torch.fx.experimental.symbolic_shapes 下 5 个 API 补充/适配测试,**不涉及算子或 API 实现代码修改**(NPU 上与 PyTorch 2.10 行为一致)。 ## API 功能说明 torch.fx.experimental.symbolic_shapes 是 PyTorch FX 动态 shape 推导体系中的符号形状模块,负责在编译/导出过程中管理 SymInt/SymBool 约束、guard 生成及 shape 相等关系。本次验证的 5 个 API 均为纯 Python 层面的符号 shape 工具,与底层计算硬件无关。 - torch.fx.experimental.symbolic_shapes.DivideByKey:整除键类,用于在符号 shape 表达中表示对某个常量做 floor division(整除)的运算模式,可通过构造参数指定除数。 - torch.fx.experimental.symbolic_shapes.DivideByKey.get:DivideByKey 实例方法,对给定数值执行 floor division,即 value // divisor,返回整除结果。 - torch.fx.experimental.symbolic_shapes.EqualityConstraint:符号 shape 约束类,用于在 FX 图导出/动态 shape 推导中表达多个 shape 源之间的相等关系,包含 source_pairs、derived_equalities、phantom_symbols、relaxed_sources、warn_only 等字段。 - torch.fx.experimental.symbolic_shapes.has_free_symbols:判断给定对象(通常为 sympy 表达式)是否包含未被绑定的自由符号,用于区分常量表达式与含符号变量的动态 shape 表达式。 - torch.fx.experimental.symbolic_shapes.guard_size_oblivious:符号 shape guard 生成过程中的辅助函数,对布尔条件做 size-oblivious 处理,使其与具体 tensor size 解耦。 ## API 全名与测试覆盖场景 | 全名 API | 测试用例 | 覆盖场景 | |----------|----------|----------| | torch.fx.experimental.symbolic_shapes.DivideByKey | 自写 TestDivideByKeyAndEqualityConstraint::test_divide_by_key + 官方 test/export/test_export.py::test_unbacked_bindings_for_divisible_u_symint(patch) | 自写:构造 DivideByKey(4),验证 str(key) 为 ".__floordiv__(4)";官方:patch 注入 torch_npu 后复用 export 场景用例 | | torch.fx.experimental.symbolic_shapes.DivideByKey.get | 自写 TestDivideByKeyAndEqualityConstraint::test_divide_by_key_get | 构造 DivideByKey(4),调用 .get(17),验证返回 4 | | torch.fx.experimental.symbolic_shapes.EqualityConstraint | 自写 TestDivideByKeyAndEqualityConstraint::test_equality_constraint_init | 以空约束参数初始化(source_pairs=[],derived_equalities=[],phantom_symbols=[],relaxed_sources=set(),warn_only=False),验证 source_pairs、phantom_symbols 为空且 warn_only=False | | torch.fx.experimental.symbolic_shapes.has_free_symbols | 官方 test/test_dynamic_shapes.py(patch) | patch 注入 import torch_npu 后复用官方用例 | | torch.fx.experimental.symbolic_shapes.guard_size_oblivious | 官方 test/dynamo/test_misc.py(patch) | patch 注入 import torch_npu 后复用官方用例 | ## 具体改动 1. 在 test/fx/test_symbolic_shapes.py 新增 TestDivideByKeyAndEqualityConstraint 类,补充 3 个自写用例 - 使用 unittest + torch.testing._internal.common_utils.run_tests - 覆盖 API:DivideByKey、DivideByKey.get、EqualityConstraint - 用例均带 @unittest.skipUnless(torch.npu.is_available()) 2. 新增 test_upstream/test/test_dynamic_shapes.py.patch - 在 import torch 后增加 import torch_npu - 适配官方 has_free_symbols 用例 - **v2.10.0 注意:** 上游相对 v2.9.0 增加 import pytest,patch 按 v2.10.0 上下文单独生成 3. 新增 test_upstream/test/dynamo/test_misc.py.patch - 在 import torch 后增加 import torch_npu - 适配官方 guard_size_oblivious 用例 4. 新增 test_upstream/test/export/test_export.py.patch - 在 import torch 后增加 import torch_npu - 适配官方 test_unbacked_bindings_for_divisible_u_symint(断言含 DivideByKey) - **v2.10.0 注意:** 上游增加 import torch.fx.traceback as fx_traceback,不可复用 v2.9.0 patch 5. 上述 3 个 patch 已对 **v2.10.0** 上游文件执行 git apply --check,验证可应用。 # 【资料变更】 不涉及 # 【接口变更】 不涉及 # 【功能验证】 **测试环境:** torch 2.10.0 + torch_npu 2.10.0,torch.npu.is_available()==True **(1)自写用例** bash cd /tmp && python3 /data/pytorch/test/fx/test_symbolic_shapes.py \ TestDivideByKeyAndEqualityConstraint.test_divide_by_key \ TestDivideByKeyAndEqualityConstraint.test_divide_by_key_get \ TestDivideByKeyAndEqualityConstraint.test_equality_constraint_init \ 2>&1 | grep -vE "recording\.py|set_unbacked_var_to_val|E0626" | tail -10 结果:  **(2)5 API 快速验证** bash cd /tmp python -c " import torch import torch_npu from torch.fx.experimental.symbolic_shapes import ( DivideByKey, EqualityConstraint, guard_size_oblivious, has_free_symbols, ) print('DivideByKey.get(17)=', DivideByKey(4).get(17)) print('guard_size_oblivious(True)=', guard_size_oblivious(True)) print('has_free_symbols(3)=', has_free_symbols(3)) print('EqualityConstraint OK') print('npu:', torch.npu.is_available()) " 结果:  **(3)官方用例 pytest** bash cd /tmp python -m pytest --rootdir=/tmp /workspace/pytorch-upstream-210/test/test_dynamic_shapes.py -k has_free_symbols -xvs python -m pytest --rootdir=/tmp /workspace/pytorch-upstream-210/test/dynamo/test_misc.py -k guard_size_oblivious -xvs python -m pytest --rootdir=/tmp /workspace/pytorch-upstream-210/test/export/test_export.py -k test_unbacked_bindings_for_divisible_u_symint -xvs > torch.fx.experimental.symbolic_shapes.DivideByKey 相关 export 用例勿用 -k DivideByKey(会 0 selected),应使用 test_unbacked_bindings_for_divisible_u_symint。 **(4)patch 可应用性** bash cd /workspace/pytorch-upstream-210 git checkout -- test/dynamo/test_misc.py test/test_dynamic_shapes.py test/export/test_export.py git apply --check <path>/test_upstream/test/test_dynamic_shapes.py.patch && echo test_dynamic_shapes OK git apply --check <path>/test_upstream/test/dynamo/test_misc.py.patch && echo test_misc OK git apply --check <path>/test_upstream/test/export/test_export.py.patch && echo test_export OK 结果:  **UT 看护:** 已新增 test/test_symbolic_shapes.py;已为 3 个官方测试文件新增 test_upstream patch,纳入社区 test_upstream 机制。 --- # 【CheckList】 - [x] 代码注释完备,正确记录错误日志(测试代码含模块 Owner 注释;不涉及业务错误日志) - [x] 代码实现进行了返回值、空指针等校验(不涉及;仅为测试与 patch) - [x] PR标题正确使用类型标签(使用 test: 前缀) - [x] PR持续集成流水线(CI)执行通过,代码检查无异常 --- See merge request: Ascend/pytorch!36834 | 1 个月前 | |
test(fx): add DimConstraints API tests Co-authored-by: shenjunfeng-2026<1766689637@qq.com> # message auto-generated for no-merge-commit merge: !38825 merge task02-symbolic-shapes-v2100 into v2.10.0 test(fx): add DimConstraints API tests Created-by: shenjunfeng-2026 Commit-by: shenjunfeng-2026 Merged-by: ascend-robot Description: <!-- PR描述模板更新日期:20260203 适用PR:https://gitcode.com/Ascend/pytorch/pull/38825 --> # 【合入来源】 > <font color="blue">**任务 https://gitcode.com/Ascend/pytorch/issues/1613**</font>\ > <font color="blue">**关联issue https://gitcode.com/Ascend/pytorch/issues/2431**</font>\ > <font color="red">**请勿携带内部流程信息(需求链接、问题单、内部issue等)**</font> - [ ] 需求 - [ ] 问题单 - [x] issue/工单 - [ ] 重构优化 - [ ] 资料更新 # 【修改方案】 > 请描述修改内容的具体实现,涉及哪些组件之间进行交互,可以用1、2、3、...进行罗列\ > 如果是需求或者重构类的PR,需要补充详细设计文档(说明上下游组件关系、时序图、类图、DFX能力等内容) 本 PR 属于 Torch-NPU API 一致性对齐任务中的测试用例补齐,目标分支为 v2.10.0,面向 torch.fx.experimental.symbolic_shapes 下 5 个关联 API 完成上游用例核查、Torch-NPU 现有覆盖复查、API 行为验证和 UT 看护补充。 本 PR 仅修改: text test/fx/test_symbolic_shapes_api.py 最终变更为 85 行新增、0 行删除:新增 4 个直接 API 测试及对应英文说明注释,不修改 torch/fx/experimental/symbolic_shapes.py、torch_npu 功能源码、原有测试逻辑或 API 对外语义。 ## 架构上下文说明 DimDynamic 与 DimConstraints 属于 PyTorch FX / Dynamo 编译前端的符号形状基础设施。它们服务于动态图捕获、动态维度约束收集、SymPy 表达式求解、强制特化识别以及约束错误信息生成,不直接执行 Tensor 数值计算。 验证对象包括 sympy.Symbol、FloorDiv、ConstantSource、torch.export.Dim 和 inspect.Signature。测试不创建 NPU Tensor、不调用 NPU 算子,也不做精度对比;重点是确认这些编译前端 API 在 v2.10.0 中可稳定调用并产生符合 PyTorch 语义的约束结果。 ## 上游社区测试情况与用例路线判断 ### 上游源码与测试检索范围 由于开发环境访问 GitHub 不稳定,使用 PyTorch 的 Gitee 镜像进行版本化检索: text https://gitee.com/mirrors/pytorch.git 本 PR 重点检查官方 v2.10.0 tag,并交叉检查 v2.7.1、v2.9.0、v2.11.0、v2.12.0,确认 API 和测试路线在相邻版本中的延续情况。各版本源码 torch/fx/experimental/symbolic_shapes.py 均存在 DimDynamic、DimConstraints.solve、forced_specializations、prettify_results 和 rewrite_with_congruences 定义。 官方测试目录执行的核心检索模式包括: bash grep -rn "dim_constraints\.solve()" test --include="*.py" grep -rn "\.forced_specializations()" test --include="*.py" grep -rn "\.prettify_results" test --include="*.py" grep -rn "\.rewrite_with_congruences(" test --include="*.py" grep -rn "DimDynamic\." test --include="*.py" 检索日志: text /workspace/user_data/task02_logs/official_pytorch_exact_api_search_20260617_223640.log ### v2.10.0 上游检索结论 | API | PyTorch v2.10.0 upstream 测试情况 | 本 PR 路线 | | --- | --- | --- | | DimConstraints.forced_specializations | 仅命中与 forced specialization 有关的测试名称,未发现对 .forced_specializations() 的直接调用。 | 上游缺少目标方法直接看护,新增聚焦自写测试。 | | DimConstraints.prettify_results | 未检索到直接调用目标方法的官方测试。 | 新增自写测试,验证强制特化说明及建议修复。 | | DimConstraints.rewrite_with_congruences | 未检索到直接调用目标方法的官方测试。 | 新增自写测试,验证表达式改写和 congruence 记录。 | | DimConstraints.solve | test/test_dynamic_shapes.py:3028 存在 dim_constraints.solve() 直接调用。 | 官方用例无需 NPU 迁移;补充轻量直接测试,形成 Torch-NPU API 文件内稳定看护。 | | DimDynamic | v2.10.0 官方测试中检索到 43 处 DimDynamic.* 直接使用。 | 不重复新增测试,复用 Torch-NPU 已有直接覆盖。 | 进一步查看 test/test_dynamic_shapes.py 中 solve() 调用上下文后确认,该官方用例只构造 SymPy 表达式、DimConstraints、TensorPropertySource、LocalSource 等元信息,并断言 _static_results 和 _dynamic_results。用例不包含 torch.randn、torch.tensor、.cuda()、.npu()、.to(device)、NPU 算子或 CPU/NPU 精度比较,所以不需要创建 test_upstream NPU 适配 patch。 后续官方版本的交叉检索结论一致:solve() 和 DimDynamic 有直接覆盖,其余 3 个目标方法缺少直接测试,说明该路线并非 v2.10.0 的单版本偶然结果。 ### Torch-NPU v2.10.0 已有测试、patch 与资料复查 在 upstream/v2.10.0 的 test、test_upstream、docs/zh/native_apis 中进行了精确计数复查。日志: text /workspace/user_data/task02_logs/ascend_pytorch_count_search_20260617_224903.log 以下 4 个方法的结果均为: text DimConstraints.forced_specializations: test=0, test_upstream=0, docs=0 DimConstraints.prettify_results: test=0, test_upstream=0, docs=0 DimConstraints.rewrite_with_congruences:test=0, test_upstream=0, docs=0 DimConstraints.solve: test=0, test_upstream=0, docs=0 粗检索曾在 test/unsupported_test_cases/.pytorch-disabled-tests*.json 中命中 test_dim_constraints_solve_full,但该条目只是禁用记录,不是测试实现;最终计数已排除 disabled JSON,避免把名称记录误判成已有测试或 patch。 DimDynamic 在 v2.10.0 的 test 中有 20 处命中;当前文件的 stateless/stateful symbolic context 测试已直接使用 DUCK 与 INFER_STRIDE,所以不增加重复枚举测试。 ## 本次覆盖 API 的功能说明、完备性分析与用例开发思路 | API | 功能说明 | 上游及当前覆盖 | 是否需要新开发用例 | 本 PR 用例思路与完备性 | | --- | --- | --- | --- | --- | | DimConstraints.rewrite_with_congruences | 根据已知符号提示值和同余关系,改写包含整除/取模的 SymPy 表达式,并记录推导出的模约束。 | v2.10.0 upstream 和 Torch-NPU 均无目标方法直接测试。 | 需要。 | 令正整数 s0 的提示值为 5,输入 FloorDiv(s0, 2);断言输出为 s0 / 2 - 1/2,并检查记录 Mod(s0 + 1, 2),同时覆盖返回值和 congruence 副作用。 | | DimConstraints.solve | 汇总已添加的等式、不等式和符号关系,执行求解,并把结论分类写入静态结果、动态结果和替换关系。 | upstream 有嵌入式直接调用;Torch-NPU v2.10.0 无聚焦测试和 patch。 | 需要轻量补充。 | 对 s0 >= 2 求解,断言静态结果为空、动态结果为 2 <= x;后续两个测试还调用 solve() 产生静态特化,交叉覆盖动态和静态路径。 | | DimConstraints.forced_specializations | 从求解结果提取原本标记为动态、但被约束迫使成为具体值的维度,并根据 source/debug name 生成可读映射。 | upstream 无目标方法直接调用;Torch-NPU v2.10.0 无测试。 | 需要。 | 配置 source x、调试名 dx 和等式 s0 == 4,求解后断言返回 {"dx = x": 4},覆盖动态标记、来源映射和具体值输出。 | | DimConstraints.prettify_results | 将约束违反和强制特化结果转换成面向用户的说明,并结合 dynamic_shapes 生成建议修复。 | upstream 与 Torch-NPU v2.10.0 均无直接测试。 | 需要。 | 使用 inspect.signature、Dim("dx") 和强制特化结果调用接口;分别断言特化标题、dx = x 来源关系和 dx = 4 建议值,避免完整文本匹配造成脆弱测试。 | | DimDynamic | 枚举符号维度动态策略,例如 DYNAMIC、DUCK、STATIC、INFER_STRIDE,供符号上下文决定尺寸和步长动态性。 | upstream 有 43 处直接使用;Torch-NPU v2.10.0 已有 DUCK、INFER_STRIDE 直接测试。 | 不需要。 | 现有 stateless/stateful symbolic context 测试已验证枚举值进入上下文后的保存行为;继续复用现有覆盖,避免重复断言。 | 本 PR 新增 4 个直接测试;其中 solve 被 3 个测试调用,forced_specializations 被 2 个测试调用,并通过后续流程形成交叉验证。现有断言已覆盖关键输出和副作用,无需为数量增加重复测试或锁定更多私有细节。 ### 开发前行为探测与断言稳定性 编写测试前检查了实际 API 签名: text DimDynamic => (*values) DimConstraints => (symbol_to_source, var_to_val, marked_dynamic, source_name_to_debug_name) rewrite_with_congruences => (self, s, expr) solve => (self) forced_specializations => (self) prettify_results => (self, original_signature, dynamic_shapes, constraint_violation_error, forced_specializations) 行为探测日志: text /workspace/user_data/task02_logs/task02_api_probe3_final_20260617_231527.log /workspace/user_data/task02_logs/task02_prettify_probe_20260618_000308.log 探测确认:提示值为 5 时 FloorDiv(s0, 2) 可稳定改写为 s0/2 - 1/2 并记录 Mod(s0 + 1, 2);下界约束可产生 2 <= x;等式特化可返回 {"dx = x": 4};格式化结果稳定包含特化标题、来源关系和 dx = 4 建议。 断言只锁定稳定语义:prettify_results 检查三个关键子串,congruence 使用字符串集合比较,动态结果和强制特化字典采用精确断言,从而兼顾回归检测与跨版本维护性。 ### v2.10.0 用例充分性与分支适用性判断 上游 solve() 调用位于较大的 dynamic-shapes 流程中,主要验证整体约束生成结果;本 PR 的测试则直接构造最小 DimConstraints 对象,明确检查 _static_results 为空且 _dynamic_results 为 {"2 <= x"}。二者关注层级不同:前者提供流程级回归,后者提供 API 级故障定位,因此不是简单重复。 forced_specializations、prettify_results、rewrite_with_congruences 在 v2.10.0 upstream 中没有直接调用,若只依赖间接流程,一旦返回映射、同余记录或提示文本发生回归,很难快速定位到具体接口。新增测试分别覆盖“求解后提取特化”“生成可读修复建议”“改写表达式并记录模关系”三类独立职责,已经覆盖各方法最核心的可观察行为。 DimDynamic 的情况相反:官方有 43 处直接使用,Torch-NPU v2.10.0 也已有 20 处相关命中,当前文件还直接验证 DUCK 和 INFER_STRIDE。继续新增只枚举成员或重复字段保存的测试不会增加有效行为覆盖,因此采用已有用例看护。该取舍以缺口和行为分支为依据,而不是要求每个任务 API 都机械增加一个新函数。 上述新增断言不依赖设备张量、随机数或硬件执行结果,输入完全确定;同时避免比较 prettify_results 的完整长文本。因而用例能够在 v2.10.0 分支稳定运行,也便于后续版本沿用。 ## 具体修改内容 本 PR 新增以下测试方法: text test_dim_constraints_rewrite_with_congruences_records_mod_guard test_dim_constraints_solve_records_dynamic_results test_dim_constraints_forced_specializations_reports_marked_dynamic_equalities test_dim_constraints_prettify_results_reports_forced_specialization 具体实现: 1. 构造带正整数假设和具体 hint 的 SymPy 符号,验证 floor division 的同余改写结果及 _congruences 记录; 2. 通过 ConstantSource("x") 建立符号到来源的映射,验证范围约束求解后进入 _dynamic_results; 3. 使用 source_name_to_debug_name={"x": "dx"} 验证强制特化输出同时保留调试名、source 和具体值; 4. 使用 inspect.signature 与 torch.export.Dim 构造动态形状输入,验证说明文本包含原因和可执行修复建议; 5. 为每个新增测试补充两行英文注释,说明测试目标和关键断言; 6. 所有改动均位于现有 UT 文件,不改变原有测试和产品代码。 ## 适配方案说明 ### API 适配方案 5 个目标 API 在源码和验证环境中均可正常导入、构造和调用,未发现缺失实现、签名不一致、返回语义异常或 NPU 设备相关错误。因此本 PR 不修改 API 实现、枚举成员和求解逻辑,只增加测试看护。 ### 用例适配方案 对无上游直接用例的 3 个方法采用自写轻量测试;对已有 upstream 调用但无需设备迁移的 solve,不创建无实际内容的 test_upstream patch,而是在现有 API 测试文件中增加聚焦断言;对 upstream 与 Torch-NPU 都已有直接覆盖的 DimDynamic 不重复开发。 用例统一放在 test/fx/test_symbolic_shapes_api.py。该文件已集中看护 DimConstraints、SymbolicContext 和 ShapeEnv,可复用现有构造与断言模式。 # 【资料变更】 > 请确认是否涉及资料变更。如涉及,需要在PR中体现,并简要说明修改内容。如不涉及,需填写“不涉及” 不涉及。 本 PR 只补充 test/fx/test_symbolic_shapes_api.py 的 UT,不修改 docs/zh/native_apis。测试补齐不改变接口支持状态、签名、调用方式或用户可见行为;资料支持度更新应按任务要求由独立资料变更处理,不与本测试 PR 混合。 # 【接口变更】 > 请确认是否涉及跨代码仓或者客户面可见的接口变更。如涉及,需要详细说明接口以及对应的变更内容,同时需要在资料中体现。如不涉及,需填写“不涉及” 不涉及。 本 PR 未修改函数、类、枚举、参数列表、返回值或跨仓调用关系,仅新增测试断言和说明注释,不产生客户面可见接口变化。 # 【功能验证】 > 说明测试场景,测试方法。如果本次测试方式与常规单元测试不同,请详细说明您的测试步骤\ > 新增/变更内容是否已新增/适配UT测试用例看护,并补充测试自验证截图 ## 验证环境 text SoC: Ascend910_9382 Python: 3.12.13 本地验证运行时: torch 2.10.0+cpu / torch_npu 2.10.0 目标代码分支: upstream/v2.10.0 venv: /workspace/user_data/venvs/torch-npu-py312 ## 完整 UT 验证 为避免从源码目录误导入未编译的 torch_npu,从 /tmp 执行完整测试文件: bash cd /tmp /workspace/user_data/venvs/torch-npu-py312/bin/python \ /workspace/user_data/pytorch/test/fx/test_symbolic_shapes_api.py 最终结果: text .................... ---------------------------------------------------------------------- Ran 20 tests in 1.123s OK 结果说明:完整测试文件中的 20 个测试全部通过,其中包括本 PR 新增的 4 个 DimConstraints 直接测试以及文件中原有的 SymbolicContext、ShapeEnv 等回归用例。本地安装运行时与目标分支均为 torch 2.10.0,完整测试文件未出现版本兼容失败;平台 CI 也在匹配 v2.10.0 的分支构建环境中完成验证。 测试日志: text /workspace/user_data/task02_logs/v2100_add_comments_20260618_134708.log ## 差异与平台检查 本地执行: bash cd /workspace/user_data/pytorch git diff --check 结果: text git diff --check: OK 同一日志同时确认注释变更为 8 行新增、0 行删除,测试文件不存在空白符错误。提交与推送后,本地 commit 与个人 fork 远端 commit 一致。 v2.10.0 最新提交: text 04bcbae7b27eeaa7dbac73c48960fae8454050a6 平台验证结果:PR #38825 的 compile、UT/测试和代码规范检查均已通过。该结果确认新增测试能够在真实 v2.10.0 分支构建环境中运行,并且本 PR 未引入代码格式、依赖或编译回归。 # 【CheckList】 > PR提交人对以下CheckList自检项进行全量自检,自检通过或不涉及,均修改 [ ] 为 [x] - [x] 代码注释完备,正确记录错误日志 - [x] 代码实现进行了返回值、空指针等校验 - [x] PR标题正确使用类型标签,如:feat、fix、refactor、docs、test等 - [x] PR持续集成流水线(CI)执行通过,代码检查无异常 See merge request: Ascend/pytorch!38825 | 1 个月前 | |
test(dynamo): add NPU alignment tests for unspecified symbols Co-authored-by: litianyu2026<1483211810@qq.com> # message auto-generated for no-merge-commit merge: !35868 merge test-unspecified-symbols-2100 into v2.10.0 test(fx): add NPU alignment tests for unspecified symbols Created-by: litianyu_2026 Commit-by: litianyu2026 Merged-by: ascend-robot Description: # 【合入来源】 > <font color="red">https://gitcode.com/Ascend/pytorch/issues/1623</font>\ > <font color="red">**请勿携带内部流程信息(需求链接、问题单、内部issue等)**</font> - [ ] 需求 - [ ] 问题单 - [x] issue/工单 - [ ] 重构优化 - [ ] 资料更新 # 【修改方案】 > 请描述修改内容的具体实现,涉及哪些组件之间进行交互,可以用1、2、3、...进行罗列\ > 如果是需求或者重构类的PR,需要补充详细设计文档(说明上下游组件关系、时序图、类图、DFX能力等内容) 1. **架构上下文补充说明(CPU vs NPU)**: 本次任务涉及的ShapeEnv 系列接口属于 PyTorch Dynamo 编译引擎前端(图追踪阶段)的基础设施。其核心功能是在编译期处理动态形状推导中的逻辑符号对象(如产生SymInt, SymFloat或施加运行时期望断言),而非处理存储实际数据的物理张量(Tensor)。因此,这些接口的逻辑天然且必须在宿主机(CPU)上由 Python/SymPy 解释器完成,物理上无法也不需要迁移至 NPU 设备执行。本次 NPU 一致性对齐的核心目的,是验证昇腾编译后端在接收到前端抛出的这些未确定动态符号时,能否正确解析包装且不发生编译期崩溃。 2. 本PR属于开源实习任务中的功能测试用例补齐。该任务共包含 5 个ShapeEnv相关接口。经客观排查与环境验证: -torch.fx.experimental.symbolic_shapes.ShapeEnv.create_unbacked_symfloat与torch.fx.experimental.symbolic_shapes.ShapeEnv.create_unbacked_symint官方已具备完善的原生用例覆盖,在 NPU 环境下天然支持。 -torch.fx.experimental.symbolic_shapes.ShapeEnv.defer_runtime_assert接口已在 v2.7.1 跑通官方原生用例,且自 v2.8.0 起被官方重命名为torch.fx.experimental.symbolic_shapes.ShapeEnv.guard_or_defer_runtime_assert,高版本已包含原生覆盖。 - 以上 3 个接口属于 NPU 天然支持/上游变更,无需在本仓补充代码。 3. 上游 PyTorch 官方测试库中,对核心接口torch.fx.experimental.symbolic_shapes.ShapeEnv.create_unspecified_symbol和torch.fx.experimental.symbolic_shapes.ShapeEnv.create_unspecified_symint_and_symbol的测试存在空白。本 PR 专门在test/fx/目录下新增专项测试文件test_unspecified_symbols.py进行看护。 4. 新增测试用例严格根据底层源码逻辑进行设计: - 针对create_unspecified_symbol:重点验证传入0和1边界值时,触发其内部do_not_specialize_zero_one=True特性,断言其返回未确定的符号表达式对象(自由符号),确保其不被静态折叠。 - 针对create_unspecified_symint_and_symbol:验证其成功将底层的符号表达式正确包装并转化为上层编译栈所需的SymInt类型对象。 # 【资料变更】 > 请确认是否涉及资料变更。如涉及,需要在PR中体现,并简要说明修改内容。如不涉及,需填写“不涉及” 不涉及。 经核查官方文档目录https://gitcode.com/Ascend/pytorch/tree/v2.7.1/docs/zh/native_apis,本次对齐的torch.fx.experimental.symbolic_shapes.ShapeEnv.create_unspecified_symbol等接口属于torch.fx.experimental下的底层编译期实验性内部接口,非对外暴露的常规 Native API。该文档目录严格记录向外部最终用户开放的稳定核心算子与工具级 API,本内部方法不属于该文档的收录统计范围,故无需在 md 文件中补充登记,当前资料状态已核查闭环。 # 【接口变更】 > 请确认是否涉及跨代码仓或者客户面可见的接口变更。如涉及,需要详细说明接口以及对应的变更内容,同时需要在资料中体现。如不涉及,需填写“不涉及” 不涉及 # 【功能验证】 > 说明测试场景,测试方法。如果本次测试方式与常规单元测试不同,请详细说明您的测试步骤\ > 新增/变更内容是否已新增/适配UT测试用例看护,并补充测试自验证截图 1. 测试场景:昇騰 NPU 单卡算力开发环境,执行图追踪核心工具类接口的功能一致性、边界值看护与原生用例兼容性验证。 2. 测试方法: - 新增自主测试:针对存在上游测试空白的接口,执行新增的专项看护脚本。 - 原生用例复测:针对上游已覆盖的接口,直接在 NPU 环境下复测原生测试集,验证底层符号推导的兼容性。 3. 测试步骤: - 步骤一:执行新增看护用例 验证torch.fx.experimental.symbolic_shapes.ShapeEnv.create_unspecified_symbol与torch.fx.experimental.symbolic_shapes.ShapeEnv.create_unspecified_symint_and_symbol接口: 执行命令如下: bash python ./fx/test_unspecified_symbols.py  - 步骤二:执行官方原生用例看护 验证torch.fx.experimental.symbolic_shapes.ShapeEnv.create_unbacked_symfloat和torch.fx.experimental.symbolic_shapes.ShapeEnv.create_unbacked_symint接口,这两个接口可同时在test/dynamo/test_misc.py中的test_shape_env_equal_unbacked下一起验证。 由于原生用例test_shape_env_equal_unbacked涉及特殊的图编译验证,在本地执行时需进行以下特殊处理: 1.注释@onlyIfTranslationValidation装饰器:该装饰器限制了用例仅在开启 Translation Validation(翻译验证)的环境下激活。本地测试环境默认未使能 TV 导致用例被跳过(Skipped),为了在本地环境中强制触发并验证该用例在 NPU 底座下的兼容性,需手动注释此行限制。 2. 使用EXPECTTEST_ACCEPT=1环境变量:该用例底层引入了expecttest框架进行符号环境输出的字面量强比对。在本地强制执行该用例时,由于环境差异或输出格式的微调,会导致原硬编码的预期字符串(Expect String)不匹配。通过声明EXPECTTEST_ACCEPT=1,可指示测试框架自动接受当前的实际正确运行输出,并自动覆写/刷新代码中的预期字面量,从而完成校验闭环。 执行命令如下: bash EXPECTTEST_ACCEPT=1 python ./dynamo/test_misc.py -v -k test_shape_env_equal_unbacked  - 步骤三:执行官方原生用例看护 torch.fx.experimental.symbolic_shapes.ShapeEnv.defer_runtime_assert被官方重命名为torch.fx.experimental.symbolic_shapes.ShapeEnv.guard_or_defer_runtime_assert 验证torch.fx.experimental.symbolic_shapes.ShapeEnv.guard_or_defer_runtime_assert接口,该接口可同时在test/test_dynamic_shapes.py中的test_floor_clean_div_axioms下验证。 执行命令如下: bash python ./test_dynamic_shapes.py -v -k test_floor_clean_div_axioms `  # 【CheckList】 > PR提交人对以下CheckList自检项进行全量自检,自检通过或不涉及,均修改 [ ] 为 [x] - [x] 代码注释完备,正确记录错误日志 - [x] 代码实现进行了返回值、空指针等校验 - [x] PR标题正确使用类型标签,如:feat、fix、refactor、docs、test等 - [x] PR持续集成流水线(CI)执行通过,代码检查无异常 See merge request: Ascend/pytorch!35868 | 2 个月前 | |
| 2 年前 |
| 文件 | 最后提交记录 | 最后更新时间 |
|---|---|---|
| 1 个月前 | ||
| 2 年前 | ||
| 2 年前 | ||
| 2 年前 | ||
| 2 年前 | ||
| 2 年前 | ||
| 2 年前 | ||
| 3 个月前 | ||
| 2 年前 | ||
| 2 个月前 | ||
| 3 个月前 | ||
| 2 个月前 | ||
| 2 个月前 | ||
| 1 个月前 | ||
| 3 个月前 | ||
| 2 个月前 | ||
| 2 个月前 | ||
| 2 年前 | ||
| 2 个月前 | ||
| 2 年前 | ||
| 2 个月前 | ||
| 2 个月前 | ||
| 2 年前 | ||
| 3 个月前 | ||
| 2 年前 | ||
| 2 年前 | ||
| 2 个月前 | ||
| 1 个月前 | ||
| 2 年前 | ||
| 2 年前 | ||
| 1 个月前 | ||
| 1 个月前 | ||
| 2 个月前 | ||
| 2 年前 |