AllocTensor
Product Support
|
Product |
Supported |
|---|---|
|
|
√ |
|
|
√ |
|
|
√ |
|
|
√ |
|
|
x |
|
|
√ |
Function
Allocates tensors from the queue. The size occupied by tensors is the length of each buffer configured when InitBuffer is used.
Prototype
- Non-inplace API: Constructs a new tensor as the object for memory management.
1 2
template <typename T> __aicore__ inline LocalTensor<T> AllocTensor()
- Inplace API: Directly uses the input tensor as the object for memory management, reducing the overhead of repeatedly creating tensors. For details about how to use the API, see How to Improve Operator Performance Through Inplace Tensor Operations.
1 2
template <typename T> __aicore__ inline void AllocTensor(LocalTensor<T>& tensor)
Parameters
|
Parameter |
Description |
|---|---|
|
T |
Data type of the tensor. |
|
Parameter |
Input/Output |
Meaning |
|---|---|---|
|
tensor |
Input |
For the inplace API, LocalTensor needs to be passed as the object for memory management. |
Restrictions
- The tensor content allocated by the non-inplace API may contain random values.
- For the non-inplace API, the depth template parameter of TQueBind must be set to a non-zero value. For the inplace API, the depth template parameter of TQueBind must be set to 0.
Returns
The return value of the non-inplace API is a LocalTensor object, and the inplace API has no return value.
Example
- Non-inplace API
1 2 3 4 5 6
AscendC::TPipe pipe; AscendC::TQueBind<AscendC::TPosition::VECOUT, AscendC::TPosition::GM, 2> que; int num = 4; int len = 1024; pipe.InitBuffer(que, num, len); // Four buffers are allocated by InitBuffer, and the size of each buffer is 1024 bytes. AscendC::LocalTensor<half> tensor1 = que.AllocTensor<half>(); // The length of the tensor allocated by AllocTensor is 1024 bytes.
- Inplace API
1 2 3 4 5 6 7
AscendC::TPipe pipe; AscendC::TQueBind<AscendC::TPosition::VECOUT, AscendC::TPosition::GM, 0> que; int num = 2; int len = 1024; pipe.InitBuffer(que, num, len); // Two buffers are allocated by InitBuffer, and the size of each buffer is 1024 bytes. AscendC::LocalTensor<half> tensor1; que.AllocTensor<half>(tensor1); // The length of the tensor allocated by AllocTensor is 1024 bytes.