当前位置: 首页 > news >正文

CANN/cannbot-skills TritonGPU操作详解

TritonGPU 操作详解

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

本文档详细描述 TritonGPU 方言(ttg)中所有操作的定义、签名和语义。所有操作签名均从 TableGen 源码精确提取。

源码参考:TritonGPUOps.td

1. 布局转换操作

1.1 ttg.convert_layout

将张量从一种布局编码转换为另一种。这是 TritonGPU 中最核心的操作之一,实际的数据移动(如共享内存读写)在此操作中发生。

项目内容
操作名ttg.convert_layout
输入$src:TT_Tensor
输出$result:TT_Tensor
TraitsSameOperandsAndResultShape,SameOperandsAndResultElementType,Pure
%result = ttg.convert_layout %src : tensor<128xf32, #blocked> -> tensor<128xf32, #dot_op>

2. 异步拷贝操作

2.1 ttg.async_copy_global_to_local

将数据从全局内存异步拷贝到共享内存(local memory)。类似于tt.load,但数据写入共享内存描述符而非分布式张量。

项目内容
操作名ttg.async_copy_global_to_local
输入$src:TT_PtrTensor(MemRead ),$result:TTG_MemDescType(MemWrite ),$mask:I1Tensor(可选),$other:TT_Type(可选)
属性$cache:TT_CacheModifierAttr(默认NONE),$evict:TT_EvictionPolicyAttr(默认NORMAL),$isVolatile:BoolAttr(默认false)
输出$token:TTG_AsyncToken
TraitsAttrSizedOperandSegments

支持的加载字节数(按计算能力):

计算能力有效加载字节数
>= 804, 8, 16
%token = ttg.async_copy_global_to_local %src, %dst mask %mask other %other cacheModifier = #tt<cache ca> : tensor<128x!tt.ptr<f32>> -> !ttg.memdesc<128xf32, #shared>

2.2 ttg.async_wait

等待异步拷贝操作完成。

项目内容
操作名ttg.async_wait
输入$asyncToken:Variadic<TTG_AsyncToken>
属性$num:I32Attr(等待的组数)
输出$retToken:TTG_AsyncToken

计算能力要求:>= 80

%token2 = ttg.async_wait %token1 {num = 0 : i32}

2.3 ttg.async_commit_group

将当前未提交的异步操作标记为一个组。

项目内容
操作名ttg.async_commit_group
输入$inputTokens:Variadic<TTG_AsyncToken>
输出$asyncToken:TTG_AsyncToken

计算能力要求:>= 80

%token = ttg.async_commit_group tokens %token1, %token2

3. 共享内存操作

3.1 ttg.local_alloc

在共享内存中分配缓冲区,返回内存描述符。

项目内容
操作名ttg.local_alloc
输入$src:TT_Tensor(可选,初始化值)
属性$alignment:I32Attr(可选)
输出$result:TTG_MemDescType

构建器:

构建器参数
基础(Type result)
带初始化(Type result, Value src)
带对齐(Type result, Value src, int32_t alignment)

额外方法:

  • isSharedMemoryAlloc():检查是否为共享内存分配
  • getAlignmentOrDefault():获取对齐值或默认值
%buf = ttg.local_alloc %src : () -> !ttg.memdesc<128xf32, #shared> %buf2 = ttg.local_alloc : () -> !ttg.memdesc<128xf32, #shared>

3.2 ttg.local_dealloc

释放共享内存缓冲区。可选操作,未显式释放的缓冲区在所有使用后的第一个后支配点被隐式释放。

项目内容
操作名ttg.local_dealloc
输入$src:TTG_MemDescType(MemFree )
ttg.local_dealloc %buf : !ttg.memdesc<128xf32, #shared>

3.3 ttg.local_load

从共享内存描述符加载到分布式张量。

