尧图网络 高端网站定制 · 原创设计
免费咨询热线
400-888-6620
免费获取方案
昇腾C数学计算API使用样例
更多样例【免费下载链接】asc-devkit本项目是CANN 推出的昇腾AI处理器专用的算子程序开发语言原生支持C和C标准规范主要由类库和语言扩展层构成提供多层级API满足多维场景算子开发诉求。项目地址: https://gitcode.com/cann/asc-devkit样例一下面的样例展示了数学库kernel侧API和Tiling API GetXxxMaxMinTmpSize的配套使用方法具体流程如下Host侧调用Tiling接口获取所需临时空间的大小并将其写入tiling data中kernel侧再读取tiling data获取相应的临时空间大小并根据此分配临时空间。Host侧Tiling API使用样例:#include vector #include register/op_def_registry.h #include register/tilingdata_base.h #include tiling/tiling_api.h namespace optiling { BEGIN_TILING_DATA_DEF(AsinCustomTilingData) TILING_DATA_FIELD_DEF(uint32_t, srcSize); TILING_DATA_FIELD_DEF(uint32_t, tmpBufferSize); END_TILING_DATA_DEF; static ge::graphStatus TilingFunc(gert::TilingContext* context) { // Input source shapes. auto shape_input context-GetInputTensor(0)-GetOriginShape(); std::vectorint64_t srcDims {shape_input.GetDim(0), shape_input.GetDim(1)}; uint32_t srcSize 1; for (auto dim : srcDims) { srcSize * dim; } uint32_t typeSize 2; ge::Shape shape(srcDims); uint32_t minValue 0; uint32_t maxValue 0; AscendC::GetAsinMaxMinTmpSize(shape, typeSize, false, maxValue, minValue); auto platformInfo context-GetPlatformInfo(); auto ascendcPlatform platform_ascendc::PlatformAscendC(platformInfo); uint64_t tailSize 0; // ub剩余空间大小 ascendcPlatform.GetCoreMemSize( platform_ascendc::CoreMemType::UB, tailSize); // 本样例中使用完整的ub空间实际情况下tailSize需要减掉用户已使用的ub空间 auto tmpSize tailSize maxValue ? maxValue : tailSize; AsinCustomTilingData tiling; tiling.set_srcSize(srcSize); tiling.set_tmpBufferSize(tmpSize); context-SetBlockDim(1); tiling.SaveToBuffer(context-GetRawTilingData()-GetData(), context-GetRawTilingData()-GetCapacity()); context-GetRawTilingData()-SetDataSize(tiling.GetDataSize()); context-SetTilingKey(1); return ge::GRAPH_SUCCESS; } } // namespace optilingkernel侧读取tiling data获取相应的临时空间大小并根据此分配临时空间#include kernel_operator.h template typename srcType class KernelAsin { public: __aicore__ inline KernelAsin() {} __aicore__ inline void Init(GM_ADDR srcGm, GM_ADDR dstGm, uint32_t srcSize, uint32_t tmpBufferSize) { srcGlobal.SetGlobalBuffer(reinterpret_cast__gm__ srcType*(srcGm), srcSize); dstGlobal.SetGlobalBuffer(reinterpret_cast__gm__ srcType*(dstGm), srcSize); pipe.InitBuffer(inQueue, 1, srcSize * sizeof(srcType)); pipe.InitBuffer(outQueue, 1, srcSize * sizeof(srcType)); pipe.InitBuffer(tmpBuf, tmpBufferSize); bufferSize srcSize; } __aicore__ inline void Process() { CopyIn(); Compute(); CopyOut(); } private: __aicore__ inline void CopyIn() { AscendC::LocalTensorsrcType srcLocal inQueue.AllocTensorsrcType(); AscendC::DataCopy(srcLocal, srcGlobal, bufferSize); inQueue.EnQue(srcLocal); } __aicore__ inline void Compute() { AscendC::LocalTensorsrcType dstLocal outQueue.AllocTensorsrcType(); AscendC::LocalTensorsrcType srcLocal inQueue.DeQuesrcType(); AscendC::LocalTensoruint8_t sharedTmpBuffer tmpBuf.Getuint8_t(); AscendC::AsinsrcType, false(dstLocal, srcLocal, sharedTmpBuffer, bufferSize); outQueue.EnQuesrcType(dstLocal); inQueue.FreeTensor(srcLocal); } __aicore__ inline void CopyOut() { AscendC::LocalTensorsrcType dstLocal outQueue.DeQuesrcType(); AscendC::DataCopy(dstGlobal, dstLocal, bufferSize); outQueue.FreeTensor(dstLocal); } private: AscendC::GlobalTensorsrcType srcGlobal; AscendC::GlobalTensorsrcType dstGlobal; AscendC::TPipe pipe; AscendC::TQueAscendC::TPosition::VECIN, 1 inQueue; AscendC::TBufAscendC::TPosition::VECCALC tmpBuf; AscendC::TQueAscendC::TPosition::VECOUT, 1 outQueue; uint32_t bufferSize 0; }; extern C __global__ __aicore__ void asin_custom(GM_ADDR srcGm, GM_ADDR dstGm, GM_ADDR workspace, GM_ADDR tiling) { GET_TILING_DATA(tilingData, tiling); KernelAsinhalf op; op.Init(srcGm, dstGm, tilingData.srcSize, tilingData.tmpBufferSize); if (TILING_KEY_IS(1)) { op.Process(); } }样例二下面的样例展示了数学库Kernel侧API和PlatformAscendC::ReserveLocalMemory的配合使用方法流程如下Host侧调用ReserveLocalMemory接口预留Unified Buffer内存空间并通过GetCoreMemSize接口获取实际可用的Unified Buffer内存大小。基于实际可用的内存大小计算能够支持的最大Shape最大数据规模。这种方式可以避免多次调用GetXXXTmpMaxMinSize接口来获取合适的临时空间大小。Host侧Tiling API使用样例#include vector #include register/op_def_registry.h #include register/tilingdata_base.h #include tiling/tiling_api.h namespace optiling { BEGIN_TILING_DATA_DEF(MathCustomTilingData) TILING_DATA_FIELD_DEF(uint32_t, calSize); END_TILING_DATA_DEF; static ge::graphStatus TilingFunc(gert::TilingContext* context) { auto platformInfo context-GetPlatformInfo(); auto ascendcPlatform platform_ascendc::PlatformAscendC(platformInfo); uint64_t tailSize 0; // Unified Buffer剩余空间大小 ascendcPlatform.ReserveLocalMemory(platform_ascendc::ReservedSize::RESERVED_SIZE_8K); ascendcPlatform.GetCoreMemSize(platform_ascendc::CoreMemType::UB, tailSize); uint64_t calSize tailSize / (3 * 4); // src dst tmp float type MathCustomTilingData tiling; tiling.set_calSize(calSize); context-SetBlockDim(1); tiling.SaveToBuffer(context-GetRawTilingData()-GetData(), context-GetRawTilingData()-GetCapacity()); context-GetRawTilingData()-SetDataSize(tiling.GetDataSize()); context-SetTilingKey(1); return ge::GRAPH_SUCCESS; } } // namespace optilingKernel侧通过读取TilingData获取相应的计算规模参数。随后Kernel调用数学库提供的不带临时空间的API执行多种数学运算#include kernel_operator.h template typename srcType class KernelMath { public: __aicore__ inline KernelMath() {} __aicore__ inline void Init(GM_ADDR srcGm, GM_ADDR dstGm, uint32_t srcSize) { srcGlobal.SetGlobalBuffer(reinterpret_cast__gm__ srcType*(srcGm), srcSize); dstGlobal.SetGlobalBuffer(reinterpret_cast__gm__ srcType*(dstGm), srcSize); pipe.InitBuffer(inQueue, 1, srcSize * sizeof(srcType)); pipe.InitBuffer(outQueue, 1, srcSize * sizeof(srcType)); pipe.InitBuffer(tmpBuf, srcSize * sizeof(srcType)); bufferSize srcSize; } __aicore__ inline void Process() { CopyIn(); Compute(); CopyOut(); } private: __aicore__ inline void CopyIn() { AscendC::LocalTensorsrcType srcLocal inQueue.AllocTensorsrcType(); AscendC::DataCopy(srcLocal, srcGlobal, bufferSize); inQueue.EnQue(srcLocal); } __aicore__ inline void Compute() { AscendC::LocalTensorsrcType dstLocal outQueue.AllocTensorsrcType(); AscendC::LocalTensorsrcType srcLocal inQueue.DeQuesrcType(); AscendC::LocalTensorsrcType tmp tmpBuf.GetsrcType(); AscendC::AsinsrcType, false(tmp, srcLocal, bufferSize); AscendC::PipeBarrierPIPE_V(); AscendC::SinsrcType, false(dstLocal, tmp, bufferSize); outQueue.EnQuesrcType(dstLocal); inQueue.FreeTensor(srcLocal); } __aicore__ inline void CopyOut() { AscendC::LocalTensorsrcType dstLocal outQueue.DeQuesrcType(); AscendC::DataCopy(dstGlobal, dstLocal, bufferSize); outQueue.FreeTensor(dstLocal); } private: AscendC::GlobalTensorsrcType srcGlobal; AscendC::GlobalTensorsrcType dstGlobal; AscendC::TPipe pipe; AscendC::TQueAscendC::TPosition::VECIN, 1 inQueue; AscendC::TBufAscendC::TPosition::VECCALC tmpBuf; AscendC::TQueAscendC::TPosition::VECOUT, 1 outQueue; uint32_t bufferSize 0; }; extern C __global__ __aicore__ void math_custom(GM_ADDR srcGm, GM_ADDR dstGm, GM_ADDR workspace, GM_ADDR tiling) { GET_TILING_DATA(tilingData, tiling); KernelMathfloat op; op.Init(srcGm, dstGm, tilingData.calSize); if (TILING_KEY_IS(1)) { op.Process(); } }样例三下面的样例展示了数学库kernel侧API和Tiling API GetXxxTmpBufferFactorSize的配套使用方法具体流程如下Host侧调用Tiling接口获取maxLiveNodeCount和extraBuf并推算算子单次最大计算元素数量将其写入tiling data中kernel侧再读取tiling data获取该值基于该值分配临时空间。Host侧Tiling API使用样例:#include vector #include cassert #include register/op_def_registry.h #include register/tilingdata_base.h #include tiling/tiling_api.h namespace optiling { BEGIN_TILING_DATA_DEF(AsinCustomTilingData) TILING_DATA_FIELD_DEF(uint32_t, srcSize); TILING_DATA_FIELD_DEF(uint32_t, tmpBufferSize); END_TILING_DATA_DEF; static ge::graphStatus TilingFunc(gert::TilingContext* context) { // Input source shapes. auto shape_input context-GetInputTensor(0)-GetOriginShape(); std::vectorint64_t srcDims {shape_input.GetDim(0), shape_input.GetDim(1)}; uint32_t srcSize 1; uint32_t srcCurSize 1; for (auto dim : srcDims) { srcSize * dim; } uint32_t typeSize 2; auto platformInfo context-GetPlatformInfo(); auto ascendcPlatform platform_ascendc::PlatformAscendC(platformInfo); uint64_t tailSize 0; // ub剩余空间大小 ascendcPlatform.GetCoreMemSize(platform_ascendc::CoreMemType::UB, tailSize); uint32_t asinMaxLiveNodeCount 0; uint32_t asinExtraBuf 0; uint32_t acosMaxLiveNodeCount 0; uint32_t acosExtraBuf 0; AscendC::GetAsinTmpBufferFactorSize(typeSize, asinMaxLiveNodeCount, asinExtraBuf); AscendC::GetAcosTmpBufferFactorSize(typeSize, acosMaxLiveNodeCount, acosExtraBuf); // tmp的大小需要减去UB上调用api接口输入和输出占用的大小 // 该示例中包括Asin接口的输入输出以及Acos的输入输出其中Asin接口的输出作为Acos的输入因此一共需要3份src的空间大小 auto tmpSize tailSize - srcSize * typeSize * 3; assert(tmpSize asinExtraBuf); assert(tmpSize acosExtraBuf); // 计算Asin算子单次最大计算元素数量 if (asinMaxLiveNodeCount ! 0) { srcAsinCurSize (tmpSize - asinExtraBuf) / asinMaxLiveNodeCount / typeSize; } else { srcAsinCurSize srcSize; } // 计算Acos算子单次最大计算元素数量 if (acosMaxLiveNodeCount ! 0) { srcAcosCurSize (tmpSize - acosExtraBuf) / acosMaxLiveNodeCount / typeSize; } else { srcAcosCurSize srcSize; } srcCurSize std::min(srcAsinCurSize, srcAcosCurSize); AsinCustomTilingData tiling; tiling.set_srcSize(srcSize); tiling.set_srcCurSize(srcCurSize); tiling.set_tmpBufferSize(tmpSize); context-SetBlockDim(1); tiling.SaveToBuffer(context-GetRawTilingData()-GetData(), context-GetRawTilingData()-GetCapacity()); context-GetRawTilingData()-SetDataSize(tiling.GetDataSize()); context-SetTilingKey(1); return ge::GRAPH_SUCCESS; } } // namespace optilingkernel侧样例:#include kernel_operator.h template typename srcType class KernelAsin { public: __aicore__ inline KernelAsin() {} __aicore__ inline void Init( GM_ADDR srcGm, GM_ADDR dstGm, uint32_t srcSizeIn, uint32_t srcCurSizeIn, uint32_t tmpBufferSize) { srcSize srcSizeIn; srcCurSize srcCurSizeIn; srcGlobal.SetGlobalBuffer(reinterpret_cast__gm__ srcType*(srcGm), srcSize); dstGlobal.SetGlobalBuffer(reinterpret_cast__gm__ srcType*(dstGm), srcSize); pipe.InitBuffer(inQueue, 1, srcSize * sizeof(srcType)); pipe.InitBuffer(outQueue, 1, srcSize * sizeof(srcType)); pipe.InitBuffer(tmpBuf1, 1, srcCurSize * sizeof(srcType)); pipe.InitBuffer(tmpBuf, 1, tmpBufferSize); } __aicore__ inline void Process() { CopyIn(); Compute(); CopyOut(); } private: __aicore__ inline void CopyIn() { AscendC::LocalTensorsrcType srcLocal inQueue.AllocTensorsrcType(); AscendC::DataCopy(srcLocal, srcGlobal, srcSize); inQueue.EnQue(srcLocal); } __aicore__ inline void Compute() { AscendC::LocalTensorsrcType dstLocal outQueue.AllocTensorsrcType(); AscendC::LocalTensorsrcType srcLocal inQueue.DeQuesrcType(); AscendC::LocalTensoruint8_t sharedTmpBuffer tmpBuf.Getuint8_t(); AscendC::LocalTensorsrcType tmpresBuffer tmpBuf1.GetsrcType(); for (int32_t offset 0; offset srcSize; offset srcCurSize) { AscendC::AsinsrcType, false(tmpresBuffer, srcLocal[offset], sharedTmpBuffer, srcCurSize); AscendC::PipeBarrierPIPE_V(); AscendC::AcossrcType, false(dstLocal[offset], tmpresBuffer, sharedTmpBuffer, srcCurSize); AscendC::PipeBarrierPIPE_V(); } outQueue.EnQuesrcType(dstLocal); inQueue.FreeTensor(srcLocal); } __aicore__ inline void CopyOut() { AscendC::LocalTensorsrcType dstLocal outQueue.DeQuesrcType(); AscendC::DataCopy(dstGlobal, dstLocal, srcSize); outQueue.FreeTensor(dstLocal); } private: AscendC::GlobalTensorsrcType srcGlobal; AscendC::GlobalTensorsrcType dstGlobal; AscendC::TPipe pipe; AscendC::TQueAscendC::TPosition::VECIN, 1 inQueue; AscendC::TBufAscendC::TPosition::VECCALC, 1 tmpBuf; AscendC::TBufAscendC::TPosition::VECCALC, 1 tmpBuf1; AscendC::TQueAscendC::TPosition::VECOUT, 1 outQueue; uint32_t srcSize, srcCurSize; }; extern C __global__ __aicore__ void kernel_asin_operator( GM_ADDR srcGm, GM_ADDR dstGm, GM_ADDR workspace, GM_ADDR tiling) { GET_TILING_DATA(tilingData, tiling); KernelAsinhalf op; op.Init(srcGm, dstGm, tilingData.srcSize, tilingData.srcCurSize, tilingData.tmpBufferSize); if (TILING_KEY_IS(1)) { op.Process(); } }样例四下面以Exp接口的关键调用代码为例辅以调用前后数据打印结果展示模板参数isReuseSource的使用及其相关影响。模板参数isReuseSource为bool类型若isReuseSource为false则接口内部计算时不复用源操作数的内存空间若isReuseSource为true接口内部计算时会复用源操作数的内存空间存放一些中间结果节省内存空间开发者需要注意接口执行完成后源操作数的内存空间不再是原始值。isReuseSource false// 调用Exp前dstLocal、srcLocal数值 // dstLocal: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] // srcLocal: [-7.5, -6.5, -5.5, -4.5, -3.5, -2.5, -1.5, -0.5, 0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5] AscendC::Expfloat, 15, false(dstLocal, srcLocal, 16); // 调用Exp后dstLocal、srcLocal数值 // dstLocal: [0.000553084, 0.00150344, 0.00408677, 0.011109, 0.0301974, 0.082085, 0.22313, // 0.606531, 1.64872, 4.48169, 12.1825, 33.1155, 90.0171, 244.692, 665.142, 1808.04] srcLocal: [-7.5, -6.5, -5.5, -4.5, // -3.5, -2.5, -1.5, -0.5, 0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5]isReuseSource true// 调用Exp前dstLocal、srcLocal数值 // dstLocal: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] // srcLocal: [-7.5, -6.5, -5.5, -4.5, -3.5, -2.5, -1.5, -0.5, 0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5] AscendC::Expfloat, 15, true(dstLocal, srcLocal, 16); // 调用Exp后dstLocal、srcLocal数值 // dstLocal: [0.000553084, 0.00150344, 0.00408677, 0.011109, 0.0301974, 0.082085, 0.22313, // 0.606531, 1.64872, 4.48169, 12.1825, 33.1155, 90.0171, 244.692, 665.142, 1808.04] srcLocal: [0.5, 0.5, 0.5, 0.5, 0.5, // 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5]【免费下载链接】asc-devkit本项目是CANN 推出的昇腾AI处理器专用的算子程序开发语言原生支持C和C标准规范主要由类库和语言扩展层构成提供多层级API满足多维场景算子开发诉求。项目地址: https://gitcode.com/cann/asc-devkit创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
RELATED

