TGEMV

指令示意图

TGEMV tile operation

简介

通用矩阵-向量乘法,生成累加器/输出 Tile。

数学语义

设:

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

1. TGEMV(基于 Tile 的 GEMV)

对于 0 <= j < N(有效矩阵乘法域中的输出元素):

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

2. TGEMV_ACC(带累加的基于 Tile 的 GEMV)

对于 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} \]

3. TGEMV_BIAS(带偏置的基于 Tile 的 GEMV)

对于 0 <= j < N(将偏置项添加到矩阵乘积):

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

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

汇编语法

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

同步形式:

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

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

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

AS Level 1(SSA)

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

AS Level 2(DPS)

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

C++ 内建接口

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

template <typename TileRes, typename TileLeft, typename TileRight, typename... WaitEvents>
PTO_INST RecordEvent TGEMV(TileRes &cMatrix, TileLeft &aMatrix, TileRight &bMatrix, WaitEvents&... events);

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 <typename TileRes, typename TileLeft, typename TileRight, typename TileBias, typename... WaitEvents>
PTO_INST RecordEvent TGEMV_BIAS(TileRes &cMatrix, TileLeft &aMatrix, TileRight &bMatrix, TileBias &biasData, WaitEvents&... events);

约束

通用形状与位置约束

以下约束在未特别说明时同时适用于 TGEMVTGEMV_ACCTGEMV_BIAS

  • 静态形状约束:
    • 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]

TGEMV / TGEMV_ACC 数据类型约束

  • 实现检查 (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

TGEMV_BIAS 的附加约束

  • 偏置 tile 的数据类型必须与 TileRes::DType 完全一致。
  • 偏置 tile 必须配置为单行。
  • 偏置 tile 的位置必须为 TileType::Bias
  • A5 附加说明
    • 除上述 GEMV 约定外,底层 A5 matmul 实现不会再单独补充一组显式的 m/k/n 运行时断言。

示例

自动(Auto)

1. TGEMV

#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 c;
  TGEMV(c, a, b);
}

2. TGEMV_ACC

#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);
}

3. TGEMV_BIAS

#include <pto/pto-inst.hpp>

using namespace pto;

void example_auto() {
  using A = TileLeft<half, 1, 16>;
  using B = TileRight<half, 16, 16>;
  using Bias = Tile<TileType::Bias, half, 1, 16>;
  using C = TileAcc<float, 1, 16>;
  A a;
  B b;
  Bias bias;
  C c;
  TGEMV_BIAS(c, a, b, bias);
}

手动(Manual)

1. TGEMV

#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 c;
  TASSIGN(a, 0x1000);
  TASSIGN(b, 0x2000);
  TASSIGN(c, 0x3000);
  TGEMV(c, a, b);
}

2. TGEMV_ACC

#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);
}

3. TGEMV_BIAS

#include <pto/pto-inst.hpp>

using namespace pto;

void example_manual() {
  using A = TileLeft<half, 1, 16>;
  using B = TileRight<half, 16, 16>;
  using Bias = Tile<TileType::Bias, half, 1, 16>;
  using C = TileAcc<float, 1, 16>;
  A a;
  B b;
  Bias bias;
  C c;
  TASSIGN(a, 0x1000);
  TASSIGN(b, 0x2000);
  TASSIGN(bias, 0x3000);
  TASSIGN(c, 0x4000);
  TGEMV_BIAS(c, a, b, bias);
}

汇编示例(ASM)

自动模式

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

手动模式

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

PTO 汇编形式

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