项目内容
操作名ttg.local_load
输入$src:TTG_MemDescType(MemRead ),$token:TTG_AsyncToken(可选)
输出$result:TT_Tensor
TraitsLocalLoadTrait
%result = ttg.local_load %buf token %async_token : !ttg.memdesc<128xf32, #shared> -> tensor<128xf32, #blocked>

3.4 ttg.local_store

将分布式张量存储到共享内存。

项目内容
操作名ttg.local_store
输入$src:TT_Tensor,$dst:TTG_MemDescType(MemWrite )
ttg.local_store %tensor, %buf : tensor<128xf32, #blocked> -> !ttg.memdesc<128xf32, #shared>

4. 内存描述符视图操作

4.1 ttg.memdesc_index

取内存描述符沿第 0 维第 i 个元素的子视图。不影响底层内存。

项目内容
操作名ttg.memdesc_index
输入$src:TTG_MemDescType,$index:I32
输出$result:TTG_MemDescType
TraitsPure,MemDescViewTrait

例如:输入2x4x16xf16,index=1 → 输出4x16xf16

%sub = ttg.memdesc_index %src[%index] : !ttg.memdesc<2x4x16xf16, #shared> -> !ttg.memdesc<4x16xf16, #shared>

4.2 ttg.memdesc_subslice

取内存描述符的子视图,指定各维偏移。不影响底层内存。

项目内容
操作名ttg.memdesc_subslice
输入$src:TTG_MemDescType
属性$offsets:DenseI32ArrayAttr
输出$result:TTG_MemDescType
TraitsPure,MemDescViewTrait

例如:输入32x16xf16,offsets=[2,1] → 输出8x16xf16,覆盖input[2:10, 1:17]

%sub = ttg.memdesc_subslice %src[0, 0] : !ttg.memdesc<32x16xf16, #shared> -> !ttg.memdesc<8x16xf16, #shared>

4.3 ttg.memdesc_trans

转置内存描述符的视图。不影响底层内存。

项目内容
操作名ttg.memdesc_trans
输入$src:TTG_MemDescType
属性$order:DenseI32ArrayAttr
输出$result:TTG_MemDescType
TraitsPure,MemDescViewTrait,TransposeOpInterface,InferTypeOpWithLayoutEquivalence,SameOperandsAndResultElementType
%transposed = ttg.memdesc_trans %src {order = [1, 0]} : !ttg.memdesc<8x16xf16, #shared> -> !ttg.memdesc<16x8xf16, #shared>

4.4 ttg.memdesc_reshape

创建不同形状的内存描述符视图。不影响底层内存。

项目内容
操作名ttg.memdesc_reshape
输入$src:TTG_MemDescType
输出$result:TTG_MemDescType
TraitsPure,MemDescViewTrait,SameOperandsAndResultElementType
%reshaped = ttg.memdesc_reshape %src : !ttg.memdesc<128xf16, #shared> -> !ttg.memdesc<8x16xf16, #shared>

4.5 ttg.memdesc_reinterpret

将内存描述符重新解释为不同类型和形状。要求原始描述符是连续的。

项目内容
操作名ttg.memdesc_reinterpret
输入$src:TTG_MemDescType
输出$result:TTG_MemDescType
TraitsPure,MemDescViewTrait
%reinterpreted = ttg.memdesc_reinterpret %src : !ttg.memdesc<128xf16, #shared> -> !ttg.memdesc<64xf32, #shared>

5. 流水线操作

5.1 ttg.predicate_stage

流水线阶段谓词,用于软件流水线。

项目内容
操作名ttg.predicate_stage
输入$iv:AnySignlessIntegerOrIndex,$ub:AnySignlessIntegerOrIndex,$step:AnySignlessIntegerOrIndex
属性$maxStage:I32Attr,$stage:I32Attr
输出$result:I1
TraitsPure,AllTypesMatch<["iv", "ub", "step"]>
%pred = ttg.predicate_stage %iv, %ub, %step maxStage 2 stage 0 : index -> i1