相关推荐

昇腾C数据过滤临时空间API

昇腾C数据过滤临时空间API

GetDropOutMaxMinTmpSize 【免费下载链接】asc-devkit 本项目是CANN 推出的昇腾AI处理器专用的算子程序开发语言,原生支持C和C标准规范,主要由类库和语言扩展层构成,提供多层级API,满足多维场景算子开发诉求。 项目地址: https:…

📅 2026/11/11 17:19:19
5分钟部署Infisical:开源密钥管理平台实战指南

5分钟部署Infisical:开源密钥管理平台实战指南

1. 项目概述:为什么你需要一个企业级密钥管理平台? 在今天的开发运维实践中,密钥、API令牌、数据库连接字符串这类敏感信息的管理,已经从“最好有”变成了“必须有”的环节。我见过太多团队还在用 .env 文件,然后小心…

📅 2026/11/6 15:01:30
SQL Server错误代码解析与故障排除指南

SQL Server错误代码解析与故障排除指南

1. SQL Server错误代码概述 SQL Server作为企业级关系型数据库管理系统,在日常运维和开发过程中难免会遇到各种错误代码。这些错误代码是SQL Server与用户沟通的重要桥梁,它们以数字形式精确描述了系统运行中遇到的问题。理解这些错误代码的含义&#xf…

