Merged
feat: support grouped matmul scale nz tiling #9728
jayshuAdd on Aug 7
feat: support grouped matmul scale nz tiling #9728
Merged
Total: 6 files changed+831-666
| @@ -225,9 +225,9 @@ aclnnStatus aclnnGroupedMatmulWeightNz( | |||
| 225 | <td>scaleOptional(aclTensorList *)</td> | 225 | <td>scaleOptional(aclTensorList *)</td> |
| 226 | <td>可选输入</td> | 226 | <td>可选输入</td> |
| 227 | <td>公式中的<code>scale</code>,代表量化参数中的缩放因子。</td> | 227 | <td>公式中的<code>scale</code>,代表量化参数中的缩放因子。</td> |
| 228 | - <td>一般情况下,长度与weight相同。综合约束请参见<a href="#约束说明">约束说明</a>。</td> | 228 | + <td>一般情况下,长度与weight相同。在mx量化场景,scaleOptional支持FRACTAL_NZ format。综合约束请参见<a href="#约束说明">约束说明</a>。</td> |
| 229 | <td>UINT64、INT64、BFLOAT16、FLOAT32、FLOAT8_E8M0<sup>2</sup></td> | 229 | <td>UINT64、INT64、BFLOAT16、FLOAT32、FLOAT8_E8M0<sup>2</sup></td> |
| 230 | - <td>ND</td> | 230 | + <td>ND/FRACTAL_NZ</td> |
| 231 | <td>-</td> | 231 | <td>-</td> |
| 232 | <td>-</td> | 232 | <td>-</td> |
| 233 | </tr> | 233 | </tr> |
| @@ -750,6 +750,17 @@ aclnnStatus aclnnGroupedMatmulWeightNz( | |||
| 750 | |0|weight单tensor|每个tensor 4维,当weight转置时,shape为(g, N, ceil(K / 64), 2);当weight不转置时,shape为(g, ceil(K / 64), N, 2)| | 750 | |0|weight单tensor|每个tensor 4维,当weight转置时,shape为(g, N, ceil(K / 64), 2);当weight不转置时,shape为(g, ceil(K / 64), N, 2)| |
| 751 | |0|weight多tensor|每个tensor 3维,当weight转置时,shape为(N, ceil(K / 64), 2);当weight不转置时,shape为(ceil(K / 64), N, 2)| | 751 | |0|weight多tensor|每个tensor 3维,当weight转置时,shape为(N, ceil(K / 64), 2);当weight不转置时,shape为(ceil(K / 64), N, 2)| |
| 752 | 752 | ||
| 753 | + - scaleOptional FRACTAL_NZ format约束: | ||
| 754 | + - 仅动态量化(mx量化)场景支持,scaleOptional数据类型必须为FLOAT8_E8M0。 | ||
| 755 | + - x和weight数据类型仅支持FLOAT8_E4M3FN。 | ||
| 756 | + - 本版本scaleOptional FRACTAL_NZ format暂不支持x或weight为FLOAT4_E2M1/FLOAT4_E1M2的场景。 | ||
| 757 | + - 仅支持transpose_weight为false的场景。 | ||
| 758 | + - 仅支持groupType为0,即M轴分组场景;不支持groupType为2的K轴分组场景。 | ||
| 759 | + - 仅支持x为单tensor、out为单tensor的场景,weight支持单tensor和多tensor两种场景。 | ||
| 760 | + - weight单tensor场景下,scaleOptional为单tensor,storage shape为(g, ceil(N / 16), ceil(K / 64), 16, 2)。 | ||
| 761 | + - weight多tensor场景下,scaleOptional为多tensor且长度与weight相同,每个tensor的storage shape为(ceil(N / 16), ceil(K / 64), 16, 2)。 | ||
| 762 | + - 非FRACTAL_NZ format下,scaleOptional继续使用上表中的原ND shape约束。 | ||
| 763 | + | ||
| 753 | - perTokenScaleOptional要满足下表: | 764 | - perTokenScaleOptional要满足下表: |
| 754 | 765 | ||
| 755 | |groupType| 使用场景 | shape限制 | | 766 | |groupType| 使用场景 | shape限制 | |
| @@ -46,7 +46,8 @@ bool CheckGroupListType(const optiling::GQmmInputInfo &inputParams) | |||
| 46 | } // namespace | 46 | } // namespace |
| 47 | namespace optiling { | 47 | namespace optiling { |
| 48 | GroupedQmmTiling::GroupedQmmTiling(gert::TilingContext *context) | 48 | GroupedQmmTiling::GroupedQmmTiling(gert::TilingContext *context) |
| 49 | - : TilingBaseClass(context), inputParams_(*(g_groupedQuantMatmulInfoFactory.Get())) | 49 | + : TilingBaseClass(context), |
| 50 | + inputParams_(*(g_groupedQuantMatmulInfoFactory.Get())) | ||
| 50 | { | 51 | { |
| 51 | Reset(); | 52 | Reset(); |
| 52 | } | 53 | } |
| @@ -59,6 +59,14 @@ constexpr uint64_t MXFP_MULTI_BASE_SIZE = 2UL; | |||
| 59 | constexpr uint64_t MXFP_BASEK_FACTOR = 64UL; | 59 | constexpr uint64_t MXFP_BASEK_FACTOR = 64UL; |
| 60 | constexpr size_t MXFP_TYPE_K_SCALE_DIM_NUM = 3; | 60 | constexpr size_t MXFP_TYPE_K_SCALE_DIM_NUM = 3; |
| 61 | constexpr size_t MXFP_TYPE_M_SCALE_DIM_NUM = 4; | 61 | constexpr size_t MXFP_TYPE_M_SCALE_DIM_NUM = 4; |
| 62 | +// Scale FRACTAL_NZ single-tensor storage shape is (group, n1, kScale, n0, c0). | ||
| 63 | +constexpr size_t MXFP_SCALE_NZ_DIM_NUM = 5; | ||
| 64 | +// Scale origin shape ends with (kScale, n, 2); this offset points to the trailing scale-pair dim. | ||
| 65 | +constexpr size_t MXFP_SCALE_ORIGIN_PAIR_DIM_OFFSET = 2; | ||
| 66 | +// Scale FRACTAL_NZ storage shape ends with (n1, kScale, 16, 2); this offset points to n0. | ||
| 67 | +constexpr size_t MXFP_SCALE_NZ_N0_DIM_OFFSET = 2; | ||
| 68 | +// Scale FRACTAL_NZ storage shape ends with (n1, kScale, 16, 2); this offset points to c0. | ||
| 69 | +constexpr size_t MXFP_SCALE_NZ_C0_DIM_OFFSET = 3; | ||
| 62 | constexpr size_t MXFP_PER_TOKEN_SCALE_DIM_NUM = 3; | 70 | constexpr size_t MXFP_PER_TOKEN_SCALE_DIM_NUM = 3; |
| 63 | constexpr uint32_t RESERVED_LENGTH = 1024U; | 71 | constexpr uint32_t RESERVED_LENGTH = 1024U; |
| 64 | constexpr uint16_t FIRST_TENSOR_INDEX = 0U; | 72 | constexpr uint16_t FIRST_TENSOR_INDEX = 0U; |
| @@ -203,9 +211,11 @@ public: | |||
| 203 | 211 | ||
| 204 | ge::Format aFormat = ge::FORMAT_ND; | 212 | ge::Format aFormat = ge::FORMAT_ND; |
| 205 | ge::Format bFormat = ge::FORMAT_ND; | 213 | ge::Format bFormat = ge::FORMAT_ND; |
| 214 | + ge::Format scaleFormat = ge::FORMAT_ND; | ||
| 206 | ge::Format cFormat = ge::FORMAT_ND; | 215 | ge::Format cFormat = ge::FORMAT_ND; |
| 207 | bool transA = false; | 216 | bool transA = false; |
| 208 | bool transB = false; | 217 | bool transB = false; |
| 218 | + bool scaleNzFlag = false; | ||
| 209 | bool hasBias = false; | 219 | bool hasBias = false; |
| 210 | bool isSingleX = false; | 220 | bool isSingleX = false; |
| 211 | bool isSingleW = false; | 221 | bool isSingleW = false; |
| @@ -263,6 +273,8 @@ protected: | |||
| 263 | virtual bool AnalyzeInputs(); | 273 | virtual bool AnalyzeInputs(); |
| 264 | virtual void PrintQuantParams(); | 274 | virtual void PrintQuantParams(); |
| 265 | bool IsMicroScaling() const; | 275 | bool IsMicroScaling() const; |
| 276 | + bool CheckQuantParamsForMXTypeM(const gert::Shape &xScaleShape, const gert::StorageShape *wScaleStorageShape, | ||
| 277 | + uint64_t expectedNSize = 0UL) const; | ||
| 266 | bool CheckQuantParamsForMXTypeM(const gert::Shape &xScaleShape, const gert::Shape &wScaleShape, | 278 | bool CheckQuantParamsForMXTypeM(const gert::Shape &xScaleShape, const gert::Shape &wScaleShape, |
| 267 | uint64_t expectedNSize = 0UL) const; | 279 | uint64_t expectedNSize = 0UL) const; |
| 268 | bool CheckShapeForWeightNz(const gert::Shape &wShape, uint64_t expectedNSize = 0UL) const; | 280 | bool CheckShapeForWeightNz(const gert::Shape &wShape, uint64_t expectedNSize = 0UL) const; |
| @@ -304,10 +316,14 @@ private: | |||
| 304 | bool CheckFp4Shape(const gert::Shape &xShape, const gert::Shape &wShape) const; | 316 | bool CheckFp4Shape(const gert::Shape &xShape, const gert::Shape &wShape) const; |
| 305 | bool CheckBiasDtype() const; | 317 | bool CheckBiasDtype() const; |
| 306 | bool CheckBiasShape(const gert::StorageShape *biasStorageShape) const; | 318 | bool CheckBiasShape(const gert::StorageShape *biasStorageShape) const; |
| 307 | - bool CheckQuantParamsForMxQuantMode(const gert::StorageShape *xScaleStorageShape, const gert::Shape &wScaleShape, | 319 | + bool CheckQuantParamsForMxQuantMode(const gert::StorageShape *xScaleStorageShape, |
| 320 | + const gert::StorageShape *wScaleStorageShape, | ||
| 308 | uint64_t expectedNSize = 0UL) const; | 321 | uint64_t expectedNSize = 0UL) const; |
| 309 | - bool CheckQuantParams(const gert::StorageShape *xScaleStorageShape, const gert::Shape &wScaleShape, | 322 | + bool CheckQuantParams(const gert::StorageShape *xScaleStorageShape, const gert::StorageShape *wScaleStorageShape, |
| 310 | uint64_t expectedNSize = 0UL) const; | 323 | uint64_t expectedNSize = 0UL) const; |
| 324 | + bool CheckScaleNzShape(const gert::StorageShape *wScaleStorageShape, uint64_t expectedNSize = 0UL) const; | ||
| 325 | + bool CheckQuantParamsForMXTypeMImpl(const gert::Shape &xScaleShape, const gert::Shape &wScaleShape, | ||
| 326 | + bool isScaleNzStorage, uint64_t expectedNSize = 0UL) const; | ||
| 311 | bool CheckQuantParamsForNonKGroupQuantMode(const gert::Shape &wScaleShape, uint64_t expectedNSize = 0UL) const; | 327 | bool CheckQuantParamsForNonKGroupQuantMode(const gert::Shape &wScaleShape, uint64_t expectedNSize = 0UL) const; |
| 312 | bool SetMKNList(); | 328 | bool SetMKNList(); |
| 313 | bool CheckDtypeForWeightNz(bool isPertokenScaleNull) const; | 329 | bool CheckDtypeForWeightNz(bool isPertokenScaleNull) const; |
| @@ -316,12 +332,11 @@ private: | |||
| 316 | virtual bool CheckCoreNum() const; | 332 | virtual bool CheckCoreNum() const; |
| 317 | bool CheckTransposeAndFormatByGroupType() const; | 333 | bool CheckTransposeAndFormatByGroupType() const; |
| 318 | bool CheckPertokenScaleDtypeForWeightNz(bool isA8W8Int, bool isA8W8Fp, bool isA4W4Fp, bool isA4W4Int) const; | 334 | bool CheckPertokenScaleDtypeForWeightNz(bool isA8W8Int, bool isA8W8Fp, bool isA4W4Fp, bool isA4W4Int) const; |
| 319 | - bool CheckWeightNzTransposedDims(const gert::Shape &wShape, uint64_t wShapeDimThird, | 335 | + bool CheckWeightNzTransposedDims(const gert::Shape &wShape, uint64_t wShapeDimThird, uint64_t wShapeDimSecond, |
| 320 | - uint64_t wShapeDimSecond, uint32_t weightNzLastDim, | 336 | + uint32_t weightNzLastDim, uint64_t logicalNSize) const; |
| 321 | - uint64_t logicalNSize) const; | ||
| 322 | bool CheckWeightNzDimNum(const gert::Shape &wShape) const; | 337 | bool CheckWeightNzDimNum(const gert::Shape &wShape) const; |
| 323 | - bool CheckWeightNzLastDim(const gert::Shape &wShape, size_t nzDimOffset, | 338 | + bool CheckWeightNzLastDim(const gert::Shape &wShape, size_t nzDimOffset, uint32_t expectedValue, |
| 324 | - uint32_t expectedValue, const char *dtypeLabel) const; | 339 | + const char *dtypeLabel) const; |
| 325 | bool CheckWeightNzSecondLastDim(const gert::Shape &wShape, size_t nzDimOffset) const; | 340 | bool CheckWeightNzSecondLastDim(const gert::Shape &wShape, size_t nzDimOffset) const; |
| 326 | bool ValidateAAndWDtype(const std::vector<ge::DataType> &legalInputDtypes) const; | 341 | bool ValidateAAndWDtype(const std::vector<ge::DataType> &legalInputDtypes) const; |
| 327 | 342 | ||
| @@ -16,7 +16,6 @@ | |||
| 16 | #ifndef GQMM_TENSOR_API_MX_KERNEL_H | 16 | #ifndef GQMM_TENSOR_API_MX_KERNEL_H |
| 17 | #define GQMM_TENSOR_API_MX_KERNEL_H | 17 | #define GQMM_TENSOR_API_MX_KERNEL_H |
| 18 | 18 | ||
| 19 | - | ||
| 20 | #include "blaze/gemm/block/block_mmad_qgmm_mx.h" | 19 | #include "blaze/gemm/block/block_mmad_qgmm_mx.h" |
| 21 | #include "blaze/gemm/kernel/kernel_qgmm_mx.h" | 20 | #include "blaze/gemm/kernel/kernel_qgmm_mx.h" |
| 22 | #include "../../grouped_matmul_utils.h" | 21 | #include "../../grouped_matmul_utils.h" |
| @@ -45,7 +44,7 @@ __aicore__ inline void GmmTensorApiMxKernel(GM_ADDR x, GM_ADDR weight, GM_ADDR b | |||
| 45 | using LayoutB = wLayout; | 44 | using LayoutB = wLayout; |
| 46 | using LayoutC = yLayout; | 45 | using LayoutC = yLayout; |
| 47 | using ProblemShape = AscendC::Te::Shape<int64_t, int64_t, int64_t, int64_t>; | 46 | using ProblemShape = AscendC::Te::Shape<int64_t, int64_t, int64_t, int64_t>; |
| 48 | - using BlockMmadPolicy = Blaze::Gemm::GroupedMatmulWithScaleMx<0>; | 47 | + using BlockMmadPolicy = Blaze::Gemm::GroupedMatmulWithScaleMx<0, false, Blaze::Gemm::KernelGroupedMmadWithScaleMx>; |
| 49 | using QgmmBlockMmad = Blaze::Gemm::Block::BlockMmad<BlockMmadPolicy, AType, LayoutA, BType, LayoutB, YType, LayoutC, | 48 | using QgmmBlockMmad = Blaze::Gemm::Block::BlockMmad<BlockMmadPolicy, AType, LayoutA, BType, LayoutB, YType, LayoutC, |
| 50 | BiasType, LayoutC>; | 49 | BiasType, LayoutC>; |
| 51 | using BlockEpilogue = Blaze::Epilogue::Block::BlockEpilogueEmpty; | 50 | using BlockEpilogue = Blaze::Epilogue::Block::BlockEpilogueEmpty; |
| @@ -54,23 +53,16 @@ __aicore__ inline void GmmTensorApiMxKernel(GM_ADDR x, GM_ADDR weight, GM_ADDR b | |||
| 54 | using Params = typename QgmmKernel::Params; | 53 | using Params = typename QgmmKernel::Params; |
| 55 | using GMMTiling = typename QgmmKernel::GMMTiling; | 54 | using GMMTiling = typename QgmmKernel::GMMTiling; |
| 56 | 55 | ||
| 57 | - GMMTiling gmmParams{static_cast<uint32_t>(gmmBaseParamsIn->groupNum), | 56 | + GMMTiling gmmParams{ |
| 58 | - static_cast<int64_t>(mmTilingDataIn->m), | 57 | + static_cast<uint32_t>(gmmBaseParamsIn->groupNum), static_cast<int64_t>(mmTilingDataIn->m), |
| 59 | - static_cast<int64_t>(mmTilingDataIn->n), | 58 | + static_cast<int64_t>(mmTilingDataIn->n), static_cast<int64_t>(mmTilingDataIn->k), |
| 60 | - static_cast<int64_t>(mmTilingDataIn->k), | 59 | + static_cast<uint32_t>(mmTilingDataIn->baseM), static_cast<uint32_t>(mmTilingDataIn->baseN), |
| 61 | - static_cast<uint32_t>(mmTilingDataIn->baseM), | 60 | + static_cast<uint32_t>(mmTilingDataIn->baseK), static_cast<uint32_t>(mmTilingDataIn->kAL1), |
| 62 | - static_cast<uint32_t>(mmTilingDataIn->baseN), | 61 | + static_cast<uint32_t>(mmTilingDataIn->kBL1), static_cast<uint32_t>(mmTilingDataIn->scaleKAL1), |
| 63 | - static_cast<uint32_t>(mmTilingDataIn->baseK), | 62 | + static_cast<uint32_t>(mmTilingDataIn->scaleKBL1), static_cast<uint8_t>(mmTilingDataIn->isBias), |
| 64 | - static_cast<uint32_t>(mmTilingDataIn->kAL1), | 63 | + static_cast<uint8_t>(mmTilingDataIn->dbL0C), static_cast<uint8_t>(mmTilingDataIn->l1BufferStage), |
| 65 | - static_cast<uint32_t>(mmTilingDataIn->kBL1), | 64 | + static_cast<int8_t>(gmmBaseParamsIn->groupType), static_cast<uint8_t>(gmmBaseParamsIn->groupListType), |
| 66 | - static_cast<uint32_t>(mmTilingDataIn->scaleKAL1), | 65 | + static_cast<uint8_t>(gmmBaseParamsIn->singleW)}; |
| 67 | - static_cast<uint32_t>(mmTilingDataIn->scaleKBL1), | ||
| 68 | - static_cast<uint8_t>(mmTilingDataIn->isBias), | ||
| 69 | - static_cast<uint8_t>(mmTilingDataIn->dbL0C), | ||
| 70 | - static_cast<uint8_t>(mmTilingDataIn->l1BufferStage), | ||
| 71 | - static_cast<int8_t>(gmmBaseParamsIn->groupType), | ||
| 72 | - static_cast<uint8_t>(gmmBaseParamsIn->groupListType), | ||
| 73 | - static_cast<uint8_t>(gmmBaseParamsIn->singleW)}; | ||
| 74 | 66 | ||
| 75 | GM_ADDR aDataAddr = reinterpret_cast<GM_ADDR>(GROUPED_MATMUL::GetTensorAddr<AType>(0, x)); | 67 | GM_ADDR aDataAddr = reinterpret_cast<GM_ADDR>(GROUPED_MATMUL::GetTensorAddr<AType>(0, x)); |
| 76 | GM_ADDR bDataAddr = gmmBaseParamsIn->singleW == 1 ? | 68 | GM_ADDR bDataAddr = gmmBaseParamsIn->singleW == 1 ? |
| @@ -102,6 +102,12 @@ constexpr CubeFormat wFormat = CubeFormat::NZ; | |||
| 102 | #else | 102 | #else |
| 103 | constexpr CubeFormat wFormat = CubeFormat::ND; | 103 | constexpr CubeFormat wFormat = CubeFormat::ND; |
| 104 | #endif | 104 | #endif |
| 105 | +#if defined(FORMAT_SCALE) && defined(FORMAT_WEIGHT) && defined(ORIG_DTYPE_SCALE) && defined(DT_FLOAT8_E8M0) && \ | ||
| 106 | + FORMAT_SCALE == FORMAT_FRACTAL_NZ && FORMAT_WEIGHT == FORMAT_FRACTAL_NZ && ORIG_DTYPE_SCALE == DT_FLOAT8_E8M0 | ||
| 107 | +constexpr bool scaleNz = true; | ||
| 108 | +#else | ||
| 109 | +constexpr bool scaleNz = false; | ||
| 110 | +#endif | ||
| 105 | 111 | ||
| 106 | } // namespace | 112 | } // namespace |
| 107 | 113 | ||
| @@ -224,9 +230,11 @@ using biasType = MatmulType<AscendC::TPosition::GM, CubeFormat::ND, DTYPE_BIAS>; | |||
| 224 | do { \ | 230 | do { \ |
| 225 | GET_TILING_DATA_MEMBER(GMMQuantBasicApiTilingData, gmmQuantParams, gmmQuantParams_, tiling); \ | 231 | GET_TILING_DATA_MEMBER(GMMQuantBasicApiTilingData, gmmQuantParams, gmmQuantParams_, tiling); \ |
| 226 | GET_TILING_DATA_MEMBER(GMMQuantBasicApiTilingData, mmTilingData, mmTilingData_, tiling); \ | 232 | GET_TILING_DATA_MEMBER(GMMQuantBasicApiTilingData, mmTilingData, mmTilingData_, tiling); \ |
| 227 | - GmmTensorApiMxKernel<DTYPE_X, DTYPE_WEIGHT, DTYPE_BIAS, DTYPE_SCALE, float, DTYPE_Y, xLayout, wLayout, \ | 233 | + using wLayoutWithScale = \ |
| 228 | - yLayout, DTYPE_L0C_LOCAL>(x, weight, bias, scale, groupList, perTokenScale, y, user1, \ | 234 | + AscendC::Std::conditional_t<scaleNz, AscendC::Std::tuple<wLayout, AscendC::Te::NNLayoutPtn>, wLayout>; \ |
| 229 | - &gmmQuantParams_, &mmTilingData_, &tPipe); \ | 235 | + GmmTensorApiMxKernel<DTYPE_X, DTYPE_WEIGHT, DTYPE_BIAS, DTYPE_SCALE, float, DTYPE_Y, xLayout, \ |
| 236 | + wLayoutWithScale, yLayout, DTYPE_L0C_LOCAL>( \ | ||
| 237 | + x, weight, bias, scale, groupList, perTokenScale, y, user1, &gmmQuantParams_, &mmTilingData_, &tPipe); \ | ||
| 230 | } while (0) | 238 | } while (0) |
| 231 | #else | 239 | #else |
| 232 | #define GMM_QUANT_MX_IMPL_CLASS(cgmctXLayout, cgmctWLayout, cgmctYLayout, xLayout, wLayout, yLayout) \ | 240 | #define GMM_QUANT_MX_IMPL_CLASS(cgmctXLayout, cgmctWLayout, cgmctYLayout, xLayout, wLayout, yLayout) \ |