5.2 ttg.mask

流水线掩码操作,包含一个区域。

项目内容
操作名ttg.mask
输入$pred:I1
输出$result:Variadic<AnyType>
区域$region:SizedRegion<1>
TraitsSingleBlock

5.3 ttg.mask.return

ttg.mask区域的终止操作。

项目内容
操作名ttg.mask.return
输入$result:Variadic<AnyType>
TraitsHasParent<"MaskOp">,Pure,Terminator,ReturnLike

6. 类型转换操作

6.1 ttg.fp4_to_fp

将打包为 i8 的 FP4 (E2M1) 数据上转换为浮点类型。每个 i8 的低 4 位表示第一个 FP4 元素,高 4 位表示第二个。

项目内容
操作名ttg.fp4_to_fp
输入$src:RankedTensorOf<[I8]>
属性$axis:I32Attr(FP4 元素打包的轴)
输出$result:TT_FloatTensor
TraitsPure
验证器hasVerifier = 1
%result = ttg.fp4_to_fp %src {axis = 1 : i32} : tensor<16x8xi8> -> tensor<16x16xf32>

7. 全局内存操作

7.1 ttg.global_scratch_alloc

在全局内存中分配当前程序私有的缓冲区。

项目内容
操作名ttg.global_scratch_alloc
属性$nbytes:I32Attr,$alignment:I32Attr
输出$result:TT_Ptr(MemAlloc )
%ptr = ttg.global_scratch_alloc {nbytes = 1024 : i32, alignment = 16 : i32} : !tt.ptr<i8>

8. Warp 特化操作

8.1 ttg.warp_specialize

在不同 warp 组上异步执行不同代码。默认区域可隐式捕获,分区区域与上方隔离。

项目内容
操作名ttg.warp_specialize
输入$explicitCaptures:Variadic<AnyType>
属性$partitionNumWarps:DenseI32ArrayAttr,$warpGroupStartIds:DenseI32ArrayAttr(可选),$requestedRegisters:DenseI32ArrayAttr(可选),$actualRegisters:DenseI32ArrayAttr(可选)
输出$defaultPassthrough:Variadic<AnyType>
区域$defaultRegion:MinSizedRegion<1>,$partitionOpHolder:SizedRegion<1>
TraitsRecursiveMemoryEffects,RecursivelySpeculatable,AsyncRegions,RegionBranchOpInterface

额外方法:

  • getPartitionRegions():获取分区区域
  • getCaptureSizeAlign():获取捕获列表的大小和对齐
  • getTotalPartitionWarps():获取额外 warp 总数
%0 = ttg.warp_specialize(%a, %b) default { %out = some_operation(%a) ttg.warp_yield %out : i32 } partition0(%arg0: i32, %arg1: i32) num_warps(8) { some_async_dispatch(%arg0, %arg1) ttg.warp_return } : (i32, i32) -> i32

8.2 ttg.warp_specialize.partitions

包含ttg.warp_specialize的隔离分区区域的容器操作。

项目内容
操作名ttg.warp_specialize.partitions
区域$partitionRegions:VariadicRegion<MinSizedRegion<1>>
TraitsIsolatedFromAbove,RecursiveMemoryEffects,RecursivelySpeculatable,Terminator,HasParent<"WarpSpecializeOp">

8.3 ttg.warp_yield

ttg.warp_specialize默认区域的终止操作。操作数作为ttg.warp_specialize的 SSA 结果传递。

项目内容
操作名ttg.warp_yield
输入$values:Variadic<AnyType>
TraitsPure,Terminator,ReturnLike,HasParent<"WarpSpecializeOp">,RegionBranchTerminatorOpInterface
ttg.warp_yield %a, %b : i32, tensor<32xbf16, #blocked>

8.4 ttg.warp_return

ttg.warp_specialize分区区域的隐式终止操作。无操作数,因为分区区域不能返回任何值。

