已合并
[v2.7.1] test: add validation cases for torch.autograd.profiler_util.Kernel.index #42082
[v2.7.1] test: add validation cases for torch.autograd.profiler_util.Kernel.index #42082
已合并
m0_66826439创建于 7月19日
m0_66826439
m0_66826439
7月19日

【合入来源】

关联 issue: #2894(社区任务第二期 - Ascend for PyTorch API 一致性开发)

【修改方案】

torch.autograd.profiler_util.Kernel.index 补齐验证用例(用例补齐场景 1.3)。

Kernel 定义于 torch/autograd/profiler_util.pyKernel = namedtuple("Kernel", ["name", "device", "duration"])Kernel.index 是 namedtuple 从 tuple 继承的标准方法,返回值在元组中的位置(name=0, device=1, duration=2),找不到抛出 ValueError

PyTorch 官方社区 test/ 目录中无 Kernel.index 的直接验证用例(test_autograd.pytest/profiler/test_profiler.py 仅从 profiler_util 导入 EventList/FunctionEvent/FunctionEventAvg/_format_time,未涉及 Kernel.index),故新增 test/autograd/test_profiler_util.py,覆盖:

  1. name/device/duration 字段值返回正确索引(0/1/2)
  2. start/stop 边界参数生效,越界查找抛出 ValueError
  3. 查找不存在值抛出 ValueError

文件按可扩展原则组织,后续 profiler_util 其他 API 可在同一文件补充。该 API 为非计算类纯 Python 方法,不涉及张量,无需 NPU 设备迁移;Kernel 在 torch_npu 中直接复用 torch 实现(torch_npu/contrib/transfer_to_npu.py:16),无需代码补齐。

【资料变更】

不涉及。已检查 docs/zh/native_apis/ 下各版本目录(pytorch_2-7-1pytorch_2-9-0pytorch_2-10-0pytorch_2-11-0pytorch_2-12-0)的 torch-autograd.mdKernelprofiler_util 下其他工具类(EventListFunctionEventInterval 等)均未列入支持度表格;Kernel.index 为非计算类纯 Python 方法,与数据类型无关,PyTorch 官方文档同样未单独列出该 tuple 继承方法,无需补充资料。详见 issue #2894。

【接口变更】

不涉及。仅新增测试文件,未修改任何对外接口。

【功能验证】

测试环境:

torch: 2.10.0+cpu
torch_npu: 2.10.0
CANN: 9.0.0
NPU: Ascend910

测试方法:

source /usr/local/Ascend/ascend-toolkit/set_env.sh
TORCH_DEVICE_BACKEND_AUTOLOAD=1 python3.12 /tmp/test_profiler_util.py -v

测试结果(20t NPU 实机环境):

root@a89d9d564fe4:/home# source /usr/local/Ascend/ascend-toolkit/set_env.sh
root@a89d9d564fe4:/home# TORCH_DEVICE_BACKEND_AUTOLOAD=1 python3.12 /tmp/test_profiler_util.py -v
test_kernel_index_raises_value_error_when_missing (__main__.TestProfilerUtilKernelApi.test_kernel_index_raises_value_error_when_missing) ... ok
test_kernel_index_returns_field_position (__main__.TestProfilerUtilKernelApi.test_kernel_index_returns_field_position) ... ok
test_kernel_index_with_start_stop_bounds (__main__.TestProfilerUtilKernelApi.test_kernel_index_with_start_stop_bounds) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.309s

OK

【CheckList】

likedislike
Pull Request已成功合入, 合并人@ascend-robot
(感谢 m0_66826439 的贡献)
m0_66826439m0_66826439
7月19日 创建了 pull request,commit f0995a83
m0_66826439m0_66826439
7月19日 关联了issue:torch.autograd.profiler_util.Kernel.index 缺少验证用例
atomgit-bot
atomgit-bot
7月19日 评论:

变更摘要

本 PR 为 torch.autograd.profiler_util.Kernel.index 方法新增验证用例,新增测试文件 test/autograd/test_profiler_util.pyKernel 是基于 namedtuple 定义的元组类(字段为 namedeviceduration),index 是其从 tuple 继承的标准方法。该变更不涉及任何接口修改或 NPU 设备迁移,仅补齐社区缺失的 API 验证覆盖。

主要改动

  • 新增 test/autograd/test_profiler_util.py 测试文件:创建独立的测试模块,用于验证 torch.autograd.profiler_util 中的 API,文件按可扩展原则组织,后续其他 profiler_util API 可在同一文件中补充。
  • test_kernel_index_returns_field_position 用例:验证 Kernel.index 按字段值返回正确的元组索引位置(name→0、device→1、duration→2)。
  • test_kernel_index_with_start_stop_bounds 用例:验证 index 方法的 start/stop 边界参数生效,合法范围内查找成功、越界查找抛出 ValueError
  • test_kernel_index_raises_value_error_when_missing 用例:验证查找不存在值时正确抛出 ValueError
