已合并
test(fx): add testcases for torch.fx.experimental.symbolic_shapes APIs v2.12.0 #36818
test(fx): add testcases for torch.fx.experimental.symbolic_shapes APIs v2.12.0 #36818
已合并
xuanzhi-2026创建于 5月26日
xuanzhi-2026
5月26日

【合入来源】

【任务背景】

本 PR 对应 pytorch-vllm 开源实习项目中的 Torch-NPU API 一致性对齐任务,公开 issue:
https://gitcode.com/Ascend/pytorch/issues/1628

本 PR 是 v2.12.0 分支的用例补齐 PR,目标是补齐 torch.fx.experimental.symbolic_shapes 下 5 个 API 的直接验证用例。同组交付 PR 如下:

【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_sizesdynamic_stridesconstraint_sizesconstraint_strides 等输入元信息,用于创建符号尺寸时描述动态维约束。

【上游社区测试情况】

已基于当前分支基线检索 test 目录中的相关覆盖情况:

  • 上游/基线中存在部分间接覆盖,例如 StatelessSymbolicContext 在 dynamo export/subclass 场景中被使用,ShapeEnvSettings 出现在 public API allowlist 中。
  • 未发现覆盖本任务 5 个 API 的集中直接 UT,尤其缺少 ShapeEnv.size_hintShapeEnv.suppress_guardsShapeEnvSettings 字段构造、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,并检查签名中包含 exprallow_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_stridesconstraint_sizesconstraint_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.1v2.9.0v2.10.0v2.11.0v2.12.0master;资料支持状态由 PR 36822 单独补齐。

【资料变更】

不涉及。本 PR 仅补充测试用例;资料支持状态已通过独立资料 PR 提交:
https://gitcode.com/Ascend/pytorch/pull/36822

【接口变更】

不涉及。未修改跨仓接口或客户可见接口行为。

【功能验证】

验证新增 5 个目标 API 测试用例,执行命令如下:

cd /workspace/user_data
python3 torch-npu-v2.12.0/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

执行结果如下:

.....
----------------------------------------------------------------------
Ran 5 tests in 0.034s

OK

【测试效果 / 日志说明】

  • 5 个目标 API 用例全部通过。
  • 新增用例为 Python 层 symbolic shape 行为验证,测试过程中不依赖新增 NPU 算子实现。
  • 当前 PR 已通过 GitCode CI 标签检查:ci-pipeline-passeddocs-ci-pipeline-success

【CheckList】

likedislike
Pull Request已成功合入, 合并人@ascend-robot
(感谢 xuanzhi-2026 的贡献)
Xxuanzhi-2026
5月26日 创建了 pull request,commit a587d4cc
Xxuanzhi-2026
5月26日 关联了issue:【开源实习】Torch-NPU API一致性对齐:补齐测试用例、API功能对齐、补齐文档(22)
ascend-robot
ascend-robot成员
5月26日 评论:

Thanks for your pull-request.
The full list of commands accepted by me can be found at here
You can get sig-info at here


PR Approval Progress

Congratulations! All modules have met the lgtm and approve requirements.

Module Approval Details

module lgtm status approve status
test 李伟, huangjingwei (2/2) 李伟 (1/1)

💡 Tip:

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

CLA Signature Pass

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

likedislike
ascend-robotascend-robot成员
5月26日 添加了label:ascend-cla/yes
此处折叠了103条消息 查看更多
Jingwei Huang
Jingwei Huang成员
6月16日 评论:

/lgtm

likedislike
liwei386成员
6月16日 评论:

/approve

likedislike
ascend-robotascend-robot成员
6月16日 添加了label:approvedlgtm
ascend-robotascend-robot成员
6月16日 合入了pull request
ascend-robot
ascend-robot成员
6月16日 评论:
流水线 pytorch_gitcode_PR_multiVersion#10813 [ commitID:a2faf85d ] 已完成
likedislike