已合并
Adapt test_jit_is_trancing in trace for NPU #37080
Adapt test_jit_is_trancing in trace for NPU #37080
已合并
yrouyi创建于 5月29日
yrouyi
yrouyi
5月29日

【合入来源】

[Usage]: torch.jit.is_tracing () ----此API,在trace模式下,测试样例缺失

请勿携带内部流程信息(需求链接、问题单、内部issue等)

【修改方案】

请描述修改内容的具体实现,涉及哪些组件之间进行交互,可以用1、2、3、...进行罗列
如果是需求或者重构类的PR,需要补充详细设计文档(说明上下游组件关系、时序图、类图、DFX能力等内容)

API 功能

torch.jit.istracing 是 TorchScript 核心执行模式判断 API,用于在静态图编译与执行阶段检测当前代码是否处于 tracing 编译上下文,支持在 tracing / scripting 混合编码场景下实现条件分支、执行路径切换、图编译行为适配,保障不同执行模式下代码逻辑的正确性,是 TorchScript 动态适配、图优化、模式感知的基础能力。

测试用例说明

  1. scrpit模式下,API torch.jit.is_tracing ()
    PyTorch 官方 test_jit.py(官方路径:pytorch/test/test_jit.py;行号位置:两个测试用例的位置大概在9783-9798行)包含test_script_is_tracing、test_script_get_tracing_state 两个测试用例。
    测试用例详解
    • test_script_get_tracing_state:调用底层私有接口判定追踪状态,依托NPU张量运算分支结果,校验底层接口识别追踪模式的有效性。
    • test_script_is_tracing:使用官方标准接口判断追踪状态,结合NPU张量执行脚本编译校验,验证公开接口在图追踪场景下判定逻辑正常。

  2. trace模式下,API torch.jit.is_tracing ()
    添加新测试用例test/test_jit_is_tracing_in_trace.py
    • 基础环境适配:引入torch_npu库启用NPU设备能力,构造NPU张量作为测试输入数据
    • 核心功能验证:校验进入JIT轨迹录制流程时,接口可正确返回真值
    测试用例详解
    • test_is_tracing_returns_true_in_trace_mode:基于接口返回结果编写分支判断逻辑,使用NPU张量执行轨迹录制。通过最终运算输出结果,确认程序成功走入轨迹模式分支,验证接口状态判定逻辑有效。
    测试用例构造说明
    由于 PyTorch 的 torch.jit.trace 机制限制,在被追踪函数内部执行时,底层的 Python 级追踪标志位(tracing flag)依然保持为 False,因此无法直接在函数内部进行断言验证。本测试转而采用严格的“行为验证”方案:利用 Tracer “仅记录单条执行路径”的特性,通过校验最终的输出结果,反向证明 is_tracing() 成功触发了目标分支,且该计算路径已被正确固化到 TorchScript 图中。

NPU 适配说明

  1. scrpit模式下,API torch.jit.is_tracing ()适配说明
    torch.jit.istracing 为 TorchScript 前端执行模式检测 API,无硬件相关底层算子依赖,NPU 完全兼容 PyTorch 原生实现,功能行为、状态语义、编译逻辑与 CPU 保持一致。此测试样例无需修改,可直接测试验证。
    • 基础环境适配:将仓库test_upstream/test/test_jit.py.patch,进行apply之后,已有import torch_npu基础环境的适配;
    • 张量设备调整:将仓库test_upstream/test/test_jit.py.patch,进行apply之后,代码已将cpu的张量修改为npu上运行。确保异步计算的结果载体(张量)运行在 NPU 上,贴合 NPU 场景使用需求。

  2. trace模式下,API torch.jit.is_tracing ()适配说明
    新增测试用例:test/test_jit_is_tracing_in_trace.py,补齐NPU设备场景下接口相关测试覆盖。
    • 基础环境适配:引入torch_npu库启用NPU设备能力,构造NPU张量作为测试输入数据。

【资料变更】

需要添加2.9.0~2.12.0的API说明
https://gitcode.com/Ascend/pytorch/tree/v2.7.1/docs/zh/native_apis 的文档中进行查找与验证。该目录下的pytorch_2-7-1文件夹下的torch-jit.md有API的说明,"torch.jit.is_tracing"全部标注"是 支持fp32"。
pytorch_2-9-0~pytorch_2-12-0没有对此API的说明,故对pytorch_2-9-0、pytorch_2-10-0、pytorch_2-11-0、pytorch_2-12-0文件夹下的torch-jit.md添加了API的说明,"torch.jit.is_tracing"全部标注"是 支持fp32"。

【接口变更】

不涉及

【功能验证】

说明测试场景,测试方法。如果本次测试方式与常规单元测试不同,请详细说明您的测试步骤
新增/变更内容是否已新增/适配UT测试用例看护,并补充测试自验证截图

测试命令:

#script模式下,测试命令
cd pytorch/test
python -m unittest test_jit.TestScript.test_script_is_tracing test_jit.TestScript.test_script_get_tracing_state -v

