hir.mmadL1 — 本地矩阵乘加L1→L0C【免费下载链接】cannbot-skillsCANNBot 是面向 CANN 开发的用于提升开发效率的系列智能体本仓库为其提供可复用的 Skills 模块。项目地址: https://gitcode.com/cann/cannbot-skills关键词mmadL1, Matrix Multiply and Add, L1, L0C, Cube Core, UnitFlag, Fractal Layout概述hir.mmadL1是 HIVM 方言中的本地矩阵乘加操作在 Cube Core 上执行。该操作从 L1 存储层次读取矩阵 A 和 B在 L0C 中执行乘加运算结果写回 L0C。计算语义为C C A x B (optional) channel_bias。该操作是 HIVM 宏操作体系中最基础的矩阵计算单元对应硬件上的 mma_tile 指令。它涉及 MTE1L1 数据搬运和 MCube 矩阵计算两个 Pipeline需要跨 Pipe 同步。mmadL1 支持转置加载a_transpose/b_transpose、HF32 加速模式、per-channel bias、以及 UnitFlag 同步条件等高级特性。Python API 对应Triton 的tl.dot操作在 Split-K 场景下可能被映射为 mmadL1。IR 操作定义从 HIVMMacroOps.td 提取def MmadL1Op : HIVM_LocalMmadOpmmadL1, [ NoMaxRankTrait, DeclareOpInterfaceMethodsOpLayoutInterface, [getOperandsTargetFractalLayout], DeclareOpInterfaceMethodsHIVMStructuredOpInterface, [getIndexingMaps] ]基类HIVM_LocalMmadOp定义HIVMMacroOps.tdclass HIVM_LocalMmadOpstring mnemonic, listTrait traits [] : HIVM_MacroOpmnemonic, !listconcat( [AttrSizedOperandSegments, CubeCoreTypeTrait, HIVMUnitFlagEnabledInterface, MacroOpPipeTraitPIPE::PIPE_MTE1, PIPE::PIPE_M, ], traits)参数说明输入操作数ins参数类型必选说明$aTensorOrMemref是矩阵 A形状为[M, K]rank 必须为 2$bTensorOrMemref是矩阵 B形状为[K, N]rank 必须为 2$init_conditionI1是L0C 数据清零条件为 true 时清零 L0C 后再使用$real_mIndex是M 维度的实际数据大小$real_kIndex是K 维度的实际数据大小$real_nIndex是N 维度的实际数据大小$cTensorOrMemref是矩阵 C输出/累加形状为[M, N]$per_channel_biasTensorOrMemref否Per-channel bias形状为[N]$sync_related_argsVariadicI64否同步相关参数由 InjectSync Pass 自动管理$unit_flag_condVariadicI1否UnitFlag 启用条件用于循环依赖场景输出操作数outs参数类型说明$result_tensorsVariadicAnyRankedTensor结果 Tensor属性属性类型必选说明$a_transposeUnitAttr否矩阵 A 在加载前转置$b_transposeUnitAttr否矩阵 B 在加载前转置$enable_HF32UnitAttr否启用 HF32 模式FP32 数据在 CUBE 计算前舍入为 HF32性能翻倍但精度降低$unit_flag_modeUnitFlagArrayAttr否每个输出 Tensor 的 UnitFlag 模式额外类方法方法返回类型说明getOpName()StringRef返回mma_tileisInitConstant(optbool)bool查询/设置 init_condition 是否为常量setInitCondition(Value)void设置 init 条件值getMatmulBiasMode()MatmulBiasMode获取 bias 模式shouldDecomposeBiasByElementAdd()bool判断是否应将 bias 分解为逐元素加法getNumSyncRelatedArgs()int获取同步参数数量getInputOperands(bool)SmallVectorValue获取输入操作数getOperandALayout()FailureOrDataLayoutAttr获取 A 的 Fractal LayoutgetOperandBLayout()FailureOrDataLayoutAttr获取 B 的 Fractal LayoutgetOperandCLayout()FailureOrDataLayoutAttr获取 C 的 Fractal LayoutgetOperandBiasLayout()FailureOrDataLayoutAttr获取 Bias 的 Fractal LayoutIR 示例基本用法%ma memref.alloc() : memref256x128xf16 %mb memref.alloc() : memref128x256xf16 %mc memref.alloc() : memref256x256xf32 %c256 arith.constant 256 : index %c128 arith.constant 128 : index %init arith.constant 1 : i1 hivm.hir.mmadL1 ins(%ma, %mb, %init, %c256, %c128, %c256 : memref256x128xf16, memref128x256xf16, i1, index, index, index) outs(%mc : memref256x256xf32)带转置%ma_t memref.alloc() : memref128x256xf16 hivm.hir.mmadL1 {a_transpose} ins(%ma_t, %mb, %init, %c256, %c128, %c256 : memref128x256xf16, memref128x256xf16, i1, index, index, index) outs(%mc : memref256x256xf32)Split-K 循环中的条件初始化%mc memref.alloc() : memref256x256xf32 %start arith.constant 0 : index %end arith.constant 1024 : index %step arith.constant 128 : index scf.for %arg0 %start to %end step %step { %ma memref.alloc() : memref256x128xf16 %mb memref.alloc() : memref128x256xf16 %init_condition arith.cmpi eq, %arg0, %start : index hivm.hir.mmadL1 ins(%ma, %mb, %init_condition, %c256, %c128, %c256 : memref256x128xf16, memref128x256xf16, i1, index, index, index) outs(%mc : memref256x256xf32) }Tensor 语义%mc tensor.empty() : tensor256x256xf32 %res hivm.hir.mmadL1 ins(%ma, %mb, %init_condition, %c256, %c128, %c256 : tensor256x128xf16, tensor128x256xf16, i1, index, index, index) outs(%mC_iter : tensor256x256xf32) - tensor256x256xf32IR 层约束与验证Rank 约束矩阵 A、B、C 的 rank 必须为 2batchMmadL1 为 3。Core Type操作必须在 Cube Core 上执行CubeCoreTypeTrait。Pipeline操作涉及 MTE1 和 M 两个 PipelineMacroOpPipeTraitPIPE::PIPE_MTE1, PIPE::PIPE_M。init_condition在 Split-K 循环中首次迭代应设置 init_condition 为 true 以清零 L0C后续迭代为 false 以累加。a_transpose / b_transpose当设置转置时输入矩阵的逻辑形状不变但数据在加载时按转置方式读取。enable_HF32仅对 FP32 数据有效将 FP32 舍入为 HF32 后计算精度降低但性能提升。per_channel_bias如果提供形状必须为[N]与矩阵 B 的列维度匹配。Fractal LayoutmmadL1 实现了OpLayoutInterface的getOperandsTargetFractalLayout方法用于确定操作数的 Fractal 布局。常见问题Q: mmadL1 和 matmul 的区别是什么A: mmadL1 是本地操作数据从 L1 读取到 L0C 计算matmul 是全局操作数据直接从 GM 读取。mmadL1 需要用户手动管理 L1 数据搬运和同步matmul 由编译器自动处理。Q: init_condition 什么时候设为 trueA: 当需要清零 L0C 累加器时设为 true通常在 Split-K 循环的第一次迭代。后续迭代设为 false 以累加部分和。Q: HF32 模式适用于什么场景A: HF32 将 FP32 舍入为 19-bit10-bit 尾数适用于对精度不敏感但需要 FP32 吞吐量的场景。相关文档源码参考HIVMMacroOps.td测试用例ops.mlirUnitFlag 详解04-Synchronization/03-unit-flag.mdFractal Layout06-Attributes-Types/02-parameterized-attrs.md【免费下载链接】cannbot-skillsCANNBot 是面向 CANN 开发的用于提升开发效率的系列智能体本仓库为其提供可复用的 Skills 模块。项目地址: https://gitcode.com/cann/cannbot-skills创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考