TSEL¶
指令示意图¶
简介¶
使用掩码Tile在两个Tile之间进行选择(逐元素选择)。
数学语义¶
对每个元素 (i, j) 在有效区域内:
\[
\mathrm{dst}_{i,j} =
\begin{cases}
\mathrm{src0}_{i,j} & \text{if } \mathrm{mask}_{i,j}\ \text{is true} \\
\mathrm{src1}_{i,j} & \text{otherwise}
\end{cases}
\]
汇编语法¶
同步形式:
%dst = tsel %mask, %src0, %src1 : !pto.tile<...>
AS Level 1(SSA)¶
%dst = pto.tsel %mask, %src0, %src1 : (!pto.tile<...>, !pto.tile<...>, !pto.tile<...>) -> !pto.tile<...>
AS Level 2(DPS)¶
pto.tsel ins(%mask, %src0, %src1 : !pto.tile_buf<...>, !pto.tile_buf<...>, !pto.tile_buf<...>) outs(%dst : !pto.tile_buf<...>)
C++内建接口¶
声明于 include/pto/common/pto_instr.hpp:
公共包含头为
<pto/pto-inst.hpp>,内部声明位于pto/common/pto_instr.hpp。
template <typename TileData, typename MaskTile, typename TmpTile, typename... WaitEvents>
PTO_INST RecordEvent TSEL(TileData &dst, MaskTile &selMask, TileData &src0, TileData &src1, TmpTile &tmp, WaitEvents &... events);
约束¶
- 实现检查 (Atlas A2/A3 训练系列产品/Atlas A2/A3 推理系列产品):
sizeof(TileData::DType)必须是2或4字节。TileData::DType必须是int16_t或uint16_t或int32_t或uint32_t或half或bfloat16_t或float。dst、src0和src1必须使用相同的元素类型。dst、src0和src1必须是行主序。- 选择域由
dst.GetValidRow()/dst.GetValidCol()决定。
- 实现检查 (Ascend 950PR/Ascend 950DT):
sizeof(TileData::DType)必须是1、2或4字节。TileData::DType必须是int8_t或uint8_t或int16_t或uint16_t或int32_t或uint32_t或half或bfloat16_t或float。dst、src0和src1必须使用相同的元素类型。dst、src0和src1必须是行主序。- 选择域由
dst.GetValidRow()/dst.GetValidCol()决定。
- 掩码编码:
- 掩码tile被解释为目标定义布局中的打包谓词位。
临时空间¶
Atlas A2/A3 训练系列产品/Atlas A2/A3 推理系列产品¶
tmp 被使用作为小型缓冲区,用于存放从掩码Tile复制到每行的比较掩码(cmpmask)。Atlas A2/A3 训练系列产品/Atlas A2/A3 推理系列产品实现使用 set_cmpmask,要求掩码数据位于特定的UB位置。
tmp的元素类型必须是uint32_t。tmp大小要求:每行至少cmpmaskLen个uint32_t元素,其中16位数据类型(half、bfloat16_t)的cmpmaskLen = 4(16字节,128位),32位数据类型(float、int32_t、uint32_t)的cmpmaskLen = 2(8字节,64位)。- 典型的
tmpTile声明:Tile<TileType::Vec, uint32_t, 1, 16>可满足大多数使用场景。
Ascend 950PR/Ascend 950DT¶
tmp 被接口接受但Ascend 950PR/Ascend 950DT实现不使用。Ascend 950PR/Ascend 950DT后端使用基于向量寄存器的掩码操作(plds、vsel),不需要暂存Tile存储。tmp 仅为了与Atlas A2/A3 训练系列产品/Atlas A2/A3 推理系列产品的API兼容性而保留在C++内建接口签名中。
示例¶
自动(Auto)¶
#include <pto/pto-inst.hpp>
using namespace pto;
void example_auto() {
using TileT = Tile<TileType::Vec, float, 16, 16>;
using MaskT = Tile<TileType::Vec, uint8_t, 16, 32, BLayout::RowMajor, -1, -1>;
using TmpT = Tile<TileType::Vec, uint32_t, 1, 16>;
TileT src0, src1, dst;
MaskT mask(16, 2);
TmpT tmp;
TSEL(dst, mask, src0, src1, tmp);
}
手动(Manual)¶
#include <pto/pto-inst.hpp>
using namespace pto;
void example_manual() {
using TileT = Tile<TileType::Vec, float, 16, 16>;
using MaskT = Tile<TileType::Vec, uint8_t, 16, 32, BLayout::RowMajor, -1, -1>;
using TmpT = Tile<TileType::Vec, uint32_t, 1, 16>;
TileT src0, src1, dst;
MaskT mask(16, 2);
TmpT tmp;
TASSIGN(src0, 0x1000);
TASSIGN(src1, 0x2000);
TASSIGN(dst, 0x3000);
TASSIGN(mask, 0x4000);
TASSIGN(tmp, 0x5000);
TSEL(dst, mask, src0, src1, tmp);
}
汇编示例(ASM)¶
自动模式¶
# 自动模式:由编译器/运行时负责资源放置与调度。
%dst = pto.tsel %mask, %src0, %src1 : (!pto.tile<...>, !pto.tile<...>, !pto.tile<...>) -> !pto.tile<...>
手动模式¶
# 手动模式:先显式绑定资源,再发射指令。
# 可选(当该指令包含 tile 操作数时):
# pto.tassign %arg0, @tile(0x1000)
# pto.tassign %arg1, @tile(0x2000)
%dst = pto.tsel %mask, %src0, %src1 : (!pto.tile<...>, !pto.tile<...>, !pto.tile<...>) -> !pto.tile<...>
PTO汇编形式¶
%dst = tsel %mask, %src0, %src1 : !pto.tile<...>
# AS Level 2 (DPS)
pto.tsel ins(%mask, %src0, %src1 : !pto.tile_buf<...>, !pto.tile_buf<...>, !pto.tile_buf<...>) outs(%dst : !pto.tile_buf<...>)