AllocTensor
功能说明
从Que中分配Tensor,Tensor所占大小为InitBuffer时设置的每块内存长度。注意,分配的Tensor内容并非全0,可能会是随机值。
函数原型
1 2 | template <typename T> __aicore__ inline LocalTensor<T> AllocTensor() |
参数说明
参数名 |
说明 |
---|---|
T |
Tensor的数据类型。 |
支持的型号
约束说明
同一个TPosition上的所有Queue,连续调用AllocTensor接口申请的Tensor数量,根据AI处理器型号的不同,有数量约束。申请Buffer时,需要满足该约束。
返回值
LocalTensor对象。
调用示例
- 示例一
1 2 3 4 5 6 7
// 使用AllocTensor分配Tensor AscendC::TPipe pipe; AscendC::TQue<AscendC::TPosition::VECOUT, 2> que; int num = 2; int len = 1024; pipe.InitBuffer(que, num, len); // InitBuffer分配内存块数为2,每块大小为1024Bytes AscendC::LocalTensor<half> tensor1 = que.AllocTensor<half>(); // AllocTensor分配Tensor长度为1024Bytes
- 示例二
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
// 连续使用AllocTensor的限制场景举例如下: AscendC::TQue<AscendC::TPosition::VECIN, 1> que0; AscendC::TQue<AscendC::TPosition::VECIN, 1> que1; AscendC::TQue<AscendC::TPosition::VECIN, 1> que2; AscendC::TQue<AscendC::TPosition::VECIN, 1> que3; AscendC::TQue<AscendC::TPosition::VECIN, 1> que4; AscendC::TQue<AscendC::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::LocalTensor<T> local1 = que0.AllocTensor<T>(); AscendC::LocalTensor<T> local2 = que1.AllocTensor<T>(); AscendC::LocalTensor<T> local3 = que2.AllocTensor<T>(); AscendC::LocalTensor<T> local4 = que3.AllocTensor<T>(); // 第5个AllocTensor会出现资源分配失败,同一个TPosition上同时Alloc出来的Tensor数量超出了4个的限制 AscendC::LocalTensor<T> local5 = que4.AllocTensor<T>(); // 此时建议通过以下方法解决: // 如果确实有多块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::LocalTensor<T> local1 = que0.AllocTensor<T>(); AscendC::LocalTensor<T> local2 = local1[offset1]; AscendC::LocalTensor<T> local3 = local1[offset2];
父主题: TQue