TPipe is a framework used to manage the global memory. You can call the InitBuffer API in TPipe to allocate memory for TQue/TBuf.
Memory allocation for TQue
template <class T> __aicore__ inline bool InitBuffer(T& que, uint8_t num, uint32_t len)
Buffer allocation for TBuf
template <TPosition bufPos> __aicore__ inline bool InitBuffer(TBuf<bufPos>& buf, uint32_t len)
Table 1 Parameters in bool InitBuffer(T& que, uint8_t num, uint32_t len) prototype definition
| Parameter | Input/Output | Description |
|---|---|---|
| que | Input | TQue object that requires buffer allocation. |
| num | Input | Number of allocated buffers. The double buffer function is enabled by setting this parameter. If num is set to 1, double buffer is disabled. If num is set to 2, double buffer is enabled. |
| len | Input | Size of each buffer. The unit is byte. If the input len does not meet the 32-byte alignment requirement, the API automatically pads up to 32-byte alignment. |
Table 2 Parameters in InitBuffer(TBuf<bufPos>& buf, uint32_t len) prototype definition
| Parameter | Input/Output | Description |
|---|---|---|
| buf | Input | TBuf object that requires buffer allocation. |
| len | Input | Size of the memory allocated to TBuf, in bytes. If the input len does not meet the 32-byte alignment requirement, the API automatically pads up to 32-byte alignment. |
Kirin 9020 series processors
Kirin 9030 series processors
Kirin X90 series processors
The number of QUE buffers on the same TPosition varies depending on the AI processor model. Number constraint exists. This constraint must be met when a buffer is allocated.
For Kirin 9020, Kirin 9030, and Kirin X90 series processors, the number cannot exceed 8.
AscendC::TQue<AscendC::TPosition::VECIN, 1> que0; AscendC::TQue<AscendC::TPosition::VECIN, 1> que1; // Not recommended: // For example, if the operator has six inputs, six buffers need to be allocated. // Respectively allocate three buffers for que0 and que1. Allocate six buffers in the VECIN position. // The number of QUE buffers on the same TPosition is limited. If the number exceeds the limit, resource allocation may fail when AllocTensor or FreeTensor is used. pipe.InitBuffer(que0, 3, len); pipe.InitBuffer(que1, 3, len); // You are advised to perform the following operations to solve the problem: // If multiple buffers are used, you can combine multiple buffers into one buffer and use the buffer through offset. pipe.InitBuffer(que0, 1, len * 3); pipe.InitBuffer(que1, 1, len * 3); /* * Three local tensors are allocated. The address of local1 is the start address of the buffer in que0. * The address of local2 is the address of local1 with offset len, and the address of local3 is the offset address of local1. * len * 2 address */ 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];
// Allocate memory to the TQue. The number of allocated buffers is 2, and the size of each buffer is 128 bytes. AscendC::TPipe pipe; // TPipe object AscendC::TQue<AscendC::TPosition::VECOUT, 2> que; // Output the management objects of the data queue. The value of QuePosition is VECOUT. uint8_t num = 2; uint32_t len = 128; pipe.InitBuffer(que, num, len);
// Allocate memory for TBuf. The allocated length is 128 bytes. AscendC::TPipe pipe; AscendC::TBuf<AscendC::TPosition::A1> buf; // Output data management object. The value of QuePosition is A1. uint32_t len = 128; pipe.InitBuffer(buf, len);