CANN/asc-devkit TBuf Get方法文档 📅 2026/7/15 15:00:17 Get产品支持情况产品是否支持Ascend 950PR/Ascend 950DT√Atlas A3 训练系列产品 / Atlas A3 推理系列产品√Atlas A2 训练系列产品 / Atlas A2 推理系列产品√Atlas 200I/500 A2 推理产品√Atlas 推理系列产品 AI Core√Atlas 推理系列产品 Vector CorexAtlas 训练系列产品√Kirin X90√Kirin 9030√功能说明从TBuf上获取指定长度的Tensor或者获取全部长度的Tensor。函数原型获取全部长度的Tensortemplate typename T __aicore__ inline LocalTensorT Get()获取指定长度的Tensortemplate typename T __aicore__ inline LocalTensorT Get(uint32_t len)参数说明表 1模板参数说明参数名称含义T待获取Tensor的数据类型。表 2参数说明参数名称输入/输出含义len输入需要获取的Tensor元素个数。约束说明len的数值是Tensor中元素的个数len*sizeof(T)不能超过TBuf初始化时的长度。返回值说明获取到的LocalTensor。调用示例// 为TBuf初始化分配内存分配内存长度为1024字节 AscendC::TPipe pipe; AscendC::TBufAscendC::TPosition::VECCALC calcBuf; // 模板参数为TPosition中的VECCALC类型 uint32_t byteLen 1024; pipe.InitBuffer(calcBuf, byteLen); // 从calcBuf获取Tensor,Tensor为pipe分配的所有内存大小为1024字节 AscendC::LocalTensorint32_t tempTensor1 calcBuf.Getint32_t(); // 从calcBuf获取Tensor,Tensor为128个int32_t类型元素的内存大小为512字节 AscendC::LocalTensorint32_t tempTensor2 calcBuf.Getint32_t(128);对同一个TBuf对象连续调用Get接口获取到的Tensor首地址是相同的不会依次向后偏移。如果需要获取偏移之后的Tensor可以使用如下方法AscendC::TPipe pipe; // 模板参数为TPosition中的VECCALC类型 AscendC::TBufAscendC::TPosition::VECCALC calcBuf; // 分配一个2048字节的连续空间 uint32_t byteLen 2048; pipe.InitBuffer(calcBuf, byteLen); // 从calcBuf获取tensor1tensor1为pipe分配的所有内存大小为2048字节 AscendC::LocalTensorint32_t tensor1 calcBuf.Getint32_t(); // 用户指定tensor1的第256个int32_t的偏移位置为tensor2的首地址实际上tensor2可以使用的内存大小为1024字节两个tensor首地址相差256 * sizeof(int32_t)字节 auto tensor2 tensor1[256];创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考