已合并
修复API入参校验和用例适配 #36711
修复API入参校验和用例适配 #36711
已合并
bellatan创建于 5月26日
bellatan
bellatan成员
5月26日
# 【合入来源】 > **如有社区issue,请关联issue链接**\ > **请勿携带内部流程信息(需求链接、问题单、内部issue等)** - [ ] 需求 - [ ] 问题单 - [x] issue/工单 - [ ] 重构优化 - [ ] 资料更新 # 【修改方案】 ### 1. device 入参类型缺少校验,导致非法 device 未按预期报错 **API:** `set_device_limit(device, cube_num, vector_num)`、`get_device_limit(device)` **API 原始约束:** `set_device_limit` 和 `get_device_limit` 的 `device` 参数语义是指定 NPU 设备卡号,应传入明确的 NPU 设备 ID。 **当前问题:** 传入 `None` 和 `float` 等非 `int` 类型时未报错,不符合 API 参数约束。 **根因分析:** 原实现中对入参 `device` 使用了 `_get_device_index(device, optional=True)` 对入参 `device` 进行兼容性处理,导致约束之外的非法入参没有被及时拒绝。 **解决方案:** 针对 `set_device_limit/get_device_limit` 这两个 API,不再使用 `_get_device_index(device, optional=True)` 做宽松解析,而是直接按照 API 约束对原始 `device` 参数做严格校验。 新加校验规则: 1. `device` 必须是 `int`; 2. `device` 不能是 `bool`; ```python if isinstance(device, bool) or not isinstance(device, int): raise TypeError( "device must be an int, but got {}{}".format( type(device).__name__, pta_error(ErrCode.TYPE) ) ) ``` **涉及的修改文件**:`torch_npu/npu/npu_config.py` ### 2. allow_hf32 入参类型缺少校验,导致非 bool 入参被错误接受 **API:** `matmul.allow_hf32`、`conv.allow_hf32` **API 原始约束:** `torch_npu.npu.matmul.allow_hf32` 的语义是控制 MatMul 类算子是否允许使用 HF32 计算,应作为布尔型配置开关使用。 `torch_npu.npu.conv.allow_hf32` 的语义是控制 Conv 类算子是否允许使用 HF32 计算,应作为布尔型配置开关使用。 **当前问题:** `allow_hf32` 缺少 bool 类型校验,传入非 bool 类型没有报错,不符合 API 参数约束。 **根因分析:** 原实现直接`"enable" if value else "disable"`,根据 Python 对象的真假值进行判断,未判断 `value` 是否为 `bool` 类型,导致非法类型未被拒绝。 **解决方案:** 新增 `isinstance(value, bool)` 校验,非 bool 类型直接抛出 `TypeError`,避免非法入参被隐式转换为开关配置。在 `_allowHF32Matmul.__setattr__` 和 `_allowHF32Conv.__setattr__` 的 `allow_hf32` 分支中增加 bool 类型校验。 新加校验规则: 1. `allow_hf32` 必须是 `bool`; 2. 只允许 `True` 或 `False`; 3. `int`、`str`、`None`、`list`、`dict` 等非 bool 类型均抛出 `TypeError`。 ```python if name == "allow_hf32": if not isinstance(value, bool): raise TypeError( "allow_hf32 must be a bool, but got {}{}".format( type(value).__name__, pta_error(ErrCode.TYPE) ) ) ``` **涉及的修改文件**:`torch_npu/npu/npu_config.py` ### 3. 用例适配 用例:test\nn\test_module_hooks.py,用例名:test_hook_inplace 问题:用例ci失败,报错正则匹配失败 根因:改用例预期报错的接口有变更,torch==2.12预期报错接口名为**BackwardHookFunctionBackward**,torch==2.13预期报错接口名变更为**BackwardHookFunction**。 解决:开放用例,用例报错提示适配,兼容两个版本的torch: `with self.assertRaisesRegex(RuntimeError, "Output 0 of BackwardHookFunctionBackward is "`修改为`with self.assertRaisesRegex(RuntimeError, "Output 0 of BackwardHookFunction(Backward)? is "` `with self.assertRaisesRegex(RuntimeError, "BackwardHookFunction(Backward)? is a view "`修改为`with self.assertRaisesRegex(RuntimeError, "Output 0 of BackwardHookFunction(Backward)? is "` # 【资料变更】 不涉及 # 【接口变更】 不涉及 # 【功能验证】 已有用例本地验证pass,CI pass 1. 新加用例:test/npu/test_torch_npu.py ![image.png](https://raw.gitcode.com/user-images/assets/7404318/a041fbeb-16cd-4654-9865-a4a02d96df17/image.png 'image.png') ![image.png](https://raw.gitcode.com/user-images/assets/7404318/c353f1d5-03d3-4300-9277-f8ce71e641ae/image.png 'image.png') 2. 已有用例:test/npu/test_torch_backends.py ![image.png](https://raw.gitcode.com/user-images/assets/7404318/1116053c-a3ab-4b93-b3da-99e1f864d63d/image.png 'image.png') 3. 已有用例:test/nn/test_module_hooks.py ![image.png](https://raw.gitcode.com/user-images/assets/7404318/90e9cb29-e0f0-4bdb-a744-a23be66a92a5/image.png 'image.png') # 【CheckList】 > PR提交人对以下CheckList自检项进行全量自检,自检通过或不涉及,均修改 [ ] 为 [x] - [x] 代码注释完备,正确记录错误日志 - [x] 代码实现进行了返回值、空指针等校验 - [x] PR标题正确使用类型标签,如:feat、fix、refactor、docs、test等 - [x] PR持续集成流水线(CI)执行通过,代码检查无异常
likedislike
Pull Request已成功合入, 合并人@ascend-robot
(感谢 bellatan 的贡献)
bellatanbellatan成员
5月26日 创建了 pull request,commit d40d2141
bellatanbellatan成员
5月26日 关联了issue:[Bug]: 部分API入参缺少校验
ascend-robotascend-robot成员
5月26日 添加了label:stat/needs-squash
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 liujunzhu, hbhu_bin (2/2) liujunzhu (1/1)
torch_npu/npu liujunzhu, hbhu_bin (2/2) liujunzhu (1/1)