📅 2026/11/10 13:58:37
MORE NEWS

更多资讯

📰

大夹子电工作业用途考前押题怎么准备才能稳过实操考试

大夹子电工作业用途考前押题怎么准备才能稳过实操考试 实操考试心里没底怕挂科?你不是一个人在战斗。很多准备考大夹子电工作业的小伙伴都遇到过这个问题,尤其是第一次接触实操考试,面对复杂的操作流程和严格的评分标准,很多人会觉得一头雾水,甚至担心自…

📰

电工证求职网站有哪些?工地太忙根本没时间复习考试,内部消息告诉你怎么应对

电工证求职网站有哪些?工地太忙根本没时间复习考试,内部消息告诉你怎么应对 工地太忙根本没时间复习考试,特别是像我们这种项目现场管理员,天天跟工期、跟人打交道,哪还有精力去准备电工证考试?但别以为电工证只是“纸老虎”,在咱们这行, 持证上岗…

📰

考电工证包就业怎么网上查询?学历不高也能报!

考电工证包就业怎么网上查询?学历不高也能报! 你是不是也担心自己学历不高,考电工证包就业这条路走不通?其实不然, 考电工证包就业 早已不是“高学历专属”,只要你有意愿、有动手能力,完全可以从零开始。而如今, 网上查询…