项目内容
操作名ttg.warp_return
TraitsPure,Terminator,ReturnLike,HasParent<"WarpSpecializePartitionsOp">

9. 操作分类速查表

类别操作关键字
布局转换ttg.convert_layout数据移动
异步拷贝ttg.async_copy_global_to_local全局→共享
异步同步ttg.async_wait,ttg.async_commit_group异步令牌
共享内存分配ttg.local_alloc,ttg.local_dealloc缓冲区管理
共享内存读写ttg.local_load,ttg.local_store共享↔寄存器
内存描述符视图ttg.memdesc_index,ttg.memdesc_subslice,ttg.memdesc_trans,ttg.memdesc_reshape,ttg.memdesc_reinterpret子视图操作
流水线ttg.predicate_stage,ttg.mask,ttg.mask.return软件流水线
类型转换ttg.fp4_to_fpFP4→FP
全局内存ttg.global_scratch_allocScratch 分配
Warp 特化ttg.warp_specialize,ttg.warp_specialize.partitions,ttg.warp_yield,ttg.warp_return多 warp 组

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

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

http://www.jsqmd.com/news/1167435/

相关文章:

  • Linux系统一键配置conda、cuda、pytorch
  • 私域运营工具有哪些?适合门店商家的小程序功能对比
  • 终极指南:让老款Mac免费升级到最新macOS的完整解决方案
  • OpCore Simplify:让OpenCore配置变得前所未有的简单
  • LingBot-Video:MoE架构下的物理合理视频生成技术解析
  • CANN Runtime内存语义同步指南
  • TMC7300+PIC18LF24K50驱动有刷直流电机方案解析
  • AutoRemesher在虚拟城市中的应用:如何优化城市模型的网格
  • 2026年7月最新宁波欧米茄官方售后客服中心地址电话及服务网点分布 - 欧米茄服务中心
  • Cosmos-Predict2.5输入输出规范:如何准备完美的文本、图像和视频输入
  • 国内免费AI助手实战:GPT-4平替方案与集成指南
  • PlantCV案例研究:在作物育种中的实际应用与效果评估
  • 2026年7月热风循环隧道炉工厂推荐,倍速链线/流水线设备/二手流水线/高温隧道炉烘干线,热风循环隧道炉厂商哪个好 - 品牌推荐师
  • Terraforming Rails测试优化完全手册:彻底解决Rails测试的随机失败问题
  • PNG/JPG/GIF 图片宽高修复:3种格式CRC爆破脚本与010 Editor实战
  • python的UV使用
  • Linux软件安装:yum与apt包管理器
  • 北京欧米茄回收价格查询和各大回收平台实测排行(2026年7月最新) - 欧米茄官方服务中心
  • Cosmos-Predict2.5部署指南:在H100、A100、B200 GPU上运行模型的完整教程
  • FUXA工业可视化平台:构建现代化SCADA系统的5个核心优势与3阶段部署策略
  • ChatGPT提示词性能压测报告(基于10万+真实交互日志):TOP 3高转化Prompt结构首次公开
  • 打印机租赁 vs 复印机租赁,隐藏套路对比
  • 3步解决macOS上BepInEx编译部署的12个关键问题
  • rsuite-table响应式设计指南:适配移动端与桌面端表格展示
  • LLM应用开发全栈实战:从Prompt工程到生产部署
  • 绍兴假眼定制趋势:2026年值得关注的义眼安装机构推荐 - 企业品牌
  • 如何使用Nexth快速搭建Web3应用:5分钟上手教程
  • C/C++类型转换(C++四大强制类型转换)
  • gemma-4-26B-A4B-it-OptiQ-4bit模型架构详解:从MoE到混合注意力机制
  • 2026泉州装修公司综合实力精选:五家报价透明、交付稳定的靠谱企业推荐 - 装企精灵GEO