news 2026/7/11 17:04:18

CANN/cannbot-skills自定义操作文档

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
CANN/cannbot-skills自定义操作文档

hir.custom — 自定义单 Pipe 操作

【免费下载链接】cannbot-skillsCANNBot 是面向 CANN 开发的用于提升开发效率的系列智能体,本仓库为其提供可复用的 Skills 模块。项目地址: https://gitcode.com/cann/cannbot-skills

关键词:custom, CustomOp, Builtin, __builtin_gather_load, __builtin_index_select, SinglePipe

概述

hir.custom是 HIVM 方言中的自定义单 Pipe 操作,允许用户在现有操作无法满足需求时编写自定义实现。该操作在单个 Pipeline 上执行,通过name属性标识操作类型。

Custom 操作适用于以下场景:

  1. 现有操作无法实现所需功能
  2. 现有操作可以实现但性能不佳
  3. 需要私有操作

内置操作(name 以__builtin开头)由编译器自带实现,用户无需指定属性即可使用。

Python API 对应:Triton 的自定义操作可以通过tl.extern等机制映射为 hir.custom。

IR 操作定义

从 HIVMOps.td 提取:

def CustomOp : HIVM_CustomOp<"custom", [SinglePipeOpTrait]> { let arguments = (ins StrAttr:$name, Variadic<AnyType>:$inputs, Variadic<AnyType>:$outputs); let results = (outs Variadic<AnyType>:$results); }

基类HIVM_CustomOp定义(HIVMOps.td):

class HIVM_CustomOp<string mnemonic, list<Trait> traits = []> : HIVM_StructuredOp<mnemonic, !listconcat([AttrSizedOperandSegments, MemoryEffects<[MemRead, MemWrite]>, HIVMInferCoreTypeInterface], traits)> { dag args = (ins StrAttr:$name, Variadic<AnyType>:$inputs, Variadic<AnyType>:$outputs); dag res = (outs Variadic<AnyType>:$results); }

参数说明

输入操作数(ins)

参数类型必选说明
$nameStrAttr操作名称,__builtin_前缀为内置操作
$inputsVariadic<AnyType>输入参数(可变数量)
$outputsVariadic<AnyType>输出/初始化参数(DestinationStyle)

输出结果(outs)

参数类型说明
$resultsVariadic<AnyType>结果值

必需属性

属性类型说明
hivm.tcore_typeTCoreTypeAttr执行的 Core 类型
hivm.pipePipeAttr执行的 Pipeline
hivm.vf_modeVFModeAttrVector 运行模式(Cube Core 时忽略)

可选属性

属性类型说明
gm_addr_args_indicesDenseI32ArrayAttr指示哪些参数包含 GM 纯地址

额外类方法

方法返回类型说明
getCoreType()optional<TCoreType>获取 Core 类型
setCoreType(TCoreType)void设置 Core 类型
getPipe()PIPE获取 Pipe
setPipe(PIPE)void设置 Pipe
getVFMode()optional<VFMode>获取 VF 模式
setVFMode(VFMode)void设置 VF 模式
getGMAddrArgsIndices()optional<SmallVector<size_t>>获取 GM 地址参数索引
setGMAddrArgsIndices(SmallVector<size_t>)void设置 GM 地址参数索引
isBuiltin()bool判断是否为内置操作
getDpsInitsMutable()MutableOperandRangeDestinationStyleOpInterface

内置操作常量

static constexpr StringLiteral kBuiltinGatherLoadName = "__builtin_gather_load"; static constexpr StringLiteral kBuiltinIndexSelectName = "__builtin_index_select";

IR 示例

自定义操作

