|
产品 |
MatmulPolicy |
TrianUpperMatmulPolicy/TrianLowerMatmulPolicy |
NBuffer33MatmulPolicy |
MatmulWithScalePolicy/SplitMMatmulPolicy/SplitNMatmulPolicy |
|---|---|---|---|---|
|
Ascend 950PR/Ascend 950DT |
√ |
√ |
√ |
√ |
|
|
√ |
√ |
√ |
x |
|
|
√ |
√ |
√ |
x |
|
|
√ |
x |
x |
x |
|
|
√ |
x |
x |
x |
|
|
x |
x |
x |
x |
|
|
x |
x |
x |
x |
一次矩阵乘指令计算的结果为baseM * baseN大小的矩阵块,称该矩阵块为基本块。若Matmul结果矩阵C中的基本块位于下三角位置,则Matmul内部做数据计算和数据搬出时,将不对该基本块进行处理,最后得到的矩阵C为一个上三角矩阵。上三角模板策略如下图所示,图示中矩阵形状的相关大小为M=N=512,K=256,baseM=baseN=baseK=32。
一次矩阵乘指令计算的结果为baseM * baseN大小的矩阵块,称该矩阵块为基本块。若Matmul结果矩阵C中的基本块位于上三角位置,则Matmul内部做数据计算和数据搬出时,将不对该基本块进行处理,最后得到的矩阵C为一个下三角矩阵。下三角模板策略如下图所示,图示中矩阵形状的相关大小为M=N=512,K=256,baseM=baseN=baseK=32。
一次矩阵乘指令计算的结果为baseM * baseN大小的矩阵块,称该矩阵块为基本块。单核计算的A矩阵切分为3x3个基本块,该3x3个A矩阵的基本块全载和保持在L1 Buffer中,每次与3x1个B矩阵的基本块计算矩阵乘,同时DoubleBuffer并行搬入下次计算所需的3x1个B矩阵基本块,直到singleCoreN方向的矩阵乘计算完成。NBuffer33模板策略如下图所示,图中singleCoreM、singleCoreN、singleCoreK表示单核内A、B矩阵的shape大小,单核计算的A矩阵切分为3x3个基本块,3x3个基本块全载在L1 Buffer上,这些基本块每次与B矩阵的3x1个基本块计算矩阵乘。
Matmul一次Iterate的计算结果从L0C Buffer搬到Unified Buffer时,采用双输出模式,即在分离模式下,AIC核与AIV核的核数比为1:2时,在调用GetTensorC接口后,Matmul一次Iterate的计算结果在矩阵的M方向一分为二,将被切分后的两块结果数据分别搬运到两个AIV核的Unified Buffer。模板策略示意图如下所示。
Matmul一次Iterate的计算结果从L0C Buffer搬到Unified Buffer时,采用双输出模式,即在分离模式下,AIC核与AIV核的核数比为1:2时,在调用GetTensorC接口后,Matmul一次Iterate的计算结果在矩阵的N方向一分为二,将被切分后的两块结果数据分别搬运到两个AIV核的Unified Buffer。模板策略示意图如下所示。
默认模板策略MatmulPolicy为模板参数的默认值,下面主要介绍TrianUpperMatmulPolicy(上三角模板策略)和TrianLowerMatmulPolicy(下三角模板策略)的使用方式。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
#include "lib/matmul_intf.h" typedef AscendC::MatmulType<AscendC::TPosition::GM, CubeFormat::ND, half> aType; typedef AscendC::MatmulType<AscendC::TPosition::GM, CubeFormat::ND, half> bType; typedef AscendC::MatmulType<AscendC::TPosition::GM, CubeFormat::ND, float> cType; typedef AscendC::MatmulType<AscendC::TPosition::GM, CubeFormat::ND, float> biasType; // Matmul定义时传入TrianUpperMatmulPolicy AscendC::Matmul<aType, bType, cType, biasType, CFG_NORM, MatmulCallBackFunc<nullptr, nullptr, nullptr>, AscendC::Impl::Detail::TrianUpperMatmulPolicy> mm; // 常规Matmul计算,最后输出上三角形式的结果 TPipe pipe; TCubeTiling tiling; REGIST_MATMUL_OBJ(&pipe, GetSysWorkSpacePtr(), mm, &tiling); mm.SetTensorA(gmA, isTransposeA); mm.SetTensorB(gmB, isTransposeB); if (tiling.isBias) { mm.SetBias(gmBias); } mm.IterateAll(gmC); mm.End(); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
#include "lib/matmul_intf.h" typedef AscendC::MatmulType<AscendC::TPosition::GM, CubeFormat::ND, half> aType; typedef AscendC::MatmulType<AscendC::TPosition::GM, CubeFormat::ND, half> bType; typedef AscendC::MatmulType<AscendC::TPosition::GM, CubeFormat::ND, float> cType; typedef AscendC::MatmulType<AscendC::TPosition::GM, CubeFormat::ND, float> biasType; // Matmul定义时传入TrianLowerMatmulPolicy AscendC::Matmul<aType, bType, cType, biasType, CFG_NORM, MatmulCallBackFunc<nullptr, nullptr, nullptr>, AscendC::Impl::Detail::TrianLowerMatmulPolicy> mm; // 常规Matmul计算,最后输出下三角形式的结果 TPipe pipe; TCubeTiling tiling; REGIST_MATMUL_OBJ(&pipe, GetSysWorkSpacePtr(), mm, &tiling); mm.SetTensorA(gmA, isTransposeA); mm.SetTensorB(gmB, isTransposeB); if (tiling.isBias) { mm.SetBias(gmBias); } mm.IterateAll(gmC); mm.End(); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
#define ASCENDC_CUBE_ONLY #include "lib/matmul_intf.h" typedef AscendC::MatmulType<AscendC::TPosition::GM, CubeFormat::ND, half> aType; typedef AscendC::MatmulType<AscendC::TPosition::GM, CubeFormat::ND, half> bType; typedef AscendC::MatmulType<AscendC::TPosition::GM, CubeFormat::ND, float> cType; typedef AscendC::MatmulType<AscendC::TPosition::GM, CubeFormat::ND, float> biasType; // Matmul定义时传入NBuffer33MatmulPolicy AscendC::Matmul<aType, bType, cType, biasType, CFG_MDL, MatmulCallBackFunc<nullptr, nullptr, nullptr>, AscendC::Impl::Detail::NBuffer33MatmulPolicy> mm; // 使用NBuffer33逻辑进行Matmul计算,最后输结果 TPipe pipe; TCubeTiling tiling; REGIST_MATMUL_OBJ(&pipe, GetSysWorkSpacePtr(), mm, &tiling); mm.SetTensorA(gmA, isTransposeA); mm.SetTensorB(gmB, isTransposeB); if (tiling.isBias) { mm.SetBias(gmBias); } mm.IterateAll(gmC); mm.End(); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
#include "lib/matmul_intf.h" typedef MatmulTypeWithScale<AscendC::TPosition::GM, AscendC::TPosition::GM, CubeFormat::ND, AType, isTransposeA> aType; typedef MatmulTypeWithScale<AscendC::TPosition::GM, AscendC::TPosition::GM, CubeFormat::ND, BType, isTransposeB> bType; typedef AscendC::MatmulType<AscendC::TPosition::GM, CubeFormat::ND, float> cType; typedef AscendC::MatmulType<AscendC::TPosition::GM, CubeFormat::ND, float> biasType; // Matmul定义时传入MatmulWithScalePolicy AscendC::Matmul<aType, bType, cType, biasType, CFG_NORM, MatmulCallBackFunc<nullptr, nullptr, nullptr>, AscendC::Impl::Detail::MatmulWithScalePolicy> mm; // MxMatmul计算逻辑,最后输出结果 TPipe pipe; TCubeTiling tiling; REGIST_MATMUL_OBJ(&pipe, GetSysWorkSpacePtr(), mm, &tiling); mm.SetTensorA(gmA, isTransposeA); mm.SetTensorB(gmB, isTransposeB); mm.SetTensorScaleA(gm_scaleA, isTransposeScaleA); mm.SetTensorScaleB(gm_scaleB, isTransposeScaleB); if (tiling.isBias) { mm.SetBias(gmBias); } mm.IterateAll(gmC); mm.End(); |
#include "lib/matmul_intf.h"
typedef AscendC::MatmulType<AscendC::TPosition::GM, CubeFormat::ND, half, LayoutMode::NONE, true> aType;
typedef AscendC::MatmulType<AscendC::TPosition::GM, CubeFormat::ND, half, LayoutMode::NONE, true> bType;
typedef AscendC::MatmulType<AscendC::TPosition::VECCALC, CubeFormat::ND, float> cType;
typedef AscendC::MatmulType<AscendC::TPosition::GM, CubeFormat::ND, float> biasType;
// Matmul定义时传入SplitMMatmulPolicy
AscendC::Matmul<aType, bType, cType, biasType, CFG_NORM, MatmulCallBackFunc<nullptr, nullptr, nullptr>, AscendC::Impl::Detail::SplitMMatmulPolicy> mm;
// Matmul计算
TPipe pipe;
TCubeTiling tiling;
REGIST_MATMUL_OBJ(&pipe, GetSysWorkSpacePtr(), mm, &tiling);
mm.SetTensorA(gmA, isTransposeA);
mm.SetTensorB(gmB, isTransposeB);
if (tiling.isBias) {
mm.SetBias(gmBias);
}
// 调用GetTensorC接口后,将Matmul一次Iterate的计算结果一分为二,搬运到两个AIV核的Unified Buffer。
pipe.InitBuffer(resultCMatrix, 1, tiling.M * tiling.N * sizeof(C_T));
mm.template Iterate<false>();
bufferC = resultCMatrix.AllocTensor<C_T>();
uint16_t nIter_ = Ceil(tiling.singleCoreN, tiling.baseN);
uint16_t mIter_ = Ceil(tiling.singleCoreM, tiling.baseM);
uint16_t mnIter_ = nIter_ * mIter_;
for (int i = 0; i < mnIter_; i++) {
mm.template GetTensorC<false>(bufferC, false, false); // false // kfc vec0 iterate
PipeBarrier<PIPE_ALL>();
}
mm.End();
resultCMatrix.EnQue(bufferC);
bufferC = resultCMatrix.DeQue<C_T>();
uint16_t baseOffset = tiling.M / 2 * tiling.N;
uint16_t stride = tiling.M / 2 * tiling.N * sizeof(C_T) / 32; // 32B
const uint16_t blockCount = tiling.M / tiling.M;
if (GetSubBlockIdxImpl() == 0) {
DataCopy(gmC, bufferC, {blockCount, stride, stride, stride});
} else {
DataCopy(gmC[baseOffset], bufferC, {blockCount, stride, stride, stride});
}
#include "lib/matmul_intf.h"
typedef AscendC::MatmulType<AscendC::TPosition::GM, CubeFormat::ND, half, LayoutMode::NONE, true> aType;
typedef AscendC::MatmulType<AscendC::TPosition::GM, CubeFormat::ND, half, LayoutMode::NONE, true> bType;
typedef AscendC::MatmulType<AscendC::TPosition::VECCALC, CubeFormat::ND, float> cType;
typedef AscendC::MatmulType<AscendC::TPosition::GM, CubeFormat::ND, float> biasType;
// Matmul定义时传入SplitNMatmulPolicy
AscendC::Matmul<aType, bType, cType, biasType, CFG_NORM, MatmulCallBackFunc<nullptr, nullptr, nullptr>, AscendC::Impl::Detail::SplitNMatmulPolicy> mm;
// Matmul计算
TPipe pipe;
TCubeTiling tiling;
REGIST_MATMUL_OBJ(&pipe, GetSysWorkSpacePtr(), mm, &tiling);
mm.SetTensorA(gmA, isTransposeA);
mm.SetTensorB(gmB, isTransposeB);
if (tiling.isBias) {
mm.SetBias(gmBias);
}
// 调用GetTensorC接口后,将Matmul一次Iterate的计算结果一分为二,搬运到两个AIV核的Unified Buffer。
pipe.InitBuffer(resultCMatrix, 1, tiling.M * tiling.N * sizeof(C_T));
mm.template Iterate<false>();
bufferC = resultCMatrix.AllocTensor<C_T>();
uint16_t nIter_ = Ceil(tiling.singleCoreN, tiling.baseN);
uint16_t mIter_ = Ceil(tiling.singleCoreM, tiling.baseM);
uint16_t mnIter_ = nIter_ * mIter_;
for (int i = 0; i < mnIter_; i++) {
mm.template GetTensorC<false>(bufferC, false, false); // false // kfc vec0 iterate
PipeBarrier<PIPE_ALL>();
}
mm.End();
resultCMatrix.EnQue(bufferC);
bufferC = resultCMatrix.DeQue<C_T>();
uint16_t baseOffset = tiling.N / 2;
uint16_t blockCount = tiling.M;
uint16_t blockLen = (tiling.N / 2 * sizeof(C_T)) / 32;
uint16_t srcStride = 0;
uint16_t dstStride = (tiling.N / 2 * sizeof(C_T)) / 32;
if (GetSubBlockIdxImpl() == 0) {
DataCopy(gmC, bufferC, {blockCount, blockLen, srcStride, dstStride});
} else {
DataCopy(gmC[baseOffset], bufferC, {blockCount, blockLen, srcStride, dstStride});
}