📰

鸡西架子工特种作业证真实经历:怕被中介坑?老手教你避坑

鸡西架子工特种作业证真实经历:怕被中介坑?老手教你避坑 别看我这行干了十年,但一开始也踩过坑。当时我跟着一个劳务班组在鸡西干活,听说要考架子工特种作业证,结果一打听,报名点五花八门,价格也是参差不齐。我那个师傅说:“别乱报,找错地方,钱花了…

📰

架子工特种作业证书多久拿证?怕考不过白交培训费的看过来

架子工特种作业证书多久拿证?怕考不过白交培训费的看过来 怕考不过白交培训费?你不是一个人。特别是像你这样刚毕业的信阳工程类学生,面对架子工特种作业证书考试,心里没底是正常的。但别慌, 架子工特种作业证书多久拿证…

📰

低压电工证审查收费吗?学历不高怕报不上名?上岗必备全攻略来了

低压电工证审查收费吗?学历不高怕报不上名?上岗必备全攻略来了 你是不是也担心自己学历不高,连低压电工证都报不上?别急,今天咱们就来聊聊【低压电工证审查收费吗】这个关键问题,还有关于【上岗必备】的那些事,统统给你讲清楚。…

TODAY

今日更新

THIS WEEK

本周精选

THIS MONTH

本月热门

读完文章,想聊聊您的网站?

告诉我们您的行业与需求,资深顾问一对一梳理方案与报价,全程免费。

📞 💬