#trace模式下,测试命令
cd pytorch/test
python test_jit_is_tracing_in_trace.py -v

测试结果:

  1. script模式下,测试结果
root@5534245c24de:/workspace/user_data/527/210/pytorch/test_upstream# python -m unittest test_jit.TestScript.test_script_is_tracing test_jit.TestScript.test_script_get_tracing_state -v
/usr/local/python3.11.14/lib/python3.11/site-packages/torch_npu/contrib/transfer_to_npu.py:362: ImportWarning: 
    *************************************************************************************************************
    The torch.Tensor.cuda and torch.nn.Module.cuda are replaced with torch.Tensor.npu and torch.nn.Module.npu now..
    The torch.cuda.DoubleTensor is replaced with torch.npu.FloatTensor cause the double type is not supported now..
    The backend in torch.distributed.init_process_group set to hccl now..
    The torch.cuda.* and torch.cuda.amp.* are replaced with torch.npu.* and torch.npu.amp.* now..
    The device parameters have been replaced with npu in the function below:
    torch.logspace, torch.randint, torch.hann_window, torch.rand, torch.full_like, torch.ones_like, torch.rand_like, torch.randperm, torch.arange, torch.frombuffer, torch.normal, torch._empty_per_channel_affine_quantized, torch.empty_strided, torch.empty_like, torch.scalar_tensor, torch.tril_indices, torch.bartlett_window, torch.ones, torch.sparse_coo_tensor, torch.randn, torch.kaiser_window, torch.tensor, torch.triu_indices, torch.as_tensor, torch.zeros, torch.randint_like, torch.full, torch.eye, torch._sparse_csr_tensor_unsafe, torch.empty, torch._sparse_coo_tensor_unsafe, torch.blackman_window, torch.zeros_like, torch.range, torch.sparse_csr_tensor, torch.randn_like, torch.from_file, torch._cudnn_init_dropout_state, torch._empty_affine_quantized, torch.linspace, torch.hamming_window, torch.empty_quantized, torch._pin_memory, torch.load, torch.set_default_device, torch.get_device_module, torch.sparse_compressed_tensor, torch.Tensor.new_empty, torch.Tensor.new_empty_strided, torch.Tensor.new_full, torch.Tensor.new_ones, torch.Tensor.new_tensor, torch.Tensor.new_zeros, torch.Tensor.to, torch.Tensor.pin_memory, torch.nn.Module.to, torch.nn.Module.to_empty
    *************************************************************************************************************
    
  warnings.warn(msg, ImportWarning)
/usr/local/python3.11.14/lib/python3.11/site-packages/torch_npu/contrib/transfer_to_npu.py:291: RuntimeWarning: torch.jit.script and torch.jit.script_method will be disabled by transfer_to_npu, which currently does not support them, if you need to enable them, please do not use transfer_to_npu.
  warnings.warn(msg, RuntimeWarning)
