TGATHER
Instruction Diagram
Introduction
Gathers/Selects elements using an index tile or a compile-time mask pattern.
Mathematical Semantics
Index-based gather (conceptual definition):
Assume R = dst.GetValidRow() and C = dst.GetValidCol(). For 0 <= i < R and 0 <= j < C:
indices[i,j] is a flat element offset relative to src0.data(), measured in source elements, not bytes or row numbers; physical padding also occupies offsets. The caller must ensure that the indices are non-negative, lie within the allocated source tile, and point to initialized data. The implementation does not perform index bounds checking.
64-Bit Mode Gather Output (A5/Ascend 950PR/Ascend 950DT)
Each source valid row selects columns offset, offset + factor, ..., and the column number must be less than C = src.GetValidCol():
| Mode | Factor | Offset |
|---|---|---|
P0101 / P1010 | 2 | 0 / 1 |
P0001 / P0010 / P0100 / P1000 | 4 | 0 / 1 / 2 / 3 |
P1111 | 1 | 0 |
The number of selected elements per row is N = C > offset ? ceil((C - offset) / factor) : 0. The results of each row are concatenated contiguously into dst.data(), with a total of src.GetValidRow() * N elements, and no padding is inserted between rows according to DstTileData::Cols. The caller must provide sufficient contiguous physical capacity and select a valid shape that can represent this contiguous output. Nothing is written when the number of source valid rows is 0 or N == 0.
For example, when the source physical shape is [2,8] and the valid shape is [2,5] using P0101, each row selects columns 0, 2, and 4, and the output is 6 contiguous elements; a destination with physical [1,8] and valid [1,6] can hold it, with the trailing 2 padding elements unchanged.
C++ Built-in APIs
Declared in include/pto/common/pto_instr.hpp:
The public include header is
<pto/pto-inst.hpp>, and the internal declaration is located inpto/common/pto_instr.hpp.
Index-Based Gather
template <typename TileDataD, typename TileDataS0, typename TileDataS1, typename TileDataTmp, typename... WaitEvents>
PTO_INST RecordEvent TGATHER(TileDataD &dst, TileDataS0 &src0, TileDataS1 &src1, TileDataTmp &tmp, WaitEvents &... events);Mask-Based Gather
template <typename DstTileData, typename SrcTileData, MaskPattern maskPattern = MaskPattern::P1111,
auto gatherType = GatherAxis::GATHER_ROW, typename... WaitEvents>
PTO_INST RecordEvent TGATHER(DstTileData &dst, SrcTileData &src, WaitEvents &... events);Comparison-Based Gather (TGather_cmp)
Gathers the indices of elements that satisfy the comparison condition with the per-row threshold scalar.
template <typename TileDataD, typename TileDataS, typename TileDataS1, typename TileDataC, typename TileDataTmp, CmpMode cmpMode, typename... WaitEvents>
PTO_INST RecordEvent TGATHER(TileDataD &dst, TileDataS &src0, TileDataS1 &k_value, TileDataC &cdst, TileDataTmp &tmp, int offset, WaitEvents &... events);For each row i of src0, compares each element src0[i, j] with the threshold k_value[i] using cmpMode (GT or EQ). The indices of matching elements are gathered into dst[i]. The number of matches per row is stored in cdst[i]. The offset parameter specifies the starting index value.
Comparison-Based Gather Constraints
- Atlas A2/A3 training products/Atlas A2/A3 inference products:
TileDataD::DTypemust beint32_toruint32_t.TileDataS::DTypemust befloat,half, orint32_t(EQ mode only).TileDataS1::DTypemust beint32_toruint32_t.cmpModemust beCmpMode::GTorCmpMode::EQ.
- Ascend 950PR/Ascend 950DT:
TileDataD::DTypemust beint32_toruint32_t.TileDataS::DTypemust beint16_t,uint16_t,int32_t,uint32_t,half, orfloat.TileDataS1::DTypemust beuint16_toruint32_t.cmpModemust beCmpMode::GTorCmpMode::EQ.
Constraints
- Index-based gather: implementation check (Atlas A2/A3 training products/Atlas A2/A3 inference products):
sizeof(DstTileData::DType)must be 2 or 4 bytes (b16/b32).sizeof(Src1TileData::DType)must be 4 bytes (b32:int32_t,uint32_t).DstTileData::DTypemust be the same type asSrc0TileData::DType.TmpTileData::DTypemust be the same type asSrc1TileData::DType.src1.GetValidCol() == TmpTileData::Colsandsrc1.GetValidRow() == TmpTileData::Rows.dst.GetValidCol() == DstTileData::Cols(contiguous destination storage).
- Index-based gather: implementation check (Ascend 950PR/Ascend 950DT):
DstTileData::DTypemust be a 1-, 2-, 4-, or 8-byte type: one ofint8_t,uint8_t,int16_t,uint16_t,int32_t,uint32_t,int64_t,uint64_t,half,bfloat16_t,float.Src1TileData::DTypemust be one ofint16_t,uint16_t,int32_t, anduint32_t.DstTileData::DTypemust be the same type asSrc0TileData::DType.- 64-bit data requires 32-bit indices (
int32_toruint32_t). - 64-bit index gather supports RowMajor/ColMajor destination and index tiles, and accesses them according to their respective physical row/column strides; the number of valid columns can be less than the number of physical columns. The valid region of the index tile must cover the valid region of the destination.
- Mask-based gather: implementation check (Atlas A2/A3 training products/Atlas A2/A3 inference products):
- The source element size must be
2or4bytes. SrcTileData::DType/DstTileData::DTypemust be one ofint16_t,uint16_t,int32_t,uint32_t,half,bfloat16_t, andfloat.dstandsrcmust both beTileType::Vecand row-major.sizeof(dst element) == sizeof(src element)anddst.GetValidCol() == DstTileData::Cols(contiguous destination storage).
- The source element size must be
- Mask-based gather: implementation check (Ascend 950PR/Ascend 950DT):
- The source element size must be
1,2,4, or8bytes. dstandsrcmust both beTileType::Vecand row-major.SrcTileData::DType/DstTileData::DTypemust be one ofint8_t,uint8_t,int16_t,uint16_t,int32_t,uint32_t,int64_t,uint64_t,half,bfloat16_t,float,float8_e4m3_t,float8_e5m2_t, andhifloat8_t.sizeof(dst element) == sizeof(src element). The output is written as a contiguous stream; the destination capacity must be sufficient, and the destination valid shape does not truncate the write.- 64-bit mode gather supports only
GatherAxis::GATHER_ROW. For the specific output length and arrangement, see Mathematical Semantics.
- The source element size must be
- Comparison-based gather: implementation check: For the type and
cmpModeconstraints, see C++ Built-in APIs → Comparison-Based Gather Constraints. - Boundary/Validity:
- Index boundaries are not verified through explicit runtime assertions; the behavior of out-of-range indices is defined by the target.
- Temporary tile:
- Index-based gather (Atlas A2/A3 training products/Atlas A2/A3 inference products): The C++ API requires an explicit
tmptile.TileDataTmp::DTypemust be the same type asTileDataS1::DType(int32_toruint32_t).src1.GetValidRow() == TileDataTmp::Rowsandsrc1.GetValidCol() == TileDataTmp::Cols. The tmp tile is used to hold thevmulsintermediate results of the b16 source type forvgather; for the b32 source type, the results are written directly todst, but the API still requirestmp. - Index-based gather (Ascend 950PR/Ascend 950DT): The
tmptile is accepted but not used. The Ascend 950PR/Ascend 950DT hardware handles index-based gather without a temporary buffer. - Comparison-based gather (Atlas A2/A3 training products/Atlas A2/A3 inference products): The C++ API requires an explicit
tmptile, which serves as a merged scratch buffer for three internal regions:- cmpsTmp (comparison result bitmap): offset 0, stored as
uint8_t, size =TileDataTmp::Rows × TileDataTmp::Colsbytes. - indexTmp (index array): offset =
TileDataTmp::Rows × TileDataTmp::Cols × sizeof(uint8_t), stored asTileDataD::DType, size =TileDataS::Rows × TileDataS::Cols × sizeof(TileDataD::DType)bytes. - cvtTmp (converted k-value array): offset =
TileDataTmp::Rows × TileDataTmp::Cols × sizeof(uint8_t)+TileDataS::Rows × TileDataS::Cols × sizeof(TileDataD::DType), stored asTileDataS::DType, size =TileDataS::Rows × sizeof(TileDataS::DType)bytes. The minimum tmp size must satisfy: $ \\text{tmpSize} \\ge \\text{Rows}_\\text{tmp} \\times \\text{Cols}_\\text{tmp} + \\text{Rows}_\\text{src} \\times \\text{Cols}_\\text{src} \\times \\text{sizeof(DType}_\\text{dst}\\text{)} + \\text{Rows}_\\text{src} \\times \\text{sizeof(DType}\_\\text{src}\\text{)} $
- cmpsTmp (comparison result bitmap): offset 0, stored as
- Comparison-based gather (Ascend 950PR/Ascend 950DT): The
tmptile is accepted but not used. Ascend 950PR/Ascend 950DT hardware handles comparison-based gather without a temporary buffer.
- Index-based gather (Atlas A2/A3 training products/Atlas A2/A3 inference products): The C++ API requires an explicit
Examples
Automatic
#include <pto/pto-inst.hpp>
using namespace pto;
void example_auto() {
using SrcT = Tile<TileType::Vec, float, 16, 16>;
using IdxT = Tile<TileType::Vec, int32_t, 16, 16>;
using DstT = Tile<TileType::Vec, float, 16, 16>;
using TmpT = Tile<TileType::Vec, int32_t, 16, 16>;
SrcT src0;
IdxT idx;
DstT dst;
TmpT tmp;
TGATHER(dst, src0, idx, tmp);
}Manual
#include <pto/pto-inst.hpp>
using namespace pto;
void example_manual() {
using SrcT = Tile<TileType::Vec, float, 16, 16>;
using DstT = Tile<TileType::Vec, float, 1, 128>;
SrcT src;
DstT dst;
TASSIGN(src, 0x1000);
TASSIGN(dst, 0x2000);
TGATHER<DstT, SrcT, MaskPattern::P0101>(dst, src);
}