尧图网站建设 尧图网络
  • 首页
  • 关于我们
  • 服务项目
  • 案例展示
  • 建站流程
  • 资讯中心
  • 联系我们
首页/资讯中心/详情

CANN/cannbot-skills TritonGPU操作详解

CANN/cannbot-skills TritonGPU操作详解
📅 发布时间:2026/7/11 12:53:29

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),仅供参考

相关新闻

  • Cosmos-Predict2.5输入输出规范:如何准备完美的文本、图像和视频输入
  • TMC7300+PIC18LF24K50驱动有刷直流电机方案解析
  • Linux软件安装:yum与apt包管理器

最新新闻

  • 如何在Windows系统中实现苹果级中文字体渲染:PingFangSC技术方案解析
  • 3个场景解析:为什么Unblob能彻底改变你的二进制文件处理方式?
  • GPT-5.6 三档分层:Sol / Terra / Luna 定价逻辑拆解指南
  • 2026严选六盘水名表名包奢侈品回收浪琴万国萧邦香奈儿古驰圣罗兰精选正规机构排名当场验货不压价 - 谊识预商务
  • 上海交大 × 百度百舸,dVLA-RL 首次打通离散扩散 VLA 强化学习
  • 网盘直链下载助手深度解析:技术原理与高效下载实战指南

日新闻

  • OpenClaw本地部署:一键直连微信的私有化AI Agent实战指南
  • Kubernetes 系列【10】控制器:ReplicaSet(副本集)
  • 怎么寄快递才能便宜呢?2026年7月寄快递省钱攻略 - 生活情报姬

周新闻

  • 基于YOLOv12的番茄成熟度智能检测系统开发
  • 终极RimWorld模组管理指南:用RimSort告别模组冲突烦恼
  • AI Agent框架开发:从理论到实践的完整指南

月新闻

  • 2026年6月公司网站搭建最新热门渠道测评:四大低成本/零代码平台对比+避坑
  • 【Linux】Linux arm 编译QT程序,出现expected “}“报错
  • 【MATLAB例程】四基站二维AOA定位与距离辅助增强对比仿真。基于角度观测和测距修正的固定目标平面定位精度分析

关于尧图

  • 公司简介
  • 团队介绍
  • 企业文化
  • 荣誉资质

服务项目

  • 定制开发
  • 电商建站
  • UI 设计
  • 运维服务

快速链接

  • 案例展示
  • 建站流程
  • 常见问题
  • 资讯中心

联系方式

  • 📍北京市朝阳区互联网产业园 A 座 10 层
  • 📞400-888-8888
  • ✉️contact@rkmt.cn
  • 🕐周一至周日 9:00-21:00

© 2024 北京尧图网络科技有限公司 版权所有 | 京 ICP 备 XXXXXXXX 号