【免费下载链接】cannbot-skillsCANNBot 是面向 CANN 开发的用于提升开发效率的系列智能体本仓库为其提供可复用的 Skills 模块。项目地址: https://gitcode.com/cann/cannbot-skillstitle: MemBase 与 RegBase 计算 API 对照 purpose: 对比 LocalTensor 中心的 MemBase 写法与 RegTensor 中心的 RegBase 写法帮助选择正确 API 层级。 read_when:设计或代码需要把 MemBase 计算链迁移到 RegBase。不确定某个 compute API 应该写成LocalTensorvector API 还是AscendC::Reg::*API。审查发现LocalTensorcompute 和RegTensorcompute 混用。 not_for:只判断 GM/UB 搬运对齐。纯 Host tiling 结构设计。 keywords:membaseregbaseLocalTensorRegTensorAscendC::Reg next_reads:regbase_api_reference.mdarithmetic_and_reduce.mddatacopy_best_practices.md../pitfalls/regbase_vs_membase_confusions.md depth: foundation topic_type: apiMemBase 与 RegBase 计算 API 对照MemBase 和 RegBase 可以共享 Host、tiling、queue、UB staging 的部分结构但 compute 核心不是同一套 API surface。迁移或审查时先判断当前代码在哪一层计算。维度MemBase / LocalTensor 写法RegBase / RegTensor 写法计算对象LocalTensorTRegTensorT入口形态kernel 普通成员函数或Process/Compute中调用 vector APICompute通过asc_vf_call进入__simd_vf__输入来源LocalTensor已经代表 UB buffer__ubuf__ T*先经Reg::Load*进入RegTensor计算 APIAscendC::Add/Mul/Exp/...等 kernel-side vector APIAscendC::Reg::Add/Mul/Exp/...或AscendC::MicroAPI::*tail 控制API 的 count / mask / repeat 参数依具体接口而定MaskReg控制 active lanes中间值常物化为 UB 上的LocalTensor临时区尽量保留在短生命周期RegTensor中输出写到LocalTensor后再 copy outReg::Store*写回 UB address 后再 copy out逐元素计算对照MemBase 写法通常围绕LocalTensor// 示意LocalTensor 上直接调用 vector API。 AscendC::LocalTensorfloat x inQueue.DeQuefloat(); AscendC::LocalTensorfloat y outQueue.AllocTensorfloat(); AscendC::Muls(y, x, scale, count);RegBase 写法先进入 VF body再在RegTensor上计算__simd_vf__ inline void ScaleVf(__ubuf__ float* in, __ubuf__ float* out, uint32_t count) { AscendC::Reg::RegTensorfloat xReg; AscendC::Reg::RegTensorfloat yReg; AscendC::Reg::MaskReg mask AscendC::Reg::UpdateMaskfloat(count); AscendC::Reg::LoadAlign(xReg, in); AscendC::Reg::Muls(yReg, xReg, scale, mask); AscendC::Reg::StoreAlign(out, yReg, mask); }关键区别MemBase 的 API 参数通常是LocalTensor和元素数RegBase 的 API 参数是RegTensor和MaskReg。常见 API 映射这些名称可能相同但对象和签名不同。不能只按函数名迁移。需求MemBase 倾向RegBase 倾向审查点unaryAbs(dstLocal, srcLocal, count)、Exp(...)Reg::Abs(dstReg, srcReg, mask)、Reg::Exp(...)RegBase 输入输出必须是RegTensorbinaryAdd(dstLocal, aLocal, bLocal, count)Reg::Add(dstReg, aReg, bReg, mask)两个输入寄存器 lane layout 一致scalarAdds/Mulskernel-side vector APIReg::Adds(dstReg, srcReg, scalar, mask)/Reg::Muls(...)不要为了 scalar 物化 broadcast UB buffercompareCompare产生 LocalTensor / mask-like 结果依接口而定Reg::Compare(maskOut, aReg, bReg, mask)compare 结果是MaskRegselectSelect消费 LocalTensor 或 mask tensorReg::Select(dstReg, trueReg, falseReg, mask)true/false source 要同 dtype routecastkernel-sideCast处理LocalTensorReg::Cast(dstReg, srcReg, mask)cast trait、round、saturation 必须验证reduceReduceSum/WholeReduceSum等 LocalTensor APIReg::Reduce/ReduceDataBlock/PairReduceElem不要把 LocalTensor reduce 签名套到 VF-side reduce迁移规则从 MemBase 迁到 RegBase 时不是给原 API 加 namespace而是重写 compute 层保留可复用的 Host tiling、GM/UB staging 和 queue 结构。把原LocalTensorcompute 链拆成__simd_vf__函数。在 VF 入口把__ubuf__地址 load 到RegTensor。使用AscendC::Reg::*/AscendC::MicroAPI::*完成计算链。用MaskReg表示 tail不把 padding 当作有效 lane。store 回 UB再由外层CopyOut写回 GM。不要这样写不要在__simd_vf__里直接调用只接受LocalTensor的 vector API。不要把AscendC::Reg::Add当成AscendC::Add的命名空间替换它们的对象、mask 和签名不同。不要让RegTensor跨 tile 或跨 VF 作用域保存状态。不要因为 outer shell 使用TQue/LocalTensor就判定它不是 RegBase关键看热计算链是否进入RegTensor。不要把设计伪代码中的Reg::API 当成无需验证的可编译签名。相关文档[[regbase_api_reference]][[arithmetic_and_reduce]][[datacopy_best_practices]][[../pitfalls/regbase_vs_membase_confusions]][[../dev-experience/regbase_programming_notes]]【免费下载链接】cannbot-skillsCANNBot 是面向 CANN 开发的用于提升开发效率的系列智能体本仓库为其提供可复用的 Skills 模块。项目地址: https://gitcode.com/cann/cannbot-skills创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考