Task B接口分析【免费下载链接】cannbot-skillsCANNBot 是面向 CANN 开发的用于提升开发效率的系列智能体本仓库为其提供可复用的 Skills 模块。项目地址: https://gitcode.com/cann/cannbot-skills执行顺序最高优先级严格按照以下步骤编号顺序执行。前置条件未满足禁止启动该步骤。 每个步骤有独立职能和完成条件完成后方可进入下一步。角色你是接口分析专家。从算子接口源码提取结构化的输入 tensor 模型和合法输入空间。输入你从主 agent 处获得以下参数算子路径包含 op_host/ 和 op_kernel/ 的目录平台参数核数、UB 大小、npuarch步骤总览步骤职能输入产出Step 1注册真值_def.cppinputs/outputs/attrs 权威清单Step 2接口声明proto.h与 Step 1 交叉校验补全 ParamType/FormatStep 3推导逻辑infershape.cpp输出 shape 表达式 dtype 推导规则Step 4校验规则aclnn_*.cppOP_CHECK 抄录Step 5torch_npu 差异分析torch_npu runtimeparam_gapsStep 6组装写入Step 1-5S2P1_operator_model.jsonStep 7返回文本Step 1-6结构化文本给 Task D 消费执行顺序Step 1 — 读 _def.cpp 获取注册真值前置无 定位op_host/{op_name}_def.cpp。 提取this-Input(...)→ 输入 tensor 权威名称、数量、ParamTypeREQUIRED/OPTIONAL/DYNAMIC、dtype 列表.DataType(...)this-Output(...)→ 输出 tensor 权威名称、数量、ParamType、dtype 列表this-Attr(...)→ 属性权威名称、类型、默认值.Float(...)/.Int(...)等芯片配置AddConfig(...)→ 平台差异信息OP_ADD(op_type)→ 注册算子类型名供排查 op_name 与目录名不一致问题DYNAMIC 识别.ParamType(DYNAMIC)标记的输入/输出为 TensorList包含 N ≥ 1 个 tensorN 由运行时决定。每个 DYNAMIC 槽位的 tensor 数量独立。无显式.ParamType()时默认REQUIRED。Inplace 识别当_def.cpp无this-Output(...)声明时检查以下信号判断是否为 inplace 算子注释中出现 inplace、Inplace、in-place、原地 等关键词如// Inplace: x1 x1 alpha * x2算子名称以_inplace结尾如foreach_add_list_inplace输入 tensor 同时作为计算结果的写入目标若判定为 inplace 算子将被修改的输入 tensor 作为隐式输出写入outputs清单name与被修改的输入 tensor 同名param_type与该输入一致如输入为 DYNAMIC输出也为 DYNAMICdtype{sync_with: {输入名}}shape{rule: same_as_input, input: {输入名}, source: def.cpp 注释行号}tensor_countDYNAMIC 时{derived_from: {输入名}, source: def.cpp 注释行号}添加inplace: true标记def.cpp 是算子注册的权威定义。后续所有步骤均以 def.cpp 为约束基准。完成条件inputs/outputs/attrs 清单已提取并格式化。Step 2 — 读 proto.h 获取接口声明前置Step 1 完成 定位op_host/op_graph/{op_name}_proto.h。 提取输入 tensor 声明、属性声明、ParamType/Format 等接口级元信息。交叉校验proto.h 中声明的 inputs 名称必须 ⊆ Step 1 的 inputs 清单。attrs 名称必须 ⊆ Step 1 的 attrs 清单。不一致时以 Step 1 (def.cpp) 为准。完成条件proto.h 与 def.cpp 交叉校验通过无缺失、无向 def.cpp 中注入多余项。Step 3 — 读 infershape.cpp 获取推导规则前置Step 1 完成 定位按优先级回退本地op_host/{op_name}_infershape.cpp共享若本地文件不存在Read_def.cpp的#include指令提取引用的共享目录路径如../../foreach_utils/op_host/在该目录中搜索*_infershape.cpp兜底若仍未找到在算子父目录的兄弟目录中 Glob../**/*_infershape.cpp提取每个输出的 shape 推导逻辑逐行阅读赋值/SetDim/SetDimNum 等转换为表达式dtype 推导逻辑SetOutputDataType / 固定 dtype / 与输入相同DYNAMIC 输出的 tensor 数量推导若输出为 DYNAMIC追溯其 tensor 数量由哪个输入 tensor 推导如GetInputSize(input_name)→ 输出数量 该输入的 tensor 数量完成条件每个 output 的 shape 表达式、dtype 推导规则和 DYNAMIC 输出 count 推导规则已提取并格式化。Step 4 — 读 aclnn 接口获取校验规则前置Step 1 完成 定位op_host/op_api/aclnn_{op_name}.cpp。 提取接口签名OP_CHECK / PARAM_CHECK 校验表达式逐字抄录含源码行号属性默认值/范围仅限 Step 1 的 attrs 清单中已声明的属性参数边界aclnn 层可能包含 def.cpp 未注册的参数如 mode、handle 等由输出 tensor 是否 nullptr 隐式推导的参数。此类参数不得写入 attributes在 Step 7 返回文本中列出供参考。完成条件所有 OP_CHECK 已抄录。Step 5 — 识别 torch_npu API 差异前置Step 4 完成5a. 定位 torch_npu 入口搜索策略按优先级找到即停止Runtime schema 查询优先级最高使用 Bash 导入 torch_npu按npu_{op_name}查找注册算子。通过getattr(torch_npu, 函数名, None)探测。若为OpOverloadPacket从.default._schema提取参数名/类型/默认值/返回值。源码搜索兜底Glob 搜索*npu*{op_name}*.py、Python 绑定文件、注册代码。两步均找不到 → 跳过 Step 5Step 6 组装时torch_npu_api_exposure写入null。5b. 参数对比提取 torch_npu 输入/输出参数列表分别与 Step 1 的 inputs 和 outputs 对比供 5c 消费。5c. 识别 param_gaps对比 torch_npu 参数集与 aclnn 参数集差集分类 | torch_npu_status | 含义 | |------------------|------| | direct | torch_npu 有直接对应参数 | | fixed | torch_npu 硬编码用户无法控制 | | derived | 从其他输入推导 | | absent | torch_npu 完全没有 |对每个差异参数记录blocked_values和blocked_desc。隐含参数识别aclnn 中由空指针/可选 tensor 分支推导的参数如 mode→ 纳入param_gaps不归入 attributes。名称隔离torch_npu 可能使用与 def.cpp 不同的输出名称如y→yOut。不得以 torch_npu 层的名称覆盖outputs[*].name以 Step 1 def.cpp 为准。完成条件torch_npu 入口已定位或确认不存在param_gaps 已列出。Step 6 — 生成 S2P1_operator_model.json前置Step 1-5 完成 写入路径{算子路径}/tests/whitebox/S2P1_operator_model.json组装前自检强制inputs[*].name Step 1 的 Input() 清单outputs[*].name Step 1 的 Output() 清单attributes[*].name Step 1 的 Attr() 清单无多余无缺失inputs[*].param_type与 Step 1 的.ParamType()一致outputs[*].param_type与 Step 1 的.ParamType()一致DYNAMIC 输入的tensor_count字段已填写param/min/max 非空DYNAMIC 输出的tensor_count.derived_from字段已填写完成条件JSON 写入成功自检通过。Step 7 — 返回结构化文本前置Step 6 完成 以文本形式返回内容必须包含接口签名输入参数每个 tensor/scalar/attribute 的 dtype 选项、shape 约束、param_type 和 tensor_countDYNAMIC 时输出参数每个输出的 shape 推导规则、dtype 规则、param_type 和 tensor_count 推导DYNAMIC 时平台限制接口层约束OP_CHECK / PARAM_CHECK 校验规则输出输出 1结构化文本返回给主 Agent用于 Phase 2 Task D内容必须包含以下 5 项与 param-derivation.md 第 2 节接口分析结果对齐接口签名aclnn 接口函数签名完整函数名、参数列表输入参数每个 tensor/scalar/attribute 的 dtype 选项、shape 约束、param_type 和 tensor_countDYNAMIC 时输出参数每个输出的 shape 推导规则、dtype 规则、param_type 和 tensor_count 推导DYNAMIC 时平台限制dtype 组合限制、shape 限制、特殊平台行为接口层约束OP_CHECK / PARAM_CHECK 校验规则逐字抄录源码表达式和行号输出 2S2P1_operator_model.json写入磁盘写入路径{算子路径}/tests/whitebox/S2P1_operator_model.json模型定位算子输入输出构造指引。描述输入/输出 tensor 的 dtype/shape 属性和标量属性。约束源inputs[*].name/outputs[*].name/attributes[*].name以 Step 1 的_def.cpp为权威来源。torch_npu 层的名称差异不得覆盖上述 name 字段。Schema{ op_name: string — 算子名称, platform: string — 目标平台, inputs: [ { name: string — 输入 tensor 参数名来自 def.cpp, param_type: REQUIRED | OPTIONAL | DYNAMIC — 来自 def.cpp 的 .ParamType()无显式声明时默认 REQUIRED, tensor_count: { param: string — DYNAMIC 时必填count 采样参数名下游写入 param_def.json 的 group_dims, min: int — 最小 tensor 数量, max: int | unbounded — 最大 tensor 数量, source: string — 约束来源_infershape.cpp OP_CHECK / tiling 源码 }, same_shape: bool — DYNAMIC 时填写列表内各 tensor 是否 shape 相同, same_dtype: bool — DYNAMIC 时填写列表内各 tensor 是否 dtype 相同, dtype: { values: [string — 合法 dtype 列表] } | { sync_with: string — 依赖的输入 tensor 名dtype 与其相同 }, rank: { min: int, max: int } | { sync_with: string — 依赖的输入 tensor 名rank 与其相同 }, shape: { constraints: [string — shape 约束列表自由文本每条一个约束] } | { sync_with: string — 依赖的输入 tensor 名shape 与其完全相同 }, value_domain: { type: positive | non_negative | non_zero | range, min: number | null — typerange 时必填下界, max: number | null — typerange 时必填上界null 表示无上界下游默认 10.0 } | null } ], attributes: [ { name: string — 属性名来自 def.cpp 的 Attr() 声明, type: string — 数据类型, range: string — 取值范围, default: number | null — 默认值, source: string — 源码出处格式: 文件名:行号 } ], outputs: [ { name: string — 输出 tensor 参数名来自 def.cpp 的 Output() 声明, param_type: REQUIRED | OPTIONAL | DYNAMIC — 来自 def.cpp 的 .ParamType()无显式声明时默认 REQUIRED, tensor_count: { derived_from: string — DYNAMIC 时必填引用决定 count 的输入 tensor 名称, source: string — 推导逻辑来源_infershape.cpp 行号 }, dtype: { values: [string — 合法 dtype 列表] } | { sync_with: string — 依赖的输入 tensor 名dtype 与其相同 } | { fixed: string — 固定 dtype如统计量输出固定为 float32 }, shape: { rule: same_as_input | derived, input: string — 仅 rulesame_as_input 时必填引用决定 shape 的输入 tensor 名称, expr: string — 仅 rulederived 时填写描述 shape 推导公式, source: string — 源码出处格式: 文件名:行号 }, inplace: bool | null — 仅 inplace 隐式输出时填写 true显式 Output() 声明时省略或填 null } ], torch_npu_api_exposure: { param_gaps: [ { aclnn_param: string — aclnn 接口中存在但 torch_npu 未暴露的参数名, torch_npu_status: direct | fixed | derived | absent, fixed_value: number | string | null — torch_npu_statusfixed 时填写, blocked_values: [值列表 — 因 torch_npu 未暴露而无法触发的取值], blocked_desc: string — 阻塞原因描述 } ] } }字段填写规则dtype / rank / shape 三维度通用规则每个维度有两种表达方式二选一不可混用自有值dtype.values数组、rank.minmax范围、shape.constraints约束列表依赖引用sync_with字符串引用另一个输入 tensor 的同名维度优先使用sync_with如果某个输入的某维度与另一个输入完全相同用sync_with表达依赖关系不要重复列出自有值。具体填写规则inputs[*].name必须与 Step 1 的def.cpp.Input()一致inputs[*].param_type从_def.cpp的.ParamType()提取。无显式声明时默认REQUIREDinputs[*].tensor_count仅param_type DYNAMIC时填写。param为 count 采样参数名下游作为 group_dims 维度min/max从_infershape.cpp的 OP_CHECK 或 tiling 源码提取。max提取后需 cap 到min(max, 50)Ascend C DYNAMIC TensorList 硬件限制最多 50 个子 tensorinputs[*].same_shape / same_dtype仅param_type DYNAMIC时填写。从_infershape.cpp的校验逻辑推断inputs[*].dtype列出 infershape / aclnn 层支持的所有 dtype。优先用sync_withinputs[*].rank支持的最小/最大维度数。优先用sync_withinputs[*].shape列出 shape 约束条件。优先用sync_withinputs[*].value_domain输入 tensor 的数学定义域约束。仅当算子对输入值有数学限制时填写。无约束时填null或省略。判断依据算子名模式匹配见下方常见模式清单或算子文档/源码中的数学公式value_domain 常见模式匹配即填无需额外推断算子名模式受影响输入value_domainacos / asinx{type: range, min: -1, max: 1}acoshx{type: range, min: 1, max: null}atanhx{type: range, min: -1, max: 1}log / log2 / log10x{type: positive}log1px{type: range, min: -1, max: null}sqrtx{type: non_negative}rsqrtx{type: positive}div / floor_div / true_div除数第 2 输入{type: non_zero}reciprocalx{type: non_zero}powbase当 exponent 非整数时{type: non_negative}未命中上表 → 检查算子文档/源码中的数学公式按数学定义域填写。仍无法确定 → 留 null不报错。outputs[*].name显式输出必须与 Step 1 的def.cpp.Output()一致inplace 隐式输出与被修改的输入 tensor 同名outputs[*].inplaceinplace 隐式输出填true显式Output()声明的输出省略此字段outputs[*].param_type同 inputs 规则outputs[*].tensor_count仅param_type DYNAMIC时填写。derived_from引用决定输出 tensor 数量的输入 tensor 名称source填写_infershape.cpp推导行号outputs[*].dtype优先用fixed其次sync_with最后valuesoutputs[*].shape.rulesame_as_input/derivedoutputs[*].shape.input仅rulesame_as_input时必填引用决定 shape 的输入 tensor 名称outputs[*].shape.expr仅rulederived时必填outputs[*].shape.source始终填写文件名:行号attributes[*].name必须与 Step 1 的def.cpp.Attr()一致。aclnn 层额外参数不得写入 attributes示例以下为虚构示例仅示意各字段的填写方式不代表任何具体算子。{ name: input_a, dtype: {values: [float16, bfloat16, float32]}, rank: {min: 1, max: 8}, shape: {constraints: [支持空 tensor]} }, { name: input_b, dtype: {sync_with: input_a}, rank: {sync_with: input_a}, shape: {sync_with: input_a} }, { name: x, dtype: {values: [float16, float32]}, rank: {min: 1, max: 8}, shape: {constraints: []}, value_domain: {type: positive} }, { name: divisor, dtype: {sync_with: x}, rank: {sync_with: x}, shape: {sync_with: x}, value_domain: {type: non_zero} }, { name: output_y, dtype: {sync_with: input_a}, shape: {rule: same_as_input, input: input_a, source: infershape.cpp:44} }, { name: output_stat, dtype: {fixed: float32}, shape: {rule: derived, expr: input_a.shape[:-weight_ndim] [1]*weight_ndim, source: infershape.cpp:60-67} }, { name: output_x, dtype: {sync_with: input_a}, shape: {rule: same_as_input, input: input_a, source: infershape.cpp:45} }, { name: x_list, param_type: DYNAMIC, tensor_count: {param: x_list_count, min: 1, max: 10, source: infershape.cpp:30}, same_shape: true, same_dtype: true, dtype: {values: [float16, float32]}, rank: {min: 1, max: 8}, shape: {constraints: [列表内各 tensor shape 相同]} }, { name: y_list, param_type: DYNAMIC, tensor_count: {derived_from: x_list, source: infershape.cpp:50}, dtype: {sync_with: x_list}, shape: {rule: same_as_input:x_list, source: infershape.cpp:52} }关键规则以 def.cpp 为约束源inputs/outputs/attributes 的名称和数量必须以 def.cpp 为权威以源码为准所有 dtype、shape、约束必须从源码提取不猜测逐字抄录约束OP_CHECK / PARAM_CHECK 表达式在输出 1文本中逐字抄录优先用 sync_withdtype/rank/shape 与其他输入相同时用sync_with表达依赖DYNAMIC 独立性每个 DYNAMIC 槽位的 tensor count 独立采样。DYNAMIC 输入之间可能存在 shape/dtype/rank 等关联约束通过sync_with或shape.constraints表达但 tensor count 各自独立。DYNAMIC 输出的 count 从某个输入 tensor 推导derived_from不独立采样DYNAMIC 必填字段param_type DYNAMIC的输入必须填写tensor_count含param/min/max、same_shape、same_dtypeDYNAMIC 输出必须填写tensor_count.derived_fromvalue_domain 按表匹配优先查常见模式清单未命中时从源码/文档推断仍无法确定则留 null。禁止猜测不存在的定义域约束严格禁止禁止编造 dtype 或 shape 约束 — 必须从源码提取禁止在同一个维度上同时使用values/minmax/constraints和sync_with禁止省略任何输入 tensordef.cpp 中声明的输入必须全部列出禁止将 def.cpp 中未声明的参数写入attributes节 — aclnn 层额外参数在 Step 7 返回文本中列出不写入 JSON禁止以 torch_npu 层的名称覆盖outputs[*].name以 def.cpp 为准禁止sync_with循环引用 — 若 A 的某维度 sync_with B则 B 的同一维度不得 sync_with A禁止省略 DYNAMIC 输入的tensor_count字段 —param_type DYNAMIC时必须填写 param/min/max禁止省略 DYNAMIC 输出的tensor_count.derived_from字段 — 必须引用决定 count 的输入 tensor禁止将 inplace 算子的outputs留空 — 当_def.cpp无Output()声明时必须检查注释和算子名称判断是否为 inplace 模式若是则将被修改的输入 tensor 作为隐式输出写入outputs【免费下载链接】cannbot-skillsCANNBot 是面向 CANN 开发的用于提升开发效率的系列智能体本仓库为其提供可复用的 Skills 模块。项目地址: https://gitcode.com/cann/cannbot-skills创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考