TEXTRACT

指令示意图

TEXTRACT tile operation

简介

从较大的源Tile中提取较小的子Tile。

数学语义

概念上从较大的 src Tile中,以 (indexRow, indexCol) 为起点复制一个较小窗口到 dst。确切的映射取决于tile布局。

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

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

汇编语法

同步形式:

%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

公共包含头为 <pto/pto-inst.hpp>,内部声明位于 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);

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

约束

通用约束或检查

  • DstTileData::DType 必须等于 SrcTileData::DType
  • 运行时边界检查:
    • indexRow + DstTileData::Rows <= SrcTileData::Rows
    • indexCol + DstTileData::Cols <= SrcTileData::Cols

Atlas A2/A3 训练系列产品/Atlas A2/A3 推理系列产品实现检查

  • 支持的元素类型:int8_thalfbfloat16_tfloat
  • 源布局必须满足以下已检查到的Atlas A2/A3 训练系列产品/Atlas A2/A3 推理系列产品提取布局之一:
    • (SFractal == ColMajor && isRowMajor),或
    • (SFractal == RowMajor && !isRowMajor)
  • 在以 TileType::Left 为目标的GEMV场景中,已检查到的源布局还允许 (SrcTileData::Rows == 1 && SrcTileData::isRowMajor)
  • 目标必须是 TileType::LeftTileType::Right,并具有目标支持的布局配置。

Ascend 950PR/Ascend 950DT实现检查

  • 支持的元素类型:int8_thifloat8_tfloat8_e5m2_tfloat8_e4m3_thalfbfloat16_tfloatfloat4_e2m1x2_tfloat4_e1m2x2_tfloat8_e8m0_t
  • 源布局必须满足以下已检查到的Ascend 950PR/Ascend 950DT提取布局之一:
    • 对于 Left / Right(SFractal == ColMajor && isRowMajor)(SFractal == RowMajor && !isRowMajor)
    • 对于 ScaleLeft(SFractal == RowMajor && isRowMajor)
    • 对于 ScaleRight(SFractal == ColMajor && !isRowMajor)
  • 在以 Left 为目标的GEMV场景中,已检查到的源布局还允许 (SrcTileData::Rows == 1 && SrcTileData::isRowMajor)
  • 目标支持 TileType::Mat -> TileType::Left/Right/ScaleTileType::Acc -> TileType::Mat(含relu、标量量化、向量量化形式)、TileType::Acc -> TileType::Vec,以及特定的 TileType::Vec -> TileType::Mat 提取路径。
  • 向量量化形式额外要求提供 FpTileData 缩放操作数,对应 TEXTRACT_FP(...) 接口。
  • 对于 TileType::Acc -> TileType::Vec,当目标为32位类型(float/int32_t)且使用 DualModeSplitN 时,切分前的 ValidCol 必须是 32 的整数倍。

Vec → Vec 抽取路径

除上述 Mat/Acc -> ... 路径外,TEXTRACT 还支持 TileType::Vec -> TileType::Vec 抽取路径(ND 与 NZ 布局),由 CheckTExtractVecToVecCommon 强制:

  • DstTileData::DType 必须等于 SrcTileData::DType
  • 支持的元素类型(A2A3 与 A5 均同):int8_tuint8_tint16_tuint16_tint32_tuint32_thalfbfloat16_tfloat(任意 1/2/4 字节标准类型)。该集合与主 tile 路径不同:新增 uint8_t/int16_t/uint16_t/int32_t/uint32_t,且在 A5 上不含 fp8/fp4 类型。
  • ND 路径:源/目标行步进须 32 字节对齐;Dst 行/列不得超过 Src

示例

自动(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<...>)