likedislike
atomgit-bot
atomgit-bot
7月19日 评论:

代码审查

审查总结

审查文件:

  • test/autograd/test_profiler_util.py — 发现 1 个 P3 问题(未使用的 import torch

按优先级统计:

  • P0: 0
  • P1: 0
  • P2: 0
  • P3: 1

总体风险评估:低风险。 该变更新增了一个针对 torch.autograd.profiler_util.Kernel.index 的测试文件,测试逻辑正确,覆盖了字段位置索引、start/stop 边界参数、以及缺失值抛出 ValueError 三个核心场景。唯一的小瑕疵是第 7 行的 import torch 未被文件直接使用,属于冗余导入,不影响测试执行和正确性。

⚠️ 已识别出整体风险,但无法提取行内评论,请参考整体评估。

likedislike
此处折叠了52条消息 查看更多
ascend-robot
ascend-robot成员
22 天前 评论:
流水线 PR-pipeline_pytorch#50813 [ commitID:5212e0bc ] 已完成
>>>代码风格自动修复执行成功(无修复内容)
阶段 任务名 状态 详情
编译构建 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_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 🛑 >>>
UT_ARM_A2_Select_Part_01 >>>
UT_ARM_A2_Select_Part_02 >>>
流水线 PR-pipeline_pytorch >>>
此流水线已支持下列评论快捷指令,仅PR创建者和白名单成员[wujinyuan1, huangjingwei, liangsongwei, yashi999, culechan, Dring, wuyouqi1, L1919_snow, qq_52711437, WhiteNight12, nomiz, xiu_21, ffmh, wanglijun55, hss-shuai, husichao, smallsilly, lanshaozuishuai, jimmyisme1, lzy0920232, alpha-junh, Sunshine_Youngster, wei_zhuoyi, zhangyihuiben, zyw-hw, zzzkeke, rmch, yangch0324, LucciC, AACAES, renyujin, wjlflyer, senzhen-town, pengjingyou, qsc97, limuan, yule100, xiaoqi-zhou, kuhn7, chenxingying, hanye02, zichun_ye, anyrenwei, kkjocker, wangzili121, Lu_G, yvjc, puddingfjz, HandsoemLemon, bigprestigee1, huawuyi, zhenyu10, dairenjie, du-jin-hang, zou-jieyu, adelaideliu, TrHan, wanlinan, Windwindzzz, pengqihw, kisnwang, yuheng_wang, honghao_wang, jizewei, zhangguoguang, sunyu-xuan, chenrayray, hbhu_bin, liujunzhu, c_34, LiNuoh, maoyuanpeng1, zzhongmin, zhaoyu65, bellatan, jiabaolin, zhuofanshen, wencaiwen, lu_zhuge, caoshuyang, molly12, lyx324521, LQ1206, gitcode-chenjiao, cai-weiwei1989, CHDong, ogqin, yuanlipingGit, xuqinglin1, lqz2, zouwei1, chaoluoa, paradox325, jackzhang1116, yaoyao, akh, yujiacheng, dengjie0116, Hubert11111, Shine_Ws, wslhj555, longqiand, OYtao666, JiaqingQiang, luyyyy, Kingbelial, zhanghaiyu0101, wenxp1018, yanliu-luoluo, ksun_sekiro, liyong328, wgzheng, tangky, vivi_is_coding, aoiaoisola, weixin_44494597, wangmengmengwang65667, hid57809721, qq_35468730, comeonup, C547032, gcw_m5OQChA4, yao_yao_ling_xian, cnnbwcy, szqfes_12, cora_19, cann_lilin, can, shawnylee233, fanglanyue0916, hhz0, LiNuohang, taohuoquan, Jesse]评论有效
  • compile、compile_inductor、compile_torchair : 运行流水线
  • retry : 重试流水线所有失败子任务
  • retry <任务名> : 仅重试指定失败子任务
  • stop : 停止流水线
likedislike
chenrayray
chenrayray成员
22 天前 评论:

/lgtm

likedislike
ascend-robotascend-robot成员
22 天前 添加了label:lgtm
ascend-robotascend-robot成员
22 天前 合入了pull request
ascend-robot
ascend-robot成员
22 天前 评论:
流水线 pytorch_gitcode_PR_multiVersion#13300 [ commitID:5212e0bc ] 已完成
likedislike