CANN/asc-devkit AllocTensor API

📅 2026/7/14 15:16:08 👁️ 阅读次数 📝 编程学习
CANN/asc-devkit AllocTensor API

AllocTensor

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

产品支持情况

产品

是否支持

Atlas A3 训练系列产品 / Atlas A3 推理系列产品

Atlas A2 训练系列产品 / Atlas A2 推理系列产品

功能说明

从Que中分配Tensor,Tensor所占大小为InitBuffer时设置的每块内存长度。

函数原型

  • non-inplace接口:构造新的Tensor作为内存管理的对象

    template <typename T> __aicore__ inline LocalTensor<T> AllocTensor()
  • inplace接口:直接使用传入的Tensor作为内存管理的对象,可以减少Tensor反复创建的开销。

    template <typename T> __aicore__ inline void AllocTensor(LocalTensor<T>& tensor)

参数说明

表 1模板参数说明

参数名

说明

T

Tensor的数据类型。

表 2参数说明

参数名称

输入/输出

含义

tensor

输入

inplace接口需要传入LocalTensor作为内存管理的对象。

约束说明

  • non-inplace接口分配的Tensor内容可能包含随机值。
  • non-inplace接口,需要将TQueBind的depth模板参数设置为非零值;inplace接口,需要将TQueBind的depth模板参数设置为0。

返回值说明

non-inplace接口返回值为LocalTensor对象,inplace接口没有返回值。

调用示例

  • non-inplace接口

    AscendC::TPipe pipe; AscendC::TQueBind<AscendC::TPosition::VECOUT, AscendC::TPosition::GM, 2> que; int num = 4; int len = 1024; pipe.InitBuffer(que, num, len); // InitBuffer分配内存块数为4,每块大小为1024Bytes AscendC::LocalTensor<half> tensor1 = que.AllocTensor<half>(); // AllocTensor分配Tensor长度为1024Bytes
  • inplace接口

    AscendC::TPipe pipe; AscendC::TQueBind<AscendC::TPosition::VECOUT, AscendC::TPosition::GM, 0> que; int num = 2; int len = 1024; pipe.InitBuffer(que, num, len); // InitBuffer分配内存块数为2,每块大小为1024Bytes AscendC::LocalTensor<half> tensor1; que.AllocTensor<half>(tensor1); // AllocTensor分配Tensor长度为1024Bytes

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

创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考