Merged
add qsmla_metadata #8762
yipyipAdd on Jul 15
add qsmla_metadata #8762
Merged
Merged fromyipyip/ops-transformermetadataintocann/ops-transformermaster
Total: 11 files changed+2670-0
| @@ -0,0 +1,17 @@ | |||
| 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 | +file(GLOB CURRENT_DIRS RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/*) | ||
| 12 | +list(REMOVE_ITEM CURRENT_DIRS tests) | ||
| 13 | +foreach(SUB_DIR ${CURRENT_DIRS}) | ||
| 14 | + if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/${SUB_DIR}/CMakeLists.txt") | ||
| 15 | + add_subdirectory(${SUB_DIR}) | ||
| 16 | + endif() | ||
| 17 | +endforeach() | ||
| @@ -0,0 +1,12 @@ | |||
| 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 | +add_op_to_compiled_list() | ||
| 11 | + | ||
| 12 | +add_modules_sources(OPTYPE quant_sparse_flash_mla_metadata ACLNNTYPE aclnn) | ||
Aattention/quant_sparse_flash_mla_metadata/op_host/op_api/aclnn_quant_sparse_flash_mla_metadata.cpp+176-0
| @@ -0,0 +1,176 @@ | |||
| 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 aclnn_quant_sparse_flash_mla_metadata.cpp | ||
| 13 | + * \brief | ||
| 14 | + */ | ||
| 15 | + | ||
| 16 | +#include "aclnn_quant_sparse_flash_mla_metadata.h" | ||
| 17 | +#include "quant_sparse_flash_mla_metadata.h" | ||
| 18 | +#include "aclnn_kernels/contiguous.h" | ||
| 19 | +#include "aclnn_kernels/reshape.h" | ||
| 20 | +#include "aclnn/aclnn_base.h" | ||
| 21 | +#include "aclnn_kernels/common/op_error_check.h" | ||
| 22 | +#include "opdev/common_types.h" | ||
| 23 | +#include "opdev/data_type_utils.h" | ||
| 24 | +#include "opdev/format_utils.h" | ||
| 25 | +#include "opdev/op_dfx.h" | ||
| 26 | +#include "opdev/op_executor.h" | ||
| 27 | +#include "opdev/op_log.h" | ||
| 28 | +#include "opdev/tensor_view_utils.h" | ||
| 29 | +#include "opdev/make_op_executor.h" | ||
| 30 | +#include "../quant_sparse_flash_mla_metadata_check.h" | ||
| 31 | + | ||
| 32 | +#ifdef __cplusplus | ||
| 33 | +extern "C" { | ||
| 34 | +#endif | ||
| 35 | + | ||
| 36 | +aclnnStatus aclnnQuantSparseFlashMlaMetadataGetWorkspaceSize( | ||
| 37 | + const aclTensor *cuSeqlensQOptional, const aclTensor *cuSeqlensOriKvOptional, | ||
| 38 | + const aclTensor *cuSeqlensCmpKvOptional, const aclTensor *sequsedQOptional, const aclTensor *sequsedOriKvOptional, | ||
| 39 | + const aclTensor *sequsedCmpKvOptional, const aclTensor *cmpResidualKvOptional, | ||
| 40 | + const aclTensor *oriTopkLengthOptional, const aclTensor *cmpTopkLengthOptional, int64_t numHeadsQ, | ||
| 41 | + int64_t numHeadsKv, int64_t headDim, int64_t quantMode, int64_t batchSize, int64_t maxSeqlenQ, | ||
| 42 | + int64_t maxSeqlenOriKv, int64_t maxSeqlenCmpKv, int64_t oriTopk, int64_t cmpTopk, | ||
| 43 | + int64_t cmpRatio, int64_t oriMaskMode, int64_t cmpMaskMode, int64_t oriWinLeft, int64_t oriWinRight, | ||
| 44 | + const char *layoutQOptional, const char *layoutKvOptional, bool hasOriKv, bool hasCmpKv, const aclTensor *metaData, | ||
| 45 | + uint64_t *workspaceSize, aclOpExecutor **executor) | ||
| 46 | +{ | ||
| 47 | + if (workspaceSize == nullptr) { | ||
| 48 | + OP_LOGE(ACLNN_ERR_INNER_NULLPTR, "workspaceSize is nullptr"); | ||
| 49 | + return ACLNN_ERR_INNER_NULLPTR; | ||
| 50 | + } | ||
| 51 | + if (executor == nullptr) { | ||
| 52 | + OP_LOGE(ACLNN_ERR_INNER_NULLPTR, "executor is nullptr"); | ||
| 53 | + return ACLNN_ERR_INNER_NULLPTR; | ||
| 54 | + } | ||
| 55 | + L2_DFX_PHASE_1(aclnnQuantSparseFlashMlaMetadata, | ||
| 56 | + DFX_IN(cuSeqlensQOptional, cuSeqlensOriKvOptional, cuSeqlensCmpKvOptional, sequsedQOptional, | ||
| 57 | + sequsedOriKvOptional, sequsedCmpKvOptional, cmpResidualKvOptional, oriTopkLengthOptional, | ||
| 58 | + cmpTopkLengthOptional, numHeadsQ, numHeadsKv, headDim, quantMode, batchSize, maxSeqlenQ, | ||
| 59 | + maxSeqlenOriKv, maxSeqlenCmpKv, oriTopk, cmpTopk, cmpRatio, oriMaskMode, | ||
| 60 | + cmpMaskMode, oriWinLeft, oriWinRight, layoutQOptional, layoutKvOptional, hasOriKv, hasCmpKv), | ||
| 61 | + DFX_OUT(metaData)); | ||
| 62 | + | ||
| 63 | + auto uniqueExecutor = CREATE_EXECUTOR(); | ||
| 64 | + CHECK_RET(uniqueExecutor.get() != nullptr, ACLNN_ERR_INNER_CREATE_EXECUTOR); | ||
| 65 | + | ||
| 66 | + const op::PlatformInfo &npuInfo = op::GetCurrentPlatformInfo(); | ||
| 67 | + uint32_t aicCoreNum = npuInfo.GetCubeCoreNum(); | ||
| 68 | + uint32_t aivCoreNum = npuInfo.GetVectorCoreNum(); | ||
| 69 | + std::string socVersionStr = npuInfo.GetSocLongVersion(); | ||
| 70 | + const char *socVersion = socVersionStr.c_str(); | ||
| 71 | + | ||
| 72 | + auto ret = ParamsCheck(cuSeqlensQOptional, cuSeqlensOriKvOptional, cuSeqlensCmpKvOptional, sequsedQOptional, | ||
| 73 | + sequsedOriKvOptional, sequsedCmpKvOptional, cmpResidualKvOptional, oriTopkLengthOptional, | ||
| 74 | + cmpTopkLengthOptional, numHeadsQ, numHeadsKv, headDim, quantMode, batchSize, maxSeqlenQ, | ||
| 75 | + maxSeqlenOriKv, maxSeqlenCmpKv, oriTopk, cmpTopk, cmpRatio, oriMaskMode, | ||
| 76 | + cmpMaskMode, oriWinLeft, oriWinRight, layoutQOptional, layoutKvOptional, hasOriKv, hasCmpKv, | ||
| 77 | + aicCoreNum, aivCoreNum, socVersion, metaData); | ||
| 78 | + CHECK_RET(ret == ACLNN_SUCCESS, ret); | ||
| 79 | + | ||
| 80 | + const aclTensor *cuSeqlensQOptionalContiguous = nullptr; | ||
| 81 | + if (cuSeqlensQOptional != nullptr) { | ||
| 82 | + cuSeqlensQOptionalContiguous = l0op::Contiguous(cuSeqlensQOptional, uniqueExecutor.get()); | ||
| 83 | + if (cuSeqlensQOptionalContiguous == nullptr) { | ||
| 84 | + OP_LOGE(ACLNN_ERR_INNER_NULLPTR, "cu_seqlens_q contiguous is null"); | ||
| 85 | + return ACLNN_ERR_INNER_NULLPTR; | ||
| 86 | + } | ||
| 87 | + } | ||
| 88 | + const aclTensor *cuSeqlensOriKvOptionalContiguous = nullptr; | ||
| 89 | + if (cuSeqlensOriKvOptional != nullptr) { | ||
| 90 | + cuSeqlensOriKvOptionalContiguous = l0op::Contiguous(cuSeqlensOriKvOptional, uniqueExecutor.get()); | ||
| 91 | + if (cuSeqlensOriKvOptionalContiguous == nullptr) { | ||
| 92 | + OP_LOGE(ACLNN_ERR_INNER_NULLPTR, "cu_seqlens_ori_kv contiguous is null"); | ||
| 93 | + return ACLNN_ERR_INNER_NULLPTR; | ||
| 94 | + } | ||
| 95 | + } | ||
| 96 | + const aclTensor *cuSeqlensCmpKvOptionalContiguous = nullptr; | ||
| 97 | + if (cuSeqlensCmpKvOptional != nullptr) { | ||
| 98 | + cuSeqlensCmpKvOptionalContiguous = l0op::Contiguous(cuSeqlensCmpKvOptional, uniqueExecutor.get()); | ||
| 99 | + if (cuSeqlensCmpKvOptionalContiguous == nullptr) { | ||
| 100 | + OP_LOGE(ACLNN_ERR_INNER_NULLPTR, "cu_seqlens_cmp_kv contiguous is null"); | ||
| 101 | + return ACLNN_ERR_INNER_NULLPTR; | ||
| 102 | + } | ||
| 103 | + } | ||
| 104 | + const aclTensor *sequsedQOptionalContiguous = nullptr; | ||
| 105 | + if (sequsedQOptional != nullptr) { | ||
| 106 | + sequsedQOptionalContiguous = l0op::Contiguous(sequsedQOptional, uniqueExecutor.get()); | ||
| 107 | + if (sequsedQOptionalContiguous == nullptr) { | ||
| 108 | + OP_LOGE(ACLNN_ERR_INNER_NULLPTR, "seqused_q contiguous is null"); | ||
| 109 | + return ACLNN_ERR_INNER_NULLPTR; | ||
| 110 | + } | ||
| 111 | + } | ||
| 112 | + const aclTensor *sequsedOriKvOptionalContiguous = nullptr; | ||
| 113 | + if (sequsedOriKvOptional != nullptr) { | ||
| 114 | + sequsedOriKvOptionalContiguous = l0op::Contiguous(sequsedOriKvOptional, uniqueExecutor.get()); | ||
| 115 | + if (sequsedOriKvOptionalContiguous == nullptr) { | ||
| 116 | + OP_LOGE(ACLNN_ERR_INNER_NULLPTR, "seqused_ori_kv contiguous is null"); | ||
| 117 | + return ACLNN_ERR_INNER_NULLPTR; | ||
| 118 | + } | ||
| 119 | + } | ||
| 120 | + const aclTensor *sequsedCmpKvOptionalContiguous = nullptr; | ||
| 121 | + if (sequsedCmpKvOptional != nullptr) { | ||
| 122 | + sequsedCmpKvOptionalContiguous = l0op::Contiguous(sequsedCmpKvOptional, uniqueExecutor.get()); | ||
| 123 | + if (sequsedCmpKvOptionalContiguous == nullptr) { | ||
| 124 | + OP_LOGE(ACLNN_ERR_INNER_NULLPTR, "seqused_cmp_kv contiguous is null"); | ||
| 125 | + return ACLNN_ERR_INNER_NULLPTR; | ||
| 126 | + } | ||
| 127 | + } | ||
| 128 | + const aclTensor *cmpResidualKvOptionalContiguous = nullptr; | ||
| 129 | + if (cmpResidualKvOptional != nullptr) { | ||
| 130 | + cmpResidualKvOptionalContiguous = l0op::Contiguous(cmpResidualKvOptional, uniqueExecutor.get()); | ||
| 131 | + if (cmpResidualKvOptionalContiguous == nullptr) { | ||
| 132 | + OP_LOGE(ACLNN_ERR_INNER_NULLPTR, "cmp_residual_kv contiguous is null"); | ||
| 133 | + return ACLNN_ERR_INNER_NULLPTR; | ||
| 134 | + } | ||
| 135 | + } | ||
| 136 | + const aclTensor *oriTopkLengthOptionalContiguous = nullptr; | ||
| 137 | + if (oriTopkLengthOptional != nullptr) { | ||
| 138 | + oriTopkLengthOptionalContiguous = l0op::Contiguous(oriTopkLengthOptional, uniqueExecutor.get()); | ||
| 139 | + if (oriTopkLengthOptionalContiguous == nullptr) { | ||
| 140 | + OP_LOGE(ACLNN_ERR_INNER_NULLPTR, "ori_topk_length contiguous is null"); | ||
| 141 | + return ACLNN_ERR_INNER_NULLPTR; | ||
| 142 | + } | ||
| 143 | + } | ||
| 144 | + const aclTensor *cmpTopkLengthOptionalContiguous = nullptr; | ||
| 145 | + if (cmpTopkLengthOptional != nullptr) { | ||
| 146 | + cmpTopkLengthOptionalContiguous = l0op::Contiguous(cmpTopkLengthOptional, uniqueExecutor.get()); | ||
| 147 | + if (cmpTopkLengthOptionalContiguous == nullptr) { | ||
| 148 | + OP_LOGE(ACLNN_ERR_INNER_NULLPTR, "cmp_topk_length contiguous is null"); | ||
| 149 | + return ACLNN_ERR_INNER_NULLPTR; | ||
| 150 | + } | ||
| 151 | + } | ||
| 152 | + | ||
| 153 | + auto output = l0op::QuantSparseFlashMlaMetadata( | ||
| 154 | + cuSeqlensQOptionalContiguous, cuSeqlensOriKvOptionalContiguous, cuSeqlensCmpKvOptionalContiguous, | ||
| 155 | + sequsedQOptionalContiguous, sequsedOriKvOptionalContiguous, sequsedCmpKvOptionalContiguous, | ||
| 156 | + cmpResidualKvOptionalContiguous, oriTopkLengthOptionalContiguous, cmpTopkLengthOptionalContiguous, | ||
| 157 | + numHeadsQ, numHeadsKv, headDim, quantMode, batchSize, maxSeqlenQ, maxSeqlenOriKv, maxSeqlenCmpKv, oriTopk, | ||
| 158 | + cmpTopk, cmpRatio, oriMaskMode, cmpMaskMode, oriWinLeft, oriWinRight, layoutQOptional, | ||
| 159 | + layoutKvOptional, hasOriKv, hasCmpKv, socVersion, aicCoreNum, aivCoreNum, metaData, uniqueExecutor.get()); | ||
| 160 | + CHECK_RET(output != nullptr, ACLNN_ERR_INNER_NULLPTR); | ||
| 161 | + | ||
| 162 | + *workspaceSize = 0; | ||
| 163 | + uniqueExecutor.ReleaseTo(executor); | ||
| 164 | + return ACLNN_SUCCESS; | ||
| 165 | +} | ||
| 166 | + | ||
| 167 | +__attribute__((visibility("default"))) aclnnStatus aclnnQuantSparseFlashMlaMetadata( | ||
| 168 | + void *workspace, uint64_t workspaceSize, aclOpExecutor *executor, aclrtStream stream) | ||
| 169 | +{ | ||
| 170 | + L2_DFX_PHASE_2(aclnnQuantSparseFlashMlaMetadata); | ||
| 171 | + return CommonOpExecutorRun(workspace, workspaceSize, executor, stream); | ||
| 172 | +} | ||
| 173 | + | ||
| 174 | +#ifdef __cplusplus | ||
| 175 | +} | ||
| 176 | +#endif | ||
Aattention/quant_sparse_flash_mla_metadata/op_host/op_api/aclnn_quant_sparse_flash_mla_metadata.h+38-0
| @@ -0,0 +1,38 @@ | |||
| 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 | +#ifndef ACLNN_quant_sparse_flash_mla_metadata_H | ||
| 12 | +#define ACLNN_quant_sparse_flash_mla_metadata_H | ||
| 13 | + | ||
| 14 | +#include "aclnn/aclnn_base.h" | ||
| 15 | + | ||
| 16 | +#ifdef __cplusplus | ||
| 17 | +extern "C" { | ||
| 18 | +#endif | ||
| 19 | + | ||
| 20 | +__attribute__((visibility("default"))) aclnnStatus aclnnQuantSparseFlashMlaMetadataGetWorkspaceSize( | ||
| 21 | + const aclTensor *cuSeqlensQOptional, const aclTensor *cuSeqlensOriKvOptional, | ||
| 22 | + const aclTensor *cuSeqlensCmpKvOptional, const aclTensor *sequsedQOptional, const aclTensor *sequsedOriKvOptional, | ||
| 23 | + const aclTensor *sequsedCmpKvOptional, const aclTensor *cmpResidualKvOptional, | ||
| 24 | + const aclTensor *oriTopkLengthOptional, const aclTensor *cmpTopkLengthOptional, int64_t numHeadsQ, | ||
| 25 | + int64_t numHeadsKv, int64_t headDim, int64_t quantMode, int64_t batchSize, int64_t maxSeqlenQ, | ||
| 26 | + int64_t maxSeqlenOriKv, int64_t maxSeqlenCmpKv, int64_t oriTopk, int64_t cmpTopk, | ||
| 27 | + int64_t cmpRatio, int64_t oriMaskMode, int64_t cmpMaskMode, int64_t oriWinLeft, int64_t oriWinRight, | ||
| 28 | + const char *layoutQOptional, const char *layoutKvOptional, bool hasOriKv, bool hasCmpKv, const aclTensor *metaData, | ||
| 29 | + uint64_t *workspaceSize, aclOpExecutor **executor); | ||
| 30 | + | ||
| 31 | +__attribute__((visibility("default"))) aclnnStatus aclnnQuantSparseFlashMlaMetadata( | ||
| 32 | + void *workspace, uint64_t workspaceSize, aclOpExecutor *executor, aclrtStream stream); | ||
| 33 | + | ||
| 34 | +#ifdef __cplusplus | ||
| 35 | +} | ||
| 36 | +#endif | ||
| 37 | + | ||
| 38 | +#endif // ACLNN_quant_sparse_flash_mla_metadata_H | ||
| @@ -0,0 +1,67 @@ | |||
| 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 quant_sparse_flash_mla_metadata.cpp | ||
| 13 | + * \brief | ||
| 14 | + */ | ||
| 15 | + | ||
| 16 | +#include "quant_sparse_flash_mla_metadata.h" | ||
| 17 | +#include "opdev/aicpu/aicpu_task.h" | ||
| 18 | +#include "opdev/make_op_executor.h" | ||
| 19 | +#include "opdev/op_def.h" | ||
| 20 | +#include "opdev/op_dfx.h" | ||
| 21 | +#include "opdev/op_executor.h" | ||
| 22 | +#include "opdev/op_log.h" | ||
| 23 | +#include "opdev/shape_utils.h" | ||
| 24 | + | ||
| 25 | +using namespace op; | ||
| 26 | +namespace l0op { | ||
| 27 | +OP_TYPE_REGISTER(QuantSparseFlashMlaMetadata); | ||
| 28 | + | ||
| 29 | +const aclTensor *QuantSparseFlashMlaMetadata( | ||
| 30 | + const aclTensor *cuSeqlensQOptional, const aclTensor *cuSeqlensOriKvOptional, | ||
| 31 | + const aclTensor *cuSeqlensCmpKvOptional, const aclTensor *sequsedQOptional, const aclTensor *sequsedOriKvOptional, | ||
| 32 | + const aclTensor *sequsedCmpKvOptional, const aclTensor *cmpResidualKvOptional, | ||
| 33 | + const aclTensor *oriTopkLengthOptional, const aclTensor *cmpTopkLengthOptional, int64_t numHeadsQ, | ||
| 34 | + int64_t numHeadsKv, int64_t headDim, int64_t quantMode, int64_t batchSize, int64_t maxSeqlenQ, | ||
| 35 | + int64_t maxSeqlenOriKv, int64_t maxSeqlenCmpKv, int64_t oriTopk, int64_t cmpTopk, | ||
| 36 | + int64_t cmpRatio, int64_t oriMaskMode, int64_t cmpMaskMode, int64_t oriWinLeft, int64_t oriWinRight, | ||
| 37 | + const char *layoutQOptional, const char *layoutKvOptional, bool hasOriKv, bool hasCmpKv, const char *socVersion, | ||
| 38 | + int64_t aicCoreNum, int64_t aivCoreNum, const aclTensor *metaData, aclOpExecutor *executor) | ||
| 39 | +{ | ||
| 40 | + L0_DFX(QuantSparseFlashMlaMetadata, cuSeqlensQOptional, cuSeqlensOriKvOptional, cuSeqlensCmpKvOptional, | ||
| 41 | + sequsedQOptional, sequsedOriKvOptional, sequsedCmpKvOptional, cmpResidualKvOptional, oriTopkLengthOptional, | ||
| 42 | + cmpTopkLengthOptional, numHeadsQ, numHeadsKv, headDim, quantMode, batchSize, maxSeqlenQ, maxSeqlenOriKv, | ||
| 43 | + maxSeqlenCmpKv, oriTopk, cmpTopk, cmpRatio, oriMaskMode, cmpMaskMode, oriWinLeft, oriWinRight, | ||
| 44 | + layoutQOptional, layoutKvOptional, hasOriKv, hasCmpKv, socVersion, aicCoreNum, aivCoreNum, metaData); | ||
| 45 | + | ||
| 46 | + static internal::AicpuTaskSpace space("QuantSparseFlashMlaMetadata"); | ||
| 47 | + | ||
| 48 | + auto ret = ADD_TO_LAUNCHER_LIST_AICPU( | ||
| 49 | + QuantSparseFlashMlaMetadata, | ||
| 50 | + OP_ATTR_NAMES({"num_heads_q", "num_heads_kv", "head_dim", "quant_mode", "batch_size", "max_seqlen_q", | ||
| 51 | + "max_seqlen_ori_kv", "max_seqlen_cmp_kv", "ori_topk", "cmp_topk", "cmp_ratio", | ||
| 52 | + "ori_mask_mode", "cmp_mask_mode", "ori_win_left", "ori_win_right", "layout_q", "layout_kv", | ||
| 53 | + "has_ori_kv", "has_cmp_kv", "soc_version", "aic_core_num", "aiv_core_num"}), | ||
| 54 | + OP_INPUT(cuSeqlensQOptional, cuSeqlensOriKvOptional, cuSeqlensCmpKvOptional, sequsedQOptional, | ||
| 55 | + sequsedOriKvOptional, sequsedCmpKvOptional, cmpResidualKvOptional, oriTopkLengthOptional, | ||
| 56 | + cmpTopkLengthOptional), | ||
| 57 | + OP_OUTPUT(metaData), | ||
| 58 | + OP_ATTR(numHeadsQ, numHeadsKv, headDim, quantMode, batchSize, maxSeqlenQ, maxSeqlenOriKv, maxSeqlenCmpKv, | ||
| 59 | + oriTopk, cmpTopk, cmpRatio, oriMaskMode, cmpMaskMode, oriWinLeft, oriWinRight, | ||
| 60 | + layoutQOptional, layoutKvOptional, hasOriKv, hasCmpKv, socVersion, aicCoreNum, aivCoreNum)); | ||
| 61 | + OP_CHECK(ret == ACL_SUCCESS, | ||
| 62 | + OP_LOGE(ACLNN_ERR_INNER_NULLPTR, "QuantSparseFlashMlaMetadata ADD_TO_LAUNCHER_LIST_AICPU failed."), | ||
| 63 | + return nullptr); | ||
| 64 | + return metaData; | ||
| 65 | +} | ||
| 66 | + | ||
| 67 | +} // namespace l0op | ||
| @@ -0,0 +1,29 @@ | |||
| 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 | +#ifndef L0_quant_sparse_flash_mla_metadata_H | ||
| 12 | +#define L0_quant_sparse_flash_mla_metadata_H | ||
| 13 | + | ||
| 14 | +#include "opdev/op_executor.h" | ||
| 15 | + | ||
| 16 | +namespace l0op { | ||
| 17 | +const aclTensor* QuantSparseFlashMlaMetadata( | ||
| 18 | + const aclTensor *cuSeqlensQOptional, const aclTensor *cuSeqlensOriKvOptional, | ||
| 19 | + const aclTensor *cuSeqlensCmpKvOptional, const aclTensor *sequsedQOptional, const aclTensor *sequsedOriKvOptional, | ||
| 20 | + const aclTensor *sequsedCmpKvOptional, const aclTensor *cmpResidualKvOptional, | ||
| 21 | + const aclTensor *oriTopkLengthOptional, const aclTensor *cmpTopkLengthOptional, int64_t numHeadsQ, | ||
| 22 | + int64_t numHeadsKv, int64_t headDim, int64_t quantMode, int64_t batchSize, int64_t maxSeqlenQ, | ||
| 23 | + int64_t maxSeqlenOriKv, int64_t maxSeqlenCmpKv, int64_t oriTopk, int64_t cmpTopk, | ||
| 24 | + int64_t cmpRatio, int64_t oriMaskMode, int64_t cmpMaskMode, int64_t oriWinLeft, int64_t oriWinRight, | ||
| 25 | + const char *layoutQOptional, const char *layoutKvOptional, bool hasOriKv, bool hasCmpKv, const char *socVersion, | ||
| 26 | + int64_t aicCoreNum, int64_t aivCoreNum, const aclTensor *metaData, aclOpExecutor *executor); | ||
| 27 | +} // namespace l0op | ||
| 28 | + | ||
| 29 | +#endif | ||
| @@ -0,0 +1,637 @@ | |||
| 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 quant_sparse_flash_mla_metadata_check.h | ||
| 13 | + * \brief | ||
| 14 | + */ | ||
| 15 | + | ||
| 16 | +#include "opdev/format_utils.h" | ||
| 17 | +#include "opdev/op_log.h" | ||
| 18 | +#include "opdev/data_type_utils.h" | ||
| 19 | +#include "opdev/tensor_view_utils.h" | ||
| 20 | +#include "../../quant_sparse_flash_mla/op_kernel/quant_sparse_flash_mla_metadata.h" | ||
| 21 | + | ||
| 22 | +#ifdef __cplusplus | ||
| 23 | +extern "C" { | ||
| 24 | +#endif | ||
| 25 | + | ||
| 26 | +namespace { | ||
| 27 | + | ||
| 28 | +enum class SparseModeQsmla : uint8_t { | ||
| 29 | + DEFAULT_MASK = 0, | ||
| 30 | + ALL_MASK, | ||
| 31 | + LEFT_UP_CAUSAL, | ||
| 32 | + RIGHT_DOWN_CAUSAL, | ||
| 33 | + BAND, | ||
| 34 | + SPARSE_BUTT, | ||
| 35 | +}; | ||
| 36 | + | ||
| 37 | +inline constexpr int64_t QSMLA_QUANT_MODE_LOWER_BOUND = 1; | ||
| 38 | +inline constexpr int64_t QSMLA_QUANT_MODE_UPPER_BOUND = 2; | ||
| 39 | +inline constexpr int64_t QSMLA_CMP_RATIO_LOWER_BOUND = 1; | ||
| 40 | +inline constexpr int64_t QSMLA_CMP_RATIO_UPPER_BOUND = 128; | ||
| 41 | +inline constexpr int64_t QSMLA_NUM_HEADS_Q_LOWER_BOUND = 1; | ||
| 42 | +inline constexpr int64_t QSMLA_NUM_HEADS_Q_UPPER_BOUND = 128; | ||
| 43 | + | ||
| 44 | +inline bool IsTensorExistQsmla(const aclTensor *tensor) | ||
| 45 | +{ | ||
| 46 | + return (tensor != nullptr) && (tensor->GetViewShape().GetDimNum() > 0) && (tensor->GetViewShape().GetDim(0) > 0); | ||
| 47 | +} | ||
| 48 | + | ||
| 49 | +int64_t GetDimNumQsmla(const aclTensor *tensor) | ||
| 50 | +{ | ||
| 51 | + if (tensor == nullptr) { | ||
| 52 | + return -1; | ||
| 53 | + } | ||
| 54 | + return tensor->GetViewShape().GetDimNum(); | ||
| 55 | +} | ||
| 56 | + | ||
| 57 | +aclDataType GetDataTypeQsmla(const aclTensor *tensor) | ||
| 58 | +{ | ||
| 59 | + aclDataType dataType = aclDataType::ACL_DT_UNDEFINED; | ||
| 60 | + if (tensor == nullptr) { | ||
| 61 | + return dataType; | ||
| 62 | + } | ||
| 63 | + aclGetDataType(tensor, &dataType); | ||
| 64 | + return dataType; | ||
| 65 | +} | ||
| 66 | + | ||
| 67 | +aclnnStatus CheckSingleParamQsmla(int64_t batchSize, int64_t maxSeqlenQ, int64_t maxSeqlenOriKv, int64_t maxSeqlenCmpKv, | ||
| 68 | + int64_t numHeadsQ, int64_t numHeadsKv, int64_t headDim, int64_t quantMode, | ||
| 69 | + int64_t oriTopk, int64_t cmpTopk, int64_t cmpRatio, int64_t oriMaskMode, | ||
| 70 | + int64_t cmpMaskMode, int64_t oriWinLeft, int64_t oriWinRight, | ||
| 71 | + const char *layoutQOptional, const char *layoutKvOptional, bool hasOriKv, | ||
| 72 | + bool hasCmpKv, uint32_t aicCoreNum, uint32_t aivCoreNum, const char *socVersion) | ||
| 73 | +{ | ||
| 74 | + // batch_size >= 0 | ||
| 75 | + if (batchSize < 0) { | ||
| 76 | + OP_LOGE(ACLNN_ERR_PARAM_INVALID, "batch_size should be >= 0, but got %lld", batchSize); | ||
| 77 | + return ACLNN_ERR_PARAM_INVALID; | ||
| 78 | + } | ||
| 79 | + // max_seqlen_q >= 0 | ||
| 80 | + if (maxSeqlenQ < 0) { | ||
| 81 | + OP_LOGE(ACLNN_ERR_PARAM_INVALID, "max_seqlen_q should be >= 0, but got %lld", maxSeqlenQ); | ||
| 82 | + return ACLNN_ERR_PARAM_INVALID; | ||
| 83 | + } | ||
| 84 | + // max_seqlen_ori_kv >= 0 | ||
| 85 | + if (maxSeqlenOriKv < 0) { | ||
| 86 | + OP_LOGE(ACLNN_ERR_PARAM_INVALID, "max_seqlen_ori_kv should be >= 0, but got %lld", maxSeqlenOriKv); | ||
| 87 | + return ACLNN_ERR_PARAM_INVALID; | ||
| 88 | + } | ||
| 89 | + // max_seqlen_cmp_kv >= 0 | ||
| 90 | + if (maxSeqlenCmpKv < 0) { | ||
| 91 | + OP_LOGE(ACLNN_ERR_PARAM_INVALID, "max_seqlen_cmp_kv should be >= 0, but got %lld", maxSeqlenCmpKv); | ||
| 92 | + return ACLNN_ERR_PARAM_INVALID; | ||
| 93 | + } | ||
| 94 | + // num_heads_q [1, 128] | ||
| 95 | + if (numHeadsQ < QSMLA_NUM_HEADS_Q_LOWER_BOUND || numHeadsQ > QSMLA_NUM_HEADS_Q_UPPER_BOUND) { | ||
| 96 | + OP_LOGE(ACLNN_ERR_PARAM_INVALID, "num_heads_q should be [%lld, %lld], but got %lld", | ||
| 97 | + QSMLA_NUM_HEADS_Q_LOWER_BOUND, QSMLA_NUM_HEADS_Q_UPPER_BOUND, numHeadsQ); | ||
| 98 | + return ACLNN_ERR_PARAM_INVALID; | ||
| 99 | + } | ||
| 100 | + // num_heads_kv: 1 | ||
| @@ -0,0 +1,32 @@ | |||
| 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 | +if (BUILD_WITH_INSTALLED_DEPENDENCY_CANN_PKG) | ||
| 12 | + if (NOT (UT_TEST_ALL OR OP_KERNEL_AICPU_UT)) | ||
| 13 | + add_definitions(-D_GLIBCXX_USE_CXX11_ABI=1) | ||
| 14 | + set(CMAKE_CXX_COMPILER ${ASCEND_DIR}/toolkit/toolchain/hcc/bin/aarch64-target-linux-gnu-g++) | ||
| 15 | + endif() | ||
| 16 | + | ||
| 17 | + # aicpu json | ||
| 18 | + file(GLOB_RECURSE JSON_FILE ${CMAKE_CURRENT_SOURCE_DIR}/*.json) | ||
| 19 | + set_property(GLOBAL APPEND PROPERTY AICPU_JSON_FILES ${JSON_FILE}) | ||
| 20 | + | ||
| 21 | + # aicpu cust kernel | ||
| 22 | + file(GLOB AICPU_SRC ${CMAKE_CURRENT_SOURCE_DIR}/*_aicpu*.cpp) | ||
| 23 | + message(STATUS "[quant_sparse_flash_mla_metadata] Found aicpu sources: ${AICPU_SRC}, ascend dir: ${ASCEND_DIR}, ophsot name: ${OPHOST_NAME}") | ||
| 24 | + | ||
| 25 | + set(OBJ_NAME quant_sparse_flash_mla_metadata_cust_obj) | ||
| 26 | + add_aicpu_cust_kernel_modules(${OBJ_NAME}) | ||
| 27 | + target_sources(${OBJ_NAME} PRIVATE ${AICPU_SRC}) | ||
| 28 | +endif() | ||
| 29 | + | ||
| 30 | +if(UT_TEST_ALL OR OP_KERNEL_AICPU_UT) | ||
| 31 | + AddAicpuOpTestCase(quant_sparse_flash_mla_metadata) | ||
| 32 | +endif() | ||
Aattention/quant_sparse_flash_mla_metadata/op_kernel_aicpu/quant_sparse_flash_mla_metadata_aicpu.cpp+1273-0
Aattention/quant_sparse_flash_mla_metadata/op_kernel_aicpu/quant_sparse_flash_mla_metadata_aicpu.h+374-0
| @@ -0,0 +1,374 @@ | |||
| 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 quant_sparse_flash_mla_metadata_aicpu.h | ||
| 13 | + * \brief | ||
| 14 | + */ | ||
| 15 | + | ||
| 16 | +#ifndef quant_sparse_flash_mla_metadata_AICPU_H | ||
| 17 | +#define quant_sparse_flash_mla_metadata_AICPU_H | ||
| 18 | + | ||
| 19 | +#include <array> | ||
| 20 | +#include <string> | ||
| 21 | +#include <vector> | ||
| 22 | +#include "cpu_context.h" | ||
| 23 | +#include "cpu_kernel.h" | ||
| 24 | +#include "cpu_tensor.h" | ||
| 25 | +#include "../../quant_sparse_flash_mla/op_kernel/quant_sparse_flash_mla_metadata.h" | ||
| 26 | +#include "../../common/op_kernel/aicpu_common.h" | ||
| 27 | + | ||
| 28 | +namespace aicpu { | ||
| 29 | +constexpr int64_t FA_TOLERANCE_RATIO = 2; | ||
| 30 | +constexpr uint32_t COST_WEIGHT_M = 6U; | ||
| 31 | +constexpr uint32_t COST_WEIGHT_S2 = 10U; | ||
| 32 | +constexpr bool ORI_KV = false; | ||
| 33 | +constexpr bool CMP_KV = true; | ||
| 34 | +constexpr uint32_t NO_MASK = 0; | ||
| 35 | +constexpr uint32_t HAS_MASK = 1; | ||
| 36 | + | ||
| 37 | +enum BlockType : uint32_t { | ||
| 38 | + ORI_NORMAL_BLOCK = 0, | ||
| 39 | + ORI_TAIL_BLOCK, | ||
| 40 | + CMP_NORMAL_BLOCK, | ||
| 41 | + CMP_TAIL_BLOCK, | ||
| 42 | + BLOCK_MAX_TYPE | ||
| 43 | +}; | ||
| 44 | + | ||
| 45 | +enum class SparseMode : uint8_t { | ||
| 46 | + DEFAULT_MASK = 0, | ||
| 47 | + ALL_MASK, | ||
| 48 | + LEFT_UP_CAUSAL, | ||
| 49 | + RIGHT_DOWN_CAUSAL, | ||
| 50 | + BAND, | ||
| 51 | + SPARSE_BUTT, | ||
| 52 | +}; | ||
| 53 | + | ||
| 54 | +enum class ValidSocVersion { | ||
| 55 | + ASCEND910 = 0, | ||
| 56 | + ASCEND950 | ||
| 57 | +}; | ||
| 58 | + | ||
| 59 | +template<class T> | ||
| 60 | +using Range = std::pair<T, T>; | ||
| 61 | + | ||
| 62 | +template<class T> | ||
| 63 | +using BlockCost = std::array<std::array<T, static_cast<size_t>(BLOCK_MAX_TYPE)>, static_cast<size_t>(BLOCK_MAX_TYPE)>; | ||
| 64 | + | ||
| 65 | +template<typename T> | ||
| 66 | +T Clip(T value, T minValue, T maxValue) | ||
| 67 | +{ | ||
| 68 | + if (value < minValue) { | ||
| 69 | + return minValue; | ||
| 70 | + } | ||
| 71 | + if (value > maxValue) { | ||
| 72 | + return maxValue; | ||
| 73 | + } | ||
| 74 | + return value; | ||
| 75 | +} | ||
| 76 | + | ||
| 77 | +template<typename T> | ||
| 78 | +inline bool IsWithinTolerance(T limit, T tolerance, T value) | ||
| 79 | +{ | ||
| 80 | + return limit + tolerance >= value; | ||
| 81 | +} | ||
| 82 | + | ||
| 83 | +// 分核功能模块输出:FD信息,包含需要归约的数据索引及其分核信息 | ||
| 84 | +struct FlashDecodeResult { | ||
| 85 | + uint32_t fdUsedVecNum { 0U }; // 归约过程使用的vector数量 | ||
| 86 | + // 1、归约任务的索引信息 | ||
| 87 | + std::vector<uint32_t> fdBN2Idx {}; // 每个归约任务的BN2索引,脚标为归约任务的序号,最大为核数-1 | ||
| 88 | + std::vector<uint32_t> fdMIdx {}; // 每个归约任务的GS1索引,脚标为归约任务的序号 | ||
| 89 | + std::vector<uint32_t> fdWorkspaceIdx {}; // 每个归约任务在workspace中的存放位置 | ||
| 90 | + std::vector<uint32_t> fdS2SplitNum {}; // 每个归约任务的S2核间切分份数,脚标为归约任务的序号 | ||
| 91 | + std::vector<uint32_t> fdMSize {}; // 每个归约任务m轴大小,脚标为归约任务的序号 | ||
| 92 | + // 2、FD负载均衡阶段,归约任务的分核(vec)信息 | ||
| 93 | + std::vector<uint32_t> fdIdx {}; // FD负载均衡阶段,每个vector处理的归约任务对应ID | ||
| 94 | + std::vector<uint32_t> fdMStart {}; // FD负载均衡阶段,每个vector处理的归约任务的m轴起点 | ||
| 95 | + std::vector<uint32_t> fdMNum {}; // FD负载均衡阶段,每个vector处理的归约任务的m轴行数 | ||
| 96 | + | ||
| 97 | + FlashDecodeResult(uint32_t aicNum, uint32_t aivNum) | ||
| 98 | + : fdBN2Idx(aicNum), | ||
| 99 | + fdMIdx(aicNum), | ||
| 100 | + fdWorkspaceIdx(aicNum), | ||
| 101 | + fdS2SplitNum(aicNum), | ||
| 102 | + fdMSize(aicNum), | ||
| 103 | + fdIdx(aivNum), | ||
| 104 | + fdMStart(aivNum), | ||
| 105 | + fdMNum(aivNum) {} | ||
| 106 | +}; | ||
| 107 | + | ||
| 108 | +// 分核功能模块输出:FA阶段的核间分核信息 | ||
| 109 | +struct SplitResult { | ||
| 110 | + uint32_t usedCoreNum { 0U }; // 使用的核数量 | ||
| 111 | + std::vector<uint32_t> bN2End {}; // 每个核处理数据的BN2结束点 | ||
| 112 | + std::vector<uint32_t> gS1End {}; // 每个核处理数据的GS1结束点 | ||
| 113 | + std::vector<uint32_t> s2End {}; // 每个核处理数据的S2结束点 | ||
| 114 | + std::vector<uint32_t> firstFdDataWorkspaceIdx {}; // 每个核第一份归约任务的存放位置 | ||
| 115 | + int64_t maxCost { 0 }; // 慢核开销 | ||
| 116 | + uint32_t numOfFdHead { 0U }; // 归约任务数量 | ||
| 117 | + uint32_t maxS2SplitNum { 0U }; // 单个归约任务最大分核数量 | ||
| 118 | + uint32_t maxS2GBaseNum { 0U }; // 单个核最大s2基本块数量 | ||
| 119 | + FlashDecodeResult fdRes { 0U, 0U }; // FD信息 | ||
| 120 | + | ||
| 121 | + SplitResult(uint32_t aicNum, uint32_t aivNum) | ||
| 122 | + : bN2End(aicNum), | ||
| 123 | + gS1End(aicNum), | ||
| 124 | + s2End(aicNum), | ||
| 125 | + firstFdDataWorkspaceIdx(aicNum), | ||
| 126 | + fdRes(aicNum, aivNum) {}; | ||
| 127 | +}; | ||
| 128 | + | ||
| 129 | +// 分核功能模块内部使用:记录切分信息 | ||
| 130 | +struct SplitInfo { | ||
| 131 | + std::vector<uint32_t> s1GBaseNum {}; // S1G方向,切了多少个基本块 | ||
| 132 | + std::vector<uint32_t> oriS2BaseNum {}; // oriS2方向,切了多少个基本块 | ||
| 133 | + std::vector<uint32_t> cmpS2BaseNum {}; // cmpS2方向,切了多少个基本块 | ||
| 134 | + std::vector<uint32_t> s1GTailSize {}; // S1G方向,尾块size | ||
| 135 | + std::vector<uint32_t> oriS2TailSize {}; // oriS2方向,尾块size | ||
| 136 | + std::vector<uint32_t> cmpS2TailSize {}; // cmpS2方向,尾块size | ||
| 137 | + bool isKvSeqAllZero { true }; | ||
| 138 | + | ||
| 139 | + explicit SplitInfo(uint32_t batchSize) | ||
| 140 | + : s1GBaseNum(batchSize), | ||
| 141 | + oriS2BaseNum(batchSize), | ||
| 142 | + cmpS2BaseNum(batchSize), | ||
| 143 | + s1GTailSize(batchSize), | ||
| 144 | + oriS2TailSize(batchSize), | ||
| 145 | + cmpS2TailSize(batchSize) {} | ||
| 146 | +}; | ||
| 147 | + | ||
| 148 | +// 分核功能模块内部使用:记录batch的开销信息 | ||
| 149 | +struct CostInfo { | ||
| 150 | + std::vector<int64_t> bN2CostOfEachBatch {}; // 整个batch的开销 | ||
| 151 | + std::vector<uint32_t> bN2BlockOfEachBatch {}; // 整个batch的开销 | ||
| 152 | + std::vector<int64_t> bN2LastBlockCostOfEachBatch {}; // batch最后一块的开销 | ||
| 153 | + uint32_t totalBlockNum { 0U }; | ||
| 154 | + int64_t totalCost { 0 }; | ||
| 155 | + int64_t maxS1GCost { 0 }; // 记录所有S1G行中的最大开销 | ||
| 156 | + | ||
| 157 | + explicit CostInfo(uint32_t batchSize) | ||
| 158 | + : bN2CostOfEachBatch(batchSize), | ||
| 159 | + bN2BlockOfEachBatch(batchSize), | ||
| 160 | + bN2LastBlockCostOfEachBatch(batchSize) {} | ||
| 161 | +}; | ||
| 162 | + | ||
| 163 | +// 分核功能模块内部使用:分核过程中,case基本信息的上下文信息,组合以减少接口传参数量 | ||
| 164 | +struct SplitContext { | ||
| 165 | + SplitInfo splitInfo { 0U }; | ||
| 166 | + CostInfo costInfo { 0U }; | ||
| 167 | + | ||
| 168 | + explicit SplitContext(uint32_t batchSize) | ||
| 169 | + : splitInfo(batchSize), | ||
| 170 | + costInfo(batchSize) {} | ||
| 171 | +}; | ||
| 172 | + | ||
| 173 | +// 分核功能模块内部使用:记录batch相关的临时信息 | ||
| 174 | +struct BatchCache { | ||
| 175 | + uint32_t bIdx { 0U }; | ||
| 176 | + uint32_t s1Size { 0U }; | ||
| 177 | + uint32_t oriS2Size { 0U }; | ||
| 178 | + uint64_t cmpRevertS2Size { 0U }; | ||
| 179 | + int64_t oriPreTokenLeftUp { 0 }; | ||
| 180 | + int64_t oriNextTokenLeftUp { 0 }; | ||
| 181 | + int64_t cmpPreTokenLeftUp { 0 }; | ||
| 182 | + int64_t cmpNextTokenLeftUp { 0 }; | ||
| 183 | + BlockCost<int64_t> typeCost {}; | ||
| 184 | +}; | ||
| 185 | + | ||
| 186 | +// 分核功能模块内部使用:记录当前行(S1G)的临时信息 | ||
| 187 | +struct S1GCache { | ||
| 188 | + uint32_t bIdx { 0U }; | ||
| 189 | + uint32_t s1GIdx { 0U }; | ||
| 190 | + uint32_t s2Start { 0U }; | ||
| 191 | + uint32_t s2End { 0U }; | ||
| 192 | + uint32_t oriS2Start { 0U }; | ||
| 193 | + uint32_t oriS2End { 0U }; | ||
| 194 | + uint32_t cmpS2Start { 0U }; // win部分与cmp部分的切分点 | ||
| 195 | + uint32_t cmpS2End { 0U }; | ||
| 196 | + int64_t s1GCost { 0 }; | ||
| 197 | + int64_t s1GLastBlockCost { 0 }; | ||
| 198 | + uint32_t s1GBlock { 0U }; | ||
| 199 | + uint32_t oriS1GBlock { 0U }; | ||
| 200 | + int64_t oriS1GCost { 0 }; | ||
| 201 | + int64_t oriS1GLastBlockCost { 0 }; | ||
| 202 | + int64_t oriS1GNormalBlockCost { 0 }; | ||
| 203 | + uint32_t cmpS1GBlock { 0U }; | ||
| 204 | + int64_t cmpS1GCost { 0 }; | ||
| 205 | + int64_t cmpS1GLastBlockCost { 0 }; | ||
| 206 | + int64_t cmpS1GNormalBlockCost { 0 }; | ||
| 207 | + int64_t oriS2TailSize {0}; | ||
| 208 | + int64_t cmpS2TailSize {0}; | ||
| 209 | +}; | ||
| 210 | + | ||
| 211 | +// 分核功能模块内部使用:记录分配过程中,当前核的负载信息 | ||
| 212 | +struct CoreCache { | ||
| 213 | + int64_t costLimit { 0 }; // 负载上限 | ||
| 214 | + int64_t cost { 0 }; // 已分配负载 | ||
| 215 | + uint32_t block { 0U }; // 已分配块数 | ||
| 216 | +}; | ||
| 217 | + | ||
| 218 | +// 分核功能模块内部使用:记录分配过程中的上下文信息 | ||
| 219 | +struct AssignContext { | ||
| 220 | + uint32_t curBIdx { 0U }; | ||
| 221 | + uint32_t curBN2Idx { 0U }; | ||
| 222 | + uint32_t curS1GIdx { 0U }; | ||
| 223 | + uint32_t curS2Idx { 0U }; | ||
| 224 | + uint32_t curCoreIdx { 0U }; | ||
| 225 | + int64_t unassignedCost { 0 }; | ||
| 226 | + uint32_t curKvSplitPart { 1U }; | ||
| 227 | + uint32_t preFdDataNum { 0U }; | ||
| 228 | + | ||
| 229 | + int64_t bN2Cost { 0 }; | ||
| 230 | + uint32_t bN2Block { 0U }; | ||
| 231 | + bool isFinished { false }; | ||
| 232 | + BatchCache batchCache {}; | ||
| 233 | + S1GCache s1GCache {}; | ||
| 234 | + CoreCache coreCache {}; | ||
| 235 | +}; | ||
| 236 | + | ||
| 237 | +class QuantSparseFlashMlaMetadataCpuKernel : public CpuKernel { | ||
| 238 | +public: | ||
| 239 | + QuantSparseFlashMlaMetadataCpuKernel() = default; | ||
| 240 | + ~QuantSparseFlashMlaMetadataCpuKernel() = default; | ||
| 241 | + uint32_t Compute(CpuKernelContext &ctx) override; | ||
| 242 | + | ||
| 243 | +private: | ||
| 244 | + bool Prepare(CpuKernelContext &ctx); | ||
| 245 | + int32_t GetQueryBatchSize(); | ||
| 246 | + void CalcOriMaskMode(); | ||
| 247 | + void CalcCmpMaskMode(); | ||
| 248 | + ValidSocVersion ProcessSocVersion(); | ||
| 249 | + bool ParamsCheck(); | ||
| 250 | + bool ParamsInit(); | ||
| 251 | + bool BalanceSchedule(SplitResult &splitRes); | ||
| 252 | + bool GenMetadata(SplitResult &splitRes); | ||
| 253 | + // util | ||
| 254 | + uint32_t GetS1SeqSize(uint32_t bIdx); | ||
| 255 | + uint32_t GetOriS2SeqSize(uint32_t bIdx); | ||
| 256 | + uint32_t GetCmpS2SeqSize(uint32_t bIdx); | ||
| 257 | + uint64_t GetRevertS2Size(uint32_t bIdx); | ||
| 258 | + uint32_t GetOriTopkLength(); | ||
| 259 | + uint32_t GetCmpTopkLength(); | ||
| 260 | + int64_t CalcOriPreTokenLeftUp(uint32_t s1Size, uint32_t s2Size); | ||
| 261 | + int64_t CalcOriNextTokenLeftUp(uint32_t s1Size, uint32_t s2Size); | ||
| 262 | + int64_t CalcCmpPreTokenLeftUp(uint32_t s1Size, uint64_t s2Size); | ||
| 263 | + int64_t CalcCmpNextTokenLeftUp(uint32_t s1Size, uint64_t s2Size); | ||
| 264 | + Range<int64_t> CalcS2TokenRange(uint32_t s1GIdx, const BatchCache &batchCache, bool isCmpKv); | ||
| 265 | + int64_t OriCalcCost(uint32_t basicM, uint32_t basicS2); | ||
| 266 | + int64_t CmpCalcCost(uint32_t basicM, uint32_t basicS2); | ||
| 267 | + void CalcCostTable(uint32_t s1GTailSize, uint32_t oriS2TailSize, uint32_t cmpS2TailSize); | ||
| 268 | + | ||
| 269 | + // cache calculation | ||
| 270 | + void CalcBatchCache(uint32_t bIdx, const SplitContext &splitContext, BatchCache &batchCache); | ||
| 271 | + void CalcOriBlockRange(const Range<int64_t> &oriS2TokenRange, const BatchCache &batchCache, S1GCache &s1GCache); | ||
| 272 | + void CalcCmpBlockRange(const Range<int64_t> &cmpS2TokenRange, const BatchCache &batchCache, S1GCache &s1GCache); | ||
| 273 | + void CalcOriS1GCache(S1GCache &s1GCache, const SplitInfo &splitInfo); | ||
| 274 | + void CalcCmpS1GCache(S1GCache &s1GCache, const SplitInfo &splitInfo); | ||
| 275 | + void GatherOriAndCmpCache(S1GCache &s1GCache); | ||
| 276 | + void CalcS1GCache(uint32_t s1GIdx, const SplitContext &splitContext, const BatchCache &batchCache, | ||
| 277 | + S1GCache &s1GCache); | ||
| 278 | + | ||
| 279 | + // preprocess | ||
| 280 | + void CalcSplitInfo(SplitContext &splitContext); | ||
| 281 | + void CalcBatchCost(uint32_t bIdx, const SplitContext &splitContext, CostInfo &costInfo); | ||
| 282 | + void CalcCostInfo(SplitContext &splitContext); | ||
| 283 | + | ||
| 284 | + // assign | ||
| 285 | + void UpdateCursor(const SplitContext &splitContext, AssignContext &assignContext); | ||
| 286 | + void AssignByBatch(const SplitContext &splitContext, AssignContext &assignContext); | ||
| 287 | + void AssignByRow(const SplitContext &splitContext, AssignContext &assignContext); | ||
| 288 | + int64_t CalcCurBlockCost(const AssignContext &assignContext); | ||
| 289 | + void AssignByBlock(const SplitContext &splitContext, AssignContext &assignContext); | ||
| 290 | + void ForceAssign(const SplitContext &splitContext, AssignContext &assignContext); | ||
| 291 | + void AssignBlocksToCore(const SplitContext &splitContext, AssignContext &assignContext, SplitResult &result); | ||
| 292 | + | ||
| 293 | + // FD | ||
| 294 | + bool IsNeedRecordFDInfo(const AssignContext &assignContext, const SplitResult &splitRes); | ||
| 295 | + void RecordFDInfo(const SplitContext &splitContext, const AssignContext &assignContext, SplitResult &result); | ||
| 296 | + | ||
| 297 | + // main | ||
| 298 | + void SplitFD(SplitResult &splitRes); | ||
| 299 | + void CalcSplitPlan(int64_t costLimit, const SplitContext &splitContext, SplitResult &result); | ||
| 300 | + | ||
| 301 | +private: | ||
| 302 | + // input | ||
| 303 | + Tensor *cuSeqlensQ_ = nullptr; | ||
| 304 | + Tensor *cuSeqlensOriKv_ = nullptr; | ||
| 305 | + Tensor *cuSeqlensCmpKv_ = nullptr; | ||
| 306 | + Tensor *sequsedQ_ = nullptr; | ||
| 307 | + Tensor *sequsedOriKv_ = nullptr; | ||
| 308 | + Tensor *sequsedCmpKv_ = nullptr; | ||
| 309 | + Tensor *cmpResidualKv_ = nullptr; | ||
| 310 | + Tensor *oriTopkLength_ = nullptr; | ||
| 311 | + Tensor *cmpTopkLength_ = nullptr; | ||
| 312 | + | ||
| 313 | + // output | ||
| 314 | + Tensor *metadata_ = nullptr; | ||
| 315 | + | ||
| 316 | + // attributes | ||
| 317 | + int32_t batchSize_ = 0; | ||
| 318 | + int32_t maxSeqlenQ_ = 0; | ||
| 319 | + int32_t numHeadsQ_ = 0; | ||
| 320 | + int32_t maxSeqlenOriKv_ = 0; | ||
| 321 | + int32_t maxSeqlenCmpKv_ = 0; | ||
| 322 | + int32_t numHeadsKv_ = 1; | ||
| 323 | + int32_t headDim_ = 0; | ||
| 324 | + int32_t oriTopK_ = 0; | ||
| 325 | + int32_t cmpTopK_ = 0; | ||
| 326 | + int32_t cmpRatio_ = 1; | ||
| 327 | + int32_t oriMaskMode_ = static_cast<int32_t>(SparseMode::BAND); | ||
| 328 | + int32_t cmpMaskMode_ = static_cast<int32_t>(SparseMode::RIGHT_DOWN_CAUSAL); | ||
| 329 | + int64_t oriWinLeft_ = 127; | ||
| 330 | + int64_t oriWinRight_ = 0; | ||
| 331 | + std::string layoutQ_ = "BSND"; | ||
| 332 | + std::string layoutKv_ = "PA_BBND"; | ||
| 333 | + bool hasOriKv_ = true; | ||
| 334 | + bool hasCmpKv_ = true; | ||
| 335 | + uint32_t aicCoreNum_ = optiling::AIC_CORE_MAX_NUM; | ||
| 336 | + uint32_t aivCoreNum_ = optiling::AIV_CORE_MAX_NUM; | ||
| 337 | + | ||
| 338 | + // attr | ||
| 339 | + std::string socVersion_ = ""; | ||
| 340 | + int64_t oriPreToken_ = 0; | ||
| 341 | + int64_t oriNextToken_ = 0; | ||
| 342 | + int64_t cmpPreToken_ = 0; | ||
| 343 | + int64_t cmpNextToken_ = 0; | ||
| 344 | + uint32_t groupSize_ = 0; | ||
| 345 | + uint32_t mBaseSize_ = 0; | ||
| 346 | + uint32_t s2BaseSize_ = 128U; | ||
| 347 | + bool isS1G_ = true; | ||
| 348 | + bool supportFd = false; | ||
| 349 | + uint32_t oriAttentionMode_ = HAS_MASK; | ||
| 350 | + uint32_t cmpAttentionMode_ = HAS_MASK; | ||
| 351 | + BlockCost<int64_t> typeCost_ = {}; | ||
| 352 | + bool isSplitG_ = false; | ||
| 353 | + bool isSparseOriKv_ = false; | ||
| 354 | + bool isSparseCmpKv_ = false; | ||
| 355 | + | ||
| 356 | +private: | ||
| 357 | + enum class ParamId : uint32_t { | ||
| 358 | + // input | ||
| 359 | + cuSeqlensQ = 0, | ||
| 360 | + cuSeqlensOriKv = 1, | ||
| 361 | + cuSeqlensCmpKv = 2, | ||
| 362 | + sequsedQ = 3, | ||
| 363 | + sequsedOriKv = 4, | ||
| 364 | + sequsedCmpKv = 5, | ||
| 365 | + cmpResidualKv = 6, | ||
| 366 | + oriTopkLength = 7, | ||
| 367 | + cmpTopkLength = 8, | ||
| 368 | + // output | ||
| 369 | + metaData = 0, | ||
| 370 | + }; | ||
| 371 | +}; | ||
| 372 | +} // namespace aicpu | ||
| 373 | + | ||
| 374 | +#endif | ||
Aattention/quant_sparse_flash_mla_metadata/op_kernel_aicpu/quant_sparse_flash_mla_metadata_aicpu.json+15-0
| @@ -0,0 +1,15 @@ | |||
| 1 | +{ | ||
| 2 | + "QuantSparseFlashMlaMetadata":{ | ||
| 3 | + "opInfo":{ | ||
| 4 | + "computeCost":"100", | ||
| 5 | + "engine":"DNN_VM_AICPU", | ||
| 6 | + "flagAsync":"False", | ||
| 7 | + "flagPartial":"False", | ||
| 8 | + "functionName":"RunCpuKernel", | ||
| 9 | + "kernelSo":"libtransformer_aicpu_kernels.so", | ||
| 10 | + "opKernelLib":"CUSTAICPUKernel", | ||
| 11 | + "userDefined":"True", | ||
| 12 | + "workspaceSize":"100" | ||
| 13 | + } | ||
| 14 | + } | ||
| 15 | +} | ||