💡 Tip:

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

CLA Signature Pass

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

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

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

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
bellatan
bellatan成员
5月26日 评论:

compile

likedislike
ascend-robotascend-robot成员
5月26日 添加了label:ci-pipeline-running
bellatanbellatan成员
5月26日 修改标题为 “修复API入参校验和用例适配”,原标题为“fix the name change of the BackwardHookFunction”
ascend-robot
ascend-robot成员
5月26日 评论:

Ascend docs pipeline is running...

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

Ascend docs pipeline is running...

likedislike
ascend-robotascend-robot成员
5月26日 删除了label:ci-pipeline-running
ascend-robotascend-robot成员
5月26日 添加了label:ci-pipeline-passed
ascend-robot
ascend-robot成员
5月26日 评论:
流水线 PR-pipeline_pytorch#26829 [ commitID:9d52734b ] 已完成
阶段 任务名 状态 详情
编译构建 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 >>>
此流水线已支持下列评论快捷指令,仅PR创建者评论有效
  • compile : 运行流水线
  • retry : 重试流水线所有失败子任务
  • retry <任务名> : 仅重试指定失败子任务
  • stop : 停止流水线
likedislike
ascend-robot
ascend-robot成员
5月26日 评论:

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

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

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

likedislike
liujunzhu
liujunzhu成员
5月26日 评论:

/approve

likedislike
ascend-robotascend-robot成员
5月26日 添加了label:approved
hbhu_bin成员
5月26日 评论:

/lgtm

likedislike
ascend-robotascend-robot成员
5月26日 添加了label:lgtm
ascend-robot
ascend-robot成员
5月26日 评论:

Review Guide

This pull-request passes review.
Committers who wrote a comment of /approve are: liujunzhu.
Reviewers who wrote a comment of /lgtm are: liujunzhu, hbhu_bin.

likedislike
ascend-robotascend-robot成员
5月26日 合入了pull request
ascend-robot
ascend-robot成员
5月26日 评论:
流水线 pytorch_gitcode_PR_multiVersion#9384 [ commitID:9d52734b ] 已完成
likedislike
ascend-robot
ascend-robot成员
5月28日 评论:

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月28日 评论:

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