TGEMV_ACC

指令示意图

TGEMV_ACC tile operation

简介

带显式累加器输入 Tile(cInMatrix)和输出 Tile(cOutMatrix)的 GEMV。

另请参见

  • 基础 GEMV 指令:docs/isa/TGEMV.md
  • 偏置变体:docs/isa/TGEMV_BIAS.md

C++ 内建接口

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

template <typename TileRes, typename TileLeft, typename TileRight, typename... WaitEvents>
PTO_INST RecordEvent TGEMV_ACC(TileRes &cOutMatrix, TileRes &cInMatrix, TileLeft &aMatrix, TileRight &bMatrix, WaitEvents &... events);

template <AccPhase Phase, typename TileRes, typename TileLeft, typename TileRight, typename... WaitEvents>
PTO_INST RecordEvent TGEMV_ACC(TileRes &cOutMatrix, TileRes &cInMatrix, TileLeft &aMatrix, TileRight &bMatrix, WaitEvents &... events);

数学语义

设:

  • M = 1
  • K = bMatrix.GetValidRow()
  • N = bMatrix.GetValidCol()

对于 0 <= j < N(累加到已有输出 Tile):

\[ \mathrm{C}_{0,j} \gets \mathrm{C}_{0,j} + \sum_{k=0}^{K-1} \mathrm{A}_{0,k} \cdot \mathrm{B}_{k,j} \]

注意: 精确的累加器行为和数据类型提升由目标/实现定义。

汇编语法

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

同步形式:

%acc1 = tgemv.acc %acc0, %a, %b : (!pto.tile<...>, !pto.tile<...>, !pto.tile<...>) -> !pto.tile<...>

AS Level 1(SSA)

%c_out = pto.tgemv.acc %c_in, %a, %b : (!pto.tile<...>, !pto.tile<...>, !pto.tile<...>) -> !pto.tile<...>

AS Level 2(DPS)

pto.tgemv.acc ins(%c_in, %a, %b : !pto.tile_buf<...>, !pto.tile_buf<...>, !pto.tile_buf<...>) outs(%c_out : !pto.tile_buf<...>)

约束

通用形状与位置约束

  • 静态形状约束:
    • TileLeft::Rows == TileRes::Rows
    • TileLeft::Cols == TileRight::Rows
    • TileRight::Cols == TileRes::Cols
  • Tile 位置约束:
    • TileLeft::Loc == Left
    • TileRight::Loc == Right
    • TileRes::Loc == Acc
  • 运行时有效尺寸约束:
    • m 必须为 1
    • kn(取自 bMatrix.GetValidRow()bMatrix.GetValidCol())必须位于 [1, 4095]

数据类型约束

  • 实现检查 (A2A3):
    • 支持的 (CType, AType, BType) 三元组:
      • (int32_t, int8_t, int8_t)
      • (float, half, half)
      • (float, float, float)
      • (float, bfloat16_t, bfloat16_t)
  • 实现检查 (A5):
    • 累加器类型必须是 int32_tfloat
    • 如果为 int32_tAType == int8_tBType == int8_t
    • 如果为 float:支持 halfbfloat16_tfloat 以及选定的 fp8 组合(目标定义)。
    • 会强制执行以下分形/布局约束:
      • Left:Loc == Left!isRowMajorSFractal == RowMajor
      • Right:Loc == RightisRowMajorSFractal == ColMajor
      • Acc:Loc == Acc!isRowMajorSFractal == RowMajor
    • 除上述 GEMV 约定外,底层 A5 matmul 实现不会再单独补充一组显式的 m/k/n 运行时断言。

示例

自动(Auto)

#include <pto/pto-inst.hpp>

using namespace pto;

void example_auto() {
  using A = TileLeft<half, 1, 16>;
  using B = TileRight<half, 16, 16>;
  using C = TileAcc<float, 1, 16>;
  A a;
  B b;
  C c0, c1;
  TGEMV_ACC(c1, c0, a, b);
}

手动(Manual)

#include <pto/pto-inst.hpp>

using namespace pto;

void example_manual() {
  using A = TileLeft<half, 1, 16>;
  using B = TileRight<half, 16, 16>;
  using C = TileAcc<float, 1, 16>;
  A a;
  B b;
  C c0, c1;
  TASSIGN(a, 0x1000);
  TASSIGN(b, 0x2000);
  TASSIGN(c0, 0x3000);
  TASSIGN(c1, 0x4000);
  TGEMV_ACC(c1, c0, a, b);
}

汇编示例(ASM)

自动模式

# 自动模式:由编译器/运行时负责资源放置与调度。
%c_out = pto.tgemv.acc %c_in, %a, %b : (!pto.tile<...>, !pto.tile<...>, !pto.tile<...>) -> !pto.tile<...>

手动模式

# 手动模式:先显式绑定资源,再发射指令。
# 可选(当该指令包含 tile 操作数时):
# pto.tassign %arg0, @tile(0x1000)
# pto.tassign %arg1, @tile(0x2000)
%c_out = pto.tgemv.acc %c_in, %a, %b : (!pto.tile<...>, !pto.tile<...>, !pto.tile<...>) -> !pto.tile<...>

PTO 汇编形式

%acc1 = tgemv.acc %acc0, %a, %b : (!pto.tile<...>, !pto.tile<...>, !pto.tile<...>) -> !pto.tile<...>
# AS Level 2 (DPS)
pto.tgemv.acc ins(%c_in, %a, %b : !pto.tile_buf<...>, !pto.tile_buf<...>, !pto.tile_buf<...>) outs(%c_out : !pto.tile_buf<...>)