TEXTRACT

指令示意图

TEXTRACT tile operation

简介

从源 Tile 中提取子 Tile。

数学语义

概念上从 src 中复制从 (indexRow, indexCol) 开始的窗口到 dst。确切的映射取决于布局。

R = dst.GetValidRow()C = dst.GetValidCol()。对于 0 <= i < R0 <= j < C

\[ \mathrm{dst}_{i,j} = \mathrm{src}_{\mathrm{indexRow}+i,\; \mathrm{indexCol}+j} \]

汇编语法

PTO-AS 形式:参见 PTO-AS 规范

同步形式:

%dst = textract %src[%r0, %r1] : !pto.tile<...> -> !pto.tile<...>

AS Level 1(SSA)

%dst = pto.textract %src, %idxrow, %idxcol : (!pto.tile<...>, dtype, dtype) -> !pto.tile<...>

AS Level 2(DPS)

pto.textract ins(%src, %idxrow, %idxcol : !pto.tile_buf<...>, dtype, dtype) outs(%dst : !pto.tile_buf<...>)

C++ 内建接口

声明于 include/pto/common/pto_instr.hpp

template <typename DstTileData, typename SrcTileData, typename... WaitEvents>
PTO_INST RecordEvent TEXTRACT(DstTileData &dst, SrcTileData &src, uint16_t indexRow = 0, uint16_t indexCol = 0, WaitEvents &... events);

template <typename DstTileData, typename SrcTileData, ReluPreMode reluMode, typename... WaitEvents>
PTO_INST RecordEvent TEXTRACT(DstTileData &dst, SrcTileData &src, uint16_t indexRow, uint16_t indexCol, WaitEvents &... events);

template <typename DstTileData, typename SrcTileData, ReluPreMode reluMode = ReluPreMode::NoRelu,
          typename... WaitEvents>
PTO_INST RecordEvent TEXTRACT(DstTileData &dst, SrcTileData &src, uint64_t preQuantScalar, uint16_t indexRow, uint16_t indexCol, WaitEvents &... events);

约束

  • 实现检查 (A2A3):
    • DstTileData::DType 必须等于 SrcTileData::DType 且必须是以下之一:int8_thalfbfloat16_tfloat
    • 源分形必须满足:(SFractal == ColMajor && isRowMajor)(SFractal == RowMajor && !isRowMajor),GEMV场景中,目标为Left时,源分形满足(SrcTileData::Rows == 1 && SrcTileData::isRowMajor)
    • 运行时边界检查:
    • indexRow + DstTileData::Rows <= SrcTileData::Rows
    • indexCol + DstTileData::Cols <= SrcTileData::Cols
    • 目标必须是 TileType::LeftTileType::Right,具有目标支持的分形配置。
  • 实现检查 (A5):
    • DstTileData::DType 必须等于 SrcTileData::DType 且必须是以下之一:int8_thifloat8_tfloat8_e5m2_tfloat8_e4m3_thalfbfloat16_tfloatfloat4_e2m1x2_tfloat4_e1m2x2_tfloat8_e8m0_t
    • 源分形必须满足:对于 Left/Right 为 (SFractal == ColMajor && isRowMajor)(SFractal == RowMajor && !isRowMajor),GEMV场景中,目标为Left时,源分形满足(SrcTileData::Rows == 1 && SrcTileData::isRowMajor);对于 ScaleLeft 为 (SFractal == RowMajor && isRowMajor),对于 ScaleRight 为 (SFractal == ColMajor && !isRowMajor)
    • 目标支持 Mat -> Left/Right/Scale,也支持特定 tile 位置的 Vec -> Mat(此目标的 TEXTRACT_IMPL 中不强制执行显式运行时边界断言)。

示例

自动(Auto)

#include <pto/pto-inst.hpp>

using namespace pto;

void example_auto() {
  using SrcT = Tile<TileType::Mat, float, 16, 16, BLayout::RowMajor, 16, 16, SLayout::ColMajor>;
  using DstT = TileLeft<float, 16, 16>;
  SrcT src;
  DstT dst;
  TEXTRACT(dst, src, /*indexRow=*/0, /*indexCol=*/0);
}

手动(Manual)

#include <pto/pto-inst.hpp>

using namespace pto;

void example_manual() {
  using SrcT = Tile<TileType::Mat, float, 16, 16, BLayout::RowMajor, 16, 16, SLayout::ColMajor>;
  using DstT = TileLeft<float, 16, 16>;
  SrcT src;
  DstT dst;
  TASSIGN(src, 0x1000);
  TASSIGN(dst, 0x2000);
  TEXTRACT(dst, src, /*indexRow=*/0, /*indexCol=*/0);
}

汇编示例(ASM)

自动模式

# 自动模式:由编译器/运行时负责资源放置与调度。
%dst = pto.textract %src, %idxrow, %idxcol : (!pto.tile<...>, dtype, dtype) -> !pto.tile<...>

手动模式

# 手动模式:先显式绑定资源,再发射指令。
# 可选(当该指令包含 tile 操作数时):
# pto.tassign %arg0, @tile(0x1000)
# pto.tassign %arg1, @tile(0x2000)
%dst = pto.textract %src, %idxrow, %idxcol : (!pto.tile<...>, dtype, dtype) -> !pto.tile<...>

PTO 汇编形式

%dst = textract %src[%r0, %r1] : !pto.tile<...> -> !pto.tile<...>
# AS Level 2 (DPS)
pto.textract ins(%src, %idxrow, %idxcol : !pto.tile_buf<...>, dtype, dtype) outs(%dst : !pto.tile_buf<...>)