Merged
易用性:增加torch_extension call c++ api前类型校验报错信息 #11968
易用性:增加torch_extension call c++ api前类型校验报错信息 #11968
Merged
chenyifanAdd on Sep 14
chenyifan
chenyifanCollaborators
Sep 14

描述

为 torch_extension 在调用 C++/pybind API 前增加 Python 侧入参类型校验,将原先含糊的 argN 报错提升为带真实参数名的 pybind 风格文案,提升易用性。

方案要点:

  • 在 cann_ops_transformer/utils/arg_check.py 提供 wrap_op_module:按 pybind __doc__ 解析参数名与类型,对明确类型错误前置拦截并改写报错
  • 在 OpBuilder.load() 统一接入,算子侧仍写 _builder.load(),无需逐算子包装
  • 策略:宁可放给 pybind,不能误拦——仅拦截明确错类型(如 str/float 当 int);bool、numpy 标量、doc 解析失败等模糊情况放行,由 pybind 裁决
  • 不做 tensor dtype/device/shape;参数个数、非法 kwargs、取值范围仍交 pybind/C++
  • 附带 @check_args / require_*,供纯 Python API 选用(非本方案主路径)

关联的Issue

https://gitcode.com/cann/ops-transformer/issues/5243

测试

  • mega_moe:get_mega_moe_ccl_buffer_size 传入 num_experts='128'(底层 moe_expert_num)时,报错含真实参数名 moe_expert_num,文案含 supported / Invoked with / Mismatched argument

文档更新

类型标签

likedislike
Pull Request has been successfully merged by CANN-robot.
(Thank you for chenyifan's contribution.)
chenyifanchenyifanCollaborators
Sep 14 Created pull request, commit c85db6d9
chenyifanchenyifanCollaborators
Sep 14 Associated issue: [Requirement|需求建议]:提升torch_extension易用性,调用c++接口时类型错误应明确参数
atomgit-bot
atomgit-bot
Sep 14 Comments:

变更摘要

本 PR 为 torch_extension 在调用 C++/pybind API 前增加 Python 侧入参类型校验:通过解析 pybind 函数 __doc__ 中的参数名与类型(_parse_pybind_doc),在调用前拦截明确的类型错误,并将原先含糊的 argN 报错改写为带真实参数名的 pybind 风格文案(_format_type_error,含 supported / Invoked with / Mismatched argument)。新模块 cann_ops_transformer/utils/arg_check.py 提供 wrap_op_module 代理包装(_CheckedOpModule)与 @check_args / require_* 校验工具,并在 OpBuilder.load() 中统一接入,算子侧继续使用 _builder.load() 即可,无需逐算子包装。策略上"宁可放给 pybind,不能误拦":仅拦截明确错类型(如 str/float 当 int),bool、numpy 标量、doc 解析失败等模糊情况放行;不校验 tensor 的 dtype/device/shape,参数个数、非法 kwargs 与取值范围仍由 pybind/C++ 裁决。

主要改动

  • OpBuilder.load() 统一接入校验代理: 在 torch_extension/cann_ops_transformer/op_builder/builder.py 中懒加载 from cann_ops_transformer.utils.arg_check import wrap_op_module,加载扩展模块后用 wrap_op_module(op_module) 包裹并缓存到 OpBuilder._loaded_ops,使所有算子复用同一校验入口。
  • 新增 arg_check.py 核心模块: 提供 wrap_op_module(基于 _CheckedOpModule 懒代理,仅包装可解析 doc 的可调用对象,解析失败则原样透传)、_parse_pybind_doc doc 解析、_format_type_error pybind 风格报错格式化,以及带 param_name/expected/value 字段的 ArgTypeError 异常。
  • 提供 @check_args 与 require_* 校验工具: 实现 check_args 装饰器(依据函数类型注解校验绑定参数并统一改写报错)及 require_int/require_float/require_str/require_bool/require_tensor/require_dtype/require_list_tensor 等 require_* 系列函数(含 Optional 变体),供纯 Python API 选用。
  • 新增 utils/__init__.py 公共导出: 从 .arg_check 导出 check_args、wrap_op_module、check_value 及全部 require_* 函数并声明 __all__,形成对外统一入口。
  • 明确"不误拦"校验边界: check_value/require_* 对 int、str、float、bool、Tensor、dtype、list、Optional/Union 等注解类型做前置校验,但对 bool(int 子类)、numpy 标量及 doc 解析失败等情况选择放行,交由 pybind 裁决,避免错误拦截。
likedislike
Incorrect?
CANN-robotCANN-robotCollaborators
Sep 14 Added label: cann-cla/yes
CANN-robot
CANN-robotCollaborators
Sep 14 Comments:

CLA Signature Pass

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

likedislike
46 messages are collapsed here。 See more
YuZhengzhong
YuZhengzhongCollaborators
Sep 14 Comments:

/approve
/lgtm

likedislike
CANN-robotCANN-robotCollaborators
Sep 14 Added label: lgtm
CANN-robot
CANN-robotCollaborators
Sep 14 Comments:

The MR can not be merged, because of CodeReview discussion not resolved

If you want to solve this problem, you can click here to do it in the FAQs.

likedislike
景明景明Collaborators
Sep 14 Resolved final issue
CANN-robotCANN-robotCollaborators
Sep 14 Merged pull request