
AllocTensor【免费下载链接】asc-devkit本项目是CANN 推出的昇腾AI处理器专用的算子程序开发语言原生支持C和C标准规范主要由类库和语言扩展层构成提供多层级API满足多维场景算子开发诉求。项目地址: https://gitcode.com/cann/asc-devkit产品支持情况Ascend 950PR/Ascend 950DT支持Atlas A3 训练系列产品/Atlas A3 推理系列产品支持Atlas A2 训练系列产品/Atlas A2 推理系列产品支持Atlas 200I/500 A2 推理产品支持Atlas 推理系列产品AI Core支持Atlas 推理系列产品Vector Core不支持Atlas 训练系列产品支持Kirin X90支持Kirin 9030支持功能说明头文件路径为basic_api/kernel_tpipe.h。从Que中分配TensorTensor所占大小为InitBuffer时设置的每块内存长度。函数原型non-inplace接口构造新的Tensor作为内存管理的对象template typename T __aicore__ inline LocalTensorT AllocTensor()inplace接口直接使用传入的Tensor作为内存管理的对象可以减少Tensor反复创建的开销具体使用指导可参考Tensor原地操作。template typename T __aicore__ inline void AllocTensor(LocalTensorT tensor)参数说明表1模板参数说明参数名说明TTensor的数据类型支持的类型请见LocalTensor相关描述。表2参数说明参数名称输入/输出含义tensor输入inplace接口需要传入LocalTensor作为内存管理的对象。约束说明同一个TPosition上的所有Queue连续调用AllocTensor接口申请的Tensor数量根据AI处理器型号的不同有数量约束。申请Buffer时需要满足该约束。Ascend 950PR/Ascend 950DT不超过8个。Atlas A3 训练系列产品/Atlas A3 推理系列产品不超过8个。Atlas A2 训练系列产品/Atlas A2 推理系列产品不超过8个。Atlas 200I/500 A2 推理产品不超过8个。Atlas 推理系列产品AI Core不超过8个。Atlas 推理系列产品Vector Core不超过8个。Atlas 训练系列产品不超过4个。Kirin X90不超过8个。Kirin 9030不超过8个。non-inplace接口分配的Tensor内容可能包含随机值。non-inplace接口需要将TQueBind的depth模板参数设置为非零值inplace接口需要将TQueBind的depth模板参数设置为0。返回值说明non-inplace接口返回值为LocalTensor对象inplace接口没有返回值。调用示例示例一// 使用AllocTensor分配Tensor AscendC::TPipe pipe; AscendC::TQueAscendC::TPosition::VECOUT, 2 que; int num 2; int len 1024; pipe.InitBuffer(que, num, len); // InitBuffer分配内存块数为2每块大小为1024Bytes AscendC::LocalTensorhalf tensor1 que.AllocTensorhalf(); // AllocTensor分配Tensor长度为1024Bytes示例二// 连续使用AllocTensor的限制场景举例如下: AscendC::TQueAscendC::TPosition::VECIN, 1 que0; AscendC::TQueAscendC::TPosition::VECIN, 1 que1; AscendC::TQueAscendC::TPosition::VECIN, 1 que2; AscendC::TQueAscendC::TPosition::VECIN, 1 que3; AscendC::TQueAscendC::TPosition::VECIN, 1 que4; AscendC::TQueAscendC::TPosition::VECIN, 1 que5; // 不建议 // 比如算子有6个输入需要申请6块buffer // 通过6个队列为其申请内存分别为que0~que5每个que分配1块,申请VECIN TPosition上的buffer总数为6 // 假设同一个TPosition上连续Alloc的Buffer数量限制为4超出该限制后使用AllocTensor/FreeTensor会出现分配资源失败 // 在NPU上可能体现为卡死等异常行为在CPU Debug场景会出现报错提示 pipe.InitBuffer(que0, 1, len); pipe.InitBuffer(que1, 1, len); pipe.InitBuffer(que2, 1, len); pipe.InitBuffer(que3, 1, len); pipe.InitBuffer(que4, 1, len); pipe.InitBuffer(que5, 1, len); AscendC::LocalTensorT local1 que0.AllocTensorT(); AscendC::LocalTensorT local2 que1.AllocTensorT(); AscendC::LocalTensorT local3 que2.AllocTensorT(); AscendC::LocalTensorT local4 que3.AllocTensorT(); // 第5个AllocTensor会出现资源分配失败同一个TPosition上同时Alloc出来的Tensor数量超出了4个的限制 AscendC::LocalTensorT local5 que4.AllocTensorT(); // 此时建议通过以下方法解决 // 如果确实有多块buffer使用,可以将多个buffer合并到一块buffer,通过偏移使用 pipe.InitBuffer(que0, 1, len * 3); pipe.InitBuffer(que1, 1, len * 3); /* * 分配出3块内存大小的LocalTensor, local1的地址为que0中buffer的起始地址 * local2的地址为local1的地址偏移len后的地址local3的地址为local1的地址偏移 * len * 2的地址 */ int32_t offset1 len; int32_t offset2 len * 2; AscendC::LocalTensorT local1 que0.AllocTensorT(); AscendC::LocalTensorT local2 local1[offset1]; AscendC::LocalTensorT local3 local1[offset2];示例三inplace接口AscendC::TPipe pipe; AscendC::TQueAscendC::TPosition::VECIN, 0 que; int num 2; int len 1024; pipe.InitBuffer(que, num, len); // InitBuffer分配内存块数为2每块大小为1024Bytes AscendC::LocalTensorhalf tensor1; que.AllocTensorhalf(tensor1); // AllocTensor分配Tensor长度为1024Bytes【免费下载链接】asc-devkit本项目是CANN 推出的昇腾AI处理器专用的算子程序开发语言原生支持C和C标准规范主要由类库和语言扩展层构成提供多层级API满足多维场景算子开发诉求。项目地址: https://gitcode.com/cann/asc-devkit创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考