|
产品 |
是否支持 |
|---|---|
|
|
√ |
|
|
√ |
|
|
√ |
|
|
√ |
|
|
x |
|
|
√ |
|
|
x |
从Que中分配Tensor,Tensor所占大小为InitBuffer时设置的每块内存长度。
1 2 |
template <typename T> __aicore__ inline LocalTensor<T> AllocTensor() |
1 2 |
template <typename T> __aicore__ inline void AllocTensor(LocalTensor<T>& tensor) |
|
参数名 |
说明 |
|---|---|
|
T |
Tensor的数据类型。 |
|
参数名称 |
输入/输出 |
含义 |
|---|---|---|
|
tensor |
输入 |
inplace接口需要传入LocalTensor作为内存管理的对象。 |
non-inplace接口返回值为LocalTensor对象,inplace接口没有返回值。
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]; |
1 2 3 4 5 6 7 |
AscendC::TPipe pipe; AscendC::TQue<AscendC::QuePosition::VECIN, 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 |