TCMPS
Instruction Diagram
Introduction
Compares a Tile with a scalar or another Tile, and writes the element-wise comparison result. The Tile form has the following backend differences in behavior.
Provides two overload forms:
- Scalar form: Compares each element of
src0with a scalar value. - Tile form: A2/A3 compares each element of
src0with the scalar broadcast from the first element of thesrc1Tile; A5 and CPU_SIM compare the correspondingsrc0[i,j]andsrc1[i,j]element by element.
Mathematical Semantics
Scalar form: for each element (i, j) in the valid region:
Tile form on A2/A3: for each element (i, j) in the valid region:
Tile form on A5 and CPU_SIM: for each element (i, j) in the valid region:
The encoding/type of dst is implementation-defined (a bit-packed mask tile, where each bit represents a comparison result).
C++ Built-in APIs
Declared in include/pto/common/pto_instr.hpp and include/pto/common/type.hpp:
The public include header is
<pto/pto-inst.hpp>, and the internal declaration is located inpto/common/pto_instr.hpp.
Scalar form: compares a tile with a scalar:
template <typename TileDataDst, typename TileDataSrc, typename... WaitEvents,
std::enable_if_t<all_events_v<WaitEvents...>, int> = 0>
PTO_INST RecordEvent TCMPS(TileDataDst& dst, TileDataSrc& src0,
typename TileDataSrc::DType scalar, CmpMode mode,
WaitEvents&... events);Tile form: compares a Tile with another Tile (see the backend behavior above):
template <typename TileDataDst, typename TileDataSrc0, typename TileDataSrc1,
typename... WaitEvents,
std::enable_if_t<is_tile_data_v<TileDataSrc1> && all_events_v<WaitEvents...>, int> = 0>
PTO_INST RecordEvent TCMPS(TileDataDst& dst, TileDataSrc0& src0,
TileDataSrc1& src1, CmpMode mode,
WaitEvents&... events);Constraints
Implementation check (Atlas A2/A3 training products/Atlas A2/A3 inference products):
TileData::DTypemust be one of the following:int32_t,float,half,uint16_t,int16_t.- The tile layout must be row-major (
TileData::isRowMajor). - When the input type is
int32_t, onlyCmpMode::EQis supported; other comparison modes fall back toEQ.
Implementation check (Ascend 950PR/Ascend 950DT):
TileData::DTypemust be one of the following:int32_t,uint32_t,int64_t,uint64_t,float,int16_t,uint16_t,half,uint8_t,int8_t,bfloat16_t.- The tile layout must be row-major (
TileData::isRowMajor).
General constraints:
- The tile positions of both
srcanddstmust be vectors (TileData::Loc == TileType::Vec). - Static valid boundary:
TileData::ValidRow <= TileData::RowsandTileData::ValidCol <= TileData::Cols. - Data types:
src0andsrc1must have the same data type. - The tile form of A5 and CPU_SIM compares
src0withsrc1element by element; A2/A3 broadcastssrc1[0,0].
- The tile positions of both
Backend shape check:
- A2/A3 and A5 assert that
src0anddsthave the same number of valid rows, but do not explicitly check the number of valid columns. - CPU_SIM does not assert that the valid shapes are the same. It computes over the valid region of
src0, limits the bit-packed result writes by the valid shape ofdst, and requires the Tile form ofsrc1to provide readable elements at the corresponding positions.
- A2/A3 and A5 assert that
Valid region:
- This operation uses
src0.GetValidRow()/src0.GetValidCol()as the iteration domain.
- This operation uses
Mask encoding:
- 64-bit inputs (Ascend 950PR/Ascend 950DT): the comparison result of column
jis stored in bitj % 8of bytej / 8of that row, least significant bit first. - For a
uint8_tmask, the valid shape can be set to[R, ceil(C / 8)], with the physicalColsaligned to 32 bytes, where[R,C]is the source valid shape. Row addresses are computed by the destination physical stride; the padding values after the last valid bit are unspecified.
- 64-bit inputs (Ascend 950PR/Ascend 950DT): the comparison result of column
Comparison mode:
- Supports
CmpMode::EQ,CmpMode::NE,CmpMode::LT,CmpMode::GT,CmpMode::LE, andCmpMode::GE(note: on Atlas A2/A3 training products/Atlas A2/A3 inference products, when the input type isint32_t, onlyCmpMode::EQis supported, and other modes fall back toEQ; Ascend 950PR/Ascend 950DT support all modes).
- Supports
Examples
Automatic
#include <pto/pto-inst.hpp>
using namespace pto;
void example_auto() {
using SrcT = Tile<TileType::Vec, float, 16, 16>;
using DstT = Tile<TileType::Vec, uint8_t, 16, 32, BLayout::RowMajor, -1, -1>;
SrcT src;
DstT dst(16, 2);
TCMPS(dst, src, 0.0f, CmpMode::GT);
}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, uint8_t, 16, 32, BLayout::RowMajor, -1, -1>;
SrcT src;
DstT dst(16, 2);
TASSIGN(src, 0x1000);
TASSIGN(dst, 0x2000);
TCMPS(dst, src, 0.0f, CmpMode::GT);
}Tile Form (Compared with Another Tile)
#include <pto/pto-inst.hpp>
using namespace pto;
void example_tile() {
using SrcT = Tile<TileType::Vec, float, 16, 16>;
using DstT = Tile<TileType::Vec, uint8_t, 16, 32, BLayout::RowMajor, -1, -1>;
SrcT src0, src1;
DstT dst(16, 2);
// A2/A3: broadcast src1[0,0]; A5 and CPU_SIM: compare elements at corresponding positions.
TCMPS(dst, src0, src1, CmpMode::GE);
}