/usr/local/python3.11.14/lib/python3.11/site-packages/torch/jit/_script.py:362: DeprecationWarning: `torch.jit.script_method` is deprecated. Please switch to `torch.compile` or `torch.export`.
  warnings.warn(
monkeytype is not installed. Skipping tests for Profile-Directed Typing
test_script_is_tracing (test_jit.TestScript.test_script_is_tracing) ... /usr/local/python3.11.14/lib/python3.11/site-packages/torch/jit/_script.py:1480: DeprecationWarning: `torch.jit.script` is deprecated. Please switch to `torch.compile` or `torch.export`.
  warnings.warn(
/usr/local/python3.11.14/lib/python3.11/site-packages/torch/jit/_serialization.py:89: DeprecationWarning: `torch.jit.save` is deprecated. Please switch to `torch.export`.
  warnings.warn(
/usr/local/python3.11.14/lib/python3.11/site-packages/torch/jit/_serialization.py:176: DeprecationWarning: `torch.jit.load` is deprecated. Please switch to `torch.export`.
  warnings.warn(
ok
test_script_get_tracing_state (test_jit.TestScript.test_script_get_tracing_state) ... ok

----------------------------------------------------------------------
Ran 2 tests in 0.160s

OK


  1. trace模式下,测试结果
root@5534245c24de:/workspace/user_data/527/210/pytorch/test/jit# python test_jit_is_tracing_in_trace.py -v
test_is_tracing_returns_true_in_trace_mode (__main__.TestJitIsTracing.test_is_tracing_returns_true_in_trace_mode)
Validates that torch.jit.is_tracing() returns True during trace recording. ... /usr/local/python3.11.14/lib/python3.11/site-packages/torch/jit/_trace.py:1000: DeprecationWarning: `torch.jit.trace` is deprecated. Please switch to `torch.compile` or `torch.export`.
  warnings.warn(
ok

----------------------------------------------------------------------
Ran 1 test in 1.378s

OK



验证结论:
NPU 环境运行script/trace模式下的测试用例,三个用例执行成功,无功能失败项。torch.jit.is_tracing 模式检测、状态返回、控制流分支适配等能力均符合 PyTorch 标准语义,适配完成且验证通过

【CheckList】

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

likedislike
Pull Request已成功合入, 合并人@ascend-robot
(感谢 yrouyi 的贡献)
yrouyiyrouyi
5月29日 创建了 pull request,commit de7c49ea
yrouyiyrouyi
5月29日 关联了issue:[Usage]: torch.jit.is_tracing () ----此API,在trace模式下,测试样例缺失
ascend-robot
ascend-robot成员
5月29日 评论:

The repo or branch is not access to PR-cooperate, please check the current repo https://gitcode.com/Ascend/pytorch.git, branch=v2.10.0

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

CLA Signature Pass

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

likedislike
ascend-robot
ascend-robot成员
5月29日 评论:

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

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

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

注意:

  1. /sync 命令可以指定同步到多个分支,仅最后一个 /sync 命令生效
  2. 如果创建的同步 PR 不正确,可通过向同步 PR 的源分支提交轻量级 PR 完善,或使用 /close 命令关闭
likedislike
ascend-robot
ascend-robot成员
5月29日 评论:

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)
likedislike
yrouyi
yrouyi
5月29日 评论:

compile

likedislike
ascend-robotascend-robot成员
5月29日 添加了label:ci-pipeline-running
ascend-robot
ascend-robot成员
5月29日 评论:

Ascend docs pipeline is running...

likedislike
ascend-robotascend-robot成员
5月29日 添加了label:docs-ci-pipeline-running
ascend-robot
ascend-robot成员
5月29日 评论:

✅ 跳过 docs ci 检查,没有需要检查的文档文件

likedislike
ascend-robotascend-robot成员
5月29日 删除了label:docs-ci-pipeline-running
ascend-robotascend-robot成员
5月29日 添加了label:docs-ci-pipeline-success
ascend-robot
ascend-robot成员
5月29日 评论:

Ascend docs pipeline is running...

likedislike
ascend-robotascend-robot成员
5月29日 删除了label:docs-ci-pipeline-success
ascend-robotascend-robot成员
5月29日 添加了label:docs-ci-pipeline-running
ascend-robot
ascend-robot成员
5月29日 评论:

✅ 跳过 docs ci 检查,没有需要检查的文档文件

likedislike
ascend-robotascend-robot成员
5月29日 删除了label:docs-ci-pipeline-running
ascend-robotascend-robot成员
5月29日 添加了label:docs-ci-pipeline-success
ascend-robotascend-robot成员
5月29日 删除了label:ci-pipeline-running
ascend-robotascend-robot成员
5月29日 添加了label:ci-pipeline-passed
ascend-robot
ascend-robot成员
5月29日 评论:
流水线 PR-pipeline_pytorch#27904 已完成
阶段 任务名 状态 详情
编译构建 Build_X86 >>>
Build_ARM >>>
Build_LibTorch_x86 >>>
Build_LibTorch_ARM >>>
Build_X86_torchair 🛑 >>>
Build_ARM_torchair 🛑 >>>
patch_test 🛑 >>>
恶意代码检查 Antipoison >>>
编码安全与规范检查 CodeCheck >>>
check_error >>>
CodeCheck_lintrunner >>>
开源片段检查 SCA >>>
开发者测试 UT_X86_Part_01 🛑 >>>
UT_X86_Part_02 🛑 >>>
UT_ARM_A3_Part_01 🛑 >>>
UT_ARM_A3_Part_02 🛑 >>>
UT_ARM_A2_Part_01 >>>
UT_ARM_A2_Part_02 >>>
UT_ARM_A2_Part_03 >>>
UT_inductor_Part_01 🛑 >>>
UT_inductor_Part_02 🛑 >>>
UT_inductor_Part_03 🛑 >>>
UT_inductor_Part_04 🛑 >>>
UT_DIST_ARM_Part_01 🛑 >>>
UT_DIST_ARM_Part_02 🛑 >>>
UT_DIST_ARM_Part_03 🛑 >>>
UT_DIST_ARM_Part_04 🛑 >>>
流水线 PR-pipeline_pytorch >>>
likedislike
Jingwei Huang
Jingwei Huang成员
5月29日 评论:

/lgtm

likedislike
liwei386成员
5月29日 评论:

/approve

likedislike
ascend-robotascend-robot成员
5月29日 添加了label:approvedlgtm
ascend-robotascend-robot成员
5月29日 合入了pull request
ascend-robot
ascend-robot成员
5月29日 评论:

The repo or branch is not access to PR-cooperate, please check the current repo https://gitcode.com/Ascend/pytorch.git, branch=v2.10.0

likedislike
ascend-robot
ascend-robot成员
5月29日 评论:
流水线 pytorch_gitcode_PR_multiVersion#9684 [ commitID:c36adb16 ] 已完成
likedislike