Gather
Applicability
|
Product |
Supported |
|---|---|
|
Atlas 350 Accelerator Card |
√ |
|
|
√ |
|
|
√ |
|
|
√ |
|
|
√ |
|
|
x |
|
|
x |
Function Usage
Gathers elements from the input tensor into the result tensor based on the provided address offset tensor.
Prototype
- Computation of the first n data elements of a tensor
1 2
template <typename T> __aicore__ inline void Gather(const LocalTensor<T>& dst, const LocalTensor<T>& src, const LocalTensor<uint32_t>& srcOffset, const uint32_t srcBaseAddr, const uint32_t count)
- High-dimensional tensor sharding computation
- Bitwise mask mode
1 2
template <typename T> __aicore__ inline void Gather(const LocalTensor<T>& dst, const LocalTensor<T>& src, const LocalTensor<uint32_t>& srcOffset, const uint32_t srcBaseAddr, const uint64_t mask[], const uint8_t repeatTime, const uint16_t dstRepStride)
- Contiguous mask mode
1 2
template <typename T> __aicore__ inline void Gather(const LocalTensor<T>& dst, const LocalTensor<T>& src, const LocalTensor<uint32_t>& srcOffset, const uint32_t srcBaseAddr, const uint64_t mask, const uint8_t repeatTime, const uint16_t dstRepStride)
- Bitwise mask mode
Parameters
|
Parameter |
Description |
|---|---|
|
T |
Operand data type. For the Atlas 350 Accelerator Card, the supported data types are uint8_t, int8_t, uint16_t, int16_t, half, bfloat16_t, uint32_t, int32_t, float, uint64_t, and int64_t. For the For the For the For the |
|
Parameter |
Input/Output |
Meaning |
|---|---|---|
|
dst |
Output |
Destination operand. The type is LocalTensor, and TPosition can be VECIN, VECCALC, or VECOUT. The start address of LocalTensor must be 32-byte aligned. |
|
src |
Input |
Source operand. The type is LocalTensor, and TPosition can be VECIN, VECCALC, or VECOUT. The start address of LocalTensor must be 32-byte aligned. Its data type must match that of dst. |
|
srcOffset |
Input |
Address offset of each element in src. The type is LocalTensor, and TPosition can be VECIN, VECCALC, or VECOUT. The start address of LocalTensor must be 32-byte aligned. The offset is relative to the start base address of src. The unit is byte. The value must meet the following requirements:
|
|
srcBaseAddr |
Input |
Start base address of src, which specifies the start position of the source operand in the gather operation. The unit is byte. Ensure that the bit width of the src element type is aligned. Otherwise, unexpected behavior occurs. |
|
count |
Input |
Number of data elements to be processed. |
|
mask/mask[] |
Input |
mask controls the elements that participate in computation in each iteration.
|
|
repeatTime |
Input |
Number of instruction iterations. Data of eight data blocks (32 bytes) is collected in each iteration. Data range: repeatTime ∈ [0,255]
For the following models:
When the operand is 8-bit, four data blocks (32 bytes) are collected in each repeat. |
|
dstRepStride |
Input |
Address stride of the operand between adjacent iterations. The unit is data block (32 bytes). |
Restrictions
- For details about the operand address alignment requirements, see General Address Alignment Restrictions.
- For details about the constraints on operand address overlapping, see General Address Overlapping Restrictions.
- For the Atlas 350 Accelerator Card, only the APIs that compute the first n data elements of a tensor are supported for the uint8_t and int8_t data types.
Examples
These examples show only part of the code used in the computation.
- Example of high-dimensional tensor sharding computation (contiguous mask mode)
1 2 3 4 5
// repeatTime = 4, mask = 128, 128 elements one repeat, 512 elements total // srcLocal and dstLocal values are of the half type, and the srcOffsetLocal value is of the uint32_t type. // srcBaseAddr = 0, indicating that the start base address of srcLocal is 0. // dstRepStride = 8, no gap between repeats AscendC::Gather(dstLocal, srcLocal, srcOffsetLocal, (uint32_t)0, 128, 4, 8);
- Example of high-dimensional tensor sharding computation (bitwise mask mode)
1 2 3 4 5 6
uint64_t mask[2] = { 0xFFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF }; // repeatTime = 4, 128 elements one repeat, 512 elements total // srcLocal and dstLocal values are of the half type, and the srcOffsetLocal value is of the uint32_t type. // srcBaseAddr = 0, indicating that the start base address of srcLocal is 0. // dstRepStride = 8, no gap between repeats AscendC::Gather(dstLocal, srcLocal, srcOffsetLocal, (uint32_t)0, mask, 4, 8);
- Example of computing the first n data elements of a tensor
1 2 3 4
uint32_t count = 512; // Number of elements involved in computation // srcLocal and dstLocal values are of the half type, and the srcOffsetLocal value is of the uint32_t type. // srcBaseAddr = 0, indicating that the start base address of srcLocal is 0. AscendC::Gather(dstLocal, srcLocal, srcOffsetLocal, (uint32_t)0, count);
Result example:
Input srcOffsetLocal: [254 252 250 ... 4 2 0] Input srcLocal (128 data elements of the half type): [0 1 2 ... 125 126 127] Initial value of output data (dstLocal): [0. 0. 0. 0. 0. 0. ... 0.] Output (dstLocal) after Gather computation: [127 126 125 ... 2 1 0]