Merged
refactor(gmmfr): dispatch arch35 quantized paths to ops-tensor #13290
zhangzhizhuoAdd on Sep 30
refactor(gmmfr): dispatch arch35 quantized paths to ops-tensor #13290
Merged
Merged fromzhangzhizhuo/ops-transformergmmfrintocann/ops-transformermaster
Total: 5 files changed+382-127
| @@ -7,7 +7,7 @@ | |||
| 7 | # INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. | 7 | # INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. |
| 8 | # See LICENSE in the root of the software repository for the full text of the License. | 8 | # See LICENSE in the root of the software repository for the full text of the License. |
| 9 | # ---------------------------------------------------------------------------- | 9 | # ---------------------------------------------------------------------------- |
| 10 | -set(OPTENSOR_TAG_ID fe1d9908c4eaff2943a3b6cea9c2467b8c427f18) | 10 | +set(OPTENSOR_TAG_ID fb30752ed5524744cee0cd21f26db5d52e54b6e4) |
| 11 | 11 | ||
| 12 | if(EXISTS "${PROJECT_SOURCE_DIR}/../ops-tensor") | 12 | if(EXISTS "${PROJECT_SOURCE_DIR}/../ops-tensor") |
| 13 | get_filename_component(OPTENSOR_SOURCE_PATH | 13 | get_filename_component(OPTENSOR_SOURCE_PATH |
| @@ -905,14 +905,14 @@ ge::graphStatus GroupedMatmulFinalizeRoutingQuantTiling::DeterministicTilingProc | |||
| 905 | uint64_t l2Size; | 905 | uint64_t l2Size; |
| 906 | ascendcPlatform.GetCoreMemSize(platform_ascendc::CoreMemType::L2, l2Size); | 906 | ascendcPlatform.GetCoreMemSize(platform_ascendc::CoreMemType::L2, l2Size); |
| 907 | deterWorkspaceSize_ = static_cast<uint32_t>((l2Size - SYS_WORKSPACE_SIZE) * DETER_WORKSPACE_RATIO); | 907 | deterWorkspaceSize_ = static_cast<uint32_t>((l2Size - SYS_WORKSPACE_SIZE) * DETER_WORKSPACE_RATIO); |
| 908 | - OP_CHECK_IF( | 908 | + OP_CHECK_IF(static_cast<uint64_t>(deterWorkspaceSize_) < static_cast<uint64_t>(inputParams_.nSize) * sizeof(float) * |
| 909 | - static_cast<int64_t>(deterWorkspaceSize_) < | 909 | + static_cast<uint64_t>(tilingData_.matmulTiling.baseM), |
| 910 | - static_cast<int64_t>(inputParams_.nSize) * static_cast<int64_t>(sizeof(float)), | 910 | + OPS_REPORT_CUBE_INNER_ERR( |
| 911 | - OPS_REPORT_CUBE_INNER_ERR(context_->GetNodeName(), | 911 | + context_->GetNodeName(), |
| 912 | - "deterministic workspace size(%u) is less than one row(N=%ld * sizeof(float)), " | 912 | + "deterministic workspace size(%u) cannot hold baseM=%d rows(N=%ld * sizeof(float)), " |
| 913 | - "cannot enable deterministic mode", | 913 | + "cannot enable deterministic mode", |
| 914 | - deterWorkspaceSize_, static_cast<int64_t>(inputParams_.nSize)), | 914 | + deterWorkspaceSize_, tilingData_.matmulTiling.baseM, static_cast<int64_t>(inputParams_.nSize)), |
| 915 | - return ge::GRAPH_FAILED); | 915 | + return ge::GRAPH_FAILED); |
| 916 | tilingData_.gmmFinalizeRoutingDataParams.deterWorkspaceSize = deterWorkspaceSize_; | 916 | tilingData_.gmmFinalizeRoutingDataParams.deterWorkspaceSize = deterWorkspaceSize_; |
| 917 | OP_LOGD(context_->GetNodeName(), "DeterministicTilingProcess: flag=%u, wsSize=%u", | 917 | OP_LOGD(context_->GetNodeName(), "DeterministicTilingProcess: flag=%u, wsSize=%u", |
| 918 | tilingData_.gmmFinalizeRoutingDataParams.deterministicFlag, | 918 | tilingData_.gmmFinalizeRoutingDataParams.deterministicFlag, |
Agmm/grouped_matmul_finalize_routing/op_kernel/arch35/grouped_matmul_finalize_routing_a8w4_blaze.h+117-0
| @@ -0,0 +1,117 @@ | |||
| 1 | +/** | ||
| 2 | + * Copyright (c) 2026 Huawei Technologies Co., Ltd. | ||
| 3 | + * This program is free software, you can redistribute it and/or modify it under the terms and conditions of | ||
| 4 | + * CANN Open Software License Agreement Version 2.0 (the "License"). | ||
| 5 | + * Please refer to the License for details. You may not use this file except in compliance with the License. | ||
| 6 | + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, | ||
| 7 | + * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. | ||
| 8 | + * See LICENSE in the root of the software repository for the full text of the License. | ||
| 9 | + */ | ||
| 10 | + | ||
| 11 | +/*! | ||
| 12 | + * \file grouped_matmul_finalize_routing_a8w4_blaze.h | ||
| 13 | + * \brief TensorAPI entry point for GroupedMatmulFinalizeRouting MX A8W4 weight quant | ||
| 14 | + * (FP8 E4M3FN x FP4 E2M1, WeightNZ). Replaces the legacy | ||
| 15 | + * GMMFRWeightQuantResplitController assembly with Blaze components. | ||
| 16 | + */ | ||
| 17 | + | ||
| 18 | +#ifndef GROUPED_MATMUL_FINALIZE_ROUTING_A8W4_BLAZE_H | ||
| 19 | +#define GROUPED_MATMUL_FINALIZE_ROUTING_A8W4_BLAZE_H | ||
| 20 | + | ||
| 21 | +#include "blaze/epilogue/block/block_epilogue_finalize_routing.h" | ||
| 22 | +#include "blaze/gemm/block/block_mmad_weight_prologue_mx.h" | ||
| 23 | +#include "blaze/gemm/kernel/kernel_wqgmm_mx_mix_finalize_routing.h" | ||
| 24 | +#include "blaze/gemm/policy/dispatch_policy.h" | ||
| 25 | +#include "weight_quant_basic_block/gmm_fr_weight_quant_tiling_data.h" | ||
| 26 | + | ||
| 27 | +namespace GroupedMatmulFinalizeRouting { | ||
| 28 | + | ||
| 29 | +template <typename XType_, typename WType_, typename ScaleType_, typename PerTokenScaleType_, typename BiasType_, | ||
| 30 | + typename YType_, typename SharedInputType_, typename LogitType_, typename RowIndexType_, bool HasBias> | ||
| 31 | +__aicore__ inline void InvokeBlazeA8W4Kernel( | ||
| 32 | + GM_ADDR x, GM_ADDR weight, GM_ADDR antiquantScale, GM_ADDR bias, GM_ADDR groupList, GM_ADDR perTokenScale, | ||
| 33 | + GM_ADDR logitsAddr, GM_ADDR rowIndexAddr, GM_ADDR y, GM_ADDR shareInput, | ||
| 34 | + const GMMFinalizeRoutingArch35Tiling::GMMFinalizeRoutingWeightQuantTilingData* __restrict tiling) | ||
| 35 | +{ | ||
| 36 | + using AType = XType_; // fp8_e4m3fn_t activation | ||
| 37 | + using BType = WType_; // fp4x2_e2m1_t packed weight | ||
| 38 | + using ScaleType = ScaleType_; // fp8_e8m0_t | ||
| 39 | + using BiasType = BiasType_; // bfloat16_t | ||
| 40 | + using SharedType = SharedInputType_; // bfloat16_t | ||
| 41 | + using LogitType = LogitType_; // float | ||
| 42 | + using RowIndexType = RowIndexType_; // int64_t | ||
| 43 | + static_assert(AscendC::IsSameType<LogitType, float>::value, "A8W4 FR kernel requires float logits"); | ||
| 44 | + static_assert(AscendC::IsSameType<RowIndexType, int64_t>::value, "A8W4 FR kernel requires int64 row index"); | ||
| 45 | + static_assert(AscendC::IsSameType<SharedType, bfloat16_t>::value, "A8W4 FR kernel requires bf16 shared input"); | ||
| 46 | + | ||
| 47 | + static_assert(AscendC::Std::is_same_v<PerTokenScaleType_, ScaleType_>, "MX scale types must match"); | ||
| 48 | + static_assert(AscendC::Std::is_same_v<YType_, float>, "A8W4 output must be float"); | ||
| 49 | + using DispatchPolicy = Blaze::Gemm::GroupedMatmulFinalizeRoutingWithWeightQuantMx; | ||
| 50 | + using LayoutA = asc::te::nd_ext_layout_ptn; | ||
| 51 | + using LayoutB = asc::te::zn_layout_ptn; | ||
| 52 | + using LayoutScaleA = asc::te::scalea_nd_layout_ptn; | ||
| 53 | + using LayoutScaleB = asc::te::scaleb_dn_layout_ptn; | ||
| 54 | + using ProblemShape = decltype(asc::te::make_shape(0UL, 0UL, 0UL)); | ||
| 55 | + using BlockScheduler = | ||
| 56 | + Blaze::Gemm::Kernel::BlockSchedulerWqgmmNResplit<decltype(asc::te::make_shape(0UL, 0UL, 0UL))>; | ||
| 57 | + using BlockMmad = | ||
| 58 | + Blaze::Gemm::Block::BlockMmad<DispatchPolicy, AscendC::Std::tuple<AType, ScaleType>, | ||
| 59 | + AscendC::Std::tuple<LayoutA, LayoutScaleA>, AscendC::Std::tuple<BType, ScaleType>, | ||
| 60 | + AscendC::Std::tuple<LayoutB, LayoutScaleB>, float, asc::te::nd_ext_layout_ptn, | ||
| 61 | + BiasType, asc::te::nd_ext_layout_ptn>; | ||
| 62 | + using BlockPrologue = Blaze::Gemm::Kernel::GmmWqgmmFrWeightPrologue<AType, BType, BiasType>; | ||
| 63 | + using BlockEpilogue = | ||
| 64 | + Blaze::Epilogue::Block::BlockEpilogueFinalizeRouting<float, float, float, int64_t, float, false, | ||
| 65 | + Blaze::Epilogue::Block::FinalizeRoutingMode::A8W4>; | ||
| 66 | + using BlockOutputInit = Blaze::Gemm::Kernel::GmmWqgmmFrOutputInit; | ||
| 67 | + using KernelImpl = | ||
| 68 | + Blaze::Gemm::Kernel::GmmWqgmmMxFinalizeRoutingKernel<ProblemShape, BlockMmad, BlockScheduler, BlockPrologue, | ||
| 69 | + BlockEpilogue, BlockOutputInit>; | ||
| 70 | + | ||
| 71 | + typename BlockMmad::Params mmadParams{}; | ||
| 72 | + mmadParams.aGmAddr = x; | ||
| 73 | + mmadParams.scaleAGmAddr = perTokenScale; | ||
| 74 | + mmadParams.scaleBGmAddr = antiquantScale; | ||
| 75 | + mmadParams.biasGmAddr = bias; | ||
| 76 | + mmadParams.cGmAddr = y; // consumed by the AIV epilogue; AIC writes the UB fixpipe buffer | ||
| 77 | + | ||
| 78 | + constexpr uint64_t MAIN_BLOCK_N = 256; | ||
| 79 | + constexpr int32_t BASE_M = 256; | ||
| 80 | + typename BlockScheduler::Params schedulerParams{}; | ||
| 81 | + schedulerParams.mainBlockCount = Blaze::Gemm::CeilDiv(tiling->nSize, MAIN_BLOCK_N); | ||
| 82 | + schedulerParams.mainBlockSize = MAIN_BLOCK_N; | ||
| 83 | + schedulerParams.coreNum = static_cast<uint64_t>(tiling->coreNum); | ||
| 84 | + schedulerParams.baseM = BASE_M; | ||
| 85 | + schedulerParams.nSize = static_cast<uint64_t>(tiling->nSize); | ||
| 86 | + typename BlockPrologue::Params prologueParams{reinterpret_cast<__gm__ BType*>(weight)}; | ||
| 87 | + typename BlockEpilogue::Params epilogueParams{}; | ||
| 88 | + epilogueParams.yGmAddr = y; | ||
| 89 | + epilogueParams.logitGmAddr = logitsAddr; | ||
| 90 | + epilogueParams.rowIndexGmAddr = rowIndexAddr; | ||
| 91 | + epilogueParams.nSize = tiling->nSize; | ||
| 92 | + typename BlockOutputInit::Params outputInitParams{}; | ||
| 93 | + outputInitParams.ptrY = reinterpret_cast<__gm__ float*>(y); | ||
| 94 | + outputInitParams.ptrSharedInput = reinterpret_cast<__gm__ SharedType*>(shareInput); | ||
| 95 | + outputInitParams.nSize = tiling->nSize; | ||
| 96 | + outputInitParams.sharedInputOffset = tiling->sharedInputOffset; | ||
| 97 | + outputInitParams.sharedInputLen = tiling->sharedInputLen; | ||
| 98 | + outputInitParams.sharedInputWeight = tiling->sharedInputWeight; | ||
| 99 | + | ||
| 100 | + typename KernelImpl::Params params{ | ||
| 101 | + ProblemShape{static_cast<uint64_t>(tiling->kSize), static_cast<uint64_t>(tiling->nSize), | ||
| 102 | + static_cast<uint64_t>(tiling->groupNum)}, | ||
| 103 | + mmadParams, | ||
| 104 | + schedulerParams, | ||
| 105 | + prologueParams, | ||
| 106 | + epilogueParams, | ||
| 107 | + outputInitParams, | ||
| 108 | + groupList, | ||
| 109 | + static_cast<uint32_t>(tiling->groupListType), | ||
| 110 | + static_cast<uint32_t>(tiling->hasBias)}; | ||
| 111 | + KernelImpl kernelImpl; | ||
| 112 | + kernelImpl(params); | ||
| 113 | +} | ||
| 114 | + | ||
| 115 | +} // namespace GroupedMatmulFinalizeRouting | ||
| 116 | + | ||
| 117 | +#endif // GROUPED_MATMUL_FINALIZE_ROUTING_A8W4_BLAZE_H | ||
| @@ -0,0 +1,141 @@ | |||
| 1 | +/** | ||
| 2 | + * Copyright (c) 2026 Huawei Technologies Co., Ltd. | ||
| 3 | + * This program is free software, you can redistribute it and/or modify it under the terms and conditions of | ||
| 4 | + * CANN Open Software License Agreement Version 2.0 (the "License"). | ||
| 5 | + * Please refer to the License for details. You may not use this file except in compliance with the License. | ||
| 6 | + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, | ||
| 7 | + * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. | ||
| 8 | + * See LICENSE in the root of the software repository for the full text of the License. | ||
| 9 | + */ | ||
| 10 | + | ||
| 11 | +/*! | ||
| 12 | + * \file grouped_matmul_finalize_routing_pertoken_blaze.h | ||
| 13 | + * \brief TensorAPI entry point for GroupedMatmulFinalizeRouting per-token dequant | ||
| 14 | + * (INT8 x INT8 / FP8 E4M3FN x FP8 E4M3FN / HIFLOAT8 x HIFLOAT8 full quantization). | ||
| 15 | + */ | ||
| 16 | + | ||
| 17 | +#ifndef GROUPED_MATMUL_FINALIZE_ROUTING_PERTOKEN_BLAZE_H | ||
| 18 | +#define GROUPED_MATMUL_FINALIZE_ROUTING_PERTOKEN_BLAZE_H | ||
| 19 | + | ||
| 20 | +#include "blaze/epilogue/block/block_epilogue_finalize_routing.h" | ||
| 21 | +#include "blaze/gemm/block/block_mmad_a8w8_mix.h" | ||
| 22 | +#include "blaze/gemm/block/block_scheduler_gmm_swat_with_tail_split.h" | ||
| 23 | +#include "blaze/gemm/kernel/kernel_qgmm_mix_pertoken_finalize_routing.h" | ||
| 24 | +#include "blaze/gemm/policy/dispatch_policy.h" | ||
| 25 | +#include "grouped_matmul_finalize_routing_tiling_data.h" | ||
| 26 | + | ||
| 27 | +namespace GroupedMatmulFinalizeRouting { | ||
| 28 | + | ||
| 29 | +template <typename LayoutA_, typename LayoutB_, int ScaleType, int RowIndexTypeValue, bool IsDeterministic_> | ||
| 30 | +__aicore__ inline void InvokeBlazePertokenKernelImpl( | ||
| 31 | + GM_ADDR x, GM_ADDR w, GM_ADDR wScale, GM_ADDR bias, GM_ADDR xScale, GM_ADDR groupList, GM_ADDR shareInput, | ||
| 32 | + GM_ADDR logit, GM_ADDR rowIndex, GM_ADDR offset, GM_ADDR y, GM_ADDR workspaceGM, | ||
| 33 | + const GMMFinalizeRoutingArch35Tiling::GMMFinalizeRoutingTilingData& tilingData) | ||
| 34 | +{ | ||
| 35 | + (void)offset; | ||
| 36 | + const auto& gmmParamsIn = tilingData.gmmFinalizeRoutingDataParams; | ||
| 37 | + const auto& matmulTiling = tilingData.matmulTiling; | ||
| 38 | + | ||
| 39 | + using AType = DTYPE_X; | ||
| 40 | + using BType = DTYPE_W; | ||
| 41 | + using CType = DTYPE_Y; | ||
| 42 | + // Keep the MMAD accumulator in int32 for INT8 inputs and float for FP8/HIFLOAT8. | ||
| 43 | + using C1Type = AscendC::Std::conditional_t<AscendC::Std::is_same_v<AType, int8_t>, int32_t, float>; | ||
| 44 | + constexpr int SCALE_TYPE_BF16 = 2; | ||
| 45 | + using WeightScaleType = AscendC::Std::conditional_t<ScaleType == SCALE_TYPE_BF16, bfloat16_t, float>; | ||
| 46 | + using BiasType = bfloat16_t; | ||
| 47 | + using RowIndexType = AscendC::Std::conditional_t<RowIndexTypeValue == 1, int32_t, int64_t>; | ||
| 48 | + using LayoutA = LayoutA_; | ||
| 49 | + using LayoutB = LayoutB_; | ||
| 50 | + using LayoutC = asc::te::nd_ext_layout_ptn; | ||
| 51 | + using LayoutBias = asc::te::nd_ext_layout_ptn; | ||
| 52 | + using ProblemShape = AscendC::Te::Shape<int64_t, int64_t, int64_t, int64_t>; | ||
| 53 | + | ||
| 54 | + using BlockMmadPolicy = | ||
| 55 | + Blaze::Gemm::MatmulWithScaleMix<0, false, Blaze::Gemm::KernelQgmmMixPertokenFinalizeRouting>; | ||
| 56 | + // The Mix block takes B as a tuple; the per-token path has no fixpipe scale, so the | ||
| 57 | + // tuple only carries the weight dtype. | ||
| 58 | + using BTypeTuple = AscendC::Std::tuple<BType>; | ||
| 59 | + using BlockMmad = Blaze::Gemm::Block::BlockMmad<BlockMmadPolicy, AType, LayoutA, BTypeTuple, LayoutB, C1Type, | ||
| 60 | + LayoutC, BiasType, LayoutBias>; | ||
| 61 | + using BlockScheduler = Blaze::Gemm::Block::BlockSchedulerGmmSwatWithTailSplit; | ||
| 62 | + | ||
| 63 | + const uint32_t kAL1 = static_cast<uint32_t>(matmulTiling.stepKa) * static_cast<uint32_t>(matmulTiling.baseK); | ||
| 64 | + const uint32_t kBL1 = static_cast<uint32_t>(matmulTiling.stepKb) * static_cast<uint32_t>(matmulTiling.baseK); | ||
| 65 | + constexpr uint32_t DOUBLE_BUFFER_NUM = 2; | ||
| 66 | + | ||
| 67 | + typename BlockMmad::Params mmadParams{}; | ||
| 68 | + mmadParams.aGmAddr = x; | ||
| 69 | + mmadParams.bGmAddr = w; | ||
| 70 | + | ||
| 71 | + using BlockEpilogue = | ||
| 72 | + Blaze::Epilogue::Block::BlockEpilogueFinalizeRouting<CType, C1Type, float, RowIndexType, WeightScaleType, | ||
| 73 | + IsDeterministic_, | ||
| 74 | + Blaze::Epilogue::Block::FinalizeRoutingMode::PERTOKEN>; | ||
| 75 | + using GmmKernel = Blaze::Gemm::Kernel::GemmUniversal<ProblemShape, BlockMmad, BlockEpilogue, BlockScheduler>; | ||
| 76 | + typename GmmKernel::SharedInputParams sharedInputParams{shareInput, | ||
| 77 | + y, | ||
| 78 | + gmmParamsIn.sharedInputOffset, | ||
| 79 | + gmmParamsIn.sharedInputLen, | ||
| 80 | + matmulTiling.N, | ||
| 81 | + gmmParamsIn.batch, | ||
| 82 | + gmmParamsIn.residualScale}; | ||
| 83 | + | ||
| 84 | + typename BlockEpilogue::Params epilogueParams{}; | ||
| 85 | + epilogueParams.yGmAddr = y; | ||
| 86 | + epilogueParams.x2ScaleGmAddr = wScale; | ||
| 87 | + epilogueParams.x1ScaleGmAddr = xScale; | ||
| 88 | + epilogueParams.biasGmAddr = bias; | ||
| 89 | + epilogueParams.logitGmAddr = logit; | ||
| 90 | + epilogueParams.rowIndexGmAddr = rowIndex; | ||
| 91 | + if constexpr (IsDeterministic_) { | ||
| 92 | + epilogueParams.workspaceGmAddr = AscendC::GetUserWorkspace(workspaceGM); | ||
| 93 | + } | ||
| 94 | + | ||
| 95 | + typename GmmKernel::GmmTiling gmmTiling{gmmParamsIn.groupNum, | ||
| 96 | + gmmParamsIn.batch, | ||
| 97 | + gmmParamsIn.sharedInputOffset, | ||
| 98 | + gmmParamsIn.sharedInputLen, | ||
| 99 | + gmmParamsIn.residualScale, | ||
| 100 | + static_cast<uint32_t>(matmulTiling.baseM), | ||
| 101 | + static_cast<uint32_t>(matmulTiling.baseN), | ||
| 102 | + static_cast<uint32_t>(matmulTiling.baseK), | ||
| 103 | + kAL1, | ||
| 104 | + kBL1, | ||
| 105 | + DOUBLE_BUFFER_NUM, | ||
| 106 | + gmmParamsIn.hasBias, | ||
| 107 | + static_cast<uint8_t>(matmulTiling.dbL0C), | ||
| 108 | + gmmParamsIn.groupListType, | ||
| 109 | + IsDeterministic_ ? gmmParamsIn.deterWorkspaceSize : 0, | ||
| 110 | + IsDeterministic_ ? static_cast<uint32_t>(matmulTiling.usedCoreNum) : 0}; | ||
| 111 | + typename GmmKernel::Params params{ | ||
| 112 | + ProblemShape{static_cast<int64_t>(matmulTiling.M), static_cast<int64_t>(matmulTiling.N), | ||
| 113 | + static_cast<int64_t>(matmulTiling.Ka), static_cast<int64_t>(1)}, | ||
| 114 | + mmadParams, | ||
| 115 | + sharedInputParams, | ||
| 116 | + epilogueParams, | ||
| 117 | + groupList, | ||
| 118 | + gmmTiling}; | ||
| 119 | + GmmKernel gmm; | ||
| 120 | + gmm(params); | ||
| 121 | + (void)workspaceGM; | ||
| 122 | +} | ||
| 123 | + | ||
| 124 | +template <typename LayoutA_, typename LayoutB_, int ScaleType, int RowIndexTypeValue> | ||
| 125 | +__aicore__ inline void InvokeBlazePertokenKernel(GM_ADDR x, GM_ADDR w, GM_ADDR wScale, GM_ADDR bias, GM_ADDR xScale, | ||
| 126 | + GM_ADDR groupList, GM_ADDR shareInput, GM_ADDR logit, GM_ADDR rowIndex, | ||
| 127 | + GM_ADDR offset, GM_ADDR y, GM_ADDR workspaceGM, GM_ADDR tilingGM) | ||
| 128 | +{ | ||
| 129 | + REGISTER_TILING_DEFAULT(GMMFinalizeRoutingArch35Tiling::GMMFinalizeRoutingTilingData); | ||
| 130 | + GET_TILING_DATA(tilingData, tilingGM); | ||
| 131 | + if (tilingData.gmmFinalizeRoutingDataParams.deterministicFlag == 1) { | ||
| 132 | + InvokeBlazePertokenKernelImpl<LayoutA_, LayoutB_, ScaleType, RowIndexTypeValue, true>( | ||
| 133 | + x, w, wScale, bias, xScale, groupList, shareInput, logit, rowIndex, offset, y, workspaceGM, tilingData); | ||
| 134 | + } else { | ||
| 135 | + InvokeBlazePertokenKernelImpl<LayoutA_, LayoutB_, ScaleType, RowIndexTypeValue, false>( | ||
| 136 | + x, w, wScale, bias, xScale, groupList, shareInput, logit, rowIndex, offset, y, workspaceGM, tilingData); | ||
| 137 | + } | ||
| 138 | +} | ||
| 139 | +} // namespace GroupedMatmulFinalizeRouting | ||
| 140 | + | ||
| 141 | +#endif | ||
| @@ -1,12 +1,12 @@ | |||
| 1 | /** | 1 | /** |
| 2 | - * Copyright (c) 2026 Huawei Technologies Co., Ltd. | 2 | + * Copyright (c) 2026 Huawei Technologies Co., Ltd. |
| 3 | - * This program is free software, you can redistribute it and/or modify it under the terms and conditions of | 3 | + * This program is free software, you can redistribute it and/or modify it under the terms and conditions of |
| 4 | - * CANN Open Software License Agreement Version 2.0 (the "License"). | 4 | + * CANN Open Software License Agreement Version 2.0 (the "License"). |
| 5 | - * Please refer to the License for details. You may not use this file except in compliance with the License. | 5 | + * Please refer to the License for details. You may not use this file except in compliance with the License. |
| 6 | - * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, | 6 | + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, |
| 7 | - * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. | 7 | + * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. |
| 8 | - * See LICENSE in the root of the software repository for the full text of the License. | 8 | + * See LICENSE in the root of the software repository for the full text of the License. |
| 9 | - */ | 9 | + */ |
| 10 | 10 | ||
| 11 | /*! | 11 | /*! |
| 12 | * \file grouped_matmul_finalize_routing_apt.cpp | 12 | * \file grouped_matmul_finalize_routing_apt.cpp |
| @@ -22,6 +22,13 @@ | |||
| 22 | #include "kernel_operator.h" | 22 | #include "kernel_operator.h" |
| 23 | #endif | 23 | #endif |
| 24 | 24 | ||
| 25 | +// Only the Ascend950 path uses the migrated Tensor API kernels. | ||
| 26 | +// Keep the macro-undefined C310 pass consistent with the existing Ascend950 build. | ||
| 27 | +#if (defined(__NPU_ARCH__) && __NPU_ARCH__ == 3510) || \ | ||
| 28 | + (!defined(__NPU_ARCH__) && defined(__CCE_AICORE__) && __CCE_AICORE__ == 310) | ||
| 29 | +#define GMM_FR_USE_TENSOR_API | ||
| 30 | +#endif | ||
| 31 | + | ||
| 25 | #if defined(ORIG_DTYPE_X) && defined(ORIG_DTYPE_W) && defined(DT_FLOAT8_E4M3FN) && defined(DT_FLOAT4_E2M1) | 32 | #if defined(ORIG_DTYPE_X) && defined(ORIG_DTYPE_W) && defined(DT_FLOAT8_E4M3FN) && defined(DT_FLOAT4_E2M1) |
| 26 | #if defined(__CCE_AICORE__) && __CCE_AICORE__ == 310 && \ | 33 | #if defined(__CCE_AICORE__) && __CCE_AICORE__ == 310 && \ |
| 27 | (!defined(__NPU_ARCH__) || (__NPU_ARCH__ != 9201 && __NPU_ARCH__ != 9202)) && \ | 34 | (!defined(__NPU_ARCH__) || (__NPU_ARCH__ != 9201 && __NPU_ARCH__ != 9202)) && \ |
| @@ -66,10 +73,14 @@ | |||
| 66 | #endif | 73 | #endif |
| 67 | #elif defined(V310_GMM_FR_ANTI_QUANT) | 74 | #elif defined(V310_GMM_FR_ANTI_QUANT) |
| 68 | // Weight Quantization scenario (伪量化场景) | 75 | // Weight Quantization scenario (伪量化场景) |
| 69 | -#include "arch35/common/basic_block_config.h" | ||
| 70 | #include "arch35/weight_quant_basic_block/gmm_fr_weight_quant_tiling_data.h" | 76 | #include "arch35/weight_quant_basic_block/gmm_fr_weight_quant_tiling_data.h" |
| 71 | #include "arch35/weight_quant_basic_block/gmm_fr_weight_quant_tiling_key.h" | 77 | #include "arch35/weight_quant_basic_block/gmm_fr_weight_quant_tiling_key.h" |
| 78 | +#if defined(GMM_FR_USE_TENSOR_API) | ||
| 79 | +#include "arch35/grouped_matmul_finalize_routing_a8w4_blaze.h" | ||
| 80 | +#else | ||
| 81 | +#include "arch35/common/basic_block_config.h" | ||
| 72 | #include "arch35/weight_quant_basic_block/gmm_fr_weight_quant_resplit_controller.h" | 82 | #include "arch35/weight_quant_basic_block/gmm_fr_weight_quant_resplit_controller.h" |
| 83 | +#endif | ||
| 73 | #elif defined(V9201_GMM_FINALIZE_ROUTING_WEIGHT_QUANT) || defined(V9201_GMM_FINALIZE_ROUTING_HIF4) | 84 | #elif defined(V9201_GMM_FINALIZE_ROUTING_WEIGHT_QUANT) || defined(V9201_GMM_FINALIZE_ROUTING_HIF4) |
| 74 | #include "lib/matmul_intf.h" | 85 | #include "lib/matmul_intf.h" |
| 75 | #include "arch35/grouped_matmul_finalize_routing_tiling_key.h" | 86 | #include "arch35/grouped_matmul_finalize_routing_tiling_key.h" |
| @@ -78,25 +89,30 @@ | |||
| 78 | // Full Quantization scenario (全量化场景) | 89 | // Full Quantization scenario (全量化场景) |
| 79 | #include "lib/matmul_intf.h" | 90 | #include "lib/matmul_intf.h" |
| 80 | #include "arch35/grouped_matmul_finalize_routing_tiling_key.h" | 91 | #include "arch35/grouped_matmul_finalize_routing_tiling_key.h" |
| 81 | -#if ORIG_DTYPE_X != DT_FLOAT8_E4M3FN && ORIG_DTYPE_X != DT_FLOAT8_E5M2 && ORIG_DTYPE_X != DT_FLOAT4_E1M2 && \ | 92 | +#if ORIG_DTYPE_X == DT_FLOAT8_E4M3FN || ORIG_DTYPE_X == DT_FLOAT8_E5M2 || ORIG_DTYPE_X == DT_FLOAT4_E1M2 || \ |
| 82 | - ORIG_DTYPE_X != DT_FLOAT4_E2M1 | 93 | + ORIG_DTYPE_X == DT_FLOAT4_E2M1 |
| 83 | -#include "arch35/grouped_matmul_finalize_routing_pertoken_dequant.h" | ||
| 84 | -#elif ORIG_DTYPE_X == DT_FLOAT4_E1M2 || ORIG_DTYPE_X == DT_FLOAT4_E2M1 | ||
| 85 | #if !defined(__NPU_ARCH__) || (__NPU_ARCH__ != 9201 && __NPU_ARCH__ != 9202) | 94 | #if !defined(__NPU_ARCH__) || (__NPU_ARCH__ != 9201 && __NPU_ARCH__ != 9202) |
| 86 | #include "arch35/grouped_matmul_finalize_routing_arch35.h" | 95 | #include "arch35/grouped_matmul_finalize_routing_arch35.h" |
| 87 | #else | 96 | #else |
| 88 | #include "arch35/grouped_matmul_finalize_routing_mx_legacy.h" | 97 | #include "arch35/grouped_matmul_finalize_routing_mx_legacy.h" |
| 89 | #endif | 98 | #endif |
| 90 | -#else | ||
| 91 | -#if !defined(__NPU_ARCH__) || (__NPU_ARCH__ != 9201 && __NPU_ARCH__ != 9202) | ||
| 92 | -#include "arch35/grouped_matmul_finalize_routing_arch35.h" | ||
| 93 | -#else | ||
| 94 | -#include "arch35/grouped_matmul_finalize_routing_mx_legacy.h" | ||
| 95 | #endif | 99 | #endif |
| 100 | +#if ORIG_DTYPE_X != DT_FLOAT4_E1M2 && ORIG_DTYPE_X != DT_FLOAT4_E2M1 | ||
| 101 | +#if defined(GMM_FR_USE_TENSOR_API) | ||
| 102 | +#include "arch35/grouped_matmul_finalize_routing_pertoken_blaze.h" | ||
| 103 | +#else | ||
| 96 | #include "arch35/grouped_matmul_finalize_routing_pertoken_dequant.h" | 104 | #include "arch35/grouped_matmul_finalize_routing_pertoken_dequant.h" |
| 97 | #endif | 105 | #endif |
| 98 | #endif | 106 | #endif |
| 107 | +#endif | ||
| 99 | 108 | ||
| 109 | +// Values match the existing host/device template-key encoding. | ||
| 110 | +static constexpr uint64_t GMM_FR_NO_TRANS = 0; | ||
检查一下是否已有公共的定义 ![]() ![]() | |||
| 111 | +static constexpr uint64_t GMM_FR_TRANS = 1; | ||
| 112 | +static constexpr uint64_t GMM_FR_SCALE_MX = 0; | ||
| 113 | +static constexpr uint64_t GMM_FR_SCALE_FP32 = 1; | ||
| 114 | +static constexpr uint64_t GMM_FR_ROW_INDEX_INT64 = 0; | ||
| 115 | +static constexpr uint64_t GMM_FR_ROW_INDEX_INT32 = 1; | ||
| 100 | static constexpr uint64_t BF16TYPE = 2; | 116 | static constexpr uint64_t BF16TYPE = 2; |
| 101 | static constexpr uint64_t LOGIT_BF16_TYPE = 1; | 117 | static constexpr uint64_t LOGIT_BF16_TYPE = 1; |
| 102 | 118 | ||
| @@ -109,12 +125,14 @@ static constexpr bool GMM_FR_WEIGHT_NZ = false; | |||
| 109 | 125 | ||
| 110 | template <int BTRANS, bool WEIGHT_NZ> | 126 | template <int BTRANS, bool WEIGHT_NZ> |
| 111 | struct GmmFrMxLayoutBSelector { | 127 | struct GmmFrMxLayoutBSelector { |
| 112 | - using type = AscendC::Std::conditional_t<BTRANS == 0, AscendC::Te::NZLayoutPtn, AscendC::Te::ZNLayoutPtn>; | 128 | + using type = |
| 129 | + AscendC::Std::conditional_t<BTRANS == GMM_FR_NO_TRANS, AscendC::Te::NZLayoutPtn, AscendC::Te::ZNLayoutPtn>; | ||
| 113 | }; | 130 | }; |
| 114 | 131 | ||
| 115 | template <int BTRANS> | 132 | template <int BTRANS> |
| 116 | struct GmmFrMxLayoutBSelector<BTRANS, false> { | 133 | struct GmmFrMxLayoutBSelector<BTRANS, false> { |
| 117 | - using type = AscendC::Std::conditional_t<BTRANS == 0, AscendC::Te::NDExtLayoutPtn, AscendC::Te::DNExtLayoutPtn>; | 134 | + using type = AscendC::Std::conditional_t<BTRANS == GMM_FR_NO_TRANS, AscendC::Te::NDExtLayoutPtn, |
| 135 | + AscendC::Te::DNExtLayoutPtn>; | ||
| 118 | }; | 136 | }; |
| 119 | 137 | ||
| 120 | template <int BTRANS, int LOGITTYPE> | 138 | template <int BTRANS, int LOGITTYPE> |
| @@ -130,6 +148,29 @@ __aicore__ inline void RunGmmFrMx(GM_ADDR x, GM_ADDR w, GM_ADDR scale, GM_ADDR b | |||
| 130 | 148 | ||
| 131 | #endif | 149 | #endif |
| 132 | 150 | ||
| 151 | +#if !defined(GMM_FR_BF16_OUTPUT) && !defined(V310_GMM_FR_ANTI_QUANT) && \ | ||
| 152 | + !defined(V9201_GMM_FINALIZE_ROUTING_WEIGHT_QUANT) && !defined(V9201_GMM_FINALIZE_ROUTING_HIF4) && \ | ||
| 153 | + ORIG_DTYPE_X != DT_FLOAT4_E1M2 && ORIG_DTYPE_X != DT_FLOAT4_E2M1 | ||
| 154 | +template <int BTRANS, int SCALETYPE, int ROWINDEXTYPE> | ||
| 155 | +__aicore__ inline void RunGmmFrPertoken(GM_ADDR x, GM_ADDR w, GM_ADDR scale, GM_ADDR bias, GM_ADDR pertokenScale, | ||
| 156 | + GM_ADDR groupList, GM_ADDR shareInput, GM_ADDR logit, GM_ADDR rowIndex, | ||
| 157 | + GM_ADDR offset, GM_ADDR y, GM_ADDR workspaceGM, GM_ADDR tilingGM) | ||
| 158 | +{ | ||
| 159 | +#if defined(GMM_FR_USE_TENSOR_API) | ||
| 160 | + using LayoutB = | ||
| 161 | + AscendC::Std::conditional_t<BTRANS == GMM_FR_NO_TRANS, asc::te::nz_layout_ptn, asc::te::zn_layout_ptn>; | ||
| 162 | + GroupedMatmulFinalizeRouting::InvokeBlazePertokenKernel<asc::te::nd_ext_layout_ptn, LayoutB, SCALETYPE, | ||
| 163 | + ROWINDEXTYPE>( | ||
| 164 | + x, w, scale, bias, pertokenScale, groupList, shareInput, logit, rowIndex, offset, y, workspaceGM, tilingGM); | ||
| 165 | +#else | ||
| 166 | + using LayoutB = | ||
| 167 | + AscendC::Std::conditional_t<BTRANS == GMM_FR_NO_TRANS, Cgmct::Gemm::layout::Nz, Cgmct::Gemm::layout::Zn>; | ||
| 168 | + grouped_matmul_finalize_routing_pertoken_dequant<Cgmct::Gemm::layout::RowMajor, LayoutB, SCALETYPE, ROWINDEXTYPE>( | ||
| 169 | + x, w, scale, bias, pertokenScale, groupList, shareInput, logit, rowIndex, offset, y, workspaceGM, tilingGM); | ||
| 170 | +#endif | ||
| 171 | +} | ||
| 172 | +#endif | ||
| 173 | + | ||
| 133 | #if defined(V310_GMM_FR_ANTI_QUANT) | 174 | #if defined(V310_GMM_FR_ANTI_QUANT) |
| 134 | template <int ATRANS, int BTRANS, int HASBIAS> | 175 | template <int ATRANS, int BTRANS, int HASBIAS> |
| 135 | #else | 176 | #else |
| @@ -150,10 +191,14 @@ __global__ __aicore__ void grouped_matmul_finalize_routing(GM_ADDR x, GM_ADDR w, | |||
| 150 | #endif | 191 | #endif |
| 151 | AscendC::InitSocState(); | 192 | AscendC::InitSocState(); |
| 152 | KERNEL_TASK_TYPE_DEFAULT(KERNEL_TYPE_MIX_AIC_1_2); | 193 | KERNEL_TASK_TYPE_DEFAULT(KERNEL_TYPE_MIX_AIC_1_2); |
| 153 | - // Weight Quantization scenario - Use GMMFRWeightQuantResplitController | ||
| 154 | REGISTER_TILING_DEFAULT(GMMFinalizeRoutingArch35Tiling::GMMFinalizeRoutingWeightQuantTilingData); | 194 | REGISTER_TILING_DEFAULT(GMMFinalizeRoutingArch35Tiling::GMMFinalizeRoutingWeightQuantTilingData); |
| 155 | GET_TILING_DATA_WITH_STRUCT(GMMFinalizeRoutingArch35Tiling::GMMFinalizeRoutingWeightQuantTilingData, tilingData, | 195 | GET_TILING_DATA_WITH_STRUCT(GMMFinalizeRoutingArch35Tiling::GMMFinalizeRoutingWeightQuantTilingData, tilingData, |
| 156 | tilingGM); | 196 | tilingGM); |
| 197 | +#if defined(GMM_FR_USE_TENSOR_API) | ||
| 198 | + GroupedMatmulFinalizeRouting::InvokeBlazeA8W4Kernel<DTYPE_X, DTYPE_W, DTYPE_SCALE, DTYPE_PERTOKEN_SCALE, DTYPE_BIAS, | ||
| 199 | + DTYPE_Y, bfloat16_t, float, int64_t, HASBIAS>( | ||
| 200 | + x, w, scale, bias, group_list, pertoken_scale, logit, row_index, y, share_input, &tilingData); | ||
| 201 | +#else | ||
| 157 | const GMMFinalizeRoutingArch35Tiling::GMMFinalizeRoutingWeightQuantTilingData* tiling = &tilingData; | 202 | const GMMFinalizeRoutingArch35Tiling::GMMFinalizeRoutingWeightQuantTilingData* tiling = &tilingData; |
| 158 | 203 | ||
| 159 | // Use pre-defined MXA8W4_NZNK config for FP8+FP4 MX (Microscaling) format | 204 | // Use pre-defined MXA8W4_NZNK config for FP8+FP4 MX (Microscaling) format |
| @@ -168,10 +213,12 @@ __global__ __aicore__ void grouped_matmul_finalize_routing(GM_ADDR x, GM_ADDR w, | |||
| 168 | controller.Init(x, w, scale, bias, group_list, pertoken_scale, logit, row_index, y, share_input, tiling); | 213 | controller.Init(x, w, scale, bias, group_list, pertoken_scale, logit, row_index, y, share_input, tiling); |
| 169 | controller.Process(); | 214 | controller.Process(); |
| 170 | #endif | 215 | #endif |
| 216 | +#endif | ||
| 171 | #elif defined(V9201_GMM_FINALIZE_ROUTING_WEIGHT_QUANT) | 217 | #elif defined(V9201_GMM_FINALIZE_ROUTING_WEIGHT_QUANT) |
| 172 | AscendC::TPipe pipe; | 218 | AscendC::TPipe pipe; |
| 173 | KERNEL_TASK_TYPE_DEFAULT(KERNEL_TYPE_MIX_AIC_1_1); | 219 | KERNEL_TASK_TYPE_DEFAULT(KERNEL_TYPE_MIX_AIC_1_1); |
| 174 | - if constexpr (ATRANS == 0 && BTRANS == 1 && SCALETYPE == 0 && ROWINDEXTYPE == 0) { | 220 | + if constexpr (ATRANS == GMM_FR_NO_TRANS && BTRANS == GMM_FR_TRANS && SCALETYPE == GMM_FR_SCALE_MX && |
| 221 | + ROWINDEXTYPE == GMM_FR_ROW_INDEX_INT64) { | ||
| 175 | grouped_matmul_finalize_routing_mx_basic_api<Cgmct::Gemm::layout::RowMajor, Cgmct::Gemm::layout::Zn>( | 222 | grouped_matmul_finalize_routing_mx_basic_api<Cgmct::Gemm::layout::RowMajor, Cgmct::Gemm::layout::Zn>( |
| 176 | x, w, scale, bias, pertoken_scale, group_list, share_input, logit, row_index, offset, y, workspaceGM, | 223 | x, w, scale, bias, pertoken_scale, group_list, share_input, logit, row_index, offset, y, workspaceGM, |
| 177 | tilingGM); | 224 | tilingGM); |
| @@ -179,11 +226,13 @@ __global__ __aicore__ void grouped_matmul_finalize_routing(GM_ADDR x, GM_ADDR w, | |||
| 179 | #elif defined(V9201_GMM_FINALIZE_ROUTING_HIF4) | 226 | #elif defined(V9201_GMM_FINALIZE_ROUTING_HIF4) |
| 180 | AscendC::TPipe pipe; | 227 | AscendC::TPipe pipe; |
| 181 | KERNEL_TASK_TYPE_DEFAULT(KERNEL_TYPE_MIX_AIC_1_1); | 228 | KERNEL_TASK_TYPE_DEFAULT(KERNEL_TYPE_MIX_AIC_1_1); |
| 182 | - if constexpr (ATRANS == 0 && BTRANS == 0 && SCALETYPE == 0 && ROWINDEXTYPE == 0) { | 229 | + if constexpr (ATRANS == GMM_FR_NO_TRANS && BTRANS == GMM_FR_NO_TRANS && SCALETYPE == GMM_FR_SCALE_MX && |
| 230 | + ROWINDEXTYPE == GMM_FR_ROW_INDEX_INT64) { | ||
| 183 | grouped_matmul_finalize_routing_mx_basic_api<Cgmct::Gemm::layout::RowMajor, Cgmct::Gemm::layout::RowMajor>( | 231 | grouped_matmul_finalize_routing_mx_basic_api<Cgmct::Gemm::layout::RowMajor, Cgmct::Gemm::layout::RowMajor>( |
| 184 | x, w, scale, bias, pertoken_scale, group_list, share_input, logit, row_index, offset, y, workspaceGM, | 232 | x, w, scale, bias, pertoken_scale, group_list, share_input, logit, row_index, offset, y, workspaceGM, |
| 185 | tilingGM); | 233 | tilingGM); |
| 186 | - } else if constexpr (ATRANS == 0 && BTRANS == 1 && SCALETYPE == 0 && ROWINDEXTYPE == 0) { | 234 | + } else if constexpr (ATRANS == GMM_FR_NO_TRANS && BTRANS == GMM_FR_TRANS && SCALETYPE == GMM_FR_SCALE_MX && |
| 235 | + ROWINDEXTYPE == GMM_FR_ROW_INDEX_INT64) { | ||
| 187 | grouped_matmul_finalize_routing_mx_basic_api<Cgmct::Gemm::layout::RowMajor, Cgmct::Gemm::layout::ColumnMajor>( | 236 | grouped_matmul_finalize_routing_mx_basic_api<Cgmct::Gemm::layout::RowMajor, Cgmct::Gemm::layout::ColumnMajor>( |
| 188 | x, w, scale, bias, pertoken_scale, group_list, share_input, logit, row_index, offset, y, workspaceGM, | 237 | x, w, scale, bias, pertoken_scale, group_list, share_input, logit, row_index, offset, y, workspaceGM, |
| 189 | tilingGM); | 238 | tilingGM); |
| @@ -192,12 +241,14 @@ __global__ __aicore__ void grouped_matmul_finalize_routing(GM_ADDR x, GM_ADDR w, | |||
| 192 | #if defined(GMM_FR_FULL_MX) | 241 | #if defined(GMM_FR_FULL_MX) |
| 193 | AscendC::TPipe pipe; | 242 | AscendC::TPipe pipe; |
| 194 | KERNEL_TASK_TYPE_DEFAULT(KERNEL_TYPE_MIX_AIC_1_2); | 243 | KERNEL_TASK_TYPE_DEFAULT(KERNEL_TYPE_MIX_AIC_1_2); |
| 195 | - if constexpr (GMM_FR_WEIGHT_NZ && ATRANS == 0 && BTRANS == 0 && SCALETYPE == 0 && ROWINDEXTYPE == 1) { | 244 | + if constexpr (GMM_FR_WEIGHT_NZ && ATRANS == GMM_FR_NO_TRANS && BTRANS == GMM_FR_NO_TRANS && |
| 196 | - RunGmmFrMx<0, LOGITTYPE>(x, w, scale, bias, pertoken_scale, group_list, share_input, logit, row_index, offset, | 245 | + SCALETYPE == GMM_FR_SCALE_MX && ROWINDEXTYPE == GMM_FR_ROW_INDEX_INT32) { |
| 197 | - y, workspaceGM, tilingGM); | 246 | + RunGmmFrMx<GMM_FR_NO_TRANS, LOGITTYPE>(x, w, scale, bias, pertoken_scale, group_list, share_input, logit, |
| 198 | - } else if constexpr (GMM_FR_WEIGHT_NZ && ATRANS == 0 && BTRANS == 1 && SCALETYPE == 0 && ROWINDEXTYPE == 1) { | 247 | + row_index, offset, y, workspaceGM, tilingGM); |
| 199 | - RunGmmFrMx<1, LOGITTYPE>(x, w, scale, bias, pertoken_scale, group_list, share_input, logit, row_index, offset, | 248 | + } else if constexpr (GMM_FR_WEIGHT_NZ && ATRANS == GMM_FR_NO_TRANS && BTRANS == GMM_FR_TRANS && |
| 200 | - y, workspaceGM, tilingGM); | 249 | + SCALETYPE == GMM_FR_SCALE_MX && ROWINDEXTYPE == GMM_FR_ROW_INDEX_INT32) { |
| 250 | + RunGmmFrMx<GMM_FR_TRANS, LOGITTYPE>(x, w, scale, bias, pertoken_scale, group_list, share_input, logit, | ||
| 251 | + row_index, offset, y, workspaceGM, tilingGM); | ||
| 201 | } | 252 | } |
| 202 | #endif | 253 | #endif |
| 203 | #else | 254 | #else |
| @@ -210,117 +261,63 @@ __global__ __aicore__ void grouped_matmul_finalize_routing(GM_ADDR x, GM_ADDR w, | |||
| 210 | #endif | 261 | #endif |
| 211 | #if ORIG_DTYPE_X != DT_FLOAT8_E4M3FN && ORIG_DTYPE_X != DT_FLOAT8_E5M2 && ORIG_DTYPE_X != DT_FLOAT4_E1M2 && \ | 262 | #if ORIG_DTYPE_X != DT_FLOAT8_E4M3FN && ORIG_DTYPE_X != DT_FLOAT8_E5M2 && ORIG_DTYPE_X != DT_FLOAT4_E1M2 && \ |
| 212 | ORIG_DTYPE_X != DT_FLOAT4_E2M1 | 263 | ORIG_DTYPE_X != DT_FLOAT4_E2M1 |
| 213 | - if constexpr (ATRANS == 0 && BTRANS == 0 && SCALETYPE == 1 && ROWINDEXTYPE == 0) { | 264 | + if constexpr (ATRANS == GMM_FR_NO_TRANS && (BTRANS == GMM_FR_NO_TRANS || BTRANS == GMM_FR_TRANS) && |
| 214 | - grouped_matmul_finalize_routing_pertoken_dequant<Cgmct::Gemm::layout::RowMajor, Cgmct::Gemm::layout::Nz, 1, 0>( | 265 | + (SCALETYPE == GMM_FR_SCALE_FP32 || SCALETYPE == BF16TYPE) && |
| 215 | - x, w, scale, bias, pertoken_scale, group_list, share_input, logit, row_index, offset, y, workspaceGM, | 266 | + (ROWINDEXTYPE == GMM_FR_ROW_INDEX_INT64 || ROWINDEXTYPE == GMM_FR_ROW_INDEX_INT32)) { |
| 216 | - tilingGM); | 267 | + RunGmmFrPertoken<BTRANS, SCALETYPE, ROWINDEXTYPE>(x, w, scale, bias, pertoken_scale, group_list, share_input, |
| 217 | - } else if constexpr (ATRANS == 0 && BTRANS == 1 && SCALETYPE == 1 && ROWINDEXTYPE == 0) { | 268 | + logit, row_index, offset, y, workspaceGM, tilingGM); |
| 218 | - grouped_matmul_finalize_routing_pertoken_dequant<Cgmct::Gemm::layout::RowMajor, Cgmct::Gemm::layout::Zn, 1, 0>( | ||
| 219 | - x, w, scale, bias, pertoken_scale, group_list, share_input, logit, row_index, offset, y, workspaceGM, | ||
| 220 | - tilingGM); | ||
| 221 | - } else if constexpr (ATRANS == 0 && BTRANS == 0 && SCALETYPE == BF16TYPE && ROWINDEXTYPE == 0) { | ||
| 222 | - grouped_matmul_finalize_routing_pertoken_dequant<Cgmct::Gemm::layout::RowMajor, Cgmct::Gemm::layout::Nz, | ||
| 223 | - BF16TYPE, 0>(x, w, scale, bias, pertoken_scale, group_list, | ||
| 224 | - share_input, logit, row_index, offset, y, | ||
| 225 | - workspaceGM, tilingGM); | ||
| 226 | - } else if constexpr (ATRANS == 0 && BTRANS == 1 && SCALETYPE == BF16TYPE && ROWINDEXTYPE == 0) { | ||
| 227 | - grouped_matmul_finalize_routing_pertoken_dequant<Cgmct::Gemm::layout::RowMajor, Cgmct::Gemm::layout::Zn, | ||
| 228 | - BF16TYPE, 0>(x, w, scale, bias, pertoken_scale, group_list, | ||
| 229 | - share_input, logit, row_index, offset, y, | ||
| 230 | - workspaceGM, tilingGM); | ||
| 231 | - } else if constexpr (ATRANS == 0 && BTRANS == 0 && SCALETYPE == 1 && ROWINDEXTYPE == 1) { | ||
| 232 | - grouped_matmul_finalize_routing_pertoken_dequant<Cgmct::Gemm::layout::RowMajor, Cgmct::Gemm::layout::Nz, 1, 1>( | ||
| 233 | - x, w, scale, bias, pertoken_scale, group_list, share_input, logit, row_index, offset, y, workspaceGM, | ||
| 234 | - tilingGM); | ||
| 235 | - } else if constexpr (ATRANS == 0 && BTRANS == 1 && SCALETYPE == 1 && ROWINDEXTYPE == 1) { | ||
| 236 | - grouped_matmul_finalize_routing_pertoken_dequant<Cgmct::Gemm::layout::RowMajor, Cgmct::Gemm::layout::Zn, 1, 1>( | ||
| 237 | - x, w, scale, bias, pertoken_scale, group_list, share_input, logit, row_index, offset, y, workspaceGM, | ||
| 238 | - tilingGM); | ||
| 239 | - } else if constexpr (ATRANS == 0 && BTRANS == 0 && SCALETYPE == BF16TYPE && ROWINDEXTYPE == 1) { | ||
| 240 | - grouped_matmul_finalize_routing_pertoken_dequant<Cgmct::Gemm::layout::RowMajor, Cgmct::Gemm::layout::Nz, | ||
| 241 | - BF16TYPE, 1>(x, w, scale, bias, pertoken_scale, group_list, | ||
| 242 | - share_input, logit, row_index, offset, y, | ||
| 243 | - workspaceGM, tilingGM); | ||
| 244 | - } else if constexpr (ATRANS == 0 && BTRANS == 1 && SCALETYPE == BF16TYPE && ROWINDEXTYPE == 1) { | ||
| 245 | - grouped_matmul_finalize_routing_pertoken_dequant<Cgmct::Gemm::layout::RowMajor, Cgmct::Gemm::layout::Zn, | ||
| 246 | - BF16TYPE, 1>(x, w, scale, bias, pertoken_scale, group_list, | ||
| 247 | - share_input, logit, row_index, offset, y, | ||
| 248 | - workspaceGM, tilingGM); | ||
| 249 | } | 269 | } |
| 250 | #elif ORIG_DTYPE_X == DT_FLOAT4_E1M2 || ORIG_DTYPE_X == DT_FLOAT4_E2M1 | 270 | #elif ORIG_DTYPE_X == DT_FLOAT4_E1M2 || ORIG_DTYPE_X == DT_FLOAT4_E2M1 |
| 251 | #if defined(__NPU_ARCH__) && (__NPU_ARCH__ == 9201 || __NPU_ARCH__ == 9202) | 271 | #if defined(__NPU_ARCH__) && (__NPU_ARCH__ == 9201 || __NPU_ARCH__ == 9202) |
| 252 | - if constexpr (ATRANS == 0 && BTRANS == 0 && SCALETYPE == 0 && ROWINDEXTYPE == 0) { | 272 | + if constexpr (ATRANS == GMM_FR_NO_TRANS && BTRANS == GMM_FR_NO_TRANS && SCALETYPE == GMM_FR_SCALE_MX && |
| 273 | + ROWINDEXTYPE == GMM_FR_ROW_INDEX_INT64) { | ||
| 253 | grouped_matmul_finalize_routing_mx_legacy<Cgmct::Gemm::layout::RowMajor, Cgmct::Gemm::layout::RowMajor>( | 274 | grouped_matmul_finalize_routing_mx_legacy<Cgmct::Gemm::layout::RowMajor, Cgmct::Gemm::layout::RowMajor>( |
| 254 | x, w, scale, bias, pertoken_scale, group_list, share_input, logit, row_index, offset, y, workspaceGM, | 275 | x, w, scale, bias, pertoken_scale, group_list, share_input, logit, row_index, offset, y, workspaceGM, |
| 255 | tilingGM); | 276 | tilingGM); |
| 256 | - } else if constexpr (ATRANS == 0 && BTRANS == 1 && SCALETYPE == 0 && ROWINDEXTYPE == 0) { | 277 | + } else if constexpr (ATRANS == GMM_FR_NO_TRANS && BTRANS == GMM_FR_TRANS && SCALETYPE == GMM_FR_SCALE_MX && |
| 278 | + ROWINDEXTYPE == GMM_FR_ROW_INDEX_INT64) { | ||
| 257 | grouped_matmul_finalize_routing_mx_legacy<Cgmct::Gemm::layout::RowMajor, Cgmct::Gemm::layout::ColumnMajor>( | 279 | grouped_matmul_finalize_routing_mx_legacy<Cgmct::Gemm::layout::RowMajor, Cgmct::Gemm::layout::ColumnMajor>( |
| 258 | x, w, scale, bias, pertoken_scale, group_list, share_input, logit, row_index, offset, y, workspaceGM, | 280 | x, w, scale, bias, pertoken_scale, group_list, share_input, logit, row_index, offset, y, workspaceGM, |
| 259 | tilingGM); | 281 | tilingGM); |
| 260 | } | 282 | } |
| 261 | #else | 283 | #else |
| 262 | - if constexpr (ATRANS == 0 && BTRANS == 0 && SCALETYPE == 0 && ROWINDEXTYPE == 0) { | 284 | + if constexpr (ATRANS == GMM_FR_NO_TRANS && BTRANS == GMM_FR_NO_TRANS && SCALETYPE == GMM_FR_SCALE_MX && |
| 263 | - RunGmmFrMx<0, LOGITTYPE>(x, w, scale, bias, pertoken_scale, group_list, share_input, logit, row_index, offset, | 285 | + ROWINDEXTYPE == GMM_FR_ROW_INDEX_INT64) { |
| 264 | - y, workspaceGM, tilingGM); | 286 | + RunGmmFrMx<GMM_FR_NO_TRANS, LOGITTYPE>(x, w, scale, bias, pertoken_scale, group_list, share_input, logit, |
| 265 | - } else if constexpr (ATRANS == 0 && BTRANS == 1 && SCALETYPE == 0 && ROWINDEXTYPE == 0) { | 287 | + row_index, offset, y, workspaceGM, tilingGM); |
| 266 | - RunGmmFrMx<1, LOGITTYPE>(x, w, scale, bias, pertoken_scale, group_list, share_input, logit, row_index, offset, | 288 | + } else if constexpr (ATRANS == GMM_FR_NO_TRANS && BTRANS == GMM_FR_TRANS && SCALETYPE == GMM_FR_SCALE_MX && |
| 267 | - y, workspaceGM, tilingGM); | 289 | + ROWINDEXTYPE == GMM_FR_ROW_INDEX_INT64) { |
| 290 | + RunGmmFrMx<GMM_FR_TRANS, LOGITTYPE>(x, w, scale, bias, pertoken_scale, group_list, share_input, logit, | ||
| 291 | + row_index, offset, y, workspaceGM, tilingGM); | ||
| 268 | } | 292 | } |
| 269 | #endif | 293 | #endif |
| 270 | #else | 294 | #else |
| 271 | #if defined(__NPU_ARCH__) && (__NPU_ARCH__ == 9201 || __NPU_ARCH__ == 9202) | 295 | #if defined(__NPU_ARCH__) && (__NPU_ARCH__ == 9201 || __NPU_ARCH__ == 9202) |
| 272 | - if constexpr (ATRANS == 0 && BTRANS == 0 && SCALETYPE == 0 && ROWINDEXTYPE == 0) { | 296 | + if constexpr (ATRANS == GMM_FR_NO_TRANS && BTRANS == GMM_FR_NO_TRANS && SCALETYPE == GMM_FR_SCALE_MX && |
| 297 | + ROWINDEXTYPE == GMM_FR_ROW_INDEX_INT64) { | ||
| 273 | grouped_matmul_finalize_routing_mx_legacy<Cgmct::Gemm::layout::RowMajor, Cgmct::Gemm::layout::RowMajor>( | 298 | grouped_matmul_finalize_routing_mx_legacy<Cgmct::Gemm::layout::RowMajor, Cgmct::Gemm::layout::RowMajor>( |
| 274 | x, w, scale, bias, pertoken_scale, group_list, share_input, logit, row_index, offset, y, workspaceGM, | 299 | x, w, scale, bias, pertoken_scale, group_list, share_input, logit, row_index, offset, y, workspaceGM, |
| 275 | tilingGM); | 300 | tilingGM); |
| 276 | - } else if constexpr (ATRANS == 0 && BTRANS == 1 && SCALETYPE == 0 && ROWINDEXTYPE == 0) { | 301 | + } else if constexpr (ATRANS == GMM_FR_NO_TRANS && BTRANS == GMM_FR_TRANS && SCALETYPE == GMM_FR_SCALE_MX && |
| 302 | + ROWINDEXTYPE == GMM_FR_ROW_INDEX_INT64) { | ||
| 277 | grouped_matmul_finalize_routing_mx_legacy<Cgmct::Gemm::layout::RowMajor, Cgmct::Gemm::layout::ColumnMajor>( | 303 | grouped_matmul_finalize_routing_mx_legacy<Cgmct::Gemm::layout::RowMajor, Cgmct::Gemm::layout::ColumnMajor>( |
| 278 | x, w, scale, bias, pertoken_scale, group_list, share_input, logit, row_index, offset, y, workspaceGM, | 304 | x, w, scale, bias, pertoken_scale, group_list, share_input, logit, row_index, offset, y, workspaceGM, |
| 279 | tilingGM); | 305 | tilingGM); |
| 280 | #else | 306 | #else |
| 281 | - if constexpr (ATRANS == 0 && BTRANS == 0 && SCALETYPE == 0 && ROWINDEXTYPE == 0) { | 307 | + if constexpr (ATRANS == GMM_FR_NO_TRANS && BTRANS == GMM_FR_NO_TRANS && SCALETYPE == GMM_FR_SCALE_MX && |
| 282 | - RunGmmFrMx<0, LOGITTYPE>(x, w, scale, bias, pertoken_scale, group_list, share_input, logit, row_index, offset, | 308 | + ROWINDEXTYPE == GMM_FR_ROW_INDEX_INT64) { |
| 283 | - y, workspaceGM, tilingGM); | 309 | + RunGmmFrMx<GMM_FR_NO_TRANS, LOGITTYPE>(x, w, scale, bias, pertoken_scale, group_list, share_input, logit, |
| 284 | - } else if constexpr (ATRANS == 0 && BTRANS == 1 && SCALETYPE == 0 && ROWINDEXTYPE == 0) { | 310 | + row_index, offset, y, workspaceGM, tilingGM); |
| 285 | - RunGmmFrMx<1, LOGITTYPE>(x, w, scale, bias, pertoken_scale, group_list, share_input, logit, row_index, offset, | 311 | + } else if constexpr (ATRANS == GMM_FR_NO_TRANS && BTRANS == GMM_FR_TRANS && SCALETYPE == GMM_FR_SCALE_MX && |
| 286 | - y, workspaceGM, tilingGM); | 312 | + ROWINDEXTYPE == GMM_FR_ROW_INDEX_INT64) { |
| 313 | + RunGmmFrMx<GMM_FR_TRANS, LOGITTYPE>(x, w, scale, bias, pertoken_scale, group_list, share_input, logit, | ||
| 314 | + row_index, offset, y, workspaceGM, tilingGM); | ||
| 287 | #endif | 315 | #endif |
| 288 | - } else if constexpr (ATRANS == 0 && BTRANS == 0 && SCALETYPE == 1 && ROWINDEXTYPE == 0) { | 316 | + } else if constexpr (ATRANS == GMM_FR_NO_TRANS && (BTRANS == GMM_FR_NO_TRANS || BTRANS == GMM_FR_TRANS) && |
| 289 | - grouped_matmul_finalize_routing_pertoken_dequant<Cgmct::Gemm::layout::RowMajor, Cgmct::Gemm::layout::Nz, 1, 0>( | 317 | + (SCALETYPE == GMM_FR_SCALE_FP32 || SCALETYPE == BF16TYPE) && |
| 290 | - x, w, scale, bias, pertoken_scale, group_list, share_input, logit, row_index, offset, y, workspaceGM, | 318 | + (ROWINDEXTYPE == GMM_FR_ROW_INDEX_INT64 || ROWINDEXTYPE == GMM_FR_ROW_INDEX_INT32)) { |
| 291 | - tilingGM); | 319 | + RunGmmFrPertoken<BTRANS, SCALETYPE, ROWINDEXTYPE>(x, w, scale, bias, pertoken_scale, group_list, share_input, |
| 292 | - } else if constexpr (ATRANS == 0 && BTRANS == 1 && SCALETYPE == 1 && ROWINDEXTYPE == 0) { | 320 | + logit, row_index, offset, y, workspaceGM, tilingGM); |
| 293 | - grouped_matmul_finalize_routing_pertoken_dequant<Cgmct::Gemm::layout::RowMajor, Cgmct::Gemm::layout::Zn, 1, 0>( | ||
| 294 | - x, w, scale, bias, pertoken_scale, group_list, share_input, logit, row_index, offset, y, workspaceGM, | ||
| 295 | - tilingGM); | ||
| 296 | - } else if constexpr (ATRANS == 0 && BTRANS == 0 && SCALETYPE == BF16TYPE && ROWINDEXTYPE == 0) { | ||
| 297 | - grouped_matmul_finalize_routing_pertoken_dequant<Cgmct::Gemm::layout::RowMajor, Cgmct::Gemm::layout::Nz, | ||
| 298 | - BF16TYPE, 0>(x, w, scale, bias, pertoken_scale, group_list, | ||
| 299 | - share_input, logit, row_index, offset, y, | ||
| 300 | - workspaceGM, tilingGM); | ||
| 301 | - } else if constexpr (ATRANS == 0 && BTRANS == 1 && SCALETYPE == BF16TYPE && ROWINDEXTYPE == 0) { | ||
| 302 | - grouped_matmul_finalize_routing_pertoken_dequant<Cgmct::Gemm::layout::RowMajor, Cgmct::Gemm::layout::Zn, | ||
| 303 | - BF16TYPE, 0>(x, w, scale, bias, pertoken_scale, group_list, | ||
| 304 | - share_input, logit, row_index, offset, y, | ||
| 305 | - workspaceGM, tilingGM); | ||
| 306 | - } else if constexpr (ATRANS == 0 && BTRANS == 0 && SCALETYPE == 1 && ROWINDEXTYPE == 1) { | ||
| 307 | - grouped_matmul_finalize_routing_pertoken_dequant<Cgmct::Gemm::layout::RowMajor, Cgmct::Gemm::layout::Nz, 1, 1>( | ||
| 308 | - x, w, scale, bias, pertoken_scale, group_list, share_input, logit, row_index, offset, y, workspaceGM, | ||
| 309 | - tilingGM); | ||
| 310 | - } else if constexpr (ATRANS == 0 && BTRANS == 1 && SCALETYPE == 1 && ROWINDEXTYPE == 1) { | ||
| 311 | - grouped_matmul_finalize_routing_pertoken_dequant<Cgmct::Gemm::layout::RowMajor, Cgmct::Gemm::layout::Zn, 1, 1>( | ||
| 312 | - x, w, scale, bias, pertoken_scale, group_list, share_input, logit, row_index, offset, y, workspaceGM, | ||
| 313 | - tilingGM); | ||
| 314 | - } else if constexpr (ATRANS == 0 && BTRANS == 0 && SCALETYPE == BF16TYPE && ROWINDEXTYPE == 1) { | ||
| 315 | - grouped_matmul_finalize_routing_pertoken_dequant<Cgmct::Gemm::layout::RowMajor, Cgmct::Gemm::layout::Nz, | ||
| 316 | - BF16TYPE, 1>(x, w, scale, bias, pertoken_scale, group_list, | ||
| 317 | - share_input, logit, row_index, offset, y, | ||
| 318 | - workspaceGM, tilingGM); | ||
| 319 | - } else if constexpr (ATRANS == 0 && BTRANS == 1 && SCALETYPE == BF16TYPE && ROWINDEXTYPE == 1) { | ||
| 320 | - grouped_matmul_finalize_routing_pertoken_dequant<Cgmct::Gemm::layout::RowMajor, Cgmct::Gemm::layout::Zn, | ||
| 321 | - BF16TYPE, 1>(x, w, scale, bias, pertoken_scale, group_list, | ||
| 322 | - share_input, logit, row_index, offset, y, | ||
| 323 | - workspaceGM, tilingGM); | ||
| 324 | } | 321 | } |
| 325 | #endif | 322 | #endif |
| 326 | #endif | 323 | #endif |


原 Cgmct Kernel 会先用 workspace 字节数除以每行大小,得到可容纳的行数,再向下对齐到 baseM。当 workspace 能容纳至少一行、但不足 baseM 行时,原 Host 的一行容量检查会通过,而 Kernel 执行 (行数 / baseM) * baseM 后,窗口大小会变为 0。例如 baseM=32、workspace 只能容纳 16 行时,对齐结果为 0。因此将 Host 最小容量检查调整为 baseM 行,提前拦截无法形成有效窗口的配置。