TMATMUL¶
指令示意图¶
简介¶
矩阵乘法 (GEMM),生成累加器/输出 Tile。
数学语义¶
Let:
M = aMatrix.GetValidRow()K = aMatrix.GetValidCol()N = bMatrix.GetValidCol()
For 0 <= i < M and 0 <= j < N (output elements in the effective matmul domain):
\[ \mathrm{C}_{i,j} = \sum_{k=0}^{K-1} \mathrm{A}_{i,k} \cdot \mathrm{B}_{k,j} \]
Exact accumulator behavior and datatype promotion are target/implementation-defined.
汇编语法¶
PTO-AS 形式:参见 PTO-AS Specification.
同步形式:
%acc = tmatmul %a, %b : (!pto.tile<...>, !pto.tile<...>) -> !pto.tile<...>
AS Level 1 (SSA)¶
%c = pto.tmatmul %a, %b : (!pto.tile<...>, !pto.tile<...>) -> !pto.tile<...>
AS Level 2 (DPS)¶
pto.tmatmul ins(%a, %b : !pto.tile_buf<...>, !pto.tile_buf<...>) outs(%c : !pto.tile_buf<...>)
AS Level 1(SSA)¶
%c = pto.tmatmul %a, %b : (!pto.tile<...>, !pto.tile<...>) -> !pto.tile<...>
AS Level 2(DPS)¶
pto.tmatmul ins(%a, %b : !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 TMATMUL(TileRes &cMatrix, TileLeft &aMatrix, TileRight &bMatrix, WaitEvents &... events);
template <AccPhase Phase, typename TileRes, typename TileLeft, typename TileRight, typename... WaitEvents>
PTO_INST RecordEvent TMATMUL(TileRes &cMatrix, TileLeft &aMatrix, TileRight &bMatrix, WaitEvents &... events);
约束¶
- 实现检查 (A2A3):
- Supported
(CType, AType, BType)triples: (int32_t, int8_t, int8_t)(float, half, half)(float, float, float)(float, bfloat16_t, bfloat16_t)- Static shape constraints:
TileLeft::Rows == TileRes::Rows,TileLeft::Cols == TileRight::Rows,TileRight::Cols == TileRes::Cols. - Tile locations:
TileLeft::Loc == Left,TileRight::Loc == Right,TileRes::Loc == Acc. - Runtime:
m/k/n(taken fromaMatrix.GetValidRow(),aMatrix.GetValidCol(),bMatrix.GetValidCol()) must be in[1, 4095].
- Supported
- 实现检查 (A5):
- Accumulator type must be
int32_torfloat. - If
int32_t:AType == int8_tandBType == int8_t. - If
float: supportshalf/bfloat16_t/floatand selected fp8 pairs (target-defined). - Static shape constraints:
TileLeft::Rows == TileRes::Rows,TileLeft::Cols == TileRight::Rows,TileRight::Cols == TileRes::Cols. - Fractal/layout constraints are enforced:
- Left:
Loc == Left,!isRowMajor,SFractal == RowMajor - Right:
Loc == Right,isRowMajor,SFractal == ColMajor - Acc:
Loc == Acc,!isRowMajor,SFractal == RowMajor - Runtime:
m/k/n(taken fromaMatrix.GetValidRow(),aMatrix.GetValidCol(),bMatrix.GetValidCol()) must be in[1, 4095].
- Accumulator type must be
示例¶
自动(Auto)¶
#include <pto/pto-inst.hpp>
using namespace pto;
void example_auto() {
using A = TileLeft<half, 16, 16>;
using B = TileRight<half, 16, 16>;
using C = TileAcc<float, 16, 16>;
A a;
B b;
C c;
TMATMUL(c, a, b);
}
手动(Manual)¶
#include <pto/pto-inst.hpp>
using namespace pto;
void example_manual() {
using A = TileLeft<half, 16, 16>;
using B = TileRight<half, 16, 16>;
using C = TileAcc<float, 16, 16>;
A a;
B b;
C c;
TASSIGN(a, 0x1000);
TASSIGN(b, 0x2000);
TASSIGN(c, 0x3000);
TMATMUL(c, a, b);
}