%empty = tensor.empty() : tensor<3x3xf32> %0 = hivm.hir.custom { hivm.tcore_type = #hivm.tcore_type<VECTOR>, hivm.pipe = #hivm.pipe<PIPE_V>, hivm.vf_mode = #hivm.vf_mode<SIMD> } "my_custom_op" ins(%arg0, %arg1, %c4_i64, %c0_i32, %c2_i64, %c1_i64, %c2_i32, %c2_i32, %c0_i32, %c0_i32 : memref<?xf32>, tensor<3x3xi64>, i64, i32, i64, i64, i32, i32, i32, i32) outs(%empty : tensor<3x3xf32>) -> tensor<3x3xf32>

内置操作(无需指定属性)

%empty = tensor.empty() : tensor<3x3xf32> %0 = hivm.hir.custom "__builtin_gather_load" ins(%arg0, %arg1, %c4_i64, %c0_i32, %c2_i64, %c1_i64, %c2_i32, %c2_i32, %c0_i32, %c0_i32 : memref<?xf32>, tensor<3x3xi64>, i64, i32, i64, i64, i32, i32, i32, i32) outs(%empty : tensor<3x3xf32>) -> tensor<3x3xf32>

__builtin_index_select

%0 = tensor.empty() : tensor<1x4x32xf32> %1 = hivm.hir.custom {extra_attr = "srcStrideLength=3", hivm.vf_mode = #hivm.vf_mode<SIMT>} "__builtin_index_select" ins(%arg0, %arg1, %c0_i32, %c9000_i64, %c0_i32, %c0_i32, %c0_i32, %c0_i32, %c0_i32, %c0_i32, %c32_i32, %c4000_i32, %c32_i32 : memref<?xf32>, tensor<16x400xi32>, i32, i64, i32, i32, i32, i32, i32, i32, i32, i32, i32) outs(%0 : tensor<1x4x32xf32>) -> tensor<1x4x32xf32>

IR 层约束与验证

  1. SinglePipeOpTrait:操作在单个 Pipeline 上执行。
  2. AttrSizedOperandSegments:inputs 和 outputs 的数量由属性决定。
  3. DestinationStyleOpInterface:outputs 作为 DPS init 操作数。
  4. Core Type:必须通过hivm.tcore_type属性指定(内置操作可省略,编译器自动推断)。
  5. Pipe:必须通过hivm.pipe属性指定(内置操作可省略)。
  6. VFMode:必须通过hivm.vf_mode属性指定(内置操作可省略,Cube Core 时忽略)。
  7. Builtin 验证:内置操作编译器会检查参数正确性并规范化属性。

常见问题

Q: 自定义操作如何提供实现?A: 当前自定义操作的用户实现机制尚未完全开放(标记为 TODO)。内置操作由编译器自动链接到模板库。

Q: __builtin_gather_load 和 hir.gather_load 的区别?A:__builtin_gather_load是通过 custom 操作机制实现的 gather load,而hir.gather_load是独立的 HIVM 操作。两者功能类似,但 custom 版本更灵活,可以通过属性配置。

Q: gm_addr_args_indices 的用途?A: 指示哪些输入参数包含 GM 的纯地址值。Lowering Pass 在处理这些参数时会保留地址值,而不是尝试从 memref 中提取数据。

相关文档

  • 源码参考:HIVMOps.td
  • 测试用例:custom-op.mlir
  • CustomMacroOp:02-custom-macro-op.md

【免费下载链接】cannbot-skillsCANNBot 是面向 CANN 开发的用于提升开发效率的系列智能体,本仓库为其提供可复用的 Skills 模块。项目地址: https://gitcode.com/cann/cannbot-skills

创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/7/11 17:02:37

PDF补丁丁:终极PDF工具箱完整指南,轻松解决PDF处理难题

PDF补丁丁&#xff1a;终极PDF工具箱完整指南&#xff0c;轻松解决PDF处理难题 【免费下载链接】PDFPatcher PDF补丁丁——PDF工具箱&#xff0c;可以编辑书签、剪裁旋转页面、解除限制、提取或合并文档&#xff0c;探查文档结构&#xff0c;提取图片、转成图片等等 项目地址…

作者头像 李华
网站建设 2026/7/11 16:57:00

AutoRemesher在时尚设计中的应用:如何优化服装模型的拓扑

AutoRemesher在时尚设计中的应用&#xff1a;如何优化服装模型的拓扑 【免费下载链接】autoremesher Automatic quad remeshing tool 项目地址: https://gitcode.com/GitHub_Trending/au/autoremesher AutoRemesher是一款强大的自动四边形网格重构工具&#xff0c;能够帮…

作者头像 李华
网站建设 2026/7/11 16:56:20

TLA2518与PIC18F87J10构建高精度数据采集系统

1. TLA2518与PIC18F87J10的硬件协同设计在工业控制和精密测量领域&#xff0c;模拟信号到数字信号的可靠转换是系统设计的核心挑战。德州仪器的TLA2518作为一款8通道12位1MSPS SAR ADC&#xff0c;与Microchip的PIC18F87J10单片机组合&#xff0c;能够构建高性价比的数据采集系…

作者头像 李华
网站建设 2026/7/11 16:54:06

如何5分钟永久解锁IDM下载管理器?开源脚本完整指南

如何5分钟永久解锁IDM下载管理器&#xff1f;开源脚本完整指南 【免费下载链接】IDM-Activation-Script IDM Activation & Trail Reset Script 项目地址: https://gitcode.com/gh_mirrors/id/IDM-Activation-Script 还在为Internet Download Manager&#xff08;IDM…

作者头像 李华