Get

Function Usage

Obtains tensors of a specified length or all lengths from TBuf.

Prototype

  • Obtains tensors of all lengths.
    1
    LocalTensor<T> Get<T>()
    
  • Obtains tensors of a specified length.
    1
    LocalTensor<T> Get<T>(uint32_t len)
    

Parameters

Table 1 Parameters

Parameter

Input/Output

Meaning

len

Input

Number of tensor elements to be obtained.

Availability

Atlas Training Series Product

Precautions

The value of len is the number of elements in the tensor. The value of len x sizeof(T) cannot exceed the length during TBuf initialization.

Returns

None

Example

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// Allocate buffer for TBuf initialization. The length of the allocated buffer is 1024 bytes.
AscendC::TPipe pipe;
AscendC::TBuf<AscendC::TPosition::VECCALC> calcBuf; // The template parameter is the VECCALC type in TPosition.
uint32_t byteLen = 1024;
pipe.InitBuffer(calcBuf, byteLen);
// Obtain a tensor from calcBuf. The tensor size is the size of all buffers allocated by the pipe (1024 bytes).
AscendC::LocalTensor<int32_t> tempTensor1 = calcBuf.Get<int32_t>();
// Obtain a tensor from calcBuf. The tensor size is the buffer size (512 bytes) of 128 int32_t elements.
AscendC::LocalTensor<int32_t> tempTensor1 = calcBuf.Get<int32_t>(128);
/* In complex computing scenarios, TBuf can be used as a temporary variable to store intermediate calculation results, avoiding complex dequeue and enqueue processes.
* The following code comes from the API for calculating distances. Matrix C needs to be divided by the point multiplication result of matrix A and matrix B. In this algorithm, all matrices are converted into vectors in advance.
 */
auto normADotB = calcBuf.Get <int32_t>(); // Store the result of point multiplication of matrix A and matrix B.
auto normB = qidVecIn.AllocTensor<DTypeOut>();
...
normB= qidVecIn.DeQue<DTypeOut>(); // Obtain matrix B.
for(int i = 0; i < tiling.baseM; i++) {
   AscendC::Muls(normADotB[i * tiling.baseN], normB, normA.GetValue(i), tiling.baseN); // Matrices A and B are converted into vectors for number multiplication. normADotB is used as a temporary variable to store the result.
}
qidVecIn.FreeTensor(normB);
...
for(int i = 0; i < tiling.baseM; i++) {
   AscendC::Mul(baseCVecFloat[i * tiling.baseN], baseCVecFloat[i * tiling.baseN], normADotB[i * tiling.baseN], tiling.baseN); // Obtain matrix C through calculation and divide matrix C by normADotB.
}