TGEMV¶
指令示意图¶
简介¶
通用矩阵-向量乘法,生成累加器/输出 Tile。
数学语义¶
设:
M = 1K = 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);
约束¶
- 实现检查 (A2A3):
- 支持的
(CType, AType, BType)三元组: (int32_t, int8_t, int8_t)(float, half, half)(float, float, float)(float, bfloat16_t, bfloat16_t)- 静态形状约束:
TileLeft::Rows == TileRes::Rows、TileLeft::Cols == TileRight::Rows、TileRight::Cols == TileRes::Cols。 - Tile 位置:
TileLeft::Loc == Left、TileRight::Loc == Right、TileRes::Loc == Acc。 - 运行时:
m必须为 1;k/n(取自bMatrix.GetValidRow()、bMatrix.GetValidCol())必须在[1, 4095]范围内。 - 偏置检查:
- 偏置 tile 的数据类型
TileBias::DType必须与结果 tile 的数据类型(TileRes::DType)完全匹配。 - 偏置 tile 必须配置为单行。
- 偏置 tile 的位置必须是
TileBias::Loc == TileType::Bias。
- 支持的
- 实现检查 (A5):
- 累加器类型必须是
int32_t或float。 - 如果是
int32_t:AType == int8_t且BType == int8_t。 - 如果是
float:支持half/bfloat16_t/float和选定的 fp8 对(目标定义)。 - 静态形状约束:
TileLeft::Rows == TileRes::Rows、TileLeft::Cols == TileRight::Rows、TileRight::Cols == TileRes::Cols。 - 强制执行分形/布局约束:
- Left:
Loc == Left、!isRowMajor、SFractal == RowMajor - Right:
Loc == Right、isRowMajor、SFractal == ColMajor - Acc:
Loc == Acc、!isRowMajor、SFractal == RowMajor - 此目标的
TMATMUL_IMPL中不强制执行对m/k/n的显式运行时范围检查。 - 运行时:
m必须为 1;k/n(取自bMatrix.GetValidRow()、bMatrix.GetValidCol())必须在[1, 4095]范围内。 - 偏置检查:
- 偏置 tile 的数据类型
TileBias::DType必须与结果 tile 的数据类型(TileRes::DType)完全匹配。 - 偏置 tile 必须配置为单行。
- 偏置 tile 的位置必须是
TileBias::Loc == TileType::Bias。
- 累加器类型必须是
示例¶
自动(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(c, 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<...>)