| 文件 | 最后提交记录 | 最后更新时间 |
|---|---|---|
test(fx): add ShapeEnv core methods tests for master Co-authored-by: chenlan114514<3470991357@qq.com> # message auto-generated for no-merge-commit merge: !37571 merge test-shapeenv-apis-master into master 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行为。 # 【功能验证】 本次提交的测试用例,其验证结论完全适用于 master 分支。理由如下: 1. **API 稳定性论证** 本次涉及的5个API均位于 torch/fx/experimental/symbolic_shapes.py,是**纯Python逻辑实现**,与NPU硬件和底层算子库完全解耦,因此其行为不受分支版本演进的影响。 2. **跨版本一致性证明** 已在2.7.1、2.9.0、2.10.0、2.11.0、2.12.0**五个不同稳定版本**上均完成了真实验证,并全部通过测试。这一方面证明了API逻辑的正确性,另一方面也反证了这些API在PyTorch 2.x系列中的行为是高度稳定和一致的。 因此,我们在多个稳定版本上获得的有效结论,可以安全地推广到 master 分支。 ## v.2.7.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 5.04s ============================================================================ ## v.2.9.0 执行命令: 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.11s ============================================================================ ## v.2.10.0 执行命令: 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 ============================================================================ ## v.2.11.0 执行命令: 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.59s ============================================================================ ## v.2.12.0 执行命令: bash python test/fx/experimental/test_symbolic_shapes.py TestShapeEnvCoreMethods 测试日志: bash ..... ---------------------------------------------------------------------- Ran 5 tests in 0.451s OK (skipped=1) # 【CheckList】 - [x] 代码注释完备,正确记录错误日志 - [x] 代码实现进行了返回值、空指针等校验 - [x] PR标题正确使用类型标签,如:feat、fix、refactor、docs、test等 - [x] PR持续集成流水线(CI)执行通过,代码检查无异常 See merge request: Ascend/pytorch!37571 | 2 个月前 | |
| 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: !34465 merge test-fx-codegen-api-master into master 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!34465 | 4 个月前 | |
| 2 年前 | ||
docs(torch.fx): 完善 experimental.symbolic_shapes 文档与测试用例 Co-authored-by: yuhongming-2026<moshui_1@qq.com> # message auto-generated for no-merge-commit merge: !35211 merge add-fx-experimental-symbolic-api-test-master into master 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!35211 | 3 个月前 | |
[test] add tests for torch.fx Graph APIs Co-authored-by: lihaokun-2026<851563813@qq.com> # message auto-generated for no-merge-commit merge: !34399 merge add-fx-graph-api-test-master into master [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!34399 | 4 个月前 | |
[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: !36532 merge fx-graph-apis-master into master [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!36532 | 3 个月前 | |
[test] Add fx graph internal API verify tests Co-authored-by: lihaokun-2026<851563813@qq.com> # message auto-generated for no-merge-commit merge: !35580 merge add-fx-graph-api-verify-master into master [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!35580 | 3 个月前 | |
test(fx): add NPU test cases for torch.fx.GraphModule APIs [master] Co-authored-by: gcw_IDzXRVNw<15104808434@163.com> # message auto-generated for no-merge-commit merge: !39596 merge test/fx-graphmodule-api-master into master test(fx): add NPU test cases for torch.fx.GraphModule APIs [master] Created-by: gcw_IDzXRVNw Commit-by: gcw_IDzXRVNw Merged-by: ascend-robot Description: # 【合入来源】 > <font color="red">**https://gitcode.com/Ascend/pytorch/issues/1856**</font> \ > <font color="red">**https://gitcode.com/Ascend/pytorch/issues/2482**</font> - [x] issue/工单 Fork: gcw_IDzXRVNw/pytorch_npu 分支: test/fx-graphmodule-api-master → Ascend/pytorch:master 关联 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_file_content | 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 全部正常工作 - TestFxGraphModuleOnNpu.setUp 包含设备检查,非 NPU 环境自动 skipTest,避免虚假通过 - torch_npu 未对任何 GraphModule API 进行覆盖/重写,所有 API 行为与 PyTorch 上游完全一致 【结论】本测试文件的设计合理,可充分保证 GraphModule 在昇腾 NPU 环境下的功能正确性与可用性。 # 【资料变更】 文档变更已在 v2.7.1 分支 PR 中合入,本 PR 仅提交测试文件。 # 【接口变更】 不涉及 # 【功能验证】 - 测试文件路径: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 test/fx/test_fx_graph_module_api.py -v test_code_consistent_after_recompile ... ok test_code_contains_forward ... ok test_code_contains_op_names ... ok test_code_returns_string ... ok test_graph_getter_has_nodes ... ok test_graph_getter_is_consistent ... ok test_graph_getter_returns_graph ... ok test_graph_setter_forward_works ... ok test_graph_setter_preserves_lint ... ok test_graph_setter_raises_on_non_graph ... ok test_graph_setter_reassigns_graph ... ok test_graph_setter_triggers_recompile ... ok test_init_from_dict ... ok test_init_from_module ... ok test_init_raises_on_bad_type ... ok test_init_sets_class_name ... ok test_code_and_graph_on_npu ... ok test_forward_on_npu ... ok test_recompile_on_npu ... ok test_print_readable_contains_child_code ... ok test_print_readable_returns_string ... ok test_recompile_preserves_forward ... ok test_recompile_returns_python_code ... ok test_add_submodule_nested ... ok test_add_submodule_overwrite_fails_on_non_module ... ok test_add_submodule_root_level ... ok test_delete_all_unused_preserves_used ... ok test_delete_all_unused_removes_orphans ... ok test_delete_submodule_existing ... ok test_delete_submodule_nested ... ok test_delete_submodule_non_module ... ok test_delete_submodule_nonexistent ... ok test_to_folder_creates_files ... ok test_to_folder_module_file_content ... 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!39596 | 2 个月前 | |
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: !34855 merge add-fx-graph-api-test-master into master 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!34855 | 4 个月前 | |
test(fx): add test for lazy_format_graph_code Co-authored-by: Lane0218<laneljc@qq.com> # message auto-generated for no-merge-commit merge: !35078 merge task2-master-test-fx-lazy-format into master 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!35078 | 3 个月前 | |
test(fx): add test cases for torch.fx.node._type_repr, torch.fx.Node.kwargs, torch.fx.node.map_arg, torch.fx.Node.next, torch.fx.Node.prev APIs Co-authored-by: hantao55_<terry.han27@qq.com> # message auto-generated for no-merge-commit merge: !37791 merge test-fx-node-master into master test(fx): add test cases for torch.fx.node._type_repr, torch.fx.Node.kwargs, torch.fx.node.map_arg, torch.fx.Node.next, torch.fx.Node.prev 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.7.1:torch 2.7.1+cpu / torch-npu 2.7.1.post2 2.9.0:torch 2.9.0+cpu / torch-npu 2.9.0 2.10.0:torch 2.10.0+cpu / torch-npu 2.10.0rc2 2.11.0:torch 2.11.0+cpu / torch-npu 2.11.0rc3 2.12.0:torch 2.12.0+cpu / torch-npu 2.12.0rc1 运行命令与结果: log (test27) [root@4e6c21e2fea3 test]# pip list | grep torch torch 2.7.1+cpu torch_npu 2.7.1.post2 (test27) [root@4e6c21e2fea3 test]# python fx/test_fx_node_api.py ................... ---------------------------------------------------------------------- Ran 19 tests in 0.044s OK (test29) [root@4e6c21e2fea3 test]# pip list | grep torch torch 2.9.0+cpu torch_npu 2.9.0 (test29) [root@4e6c21e2fea3 test]# python fx/test_fx_node_api.py ................... ---------------------------------------------------------------------- Ran 19 tests in 0.046s OK (test210) [root@4e6c21e2fea3 test]# pip list | grep torch torch 2.10.0+cpu torch_npu 2.10.0rc2 (test210) [root@4e6c21e2fea3 test]# python fx/test_fx_node_api.py ................... ---------------------------------------------------------------------- Ran 19 tests in 0.045s OK (test211) [root@4e6c21e2fea3 test]# pip list | grep torch torch 2.11.0+cpu torch_npu 2.11.0rc3 (test211) [root@4e6c21e2fea3 test]# python fx/test_fx_node_api.py ................... ---------------------------------------------------------------------- Ran 19 tests in 0.050s OK (test212) [root@4e6c21e2fea3 test]# pip list | grep torch torch 2.12.0+cpu torch_npu 2.12.0rc1 (test212) [root@4e6c21e2fea3 test]# python fx/test_fx_node_api.py ................... ---------------------------------------------------------------------- Ran 19 tests in 0.052s OK # 【CheckList】 - [x] 代码注释完备,正确记录错误日志 - [x] 代码实现进行了返回值、空指针等校验 - [x] PR标题正确使用类型标签,如:feat、fix、refactor、docs、test等 - [x] PR持续集成流水线(CI)执行通过,代码检查无异常 See merge request: Ascend/pytorch!37791 | 3 个月前 | |
| 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: !37263 merge symbolic-shapes-propagate-unbacked-symints-master into master 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-master/test/fx/test_fx_propagate_unbacked_symints.py  python /workspace/user_data/pytorch-v2.7.1/test/fx/test_fx_propagate_unbacked_symints.py  python /workspace/user_data/pytorch-v2.9.0/test/fx/test_fx_propagate_unbacked_symints.py  python /workspace/user_data/pytorch-v2.10.0/test/fx/test_fx_propagate_unbacked_symints.py  python /workspace/user_data/pytorch-v2.11.0/test/fx/test_fx_propagate_unbacked_symints.py  python /workspace/user_data/pytorch-v2.12.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!37263 | 3 个月前 | |
| 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: !37614 merge test-fx-create-args-for-root-master into master 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 均已标注为"是"(支持),文档状态与实际一致,无需修改文档。 # 【接口变更】 > 不涉及 # 【功能验证】 在 master 对应环境上执行该用例,通过,日志如下: (venv_2.12.0) root@npu1-2:/tmp# python3 test_fx_tracer_create_arg.py ............ ...... ---------------------------------------------------------------------- Ran 19 tests in 0.998s OK # 【CheckList】 - [x] 代码注释完备,正确记录错误日志 - [x] 代码实现进行了返回值、空指针等校验 - [x] PR标题正确使用类型标签,如:feat、fix、refactor、docs、test等 - [x] PR持续集成流水线(CI)执行通过,代码检查无异常 See merge request: Ascend/pytorch!37614 | 3 个月前 | |
test: add NPU coverage for torch.fx.Tracer Co-authored-by: ShangjingLee<1755397894@qq.com> # message auto-generated for no-merge-commit merge: !36580 merge fix-api-torch.fx.Tracer-master into master test: add NPU coverage for torch.fx.Tracer Created-by: ShangjingLi Commit-by: ShangjingLee Merged-by: ascend-robot Description: <!-- PR描述模板更新日期:20260203 --> # 【合入来源】 - [ ] 需求 - [ ] 问题单 - [x] issue/工单 - [ ] 重构优化 - [ ] 资料更新 本次 API 一致性验证接口: - torch.fx.Tracer # 【修改方案】 本次 PR 在已有 torch-npu 测试文件中新增补充用例: - test/fx/test_fx_tracer_transformer_api.py ## 1. 官方用例覆盖情况分析 PyTorch 官方原生 test/test_fx.py 中已经存在 torch.fx.Tracer 相关用例。覆盖情况如下: | 原生用例 | 已覆盖内容 | NPU 适配情况 | 是否仍有缺口 | | --- | --- | --- | --- | | TestFX.test_trace_multiple_funcs | 覆盖 Tracer().trace(module)、GraphModule 执行、tracer.traced_func_name 追踪其它 forward 函数,以及缺失函数名异常路径。 | 原始仓库已有 test_upstream/test/test_fx.py.patch 适配,将输入改为 NPU Tensor。 | 已覆盖基础 Tracer tracing 行为,但不覆盖带 Parameter、buffer 的 module 状态场景。 | | TestFX.test_stack_traces | 覆盖 Tracer.record_stack_traces = True 时 graph node 记录 stack trace。 | 属于 tracing 元信息验证,不是本次新增 NPU 张量计算缺口。 | 不覆盖 module 状态、多输入多输出、eval 模式一致性。 | | TestFX.test_lineno_map | 覆盖 Tracer().trace(...) 生成 graph 后的行号映射。 | 属于 graph/debug 元信息验证,不是本次新增 NPU 张量计算缺口。 | 不覆盖 module 状态、多输入多输出、eval 模式一致性。 | | TestFX.test_fx_create_arg | 覆盖自定义 Tracer 子类在 tracing 过程中的参数创建逻辑。 | 该用例偏向 create_arg 行为验证,不作为本任务新增类级别场景。 | 不覆盖 NPU Parameter、buffer 和 GraphModule 数值一致性。 | | TestFX.test_deepcopy_tracer | 覆盖 Tracer 对象 deepcopy 行为。 | 不涉及张量计算。 | 不覆盖 NPU 执行一致性。 | 结论: 1. 官方原生用例已经覆盖 torch.fx.Tracer 的基础 tracing、traced function 切换、stack trace、行号映射和对象复制等行为。 2. TestFX.test_trace_multiple_funcs 涉及张量计算,原始仓库已有 patch 进行 NPU 适配,本 PR 不重复修改该 patch。 3. 官方原生用例对以下类级别场景覆盖不充分:带 NPU Parameter 的 module、带 NPU buffer 的 module、多输入、多输出、eval 模式,以及原 module 与 GraphModule 在 NPU 上的数值一致性。因此本 PR 新增 torch-npu 测试用例补充该缺口。 ## 2. 新增补充用例 在已有测试文件中新增用例: - test/fx/test_fx_tracer_transformer_api.py 新增用例: - TestFxTracerApi.test_tracer_module_state 该用例补充官方原生用例未充分覆盖的 Tracer 类级别场景,包括 module 状态、多输入、多输出和 eval 模式。 用例逻辑: 1. 定义 MyModule。 2. 在 module 中创建 NPU Parameter: python self.weight = torch.nn.Parameter(torch.ones(2, 3).npu()) 3. 在 module 中注册 NPU buffer: python self.register_buffer("bias", torch.ones(2, 3).npu()) 4. module 中包含 Dropout(p=0.5),用于验证 eval 模式下的 tracing 和执行。 5. forward(x, y) 执行多输入张量计算: python out = self.dropout(x + y + self.weight + self.bias) return out, out.neg() 6. 将 module 设置为 eval 模式。 7. 创建 NPU 输入: python x, y = torch.randn(2, 3).npu(), torch.randn(2, 3).npu() 8. 使用 Tracer().trace(module) 生成 FX graph。 9. 使用 GraphModule(tracer.root, graph) 构造可执行 GraphModule。 10. 将 GraphModule 设置为 eval 模式。 11. 分别执行原 module 和 GraphModule,校验两个输出均数值一致。 12. 校验两个输出设备均为 NPU: python self.assertEqual(result[0].device.type, "npu") self.assertEqual(result[1].device.type, "npu") 13. 校验 graph 中存在 get_attr 节点,说明 Parameter 或 buffer 属性被记录进 FX graph。 该新增用例涉及的张量计算包括: - x + y - 加 NPU Parameter:self.weight - 加 NPU buffer:self.bias - eval 模式下执行 Dropout - out.neg() 输入、Parameter 和 buffer 均创建在 NPU 上,并显式校验输出设备为 npu。 # 【资料变更】 已检查资料支持情况,不需要修改资料。 检查结果: 1. 当前 API 支持列表位于 docs/api/torch_npu_apis.md。 2. 在 docs/api/torch_npu_apis.md 中有到 torch.fx.Tracer。 3. 文档中 torch.fx.Tracer 的支持状态为 Y。 4. 本次新增用例 test/fx/test_fx_tracer_api.py 已验证 torch.fx.Tracer 在 NPU 上可追踪带 Parameter、buffer、多输入、多输出和 eval 模式的 module,GraphModule 输出与原 module 数值一致,且输出设备为 NPU。 结论: - 文档中 torch.fx.Tracer 的支持状态与本次验证结果一致。 - 本次 PR 不需要补充或修改官方资料文件。 # 【接口变更】 不涉及。 本次只新增测试用例,不修改 torch.fx.Tracer 实现,不涉及跨代码仓接口变更,不涉及客户面可见接口变更。 # 【功能验证】 ## 1. 新增补充用例验证 验证命令: bash cd /workspace python /workspace/pytorch/test/fx/test_fx_tracer_transformer_api.py TestFxTracerApi.test_tracer_module_state 2>&1 | tee tracer_api.log 已验证结果: text . ---------------------------------------------------------------------- Ran 1 test in 1.145s OK 验证结论: 1. Tracer().trace(module) 可以追踪带 NPU Parameter 和 NPU buffer 的 module。 2. 生成的 GraphModule 与原 module 在 NPU 输入下输出数值一致。 3. 多输入、多输出结构执行正常。 4. eval 模式保持一致。 5. 输出 Tensor 设备均为 NPU。 6. graph 中存在 get_attr 节点,说明 module 状态被记录进 FX graph。 # 【CheckList】 - [x] 代码注释完备,正确记录错误日志 - [x] 代码实现进行了返回值、空指针等校验 - [x] PR标题正确使用类型标签,如:feat、fix、refactor、docs、test等 - [x] PR持续集成流水线(CI)执行通过,代码检查无异常 See merge request: Ascend/pytorch!36580 | 3 个月前 | |
| 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: !34556 merge codex/fx-graph-apis-master into master [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!34556 | 4 个月前 | |
| 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: !34873 merge test-proxy-tensor-api-master into master 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!34873 | 3 个月前 | |
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: !38164 merge test-shape-env-api-alignment-master into master 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.134s OK # 【CheckList】 - [x] 代码注释完备,正确记录错误日志 - [x] 代码实现进行了返回值、空指针等校验 - [x] PR标题正确使用类型标签,如:feat、fix、refactor、docs、test等 - [x] PR持续集成流水线(CI)执行通过,代码检查无异常 See merge request: Ascend/pytorch!38164 | 2 个月前 | |
| 2 年前 | ||
| 2 年前 | ||
test: 补齐torch.fx.experimental.symbolic_shapes NPU 适配验证与统一运行脚本 Co-authored-by: drizzlezyk<drizzlezyk@163.com> # message auto-generated for no-merge-commit merge: !36866 merge master into master test: 补齐torch.fx.experimental.symbolic_shapes NPU 适配验证与统一运行脚本 Created-by: cuiyunhao-2026 Commit-by: drizzlezyk Merged-by: ascend-robot Description: # 【合入来源】 > <font color="red">**如有社区issue,请关联issue链接**</font>\ > <font color="red">**请勿携带内部流程信息(需求链接、问题单、内部issue等)**</font> - [ ] 需求 - [ ] 问题单 - [x] issue/工单 - [ ] 重构优化 - [x] 资料更新 关联issue:https://gitcode.com/Ascend/pytorch/issues/1625 # 【修改方案】 完善 Torch-NPU 对 torch.fx.experimental.symbolic_shapes.ShapeEnv 类 5 个 API 在 NPU 环境下的兼容性验证,覆盖以下 API: | API | 说明 | |-----|------| | torch.fx.experimental.symbolic_shapes.ShapeEnv.format_guards | 将 ShapeEnv 中的 guard 表达式格式化为字符串,verbose=True 时附加源码位置 | | torch.fx.experimental.symbolic_shapes.ShapeEnv.freeze | 冻结 ShapeEnv 停止累积 guards,设置 frozen=True | | torch.fx.experimental.symbolic_shapes.ShapeEnv.freeze_runtime_asserts | 冻结 ShapeEnv 停止添加延迟 runtime asserts,设置 runtime_asserts_frozen=True | | torch.fx.experimental.symbolic_shapes.ShapeEnv.get_axioms | 返回 runtime asserts 和 guards 的组合,支持 symbols 筛选和 compute_hint 参数 | | torch.fx.experimental.symbolic_shapes.ShapeEnv.get_implications | 给定布尔表达式,返回所有蕴含谓词及其真值 | **1. 上游社区用例情况分析** 在 PyTorch 上游仓库(github.com/pytorch/pytorch)中搜索这 5 个 API: - format_guards:仅在 test/functorch/test_aotdispatch.py(第10150行)和 test/test_proxy_tensor.py(第1963行)中间接调用,无直接 API 验证用例 - freeze:上游 test/ 目录下无任何 ShapeEnv.freeze() 的直接调用 - freeze_runtime_asserts:上游 test/ 目录下无任何调用 - get_axioms:上游 test/ 目录下无任何直接调用(仅在 torch/_inductor/sizevars.py 等内部使用) - get_implications:上游 test/ 目录下无任何直接调用(仅在 symbolic_shapes.py 内部被其他方法调用) 上游 test/fx/ 目录下不存在 test_symbolic_shapes.py 文件,该文件为 torch-npu 社区自建。 结论:5 个 API 均属于上游"无直接用例"场景,按 1.3 规范自写用例,提交到 torch-npu 的 test 目录。 **2. NPU 适配方案** 5 个 API 均为 ShapeEnv 类的纯 Python 方法,操作 sympy 符号表达式与内部状态,不涉及张量计算和 NPU 算子调用,在 NPU 上行为与 CPU 完全一致,无需做任何 NPU 适配修改。 **3. 新增测试用例** 在已有 test/fx/test_symbolic_shapes.py 的 TestShapeEnvNPU 类中追加 5 个测试方法: | 测试方法 | 覆盖场景 | |---------|---------| | test_torch_fx_experimental_symbolic_shapes_ShapeEnv_format_guards | 空 guards 返回空字符串;多条 guards 格式化输出;verbose 模式含源码位置信息 | | test_torch_fx_experimental_symbolic_shapes_ShapeEnv_freeze | 冻结前后 frozen 状态切换 | | test_torch_fx_experimental_symbolic_shapes_ShapeEnv_freeze_runtime_asserts | 冻结前后 runtime_asserts_frozen 状态切换 | | test_torch_fx_experimental_symbolic_shapes_ShapeEnv_get_axioms | 验证返回类型为 tuple;symbols 参数筛选 | | test_torch_fx_experimental_symbolic_shapes_ShapeEnv_get_implications | Eq/Lt/Ne/Le 表达式蕴含推导 | **4. API 补齐** 不需要。5 个 API 均为纯 Python 方法,在 NPU 上运行结果与 CPU 完全一致。 **5. 多版本分支合入** | 目标分支 | PR | |---------|--------| | v2.7.1 | https://gitcode.com/Ascend/pytorch/pull/36867 | | v2.9.0 | https://gitcode.com/Ascend/pytorch/pull/36871 | | v2.10.0 | https://gitcode.com/Ascend/pytorch/pull/36872 | | v2.11.0 | https://gitcode.com/Ascend/pytorch/pull/36873 | | v2.12.0 | https://gitcode.com/Ascend/pytorch/pull/36874 | | master | https://gitcode.com/Ascend/pytorch/pull/36866 | 文档 PR:https://gitcode.com/Ascend/pytorch/pull/36879 # 【资料变更】 涉及。torch.fx.experimental.symbolic_shapes 下的 5 个 API 实际已支持,但 docs/zh/native_apis 中缺少对应文档。 v2.7.1 分支资料变更内容: 1. 新增 docs/zh/native_apis/pytorch_2-7-1/torch-fx-experimental-symbolic_shapes.md、pytorch_2-9-0/torch-fx-experimental-symbolic_shapes.md、pytorch_2-10-0/torch-fx-experimental-symbolic_shapes.md、pytorch_2-11-0/torch-fx-experimental-symbolic_shapes.md、pytorch_2-12-0/torch-fx-experimental-symbolic_shapes.md 2. 修改对应版本 PyTorch-*.md,添加子模块链接 API 支持情况: | API名称 | 是否支持 | 限制与说明 | |---------|---------|-----------| | torch.fx.experimental.symbolic_shapes.ShapeEnv | 是 | - | | torch.fx.experimental.symbolic_shapes.ShapeEnv.format_guards | 是 | - | | torch.fx.experimental.symbolic_shapes.ShapeEnv.freeze | 是 | - | | torch.fx.experimental.symbolic_shapes.ShapeEnv.freeze_runtime_asserts | 是 | - | | torch.fx.experimental.symbolic_shapes.ShapeEnv.get_axioms | 是 | - | | torch.fx.experimental.symbolic_shapes.ShapeEnv.get_implications | 是 | - | 资料 PR 仅合入 v2.7.1 分支,刷新 pytorch_2-7-1、pytorch_2-9-0、pytorch_2-10-0、pytorch_2-11-0、pytorch_2-12-0 五个版本的 API 支持情况。v2.9.0 及以后分支无 docs/zh/native_apis 目录,无需处理。 # 【接口变更】 不涉及(无新增/修改对外 C++ 或 Python 接口,仅新增测试与文档) # 【功能验证】 **1. 测试环境** - 设备:Ascend NPU - 框架:对应分支 Torch-NPU + PyTorch - 说明:5 个 API 为 ShapeEnv 类的纯 Python 方法,操作符号表达式与内部状态,不涉及张量计算,在 NPU 上行为与 CPU 完全一致 **2. 测试命令** bash cd <pytorch-repo-root> python test/fx/test_symbolic_shapes.py **3.测试结果**  **4.与上游用例的关系** |验证维度|上游现有用例 |本 PR 新增用例 | |--------|---------|---------| |ShapeEnv.format_guards |无直接用例(test_aotdispatch.py 间接调用) |已覆盖(空guards/有guards/verbose) | |ShapeEnv.freeze |无任何调用 |已覆盖(状态切换) | |ShapeEnv.freeze_runtime_asserts |无任何调用 |已覆盖(状态切换) | |ShapeEnv.get_axioms |无直接用例 |已覆盖(空/guards/symbols/hint) | |ShapeEnv.get_implications |无直接用例 |已覆盖(Eq/Lt/Ne/Le 蕴含推导) | |NPU 侧验证闭环 |不完整 |本 PR 补齐 | # 【CheckList】 > PR提交人对以下CheckList自检项进行全量自检,自检通过或不涉及,均修改 [ ] 为 [x] - [x] 代码注释完备,正确记录错误日志 - [x] 代码实现进行了返回值、空指针等校验 - [x] PR标题正确使用类型标签,如:feat、fix、refactor、docs、test等 - [x] PR持续集成流水线(CI)执行通过,代码检查无异常 See merge request: Ascend/pytorch!36866 | 2 个月前 | |
test(fx): add testcases for torch.fx.experimental.symbolic_shapes APIs master Co-authored-by: xuanzhi-2026<814906706@qq.com> # message auto-generated for no-merge-commit merge: !36820 merge test-symbolic-shapes-api-master into master test(fx): add testcases for torch.fx.experimental.symbolic_shapes APIs master Created-by: xuanzhi-2026 Commit-by: xuanzhi-2026 Merged-by: ascend-robot Description: <!-- PR描述模板更新日期:20260203 --> # 【合入来源】 - [ ] 需求 - [ ] 问题单 - [x] issue/工单 - [ ] 重构优化 - [ ] 资料更新 # 【任务背景】 本 PR 对应 pytorch-vllm 开源实习项目中的 Torch-NPU API 一致性对齐任务,公开 issue: https://gitcode.com/Ascend/pytorch/issues/1628 本 PR 是 master 分支的用例补齐 PR,目标是补齐 torch.fx.experimental.symbolic_shapes 下 5 个 API 的直接验证用例。同组交付 PR 如下: - v2.7.1 用例:https://gitcode.com/Ascend/pytorch/pull/36792 - v2.9.0 用例:https://gitcode.com/Ascend/pytorch/pull/36799 - v2.10.0 用例:https://gitcode.com/Ascend/pytorch/pull/36804 - v2.11.0 用例:https://gitcode.com/Ascend/pytorch/pull/36815 - v2.12.0 用例:https://gitcode.com/Ascend/pytorch/pull/36818 - master 用例:https://gitcode.com/Ascend/pytorch/pull/36820 - 资料支持状态:https://gitcode.com/Ascend/pytorch/pull/36822 # 【API 功能介绍】 | API | 功能说明 | | --- | --- | | torch.fx.experimental.symbolic_shapes.ShapeEnv.size_hint | 对确定的符号表达式或 sympy 表达式求可用于动态 shape guard/约束判断的整型 hint;allow_none 控制无法求值时是否允许返回 None。 | | torch.fx.experimental.symbolic_shapes.ShapeEnv.suppress_guards | ShapeEnv 提供的上下文管理器,用于在局部代码块内临时抑制 guard 记录,避免辅助求值过程额外生成 guards。 | | torch.fx.experimental.symbolic_shapes.ShapeEnvSettings | ShapeEnv 的配置数据结构,保存动态 shape 相关开关,例如 scalar output、dynamic output shape、duck shape、deferred runtime assert 等行为配置。 | | torch.fx.experimental.symbolic_shapes.StatefulSymbolicContext | 带状态的符号上下文,保存 tensor_source 以及 ShapeEnv/source/symbol 的缓存,用于同一张量符号化过程中的符号复用。 | | torch.fx.experimental.symbolic_shapes.StatelessSymbolicContext | 无状态符号上下文,记录 dynamic_sizes、dynamic_strides、constraint_sizes、constraint_strides 等输入元信息,用于创建符号尺寸时描述动态维约束。 | # 【上游社区测试情况】 已基于当前分支基线检索 test 目录中的相关覆盖情况: - 上游/基线中存在部分间接覆盖,例如 StatelessSymbolicContext 在 dynamo export/subclass 场景中被使用,ShapeEnvSettings 出现在 public API allowlist 中。 - 未发现覆盖本任务 5 个 API 的集中直接 UT,尤其缺少 ShapeEnv.size_hint、ShapeEnv.suppress_guards、ShapeEnvSettings 字段构造、StatefulSymbolicContext/StatelessSymbolicContext 默认状态的直接行为断言。 - 因此本任务按 issue 要求在 torch-npu test/fx 目录新增自写 UT,而不是搬运现有上游 patch。 # 【修改方案 / 用例开发思路】 在 test/fx/test_symbolic_shapes_api.py 中新增 5 个直接测试用例: 1. test_shape_env_size_hint:构造 ShapeEnv,验证 sympy.Integer(8) 可返回 size hint 8,并检查签名中包含 expr、allow_none,且 allow_none 默认值为 False。 2. test_shape_env_suppress_guards:验证 ShapeEnv.suppress_guards() 可作为上下文管理器使用,并在上下文内完成 size_hint 求值。 3. test_shape_env_settings:通过 dataclasses.fields 获取当前版本支持字段,只对当前版本存在的字段构造 ShapeEnvSettings,保证跨版本稳定;随后断言配置值可正确保留。 4. test_stateless_symbolic_context:以 DimDynamic.DUCK 构造 StatelessSymbolicContext,验证 dynamic_sizes、默认 dynamic_strides、constraint_sizes、constraint_strides。 5. test_stateful_symbolic_context:以 ConstantSource("x") 构造 StatefulSymbolicContext,验证 tensor_source、空 symbol cache 和动态维配置。 # 【完备性分析】 - 本 PR 覆盖了 5 个目标 API 的可访问性、核心调用路径、关键返回值、构造参数、默认字段与上下文管理器行为。 - 这 5 个 API 属于 torch.fx.experimental.symbolic_shapes 的 Python 层符号 shape 元信息/上下文能力,不涉及新增 NPU 算子,也不改变跨仓接口或运行时计算逻辑。 - 当前结论:需要新增测试用例,已在本 PR 完成;不需要新增 API 功能实现,不需要新增 NPU kernel,不需要新增资料以外的接口说明。 - 同组 PR 已覆盖 v2.7.1、v2.9.0、v2.10.0、v2.11.0、v2.12.0 和 master;资料支持状态由 PR 36822 单独补齐。 # 【资料变更】 不涉及。本 PR 仅补充测试用例;资料支持状态已通过独立资料 PR 提交: https://gitcode.com/Ascend/pytorch/pull/36822 # 【接口变更】 不涉及。未修改跨仓接口或客户可见接口行为。 # 【功能验证】 验证新增 5 个目标 API 测试用例,执行命令如下: ~~~bash cd /workspace/user_data python3 torch-npu-master/test/fx/test_symbolic_shapes_api.py \ TestSymbolicShapesAPI.test_shape_env_size_hint \ TestSymbolicShapesAPI.test_shape_env_suppress_guards \ TestSymbolicShapesAPI.test_shape_env_settings \ TestSymbolicShapesAPI.test_stateless_symbolic_context \ TestSymbolicShapesAPI.test_stateful_symbolic_context ~~~ 执行结果如下: ~~~text ..... ---------------------------------------------------------------------- Ran 5 tests in 0.032s OK ~~~ # 【测试效果 / 日志说明】 - 5 个目标 API 用例全部通过。 - 新增用例为 Python 层 symbolic shape 行为验证,测试过程中不依赖新增 NPU 算子实现。 - 当前 PR 已通过 GitCode CI 标签检查:ci-pipeline-passed、docs-ci-pipeline-success。 # 【CheckList】 - [x] 代码注释完备,正确记录错误日志 - [x] 代码实现进行了返回值、空指针等校验 - [x] PR标题正确使用类型标签,如:feat、fix、refactor、docs、test等 - [x] PR持续集成流水线(CI)执行通过,代码检查无异常 See merge request: Ascend/pytorch!36820 | 2 个月前 | |
test(dynamo): add NPU alignment tests for unspecified symbols Co-authored-by: litianyu2026<1483211810@qq.com> # message auto-generated for no-merge-commit merge: !35864 merge test-unspecified-symbols into master 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接口,该接口可同时在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!35864 | 3 个月前 | |
| 2 年前 |
| 文件 | 最后提交记录 | 最后更新时间 |
|---|---|---|
| 2 个月前 | ||
| 2 年前 | ||
| 2 年前 | ||
| 2 年前 | ||
| 2 年前 | ||
| 2 年前 | ||
| 2 年前 | ||
| 4 个月前 | ||
| 2 年前 | ||
| 3 个月前 | ||
| 4 个月前 | ||
| 3 个月前 | ||
| 3 个月前 | ||
| 2 个月前 | ||
| 4 个月前 | ||
| 3 个月前 | ||
| 3 个月前 | ||
| 2 年前 | ||
| 3 个月前 | ||
| 2 年前 | ||
| 3 个月前 | ||
| 3 个月前 | ||
| 2 年前 | ||
| 4 个月前 | ||
| 2 年前 | ||
| 2 年前 | ||
| 3 个月前 | ||
| 2 个月前 | ||
| 2 年前 | ||
| 2 年前 | ||
| 2 个月前 | ||
| 2 个月前 | ||
| 3 个月前 | ||
| 2 年前 |