Merged
fused_infer_attention_score算子arch38下host侧Tiling代码重构 #16968
HuLinjieAdd on 4 days ago
fused_infer_attention_score算子arch38下host侧Tiling代码重构 #16968
Merged
Merged fromHuLinjie/ops-transformerdevintocann/ops-transformermaster
Total: 20 files changed+6416-5122
| @@ -89,7 +89,15 @@ if(NOT BUILD_OPS_RTY_KERNEL) | |||
| 89 | ../op_host/arch35/fused_infer_attention_score_tiling_v4.cpp | 89 | ../op_host/arch35/fused_infer_attention_score_tiling_v4.cpp |
| 90 | ../op_host/arch35/fused_infer_attention_score_tiling_impl.cpp | 90 | ../op_host/arch35/fused_infer_attention_score_tiling_impl.cpp |
| 91 | ../op_host/arch38/fused_infer_attention_score_tiling_arch38.cpp | 91 | ../op_host/arch38/fused_infer_attention_score_tiling_arch38.cpp |
| 92 | + ../op_host/arch38/fia_arch38_context_convert.cpp | ||
| 92 | ../op_host/arch38/fia_arch38_prompt_tiling.cpp | 93 | ../op_host/arch38/fia_arch38_prompt_tiling.cpp |
| 94 | + ../op_host/arch38/fia_arch38_dataconvert.cpp | ||
| 95 | + ../op_host/arch38/fia_arch38_quant_check.cpp | ||
| 96 | + ../op_host/arch38/fia_arch38_pa_check.cpp | ||
| 97 | + ../op_host/arch38/fia_arch38_mask_sparse_check.cpp | ||
| 98 | + ../op_host/arch38/fia_arch38_feature_check.cpp | ||
| 99 | + ../op_host/arch38/fia_arch38_shape_check.cpp | ||
| 100 | + ../op_host/arch38/fia_arch38_tiling_compute.cpp | ||
| 93 | ../op_host/fused_infer_attention_score_tiling_info_parser.cpp | 101 | ../op_host/fused_infer_attention_score_tiling_info_parser.cpp |
| 94 | ${ARCH35_CHECKER_SRC_FILES} | 102 | ${ARCH35_CHECKER_SRC_FILES} |
| 95 | ../op_host/arch22/fused_infer_attention_score_tiling_v3.cpp | 103 | ../op_host/arch22/fused_infer_attention_score_tiling_v3.cpp |
| @@ -8,6 +8,19 @@ | |||
| 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 | +/*! | ||
| 12 | + * \file fia_arch38_context.h | ||
| 13 | + * \brief FIA arch38 算子输入上下文聚合结构定义(输入解析层,不参与序列化) | ||
| 14 | + * | ||
| 15 | + * 本头文件声明 host 侧解析算子输入时使用的上下文结构,把 gert 侧的 Tensor/StorageShape/DataType | ||
| 16 | + * 指针统一聚合为便于 tiling 逻辑访问的纯结构体: | ||
| 17 | + * - FiaArch38CompileInfo:编译期信息(各核数、UB/L1/L0 空间、SoC 版本); | ||
| 18 | + * - FiaArch38ContextParams:算子全部可选/必选输入的指针与标量聚合,是 tiling 各校验的入参基座; | ||
| 19 | + * - FiaArch38RequiredParaInfo / FiaArch38OptionalParaInfo:必选/可选参数描述; | ||
| 20 | + * - FiaArch38IncreContext:IFA 增量(复用 PFA 模板)场景的输入描述。 | ||
| 21 | + * 这些结构仅用于 host 侧数据交换,不参与算子 tiling 数据的序列化。 | ||
| 22 | + */ | ||
| 23 | + | ||
| 11 | #ifndef FIA_ARCH38_CONTEXT_H | 24 | #ifndef FIA_ARCH38_CONTEXT_H |
| 12 | #define FIA_ARCH38_CONTEXT_H | 25 | #define FIA_ARCH38_CONTEXT_H |
| 13 | 26 | ||
| @@ -19,7 +32,9 @@ | |||
| 19 | #include "register/op_def_registry.h" | 32 | #include "register/op_def_registry.h" |
| 20 | 33 | ||
| 21 | namespace optiling { | 34 | namespace optiling { |
| 35 | +namespace arch38 { | ||
| 22 | 36 | ||
| 37 | +// 编译期硬件信息:各核数量、片上/UB/L1/L0 空间与 SoC 版本,供 tiling 切分与 workspace 估算使用 | ||
| 23 | struct FiaArch38CompileInfo { | 38 | struct FiaArch38CompileInfo { |
| 24 | uint32_t aivNum; | 39 | uint32_t aivNum; |
| 25 | uint32_t aicNum; | 40 | uint32_t aicNum; |
| @@ -32,6 +47,7 @@ struct FiaArch38CompileInfo { | |||
| 32 | platform_ascendc::SocVersion socShortName; | 47 | platform_ascendc::SocVersion socShortName; |
| 33 | }; | 48 | }; |
| 34 | 49 | ||
| 50 | +// 算子输入上下文聚合:把 gert 侧的 tensor/shape/dtype 指针与标量统一收纳,作为各校验函数的入参基座 | ||
| 35 | struct FiaArch38ContextParams { | 51 | struct FiaArch38ContextParams { |
| 36 | const gert::Tensor* pseShift = nullptr; | 52 | const gert::Tensor* pseShift = nullptr; |
| 37 | const gert::Tensor* attentionMask = nullptr; | 53 | const gert::Tensor* attentionMask = nullptr; |
| @@ -103,11 +119,11 @@ struct FiaArch38ContextParams { | |||
| 103 | 119 | ||
| 104 | const int64_t* innerPrecisePtr = nullptr; | 120 | const int64_t* innerPrecisePtr = nullptr; |
| 105 | const int64_t* headsNumber = nullptr; | 121 | const int64_t* headsNumber = nullptr; |
| 106 | - const int32_t* sparseMode = nullptr; | 122 | + const int64_t* sparseMode = nullptr; |
| 107 | const int64_t* preToken = nullptr; | 123 | const int64_t* preToken = nullptr; |
| 108 | const int64_t* nextToken = nullptr; | 124 | const int64_t* nextToken = nullptr; |
| 109 | const float* scaleValue = nullptr; | 125 | const float* scaleValue = nullptr; |
| 110 | - const int32_t* blockSize = nullptr; | 126 | + const int64_t* blockSize = nullptr; |
| 111 | const char* layout = nullptr; | 127 | const char* layout = nullptr; |
| 112 | const int64_t* numKeyValueHeads = nullptr; | 128 | const int64_t* numKeyValueHeads = nullptr; |
| 113 | size_t* workspaceSize = nullptr; | 129 | size_t* workspaceSize = nullptr; |
| @@ -140,16 +156,19 @@ struct FiaArch38ContextParams { | |||
| 140 | bool isBaseApiTilingKey = false; | 156 | bool isBaseApiTilingKey = false; |
| 141 | }; | 157 | }; |
| 142 | 158 | ||
| 159 | +// 必选参数描述:编译期 tensor 描述 + 形状 | ||
| 143 | struct FiaArch38RequiredParaInfo { | 160 | struct FiaArch38RequiredParaInfo { |
| 144 | const gert::CompileTimeTensorDesc* desc; | 161 | const gert::CompileTimeTensorDesc* desc; |
| 145 | const gert::StorageShape* shape; | 162 | const gert::StorageShape* shape; |
| 146 | }; | 163 | }; |
| 147 | 164 | ||
| 165 | +// 可选参数描述:编译期 tensor 描述 + tensor(可能为 null) | ||
| 148 | struct FiaArch38OptionalParaInfo { | 166 | struct FiaArch38OptionalParaInfo { |
| 149 | const gert::CompileTimeTensorDesc* desc; | 167 | const gert::CompileTimeTensorDesc* desc; |
| 150 | const gert::Tensor* tensor; | 168 | const gert::Tensor* tensor; |
| 151 | }; | 169 | }; |
| 152 | 170 | ||
| 171 | +// IFA 增量(复用 PFA 模板)场景的输入描述,供 IsArch38IncreScenarioSupported 做增量边界校验 | ||
| 153 | struct FiaArch38IncreContext { | 172 | struct FiaArch38IncreContext { |
| 154 | FiaArch38RequiredParaInfo query = {nullptr, nullptr}; | 173 | FiaArch38RequiredParaInfo query = {nullptr, nullptr}; |
| 155 | FiaArch38RequiredParaInfo key = {nullptr, nullptr}; | 174 | FiaArch38RequiredParaInfo key = {nullptr, nullptr}; |
| @@ -161,11 +180,11 @@ struct FiaArch38IncreContext { | |||
| 161 | 180 | ||
| 162 | FiaArch38RequiredParaInfo attenOut = {nullptr, nullptr}; | 181 | FiaArch38RequiredParaInfo attenOut = {nullptr, nullptr}; |
| 163 | 182 | ||
| 164 | - const uint32_t* numHeads = nullptr; | 183 | + const int64_t* numHeads = nullptr; |
| 165 | - const uint32_t* kvHeadNums = nullptr; | 184 | + const int64_t* kvHeadNums = nullptr; |
| 166 | - const char* layOut = nullptr; | ||
| 167 | }; | 185 | }; |
| 168 | 186 | ||
| 187 | +} // namespace arch38 | ||
| 169 | } // namespace optiling | 188 | } // namespace optiling |
| 170 | 189 | ||
| 171 | #endif // FIA_ARCH38_CONTEXT_H | 190 | #endif // FIA_ARCH38_CONTEXT_H |
| @@ -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 | +/*! | ||
| 12 | + * \file fia_arch38_context_convert.h | ||
| 13 | + * \brief PFA/IFA 上下文转换入口:将 gert::TilingContext 收敛为 PFA/IFA 上下文参数 | ||
| 14 | + */ | ||
| 15 | + | ||
| 16 | +#ifndef FIA_ARCH38_CONTEXT_CONVERT_H | ||
| 17 | +#define FIA_ARCH38_CONTEXT_CONVERT_H | ||
| 18 | + | ||
| 19 | +#include "fia_arch38_context.h" | ||
| 20 | + | ||
| 21 | +namespace optiling { | ||
| 22 | +namespace arch38 { | ||
| 23 | + | ||
| 24 | +ge::graphStatus ConvertContextToParamsPFA(gert::TilingContext* context, FiaArch38ContextParams& contextKeyParams, | ||
| 25 | + bool isMaxWorkspace); | ||
| 26 | +ge::graphStatus ConvertContextToParamsIFA(gert::TilingContext& context, FiaArch38IncreContext& ifaContext); | ||
| 27 | +bool GetMaxWorkspaceFlag(gert::TilingContext& context); | ||
| 28 | + | ||
| 29 | +} // namespace arch38 | ||
| 30 | +} // namespace optiling | ||
| 31 | + | ||
| 32 | +#endif // FIA_ARCH38_CONTEXT_CONVERT_H | ||
| @@ -0,0 +1,438 @@ | |||
| 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 | +#include "fia_arch38_prompt_tiling.h" | ||
| 12 | + | ||
| 13 | +using namespace ge; | ||
| 14 | +using namespace AscendC; | ||
| 15 | +using namespace matmul_tiling; | ||
| 16 | +namespace optiling { | ||
| 17 | + | ||
| 18 | +namespace arch38 { | ||
| 19 | + | ||
| 20 | +void FiaArch38PromptTiling::SetMultiCoreParamsRegbase(int64_t totalSize, int64_t actualUsedCoreNum) | ||
| 21 | +{ | ||
| 22 | + faTilingAdapter.multiCoreParamsRegbase.set_coreNum(static_cast<int32_t>(actualUsedCoreNum)); | ||
| 23 | + faTilingAdapter.multiCoreParamsRegbase.set_totalSize(totalSize); | ||
| 24 | + faTilingAdapter.multiCoreParamsRegbase.set_splitFactorSize(CeilDivision(totalSize, actualUsedCoreNum)); | ||
| 25 | + faTilingAdapter.multiCoreParamsRegbase.set_splitFactorTailSize( | ||
| 26 | + CalcTailSize(totalSize, faTilingAdapter.multiCoreParamsRegbase.get_splitFactorSize())); | ||
| 27 | +} | ||
| 28 | + | ||
| 29 | +bool FiaArch38PromptTiling::CheckArch38PlatformSupported(const FiaArch38ContextParams& contextKeyParams, | ||
| 30 | + const PFAShapeInfo& queryShapeInfo, | ||
| 31 | + const PFAShapeInfo& valueShapeInfo) const | ||
| 32 | +{ | ||
| 33 | + // 以下约束只与平台已注册的模板/tilingKey 有关,不依赖 IFA/合轴改写后的 n/s, | ||
| 34 | + // 因此 IFA / MLA / 合轴路径也必须满足,不能放在 early-return 之后。 | ||
| 35 | + OP_CHECK_IF((inputLayout != InputLayout::BNSD), | ||
| 36 | + OP_LOGE_FOR_INVALID_FORMAT_WITH_REASON(contextKeyParams.opName, "input_layout", contextKeyParams.layout, | ||
| 37 | + "only BNSD layout is supported on this platform"), | ||
| 38 | + return false); | ||
| 39 | + | ||
| 40 | + OP_CHECK_IF((!IsArch38SupportedHeadSize(static_cast<int64_t>(queryShapeInfo.d)) || | ||
| 41 | + !IsArch38SupportedHeadSize(static_cast<int64_t>(valueShapeInfo.d))), | ||
| 42 | + OP_LOGE_FOR_INVALID_SHAPESIZES_WITH_REASON( | ||
| 43 | + contextKeyParams.opName, "query, value", | ||
| 44 | + (std::to_string(queryShapeInfo.d) + ", " + std::to_string(valueShapeInfo.d)).c_str(), | ||
| 45 | + "head size only supports 64, 80, 128 or 256 on this platform"), | ||
| 46 | + return false); | ||
| 47 | + | ||
| 48 | + // 本平台注册的模板中 query d 恒等于 value d。 | ||
| 49 | + OP_CHECK_IF((queryShapeInfo.d != valueShapeInfo.d), | ||
| 50 | + OP_LOGE_FOR_INVALID_VALUES_WITH_REASON( | ||
| 51 | + contextKeyParams.opName, "query.d, value.d", | ||
| 52 | + (std::to_string(queryShapeInfo.d) + ", " + std::to_string(valueShapeInfo.d)).c_str(), | ||
| 53 | + "query d and value d must be equal on this platform."), | ||
| 54 | + return false); | ||
| 55 | + | ||
| 56 | + // 仅放行已注册 tiling key 的 dtype 组合,避免分发到不存在的 kernel。 | ||
| 57 | + OP_CHECK_IF( | ||
| 58 | + !IsArch38SupportedDataType(contextKeyParams.inputDataType, contextKeyParams.kDataType, | ||
| 59 | + contextKeyParams.outputDataType, static_cast<int64_t>(queryShapeInfo.d)), | ||
| 60 | + OP_LOGE_FOR_INVALID_DTYPES_WITH_REASON( | ||
| 61 | + contextKeyParams.opName, "query, key, attentionOut", | ||
| 62 | + (GetPfaDataTypeStr(contextKeyParams.inputDataType) + ", " + GetPfaDataTypeStr(contextKeyParams.kDataType) + | ||
| 63 | + ", " + GetPfaDataTypeStr(contextKeyParams.outputDataType)) | ||
| 64 | + .c_str(), | ||
| 65 | + "only fp16/int8 query/key/value with fp16/int8 output is supported on this platform, " | ||
| 66 | + "and head size 256 only supports fp16 query/key/value with fp16 output."), | ||
| 67 | + return false); | ||
| 68 | + | ||
| 69 | + // head size 256 仅有带 mask 场景的 tiling key。 | ||
| 70 | + OP_CHECK_IF(((static_cast<int64_t>(queryShapeInfo.d) == ARCH38_SUPPORTED_D_256) && (!enableMask)), | ||
| 71 | + OP_LOGE_FOR_INVALID_VALUE_WITH_REASON(contextKeyParams.opName, "atten_mask", "absent", | ||
| 72 | + "head size 256 requires atten_mask on this platform."), | ||
| 73 | + return false); | ||
| 74 | + | ||
| 75 | + return true; | ||
| 76 | +} | ||
| 77 | + | ||
| 78 | +bool FiaArch38PromptTiling::CheckArch38ScenarioSupported(const FiaArch38ContextParams& contextKeyParams, | ||
| 79 | + const PFAShapeInfo& queryShapeInfo, | ||
| 80 | + const PFAShapeInfo& valueShapeInfo) const | ||
| 81 | +{ | ||
| 82 | + if (!CheckArch38UnsupportedFeatures(contextKeyParams)) { | ||
| 83 | + return false; | ||
| 84 | + } | ||
| 85 | + | ||
| 86 | + // 平台级约束(layout / head size / dQ == dV / dtype / D256 mask)对所有路径生效。 | ||
| 87 | + if (!CheckArch38PlatformSupported(contextKeyParams, queryShapeInfo, valueShapeInfo)) { | ||
| 88 | + return false; | ||
| 89 | + } | ||
| 90 | + | ||
| 91 | + // 带 mask 的 band(sparseMode=4)会生成 +2e8 的 tilingKey,本平台没有注册; | ||
| 92 | + // 不带 mask 的 band 仍走普通的 no-mask key,IFA / 合轴可继续使用。 | ||
| 93 | + OP_CHECK_IF( | ||
| 94 | + (enableMask && (sparseModeVal == SPARSE_MODE_BAND)), | ||
| 95 | + OP_LOGE_FOR_INVALID_VALUES_WITH_REASON(contextKeyParams.opName, "sparse_mode, atten_mask", "band mask present", | ||
| 96 | + "band mask is not supported on this platform"), | ||
| 97 | + return false); | ||
| 98 | + | ||
| 99 | + // IFA / 合轴在调用点前已按 gSize 改写 queryShapeInfo.n/.s,下面 qs == kvs、qs > 1 等 | ||
| 100 | + // PFA 原始形状约束不适用,因此这里只跳过形状相关检查(平台级约束已在上方执行)。 | ||
| 101 | + if (enableIFA || enablePFAMerge) { | ||
| 102 | + return true; | ||
| 103 | + } | ||
| 104 | + | ||
| 105 | + if (!CheckArch38ShapeConstraints(contextKeyParams, queryShapeInfo)) { | ||
| 106 | + return false; | ||
| 107 | + } | ||
| 108 | + | ||
| 109 | + if (!enableMask) { | ||
| 110 | + return true; | ||
| 111 | + } | ||
| 112 | + return CheckArch38MaskConstraints(contextKeyParams); | ||
| 113 | +} | ||
| 114 | + | ||
| 115 | +bool FiaArch38PromptTiling::CheckArch38UnsupportedFeatures(const FiaArch38ContextParams& contextKeyParams) const | ||
| 116 | +{ | ||
| 117 | + // 本平台没有 queryRope/keyRope 对应的模板:所有 tiling key 都以 hasRope == false 实例化, | ||
| 118 | + // 若放行 PFARope 场景会静默忽略 rope 输入。 | ||
| 119 | + OP_CHECK_IF(enablePFARope, | ||
| 120 | + OP_LOGE_FOR_INVALID_VALUE_WITH_REASON(contextKeyParams.opName, "queryRope/keyRope", "not empty", | ||
| 121 | + "queryRope/keyRope is not supported on this platform."), | ||
| 122 | + return false); | ||
| 123 | + | ||
| 124 | + // 本平台同样没有 MLA 对应的模板:entry 中不存在 +3e7(MLA)key,也没有 d=192/576 的组合, | ||
| 125 | + // 放行只会生成未注册的 key 并分发失败,此处显式拒绝。 | ||
| 126 | + OP_CHECK_IF((enableIFAMLA || enablePFAMLA), | ||
| 127 | + OP_LOGE_FOR_INVALID_VALUE_WITH_REASON(contextKeyParams.opName, "queryRope/keyRope or query.d, value.d", | ||
| 128 | + "MLA", "MLA is not supported on this platform."), | ||
| 129 | + return false); | ||
| 130 | + return true; | ||
| 131 | +} | ||
| 132 | + | ||
| 133 | +bool FiaArch38PromptTiling::CheckArch38ShapeConstraints(const FiaArch38ContextParams& contextKeyParams, | ||
| 134 | + const PFAShapeInfo& queryShapeInfo) const | ||
| 135 | +{ | ||
| 136 | + OP_CHECK_IF((queryShapeInfo.b != 1U && queryShapeInfo.b != 9U), | ||
| 137 | + OP_LOGE_FOR_INVALID_VALUE_WITH_REASON(contextKeyParams.opName, "query batch", | ||
| 138 | + std::to_string(queryShapeInfo.b).c_str(), | ||
| 139 | + "batch size must be 1 or 9 on this platform"), | ||
| 140 | + return false); | ||
| 141 | + | ||
| 142 | + OP_CHECK_IF((queryShapeInfo.s != static_cast<uint64_t>(S2)), | ||
| 143 | + OP_LOGE_FOR_INVALID_SHAPESIZES_WITH_REASON( | ||
| 144 | + contextKeyParams.opName, "query, key/value", | ||
| 145 | + (std::to_string(queryShapeInfo.s) + ", " + std::to_string(S2)).c_str(), | ||
| 146 | + "query seq length must equal kv seq length on this platform"), | ||
| 147 | + return false); | ||
| 148 | + | ||
| 149 | + OP_CHECK_IF(((queryShapeInfo.s <= 1UL) || (S2 <= 1U)), | ||
| 150 | + OP_LOGE_FOR_INVALID_SHAPESIZES_WITH_REASON( | ||
| 151 | + contextKeyParams.opName, "query, key/value", | ||
| 152 | + (std::to_string(queryShapeInfo.s) + ", " + std::to_string(S2)).c_str(), | ||
| 153 | + "query and kv seq length must be greater than 1 on this platform"), | ||
| 154 | + return false); | ||
| 155 | + | ||
| 156 | + OP_CHECK_IF((queryShapeInfo.s > static_cast<uint64_t>(ARCH38_SUPPORTED_MAX_SEQ)), | ||
| 157 | + OP_LOGE_FOR_INVALID_SHAPESIZE_WITH_REASON( | ||
| 158 | + contextKeyParams.opName, "query", std::to_string(queryShapeInfo.s).c_str(), | ||
| 159 | + "seq length must be less than or equal to ARCH38_SUPPORTED_MAX_SEQ on this platform"), | ||
| 160 | + return false); | ||
| 161 | + | ||
| 162 | + return CheckArch38HeadAndSparse(contextKeyParams); | ||
| 163 | +} | ||
| 164 | + | ||
| 165 | +bool FiaArch38PromptTiling::CheckArch38HeadAndSparse(const FiaArch38ContextParams& contextKeyParams) const | ||
| 166 | +{ | ||
| 167 | + const int64_t nQ = *contextKeyParams.headsNumber; | ||
| 168 | + const int64_t nKV = (*contextKeyParams.numKeyValueHeads > 0) ? *contextKeyParams.numKeyValueHeads : nQ; | ||
| 169 | + OP_CHECK_IF( | ||
| 170 | + ((nQ <= 0) || (nQ > ARCH38_SUPPORTED_MAX_N) || (nKV <= 0) || (nKV > ARCH38_SUPPORTED_MAX_N)), | ||
| 171 | + OP_LOGE_FOR_INVALID_VALUES_WITH_REASON(contextKeyParams.opName, "num_heads, num_key_value_heads", | ||
| 172 | + (std::to_string(nQ) + ", " + std::to_string(nKV)).c_str(), | ||
| 173 | + "head num must be in (0, ARCH38_SUPPORTED_MAX_N] on this platform"), | ||
| 174 | + return false); | ||
| 175 | + | ||
| 176 | + // Guarantees the kernel never takes the InitOutput branch. | ||
| 177 | + OP_CHECK_IF((needInit != 0U), | ||
| 178 | + OP_LOGE_FOR_INVALID_VALUES_WITH_REASON( | ||
| 179 | + contextKeyParams.opName, "actual_seq_lengths, pre_tokens, leftPadding", "needInit = 1", | ||
| 180 | + "output must be fully covered by the main loop, check actual_seq_lengths / preTokens / " | ||
| 181 | + "left padding configuration"), | ||
| 182 | + return false); | ||
| 183 | + | ||
| 184 | + OP_CHECK_IF((sparseModeVal != SPARSE_MODE_NO_MASK), | ||
| 185 | + OP_LOGE_FOR_INVALID_VALUE_WITH_REASON(contextKeyParams.opName, "sparse_mode", | ||
| 186 | + std::to_string(sparseModeVal).c_str(), | ||
| 187 | + "attention mask only supports sparseMode = 0 on this platform"), | ||
| 188 | + return false); | ||
| 189 | + return true; | ||
| 190 | +} | ||
| 191 | + | ||
| 192 | +bool FiaArch38PromptTiling::CheckArch38MaskConstraints(const FiaArch38ContextParams& contextKeyParams) const | ||
| 193 | +{ | ||
| 194 | + // With sparseMode = 0, nextTokensPerbatch always equals this attribute, no per-batch conversion needed. | ||
| 195 | + OP_CHECK_IF((sparseNextTokens != 0), | ||
| 196 | + OP_LOGE_FOR_INVALID_VALUE_WITH_REASON(contextKeyParams.opName, "next_tokens", | ||
| 197 | + std::to_string(sparseNextTokens).c_str(), | ||
| 198 | + "attention mask requires nextTokens = 0"), | ||
| 199 | + return false); | ||
| 200 | + | ||
| 201 | + OP_CHECK_IF((sparsePreTokens <= 0), | ||
| 202 | + OP_LOGE_FOR_INVALID_VALUE_WITH_REASON(contextKeyParams.opName, "pre_tokens", | ||
| 203 | + std::to_string(sparsePreTokens).c_str(), | ||
| 204 | + "attention mask requires preTokens > 0"), | ||
| 205 | + return false); | ||
| 206 | + return true; | ||
| 207 | +} | ||
| 208 | + | ||
| 209 | +bool FiaArch38PromptTiling::IsFlashDecode() const | ||
| 210 | +{ | ||
| 211 | + return false; | ||
| 212 | +} | ||
| 213 | + | ||
| 214 | +ge::graphStatus FiaArch38PromptTiling::SplitBNS(FiaArch38FlashAttentionTilingData& tilingData, uint64_t bng) | ||
| 215 | +{ | ||
| 216 | + uint64_t batchSize = tilingData.fiaArch38AttentionBaseParams.get_batchSize(); | ||
| 217 | + uint64_t headNumSize = tilingData.fiaArch38AttentionBaseParams.get_headNumSize() * gSize; | ||
| 218 | + uint64_t headDimAlign = AlignUp(tilingData.fiaArch38AttentionBaseParams.get_vHeadSize(), BYTE_BLOCK); | ||
| 219 | + uint32_t kvSplitLimit = 256U; // 256: 经验值 | ||
| 220 | + int64_t kvSplitPart = 0; | ||
| 221 | + if (bng != 0) { | ||
| 222 | + kvSplitPart = aicNum / bng; | ||
| 223 | + } | ||
| 224 | + if (kvSplitPart < 1) { | ||
| 225 | + kvSplitPart = 1; // kvSplitPart 至少为 1,避免后续按份切分时除零 | ||
| 226 | + } | ||
| 227 | + while (((maxActualseqKV / kvSplitPart) < kvSplitLimit) && (kvSplitPart > 1)) { | ||
| 228 | + kvSplitPart--; | ||
| 229 | + } | ||
| 230 | + | ||
| 231 | + faTilingAdapter.inputParamsRegbase.set_kvSplitPart(kvSplitPart); | ||
| 232 | + faTilingAdapter.inputParamsRegbase.set_accumOutSize(batchSize * headNumSize * kvSplitPart * headDimAlign); | ||
| 233 | + faTilingAdapter.inputParamsRegbase.set_logSumExpSize(batchSize * headNumSize * kvSplitPart * | ||
| 234 | + (BYTE_BLOCK / sizeof(float))); | ||
| 235 | + | ||
| 236 | + return ge::GRAPH_SUCCESS; | ||
| 237 | +} | ||
| 238 | + | ||
| 239 | +void FiaArch38PromptTiling::SetAttenMaskCompressMode() | ||
| 240 | +{ | ||
| 241 | + static std::map<uint32_t, uint8_t> sparseToCompressModeMap = { | ||
| 242 | + {SPARSE_MODE_NO_MASK, PfaAttenMaskCompressMode::PFA_NO_COMPRESS_MODE}, | ||
| 243 | + {SPARSE_MODE_ALL_MASK, PfaAttenMaskCompressMode::PFA_NO_COMPRESS_MODE}, | ||
| 244 | + {SPARSE_MODE_LEFT_UP, PfaAttenMaskCompressMode::PFA_LEFT_UP_CAUSAL_MODE}, | ||
| 245 | + {SPARSE_MODE_RIGHT_DOWN, PfaAttenMaskCompressMode::PFA_RIGHT_DOWN_CAUSAL_MODE}, | ||
| 246 | + {SPARSE_MODE_BAND, PfaAttenMaskCompressMode::PFA_BAND_MODE}}; | ||
| 247 | + auto itr = sparseToCompressModeMap.find(sparseModeVal); | ||
| 248 | + if (itr == sparseToCompressModeMap.end()) { | ||
| 249 | + faTilingAdapter.inputParamsRegbase.set_attenMaskCompressMode(0); | ||
| 250 | + } else { | ||
| 251 | + faTilingAdapter.inputParamsRegbase.set_attenMaskCompressMode(itr->second); | ||
| 252 | + } | ||
| 253 | +} | ||
| 254 | + | ||
| 255 | +void FiaArch38PromptTiling::SetLayoutType() | ||
| 256 | +{ | ||
| 257 | + static std::map<InputLayout, LayoutType> layoutStrToLayoutTypeMap = { | ||
| 258 | + {InputLayout::BSH, LayoutType::LAYOUT_BSH}, | ||
| 259 | + {InputLayout::TND, LayoutType::LAYOUT_TND}, | ||
| 260 | + {InputLayout::BSND, LayoutType::LAYOUT_BSND}, | ||
| 261 | + {InputLayout::BNSD, LayoutType::LAYOUT_BNSD}, | ||
| 262 | + }; | ||
| 263 | + auto itr = layoutStrToLayoutTypeMap.find(inputLayout); | ||
| 264 | + if (itr == layoutStrToLayoutTypeMap.end()) { | ||
| 265 | + faTilingAdapter.inputParamsRegbase.set_layoutType(static_cast<uint8_t>(0)); | ||
| 266 | + } else { | ||
| 267 | + faTilingAdapter.inputParamsRegbase.set_layoutType(static_cast<uint8_t>(itr->second)); | ||
| 268 | + } | ||
| 269 | +} | ||
| 270 | + | ||
| 271 | +ge::graphStatus FiaArch38PromptTiling::SetQKVStartIdx(FiaArch38ContextParams& contextKeyParams) | ||
| 272 | +{ | ||
| 273 | + auto& inputParams = faTilingAdapter.inputParamsRegbase; | ||
| 274 | + inputParams.set_qStartIdx(0); | ||
| 275 | + inputParams.set_kvStartIdx(0); | ||
| 276 | + if (!enableAlibiPse) { | ||
| 277 | + return ge::GRAPH_SUCCESS; | ||
| 278 | + } | ||
| 279 | + int64_t qStartIdx = 0; | ||
| 280 | + int64_t kvStartIdx = 0; | ||
| 281 | + auto qStartIdxTensor = contextKeyParams.qStartIdx; | ||
| 282 | + if (qStartIdxTensor != nullptr) { | ||
| 283 | + if (qStartIdxTensor->GetShapeSize() >= 1) { | ||
| 284 | + const int64_t* value = qStartIdxTensor->GetData<int64_t>(); | ||
| 285 | + if (value != nullptr) { | ||
| 286 | + qStartIdx = value[0]; | ||
| 287 | + OP_CHECK_IF(qStartIdx > INT32_MAX || qStartIdx < INT32_MIN, | ||
| 288 | + OP_LOGE_FOR_INVALID_VALUE_WITH_REASON(contextKeyParams.opName, "q_start_idx", | ||
| 289 | + std::to_string(qStartIdx).c_str(), | ||
| 290 | + "qStartIdx must be in range [INT32_MIN, INT32_MAX]"), | ||
| 291 | + return ge::GRAPH_FAILED); | ||
| 292 | + inputParams.set_qStartIdx(qStartIdx); | ||
| 293 | + } | ||
| 294 | + } | ||
| 295 | + } | ||
| 296 | + | ||
| 297 | + auto kvStartIdxTensor = contextKeyParams.kvStartIdx; | ||
| 298 | + if (kvStartIdxTensor != nullptr) { | ||
| 299 | + if (kvStartIdxTensor->GetShapeSize() >= 1) { | ||
| 300 | + const int64_t* kvValue = kvStartIdxTensor->GetData<int64_t>(); | ||
| 301 | + if (kvValue != nullptr) { | ||
| 302 | + kvStartIdx = kvValue[0]; | ||
| 303 | + OP_CHECK_IF(kvStartIdx > INT32_MAX || kvStartIdx < INT32_MIN, | ||
| 304 | + OP_LOGE_FOR_INVALID_VALUE_WITH_REASON(contextKeyParams.opName, "kv_start_idx", | ||
| 305 | + std::to_string(kvStartIdx).c_str(), | ||
| 306 | + "kvStartIdx must be in range [INT32_MIN, INT32_MAX]"), | ||
| 307 | + return ge::GRAPH_FAILED); | ||
| 308 | + inputParams.set_kvStartIdx(kvStartIdx); | ||
| 309 | + } | ||
| 310 | + } | ||
| 311 | + } | ||
| 312 | + // 当kvStartIdx - qStartIdx超出范围后,由于编译器不支持大数值类型转换,kernel侧int_64转float类型时可能发生截断。 | ||
| 313 | + OP_CHECK_IF( | ||
| 314 | + kvStartIdx - qStartIdx > INT32_MAX || kvStartIdx - qStartIdx < INT32_MIN, | ||
| 315 | + OP_LOGE_FOR_INVALID_VALUES_WITH_REASON(contextKeyParams.opName, "q_start_idx, kv_start_idx", | ||
| 316 | + (std::to_string(qStartIdx) + ", " + std::to_string(kvStartIdx)).c_str(), | ||
| 317 | + "kvStartIdx - qStartIdx must be in range [INT32_MIN, INT32_MAX]"), | ||
| 318 | + return ge::GRAPH_FAILED); | ||
| 319 | + return ge::GRAPH_SUCCESS; | ||
| 320 | +} | ||
| 321 | + | ||
| 322 | +void FiaArch38PromptTiling::PFATilingDataconvert(FiaArch38FlashAttentionTilingData& tilingData) | ||
| 323 | +{ | ||
| 324 | + SetLayoutType(); | ||
| 325 | + SetInputParamsShape(tilingData); | ||
| 326 | + SetInputParamsPseAndMask(tilingData); | ||
| 327 | + SetInputParamsPA(tilingData); | ||
| 328 | + SetInitOutputParams(tilingData); | ||
| 329 | +} | ||
| 330 | + | ||
| 331 | +void FiaArch38PromptTiling::SetInputParamsShape(FiaArch38FlashAttentionTilingData& tilingData) | ||
| 332 | +{ | ||
| 333 | + auto& inputParams = faTilingAdapter.inputParamsRegbase; | ||
| 334 | + inputParams.set_bSize(tilingData.fiaArch38AttentionBaseParams.get_batchSize()); | ||
| 335 | + // 将GS1合轴与不合轴场景下,有不同含义的n2Size、gSize与s1Size参数,转化为各自实际的值 | ||
| 336 | + if (enableIFAMLA || enableIFA || enablePFAMerge) { | ||
| 337 | + inputParams.set_n2Size(tilingData.fiaArch38AttentionBaseParams.get_headNumSize()); | ||
| 338 | + inputParams.set_gSize(tilingData.fiaArch38AttentionBaseParams.get_gOfMla()); | ||
| 339 | + inputParams.set_s1Size(tilingData.fiaArch38AttentionBaseParams.get_seqSize() / | ||
| 340 | + tilingData.fiaArch38AttentionBaseParams.get_gOfMla()); | ||
| 341 | + } else { | ||
| 342 | + inputParams.set_n2Size(tilingData.fiaArch38AttentionBaseParams.get_headNumSize() / | ||
| 343 | + tilingData.fiaArch38AttentionBaseParams.get_headNumRatio()); | ||
| 344 | + inputParams.set_gSize(tilingData.fiaArch38AttentionBaseParams.get_headNumRatio()); | ||
| 345 | + inputParams.set_s1Size(tilingData.fiaArch38AttentionBaseParams.get_seqSize()); | ||
| 346 | + } | ||
| 347 | + inputParams.set_s2Size(tilingData.fiaArch38AttentionBaseParams.get_seqInnerSize()); | ||
| 348 | + inputParams.set_alignedS2(0); // 默认值 | ||
| 349 | + inputParams.set_dSize(tilingData.fiaArch38AttentionBaseParams.get_headSize()); | ||
| 350 | + inputParams.set_dSizeV(tilingData.fiaArch38AttentionBaseParams.get_vHeadSize()); | ||
| 351 | + inputParams.set_dSizeRope(ROPE_DIMENSION_SIZE_TILING_V2); | ||
| 352 | + inputParams.set_scaleValue(tilingData.fiaArch38AttentionBaseParams.get_scaleValue()); | ||
| 353 | + inputParams.set_preTokens(tilingData.fiaArch38AttentionBaseParams.get_preTokens()); | ||
| 354 | + inputParams.set_nextTokens(tilingData.fiaArch38AttentionBaseParams.get_nextTokens()); | ||
| 355 | + inputParams.set_pseS1Size(tilingData.fiaArch38AttentionBaseParams.get_pseShiftS1Size()); | ||
| 356 | + inputParams.set_pseS2Size(tilingData.fiaArch38AttentionBaseParams.get_pseShiftS2Size()); | ||
| 357 | +} | ||
| 358 | + | ||
| 359 | +void FiaArch38PromptTiling::SetInputParamsPseAndMask(FiaArch38FlashAttentionTilingData& tilingData) | ||
| 360 | +{ | ||
| 361 | + auto& inputParams = faTilingAdapter.inputParamsRegbase; | ||
| 362 | + if (!enableAlibiPse) { | ||
| 363 | + inputParams.set_pseBSize(tilingData.fiaArch38AttentionSingleCoreParams.get_pseShiftBatch()); | ||
| 364 | + inputParams.set_pseShapeType(0); // 对应训练 PSE_B_N2_G_S1_S2 | ||
| 365 | + inputParams.set_pseType(0); // 对应训练 PSE_OUTER_MUL_ADD_TYPE | ||
| 366 | + } | ||
| 367 | + inputParams.set_bandIndex(0); // 训练代码中在TND场景生效,用于计算s2方向循环的起始位置 | ||
| 368 | + inputParams.set_pseEncodeType(0); // 默认值 | ||
| 369 | + inputParams.set_pseAlibiBaseS1(0); // 默认值 | ||
| 370 | + inputParams.set_pseAlibiBaseS2(0); // 默认值 | ||
| 371 | + inputParams.set_attenMaskShapeType(attenMaskShapeType); | ||
| 372 | + inputParams.set_attenMaskDataType(1); // 默认值 | ||
| 373 | + SetAttenMaskCompressMode(); | ||
| 374 | + inputParams.set_implMode(static_cast<uint8_t>(HIGH_PRECISION)); | ||
| 375 | + inputParams.set_sparseType(sparseType); | ||
| 376 | + inputParams.set_needDropMaskOp(0); // 默认值 | ||
| 377 | + inputParams.set_keepProb(0); // 默认值 | ||
| 378 | + inputParams.set_keepProbUint8(0); // 默认值 | ||
| 379 | + inputParams.set_dropMaskOuter(0); // 默认值 | ||
| 380 | + inputParams.set_remain(0); // 默认值 | ||
| 381 | + inputParams.set_attenMaskS2Size(tilingData.fiaArch38AttentionBaseParams.get_maskKVsSize()); | ||
| 382 | + inputParams.set_rsv1(0); // 默认值 | ||
| 383 | + inputParams.set_s1SparseValidSize(0); // 临时默认值 | ||
| 384 | + inputParams.set_s2SparseValidSize(0); // 临时默认值 | ||
| 385 | + inputParams.set_seed(0); // 默认值 | ||
| 386 | + inputParams.set_offset(0); // 默认值 | ||
| 387 | +} | ||
| 388 | + | ||
| 389 | +void FiaArch38PromptTiling::SetInputParamsPA(FiaArch38FlashAttentionTilingData& tilingData) | ||
| 390 | +{ | ||
| 391 | + auto& inputParams = faTilingAdapter.inputParamsRegbase; | ||
| 392 | + // PFA | ||
| 393 | + inputParams.set_blockSize(tilingData.fiaArch38AttentionBaseParams.get_blockSize()); | ||
| 394 | + inputParams.set_blockTableDim2(tilingData.fiaArch38AttentionBaseParams.get_blockTableDim2()); | ||
| 395 | + inputParams.set_paBlockNumSum(tilingData.fiaArch38AttentionBaseParams.get_PABlockNumSum()); | ||
| 396 | + inputParams.set_prefixSeqInnerSize(tilingData.fiaArch38AttentionBaseParams.get_prefixSeqInnerSize()); | ||
| 397 | + inputParams.set_paLayoutType(tilingData.fiaArch38AttentionBaseParams.get_PAlayoutType()); | ||
| 398 | + inputParams.set_attenMaskS1Size(tilingData.fiaArch38AttentionBaseParams.get_maskQsSize()); | ||
| 399 | + inputParams.set_isActualSeqLengthsNull(tilingData.fiaArch38AttentionBaseParams.get_isActualSeqLengthsNull()); | ||
| 400 | + inputParams.set_isActualSeqLengthsKVNull(tilingData.fiaArch38AttentionBaseParams.get_isActualSeqLengthsKVNull()); | ||
| 401 | + inputParams.set_actualSeqLengthsSize(tilingData.fiaArch38AttentionBaseParams.get_actualSeqLengthsSize()); | ||
| 402 | + inputParams.set_actualSeqLengthsKVSize(tilingData.fiaArch38AttentionBaseParams.get_actualSeqLengthsKVSize()); | ||
| 403 | + inputParams.set_deqScaleFlag(tilingData.fiaArch38AttentionBaseParams.get_deqScaleFlag()); | ||
| 404 | + inputParams.set_deqScale2Flag(tilingData.fiaArch38AttentionBaseParams.get_deqScale2Flag()); | ||
| 405 | + inputParams.set_isKvContinuous(tilingData.fiaArch38AttentionBaseParams.get_isKvContinuous()); | ||
| 406 | + inputParams.set_fromFused(tilingData.fiaArch38AttentionBaseParams.get_fromFused()); | ||
| 407 | + inputParams.set_isBSNDOut(tilingData.fiaArch38AttentionBaseParams.get_isBSNDOut()); | ||
| 408 | + inputParams.set_isGqa((tilingData.fiaArch38AttentionBaseParams.get_isIFA() != 0) || enablePFAMerge); | ||
| 409 | + inputParams.set_isSoftMaxLseEnable(tilingData.fiaArch38AttentionBaseParams.get_isSoftMaxLseEnable()); | ||
| 410 | + inputParams.set_isActualSharedPrefixLenNull( | ||
| 411 | + tilingData.fiaArch38AttentionBaseParams.get_isActualSharedPrefixLenNull()); | ||
| 412 | + inputParams.set_isQHasLeftPadding(tilingData.fiaArch38AttentionBaseParams.get_isQHasLeftPadding()); | ||
| 413 | + inputParams.set_isKVHasLeftPadding(tilingData.fiaArch38AttentionBaseParams.get_isKVHasLeftPadding()); | ||
| 414 | + inputParams.set_ropeHeadSize(tilingData.fiaArch38AttentionBaseParams.get_ropeHeadSize()); | ||
| 415 | + inputParams.set_isRowInvalid(static_cast<uint8_t>(tilingData.fiaArch38AttentionBaseParams.get_isRowInvalid())); | ||
| 416 | + inputParams.set_headNumRatio(tilingData.fiaArch38AttentionBaseParams.get_headNumRatio()); | ||
| 417 | +} | ||
| 418 | + | ||
| 419 | +void FiaArch38PromptTiling::SetInitOutputParams(FiaArch38FlashAttentionTilingData& tilingData) | ||
| 420 | +{ | ||
| 421 | + auto& initOutputParams = faTilingAdapter.initOutputParams; | ||
| 422 | + initOutputParams.set_singleCoreSize(tilingData.fiaArch38AttentionInitOutputParams.get_singleCoreSize()); | ||
| 423 | + initOutputParams.set_totalOutputSize(tilingData.fiaArch38AttentionInitOutputParams.get_totalOutputSize()); | ||
| 424 | + initOutputParams.set_totalSoftMaxLseOutputSize( | ||
| 425 | + tilingData.fiaArch38AttentionInitOutputParams.get_totalSoftMaxLseOutputSize()); | ||
| 426 | + initOutputParams.set_needInit(static_cast<uint8_t>(tilingData.fiaArch38AttentionInitOutputParams.get_needInit())); | ||
| 427 | + initOutputParams.set_isOneN(static_cast<uint8_t>(tilingData.fiaArch38AttentionInitOutputParams.get_isOneN())); | ||
| 428 | + | ||
| 429 | + auto& inputParams = faTilingAdapter.inputParamsRegbase; | ||
| 430 | + inputParams.set_isPostQuantPerChnl(tilingData.fiaArch38AttentionBaseParams.get_isQuant2Perchannel()); | ||
| 431 | + inputParams.set_isPostQuantBF16(tilingData.fiaArch38AttentionBaseParams.get_isQuant2BF16()); | ||
| 432 | + inputParams.set_antiquantPerTensorFlag(0); | ||
| 433 | + inputParams.set_antiquantPerHeadFlag(0); | ||
| 434 | + inputParams.set_antiquantParaSeqSize(1); | ||
| 435 | +} | ||
| 436 | + | ||
| 437 | +} // namespace arch38 | ||
| 438 | +} // namespace optiling | ||
| @@ -0,0 +1,483 @@ | |||
| 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 | +#include "fia_arch38_prompt_tiling.h" | ||
| 12 | + | ||
| 13 | +using namespace ge; | ||
| 14 | +using namespace AscendC; | ||
| 15 | +using namespace matmul_tiling; | ||
| 16 | +namespace optiling { | ||
| 17 | + | ||
| 18 | +namespace arch38 { | ||
| 19 | + | ||
| 20 | +bool FiaArch38PromptTiling::CheckMaskShape(FiaArch38ContextParams& contextKeyParams, MaskShapeInfo& info, | ||
| 21 | + const uint32_t sQ, const uint32_t sK, const uint32_t batchSize) const | ||
| 22 | +{ | ||
| 23 | + const int64_t* sparseMode = contextKeyParams.sparseMode; | ||
| 24 | + const gert::StorageShape* attenMaskShape = contextKeyParams.attentionMaskShape; | ||
| 25 | + size_t attenMaskDim = attenMaskShape->GetStorageShape().GetDimNum(); | ||
| 26 | + int64_t attenMaskN = 1U; | ||
| 27 | + if (attenMaskDim == MASKDIM_2) { | ||
| 28 | + if (enableIFA) { | ||
| 29 | + info.attenMaskBatch = attenMaskShape->GetStorageShape().GetDim(0); | ||
| 30 | + info.attenMaskS1 = 1; | ||
| 31 | + info.attenMaskS2 = attenMaskShape->GetStorageShape().GetDim(1); | ||
| 32 | + info.strMaskShape = std::to_string(info.attenMaskBatch) + ", " + std::to_string(info.attenMaskS2); | ||
| 33 | + } else { | ||
| 34 | + info.attenMaskS1 = attenMaskShape->GetStorageShape().GetDim(0); | ||
| 35 | + info.attenMaskS2 = attenMaskShape->GetStorageShape().GetDim(1); | ||
| 36 | + info.strMaskShape = std::to_string(info.attenMaskS1) + ", " + std::to_string(info.attenMaskS2); | ||
| 37 | + } | ||
| 38 | + } else if (attenMaskDim == MASKDIM_3) { | ||
| 39 | + info.attenMaskBatch = attenMaskShape->GetStorageShape().GetDim(0); | ||
| 40 | + info.attenMaskS1 = attenMaskShape->GetStorageShape().GetDim(1); | ||
| 41 | + info.attenMaskS2 = | ||
| 42 | + attenMaskShape->GetStorageShape().GetDim(2); // 2: When the dim is 3, the second dimension is S2. | ||
| 43 | + info.strMaskShape = std::to_string(info.attenMaskBatch) + ", " + std::to_string(info.attenMaskS1) + ", " + | ||
| 44 | + std::to_string(info.attenMaskS2); | ||
| 45 | + } else if (attenMaskDim == MASKDIM_4) { | ||
| 46 | + info.attenMaskBatch = attenMaskShape->GetStorageShape().GetDim(0); | ||
| 47 | + attenMaskN = attenMaskShape->GetStorageShape().GetDim(1); | ||
| 48 | + info.attenMaskS1 = | ||
| 49 | + attenMaskShape->GetStorageShape().GetDim(2); // 2: When the dim is 4, the second dimension is S1. | ||
| 50 | + info.attenMaskS2 = | ||
| 51 | + attenMaskShape->GetStorageShape().GetDim(3); // 3: When the dim is 4, the third dimension is S2. | ||
| 52 | + info.strMaskShape = std::to_string(info.attenMaskBatch) + ", " + std::to_string(attenMaskN) + ", " + | ||
| 53 | + std::to_string(info.attenMaskS1) + ", " + std::to_string(info.attenMaskS2); | ||
| 54 | + } else { | ||
| 55 | + OP_LOGE_FOR_INVALID_SHAPEDIM_WITH_REASON(contextKeyParams.opName, "atten_mask", | ||
| 56 | + std::to_string(attenMaskDim).c_str(), | ||
| 57 | + "attenMask dim must be 2, 3 or 4"); | ||
| 58 | + return false; | ||
| 59 | + } | ||
| 60 | + | ||
| 61 | + if (enableIFA) { | ||
| 62 | + info.checkMask = (info.attenMaskBatch == batchSize) && (info.attenMaskS1 == 1) && (info.attenMaskS2 >= S2); | ||
| 63 | + } else if (isDefaultSparseMode || (sparseMode != nullptr && *sparseMode == SPARSE_MODE_ALL_MASK)) { | ||
| 64 | + info.checkMask = (info.attenMaskS1 >= sQ) && (info.attenMaskS2 >= sK) && | ||
| 65 | + (info.attenMaskBatch == 1 || info.attenMaskBatch == batchSize); | ||
| 66 | + } else if ((sparseMode != nullptr) && | ||
| 67 | + ((*sparseMode == SPARSE_MODE_LEFT_UP) || (*sparseMode == SPARSE_MODE_RIGHT_DOWN) || | ||
| 68 | + (*sparseMode == SPARSE_MODE_BAND))) { | ||
| 69 | + info.checkMask = (info.attenMaskBatch == 1) && (attenMaskN == 1) && | ||
| 70 | + (info.attenMaskS1 == SPARSE_OPTIMIZE_ATTENTION_SIZE) && | ||
| 71 | + (info.attenMaskS2 == SPARSE_OPTIMIZE_ATTENTION_SIZE); | ||
| 72 | + } | ||
| 73 | + return true; | ||
| 74 | +} | ||
| 75 | + | ||
| 76 | +void FiaArch38PromptTiling::SetSparseModeData(FiaArch38ContextParams& contextKeyParams, | ||
| 77 | + FiaArch38FlashAttentionTilingData& tilingData) | ||
| 78 | +{ | ||
| 79 | + const gert::StorageShape* attenMaskShape = contextKeyParams.attentionMaskShape; | ||
| 80 | + const int64_t* sparseMode = contextKeyParams.sparseMode; | ||
| 81 | + const int64_t* preTokens = contextKeyParams.preToken; | ||
| 82 | + const int64_t* nextTokens = contextKeyParams.nextToken; | ||
| 83 | + SetAttenMaskBatch(attenMaskShape, tilingData); | ||
| 84 | + ClampSparseTokens(preTokens, nextTokens); | ||
| 85 | + if ((attenMaskShape != nullptr) && (sparseMode != nullptr)) { | ||
| 86 | + if (*sparseMode == SPARSE_MODE_LEFT_UP) { | ||
| 87 | + sparsePreTokens = SPARSE_MODE_INT_MAX; | ||
| 88 | + sparseNextTokens = 0; | ||
| 89 | + // Right down tokens are calculated on the kernel side. | ||
| 90 | + } else if (*sparseMode == SPARSE_MODE_RIGHT_DOWN) { | ||
| 91 | + sparsePreTokens = SPARSE_MODE_INT_MAX; | ||
| 92 | + } else if (*sparseMode == SPARSE_MODE_ALL_MASK) { | ||
| 93 | + sparsePreTokens = SPARSE_MODE_INT_MAX; | ||
| 94 | + sparseNextTokens = SPARSE_MODE_INT_MAX; | ||
| 95 | + } else if (*sparseMode == SPARSE_MODE_BAND) { | ||
| 96 | + isBandMode = true; | ||
| 97 | + } | ||
| 98 | + if (enableIFA && !enableIFAMLA) { | ||
| 99 | + sparseModeVal = SPARSE_MODE_NO_MASK; | ||
| 100 | + sparsePreTokens = SPARSE_MODE_INT_MAX; | ||
| 101 | + sparseNextTokens = SPARSE_MODE_INT_MAX; | ||
| 102 | + } | ||
| 103 | + sparseModeVal = static_cast<int32_t>(*sparseMode); | ||
| 104 | + OP_LOGI(contextKeyParams.opName, "sparseMode is %d.", sparseModeVal); | ||
| 105 | + } | ||
| 106 | + if ((sparseMode != nullptr) && (*sparseMode == SPARSE_MODE_NO_MASK)) { | ||
| 107 | + // sparse mode need process attention mask when empty tensor scenes as same. | ||
| 108 | + if (((attenMaskShape != nullptr) && (attenMaskShape->GetStorageShape().GetShapeSize() == 0)) || | ||
| 109 | + (attenMaskShape == nullptr)) { | ||
| 110 | + sparsePreTokens = SPARSE_MODE_INT_MAX; | ||
| 111 | + sparseNextTokens = SPARSE_MODE_INT_MAX; | ||
| 112 | + sparseModeVal = static_cast<int32_t>(*sparseMode); | ||
| 113 | + } | ||
| 114 | + } | ||
| 115 | + if (isDefaultSparseMode && enableLeftPadding) { | ||
| 116 | + // For scenes with sparse mode=0 and left padding, the attention mask part is fully calculated | ||
| 117 | + sparsePreTokens = SPARSE_MODE_INT_MAX; | ||
| 118 | + sparseNextTokens = SPARSE_MODE_INT_MAX; | ||
| 119 | + } | ||
| 120 | +} | ||
| 121 | + | ||
| 122 | +void FiaArch38PromptTiling::SetAttenMaskBatch(const gert::StorageShape* attenMaskShape, | ||
| 123 | + FiaArch38FlashAttentionTilingData& tilingData) | ||
| 124 | +{ | ||
| 125 | + size_t attenMaskBatch = 1; | ||
| 126 | + if ((attenMaskShape != nullptr) && (attenMaskShape->GetStorageShape().GetDimNum() != MASKDIM_2)) { | ||
| 127 | + // 2: target dimension of attenMask | ||
| 128 | + attenMaskBatch = attenMaskShape->GetStorageShape().GetDim(0); | ||
| 129 | + } | ||
| 130 | + if ((attenMaskShape != nullptr) && enableIFA && (attenMaskShape->GetStorageShape().GetDimNum() == MASKDIM_2)) { | ||
| 131 | + attenMaskBatch = attenMaskShape->GetStorageShape().GetDim(0); // IFA mask shape (B,S2) | ||
| 132 | + } | ||
| 133 | + tilingData.fiaArch38AttentionSingleCoreParams.set_attenMaskBatch(attenMaskBatch); | ||
| 134 | +} | ||
| 135 | + | ||
| 136 | +void FiaArch38PromptTiling::ClampSparseTokens(const int64_t* preTokens, const int64_t* nextTokens) | ||
| 137 | +{ | ||
| 138 | + if (*preTokens > SPARSE_MODE_INT_MAX) { | ||
| 139 | + sparsePreTokens = SPARSE_MODE_INT_MAX; | ||
| 140 | + } else if (*preTokens < -(SPARSE_MODE_INT_MAX)) { | ||
| 141 | + sparsePreTokens = -(SPARSE_MODE_INT_MAX); | ||
| 142 | + } else { | ||
| 143 | + sparsePreTokens = *preTokens; | ||
| 144 | + } | ||
| 145 | + | ||
| 146 | + if (*nextTokens > SPARSE_MODE_INT_MAX) { | ||
| 147 | + sparseNextTokens = SPARSE_MODE_INT_MAX; | ||
| 148 | + } else if (*nextTokens < -(SPARSE_MODE_INT_MAX)) { | ||
| 149 | + sparseNextTokens = -(SPARSE_MODE_INT_MAX); | ||
| 150 | + } else { | ||
| 151 | + sparseNextTokens = *nextTokens; | ||
| 152 | + } | ||
| 153 | +} | ||
| 154 | + | ||
| 155 | +bool FiaArch38PromptTiling::CheckMaskShapeCrossSparse(FiaArch38ContextParams& contextKeyParams, | ||
| 156 | + const int64_t* sparseMode, uint64_t sQ, const uint64_t sK, | ||
| 157 | + const uint32_t batchSize) | ||
| 158 | +{ | ||
| 159 | + if ((contextKeyParams.fromTilingSink != 0) || (!enableMask)) { | ||
| 160 | + return true; | ||
| 161 | + } | ||
| 162 | + if (enableIFA || enableIFAMLA || enablePFAMerge) { | ||
| 163 | + sQ /= gSize; // 合轴场景使用原始的seq长度校验 | ||
| 164 | + } | ||
| 165 | + MaskShapeInfo info; | ||
| 166 | + if (!CheckMaskShape(contextKeyParams, info, sQ, sK, batchSize)) { | ||
| 167 | + return false; | ||
| 168 | + } | ||
| 169 | + if (enableIFA) { | ||
| 170 | + OP_CHECK_IF(!info.checkMask, | ||
| 171 | + OP_LOGE_FOR_INVALID_SHAPESIZES_WITH_REASON( | ||
| 172 | + contextKeyParams.opName, "atten_mask", | ||
| 173 | + (std::to_string(info.attenMaskBatch) + ", " + std::to_string(info.attenMaskS1) + ", " + | ||
| 174 | + std::to_string(info.attenMaskS2)) | ||
| 175 | + .c_str(), | ||
| 176 | + "attenMask batch, Q_S and KV_S are invalid for the given sparse mode"), | ||
| 177 | + return false); | ||
| 178 | + } else { | ||
| 179 | + if (isDefaultSparseMode || ((sparseMode != nullptr) && (*sparseMode == SPARSE_MODE_ALL_MASK))) { | ||
| 180 | + OP_CHECK_IF(!info.checkMask, | ||
| 181 | + OP_LOGE_FOR_INVALID_SHAPESIZES_WITH_REASON( | ||
| 182 | + contextKeyParams.opName, "atten_mask", | ||
| 183 | + (std::to_string(info.attenMaskBatch) + ", " + std::to_string(info.attenMaskS1) + ", " + | ||
| 184 | + std::to_string(info.attenMaskS2)) | ||
| 185 | + .c_str(), | ||
| 186 | + "attenMask batch, Q_S and KV_S are invalid for the given sparse mode"), | ||
| 187 | + return false); | ||
| 188 | + } | ||
| 189 | + if ((sparseMode != nullptr) && | ||
| 190 | + ((*sparseMode == SPARSE_MODE_LEFT_UP) || (*sparseMode == SPARSE_MODE_RIGHT_DOWN) || | ||
| 191 | + (*sparseMode == SPARSE_MODE_BAND)) && | ||
| 192 | + !info.checkMask) { | ||
| 193 | + OP_LOGE_FOR_INVALID_SHAPE_WITH_REASON( | ||
| 194 | + contextKeyParams.opName, "atten_mask", info.strMaskShape.c_str(), | ||
| 195 | + "attenMask shape must be (2048, 2048), (1, 2048, 2048) or (1, 1, 2048, 2048) for this sparse mode"); | ||
| 196 | + return false; | ||
| 197 | + } | ||
| 198 | + } | ||
| 199 | + attenMaskShapeType = info.attenMaskBatch > 1 ? 1 : 2; // 1 for multi-batch and 2 for 1 batch, same as fa | ||
| 200 | + return true; | ||
| 201 | +} | ||
| 202 | + | ||
| 203 | +bool FiaArch38PromptTiling::CheckMaskTypeAndShape(FiaArch38ContextParams& contextKeyParams, | ||
| 204 | + FiaArch38FlashAttentionTilingData& tilingData) | ||
| 205 | +{ | ||
| 206 | + const gert::StorageShape* attenMaskShape = contextKeyParams.attentionMaskShape; | ||
| 207 | + OP_CHECK_IF((attenMaskShape->GetStorageShape().GetShapeSize() == gert::Shape::kInvalidDimValue), | ||
| 208 | + ReportTilingFailure(context_, "get the shape size of atten_mask failed"), return false); | ||
| 209 | + | ||
| 210 | + if (!CheckMaskDtype(contextKeyParams)) { | ||
| 211 | + return false; | ||
| 212 | + } | ||
| 213 | + | ||
| 214 | + const size_t attenMaskDimNum = attenMaskShape->GetStorageShape().GetDimNum(); | ||
| 215 | + OP_CHECK_IF((attenMaskDimNum < 2), | ||
| 216 | + OP_LOGE_FOR_INVALID_SHAPEDIM_WITH_REASON(contextKeyParams.opName, "atten_mask", | ||
| 217 | + std::to_string(attenMaskDimNum).c_str(), | ||
| 218 | + "atten_mask dim must be at least 2"), | ||
| 219 | + return false); | ||
| 220 | + | ||
| 221 | + // 1: last frist dim, 2: last second dim | ||
| 222 | + int64_t maskKVsSize = attenMaskShape->GetStorageShape().GetDim(attenMaskDimNum - 1); | ||
| 223 | + int64_t maskQsSize = attenMaskShape->GetStorageShape().GetDim(attenMaskDimNum - 2); // 2 for Q dim index | ||
| 224 | + | ||
| 225 | + if (enableIFA) { | ||
| 226 | + maskQsSize = 1; | ||
| 227 | + } | ||
| 228 | + | ||
| 229 | + tilingData.fiaArch38AttentionBaseParams.set_maskKVsSize(maskKVsSize); | ||
| 230 | + tilingData.fiaArch38AttentionBaseParams.set_maskQsSize(maskQsSize); | ||
| 231 | + return true; | ||
| 232 | +} | ||
| 233 | + | ||
| 234 | +bool FiaArch38PromptTiling::CheckMaskDtype(const FiaArch38ContextParams& contextKeyParams) | ||
| 235 | +{ | ||
| 236 | + auto maskDataType = contextKeyParams.maskDataType; | ||
| 237 | + std::vector<ge::DataType> allowedMaskDtypes = {ge::DT_FLOAT16, ge::DT_BOOL, ge::DT_INT8, ge::DT_UINT8}; | ||
| 238 | + std::vector<uint32_t> maskTypeSizeArray = {FLOAT16_SIZE, BOOL_SIZE, INT8_SIZE, UINT8_SIZE}; | ||
| 239 | + | ||
| 240 | + auto maskTypeCheck = std::find(allowedMaskDtypes.begin(), allowedMaskDtypes.end(), maskDataType); | ||
| 241 | + | ||
| 242 | + OP_CHECK_IF((maskTypeCheck == allowedMaskDtypes.end()), | ||
| 243 | + OP_LOGE_FOR_INVALID_DTYPE_WITH_REASON(contextKeyParams.opName, "atten_mask", | ||
| 244 | + GetPfaDataTypeStr(maskDataType).c_str(), | ||
| 245 | + "maskType should be DT_FLOAT16, DT_BOOL, DT_INT8 or DT_UINT8"), | ||
| 246 | + return false); | ||
| 247 | + int32_t maskTypeIndex = std::distance(allowedMaskDtypes.begin(), maskTypeCheck); | ||
| 248 | + maskElemSize = maskTypeSizeArray[maskTypeIndex]; | ||
| 249 | + | ||
| 250 | + // When in fp16 high-precision mode, the mask type only supports bool or int8. | ||
| 251 | + OP_CHECK_IF(((inputType == ge::DT_FLOAT16) && (innerPrecise == HIGH_PRECISION)) && (maskDataType != ge::DT_BOOL) && | ||
| 252 | + (maskDataType != ge::DT_INT8) && (maskDataType != ge::DT_UINT8), | ||
| 253 | + OP_LOGE_FOR_INVALID_DTYPE_WITH_REASON( | ||
| 254 | + contextKeyParams.opName, "atten_mask", GetPfaDataTypeStr(maskDataType).c_str(), | ||
| 255 | + "when innerprecise is 2/3 and input type is fp16, maskType should be bool, int8 or uint8"), | ||
| 256 | + return false); | ||
| 257 | + // When bf16, the mask type only supports bool or int8. | ||
| 258 | + OP_CHECK_IF((inputType == ge::DT_BF16) && (maskDataType != ge::DT_BOOL) && (maskDataType != ge::DT_INT8) && | ||
| 259 | + (maskDataType != ge::DT_UINT8), | ||
| 260 | + OP_LOGE_FOR_INVALID_DTYPE_WITH_REASON( | ||
| 261 | + contextKeyParams.opName, "atten_mask", GetPfaDataTypeStr(maskDataType).c_str(), | ||
| 262 | + "when input type is bfloat16, maskType should be bool, int8 or uint8"), | ||
| 263 | + return false); | ||
| 264 | + return true; | ||
| 265 | +} | ||
| 266 | + | ||
| 267 | +void FiaArch38PromptTiling::SetSparseType(uint32_t qS) | ||
| 268 | +{ | ||
| 269 | + if (sparseModeVal == SPARSE_MODE_NO_MASK) { | ||
| 270 | + if (sparsePreTokens >= qS && sparseNextTokens == 0) { | ||
| 271 | + sparseType = static_cast<uint8_t>(PfaSparseEnum::PFA_CAUSAL); | ||
| 272 | + } else if (sparsePreTokens >= qS && !enablePA && sparseNextTokens >= S2) { | ||
| 273 | + sparseType = static_cast<uint8_t>(PfaSparseEnum::PFA_ALL); | ||
| 274 | + } else { | ||
| 275 | + sparseType = static_cast<uint8_t>(PfaSparseEnum::PFA_BAND); | ||
| 276 | + } | ||
| 277 | + } else if (sparseModeVal == SPARSE_MODE_ALL_MASK) { | ||
| 278 | + sparseType = static_cast<uint8_t>(PfaSparseEnum::PFA_ALL); | ||
| 279 | + } else if (sparseModeVal == SPARSE_MODE_LEFT_UP) { | ||
| 280 | + sparseType = static_cast<uint8_t>(PfaSparseEnum::PFA_CAUSAL); | ||
| 281 | + } else if (sparseModeVal == SPARSE_MODE_RIGHT_DOWN) { | ||
| 282 | + if (!enablePA && qS == S2) { | ||
| 283 | + sparseType = static_cast<uint8_t>(PfaSparseEnum::PFA_CAUSAL); | ||
| 284 | + } else { | ||
| 285 | + sparseType = static_cast<uint8_t>(PfaSparseEnum::PFA_BAND); | ||
| 286 | + } | ||
| 287 | + } else if (sparseModeVal == SPARSE_MODE_BAND) { | ||
| 288 | + sparseType = static_cast<uint8_t>(PfaSparseEnum::PFA_BAND); | ||
| 289 | + } | ||
| 290 | +} | ||
| 291 | + | ||
| 292 | +bool FiaArch38PromptTiling::CheckSparseMode(FiaArch38ContextParams& contextKeyParams, uint32_t qS, | ||
| 293 | + FiaArch38FlashAttentionTilingData& tilingData) | ||
| 294 | +{ | ||
| 295 | + const int64_t* sparseMode = contextKeyParams.sparseMode; | ||
| 296 | + | ||
| 297 | + bool sparseCheck = false; | ||
| 298 | + if (sparseMode != nullptr) { | ||
| 299 | + sparseCheck = ((*sparseMode != SPARSE_MODE_NO_MASK) && (*sparseMode != SPARSE_MODE_LEFT_UP) && | ||
| 300 | + (*sparseMode != SPARSE_MODE_RIGHT_DOWN) && (*sparseMode != SPARSE_MODE_ALL_MASK) && | ||
| 301 | + (*sparseMode != SPARSE_MODE_BAND)); | ||
| 302 | + OP_CHECK_IF(sparseCheck, | ||
| 303 | + OP_LOGE_FOR_INVALID_VALUE_WITH_REASON( | ||
| 304 | + contextKeyParams.opName, "sparse_mode", std::to_string(*sparseMode).c_str(), | ||
| 305 | + "sparse_mode is out of range, only 0, 1, 2, 3, 4 are supported"), | ||
| 306 | + return false); | ||
| 307 | + } | ||
| 308 | + SetSparseModeData(contextKeyParams, tilingData); | ||
| 309 | + | ||
| 310 | + if (!CheckSparseTokensRange(contextKeyParams, qS)) { | ||
| 311 | + return false; | ||
| 312 | + } | ||
| 313 | + | ||
| 314 | + SetSparseType(qS); | ||
| 315 | + return true; | ||
| 316 | +} | ||
| 317 | + | ||
| 318 | +bool FiaArch38PromptTiling::CheckSparseTokensRange(const FiaArch38ContextParams& contextKeyParams, uint32_t qS) const | ||
| 319 | +{ | ||
| 320 | + const int64_t* sparseMode = contextKeyParams.sparseMode; | ||
| 321 | + const int64_t* nextTokens = contextKeyParams.nextToken; | ||
| 322 | + const int64_t* preTokens = contextKeyParams.preToken; | ||
| 323 | + const gert::StorageShape* attenMaskShape = contextKeyParams.attentionMaskShape; | ||
| 324 | + | ||
| 325 | + OP_CHECK_IF(((attenMaskShape != nullptr) && (sparseMode != nullptr) && (*sparseMode == SPARSE_MODE_BAND) && | ||
| 326 | + (*preTokens < 0 && *nextTokens < 0)), | ||
| 327 | + OP_LOGE_FOR_INVALID_VALUES_WITH_REASON( | ||
| 328 | + contextKeyParams.opName, "pre_tokens, next_tokens", | ||
| 329 | + (std::to_string(*preTokens) + ", " + std::to_string(*nextTokens)).c_str(), | ||
| 330 | + "preTokens and nextTokens must not be negative in band mode"), | ||
| 331 | + return false); | ||
| 332 | + | ||
| 333 | + OP_CHECK_IF((sparsePreTokens < 0) && (sparseNextTokens < 0), | ||
| 334 | + OP_LOGE_FOR_INVALID_VALUES_WITH_REASON( | ||
| 335 | + contextKeyParams.opName, "pre_tokens, next_tokens", | ||
| 336 | + (std::to_string(sparsePreTokens) + ", " + std::to_string(sparseNextTokens)).c_str(), | ||
| 337 | + "preTokens and nextTokens must not be negative"), | ||
| 338 | + return false); | ||
| 339 | + | ||
| 340 | + OP_CHECK_IF((sparseNextTokens * (-1)) > sparsePreTokens, | ||
| 341 | + OP_LOGE_FOR_INVALID_VALUES_WITH_REASON( | ||
| 342 | + contextKeyParams.opName, "pre_tokens, next_tokens", | ||
| 343 | + (std::to_string(sparsePreTokens) + ", " + std::to_string(sparseNextTokens)).c_str(), | ||
| 344 | + "nexttoken line should be higher than pretoken line"), | ||
| 345 | + return false); | ||
| 346 | + | ||
| 347 | + OP_CHECK_IF(isDefaultSparseMode && (sparseNextTokens < 0) && (sparseNextTokens * (-1)) >= static_cast<int32_t>(qS), | ||
| 348 | + OP_LOGE_FOR_INVALID_VALUE_WITH_REASON( | ||
| 349 | + contextKeyParams.opName, "next_tokens", std::to_string(sparseNextTokens).c_str(), | ||
| 350 | + "nextTokens absolute value should be smaller than the length of query"), | ||
| 351 | + return false); | ||
| 352 | + | ||
| 353 | + OP_CHECK_IF( | ||
| 354 | + isDefaultSparseMode && (sparsePreTokens < 0) && | ||
| 355 | + (sparsePreTokens * (-1) >= (static_cast<int32_t>(S2) + static_cast<int32_t>(actualSharedPrefixLen))), | ||
| 356 | + OP_LOGE_FOR_INVALID_VALUE_WITH_REASON( | ||
| 357 | + contextKeyParams.opName, "pre_tokens", std::to_string(sparsePreTokens).c_str(), | ||
| 358 | + "preTokens absolute value should be smaller than the length of key and value"), | ||
| 359 | + return false); | ||
| 360 | + | ||
| 361 | + return true; | ||
| 362 | +} | ||
| 363 | + | ||
| 364 | +bool FiaArch38PromptTiling::CheckPACrossover(FiaArch38ContextParams& contextKeyParams, PFAShapeInfo& queryShapeInfo) | ||
| 365 | +{ | ||
| 366 | + if (enablePA) { | ||
| 367 | + OP_CHECK_IF((isKVHasPrefix), | ||
| 368 | + OP_LOGE_FOR_INVALID_VALUES_WITH_REASON( | ||
| 369 | + contextKeyParams.opName, "prefix, block_table", "system prefix with page attention", | ||
| 370 | + "when system prefix is used, page attention is not supported"), | ||
| 371 | + return false); | ||
| 372 | + OP_CHECK_IF((enableKVAntiquant), | ||
| 373 | + OP_LOGE_FOR_INVALID_VALUES_WITH_REASON(contextKeyParams.opName, "block_table, antiquant_scale", | ||
| 374 | + "block_table present with antiquant", | ||
| 375 | + "antiquant is not supported when blockTable is not null"), | ||
| 376 | + return false); | ||
| 377 | + } | ||
| 378 | + if (enableActSeqLenKV && !enableTensorList) { | ||
| 379 | + const gert::Tensor* actSeqLenKV = contextKeyParams.actualSequenceLengthKV; | ||
| 380 | + uint32_t actSeqLenKVSize = std::min(static_cast<uint32_t>(actSeqLenKVDims), queryShapeInfo.b); | ||
| 381 | + for (uint32_t i = LOOP_BEGIN_NUM; i < actSeqLenKVSize; ++i) { | ||
| 382 | + if (enablePA && (actSeqLenKV->GetData<int64_t>()[i] < 0)) { | ||
| 383 | + OP_LOGE_FOR_INVALID_VALUE_WITH_REASON(contextKeyParams.opName, "actual_seq_lengths_kv", | ||
| 384 | + std::to_string(actSeqLenKV->GetData<int64_t>()[i]).c_str(), | ||
| 385 | + "actual_seq_lengths_kv must be >= 0"); | ||
| 386 | + return false; | ||
| 387 | + } | ||
| 388 | + } | ||
| 389 | + } | ||
| 390 | + return true; | ||
| 391 | +} | ||
| 392 | + | ||
| 393 | +bool FiaArch38PromptTiling::CheckMaskCrossIFAMLA(FiaArch38ContextParams& contextKeyParams, const int64_t* sparseMode, | ||
| 394 | + uint64_t queryS) | ||
| 395 | +{ | ||
| 396 | + if (sparseMode == nullptr) { | ||
| 397 | + return true; | ||
| 398 | + } | ||
| 399 | + if (queryS == 1U) { | ||
| 400 | + OP_CHECK_IF(!((*sparseMode == SPARSE_MODE_NO_MASK) && (!enableMask)), | ||
| 401 | + OP_LOGE_FOR_INVALID_VALUES_WITH_REASON( | ||
| 402 | + contextKeyParams.opName, "sparse_mode, atten_mask", | ||
| 403 | + (std::to_string(*sparseMode) + ", " + std::string(enableMask ? "present" : "absent")).c_str(), | ||
| 404 | + "only sparse 0 without mask is supported when ifa mla and query sequence length is 1"), | ||
| 405 | + return false); | ||
| 406 | + } else { | ||
| 407 | + OP_CHECK_IF(!(((*sparseMode == SPARSE_MODE_RIGHT_DOWN) && (enableMask)) || | ||
| 408 | + ((*sparseMode == SPARSE_MODE_NO_MASK) && (!enableMask))), | ||
| 409 | + OP_LOGE_FOR_INVALID_VALUES_WITH_REASON( | ||
| 410 | + contextKeyParams.opName, "sparse_mode, atten_mask", | ||
| 411 | + (std::to_string(*sparseMode) + ", " + std::string(enableMask ? "present" : "absent")).c_str(), | ||
| 412 | + "only sparse 3 with mask or sparse 0 without mask is supported when ifa mla and " | ||
| 413 | + "query sequence length is > 1"), | ||
| 414 | + return false); | ||
| 415 | + } | ||
| 416 | + return true; | ||
| 417 | +} | ||
| 418 | + | ||
| 419 | +bool FiaArch38PromptTiling::CheckMaskCrossover(FiaArch38ContextParams& contextKeyParams, PFAShapeInfo& queryShapeInfo, | ||
| 420 | + FiaArch38FlashAttentionTilingData& tilingData) | ||
| 421 | +{ | ||
| 422 | + auto maskDataType = contextKeyParams.maskDataType; | ||
| 423 | + const int64_t* sparseMode = contextKeyParams.sparseMode; | ||
| 424 | + const gert::StorageShape* attenMaskShape = contextKeyParams.attentionMaskShape; | ||
| 425 | + if ((sparseMode != nullptr) && (*sparseMode == SPARSE_MODE_LEFT_UP || *sparseMode == SPARSE_MODE_RIGHT_DOWN || | ||
| 426 | + *sparseMode == SPARSE_MODE_ALL_MASK || *sparseMode == SPARSE_MODE_BAND)) { | ||
| 427 | + OP_CHECK_IF(((attenMaskShape != nullptr) && (attenMaskShape->GetStorageShape().GetShapeSize() == 0)) || | ||
| 428 | + (attenMaskShape == nullptr), | ||
| 429 | + OP_LOGE_WITH_INVALID_INPUT(contextKeyParams.opName, "atten_mask"), return false); | ||
| 430 | + | ||
| 431 | + // When sparse=2, 3, 4, the mask type only supports bool, int8, uint8 | ||
| 432 | + OP_CHECK_IF((*sparseMode != SPARSE_MODE_ALL_MASK) && (maskDataType != ge::DT_BOOL) && | ||
| 433 | + (maskDataType != ge::DT_INT8) && (maskDataType != ge::DT_UINT8), | ||
| 434 | + OP_LOGE_FOR_INVALID_DTYPE_WITH_REASON( | ||
| 435 | + contextKeyParams.opName, "atten_mask", GetPfaDataTypeStr(maskDataType).c_str(), | ||
| 436 | + "maskType should be bool, int8 or uint8 for this sparse mode"), | ||
| 437 | + return false); | ||
| 438 | + } | ||
| 439 | + // FP16 mask type does not support invalid line correction. | ||
| 440 | + OP_CHECK_IF((maskDataType == ge::DT_FLOAT16 && | ||
| 441 | + static_cast<bool>(tilingData.fiaArch38AttentionBaseParams.get_isRowInvalid())), | ||
| 442 | + OP_LOGE_FOR_INVALID_DTYPE_WITH_REASON(contextKeyParams.opName, "atten_mask", | ||
| 443 | + GetPfaDataTypeStr(maskDataType).c_str(), | ||
| 444 | + "maskType should not be float16 when innerPrecise is 2 or 3"), | ||
| 445 | + return false); | ||
| 446 | + if (!CheckMaskShapeCrossSparse(contextKeyParams, sparseMode, queryShapeInfo.s, S2 + actualSharedPrefixLen, | ||
| 447 | + queryShapeInfo.b)) { | ||
| 448 | + return false; | ||
| 449 | + } | ||
| 450 | + if (enableIFAMLA && (!CheckMaskCrossIFAMLA(contextKeyParams, sparseMode, queryShapeInfo.s / gSize))) { | ||
| 451 | + return false; | ||
| 452 | + } | ||
| 453 | + return true; | ||
| 454 | +} | ||
| 455 | + | ||
| 456 | +bool FiaArch38PromptTiling::CheckTNDLayoutCrossover(FiaArch38ContextParams& contextKeyParams) | ||
| 457 | +{ | ||
| 458 | + if (inputLayout != InputLayout::TND) { | ||
| 459 | + return true; | ||
| 460 | + } | ||
| 461 | + | ||
| 462 | + OP_CHECK_IF(enableLeftPadding, | ||
| 463 | + OP_LOGE_FOR_INVALID_VALUES_WITH_REASON(contextKeyParams.opName, "input_layout, leftPadding", | ||
| 464 | + "TND with left padding", | ||
| 465 | + "when layout is TND, left padding is not supported"), | ||
| 466 | + return false); | ||
| 467 | + | ||
| 468 | + OP_CHECK_IF(enableTensorList, | ||
| 469 | + OP_LOGE_FOR_INVALID_VALUES_WITH_REASON(contextKeyParams.opName, "input_layout, tensorlist", | ||
| 470 | + "TND with tensorlist", | ||
| 471 | + "when layout is TND, tensorlist is not supported"), | ||
| 472 | + return false); | ||
| 473 | + | ||
| 474 | + OP_CHECK_IF(enablePseShift, | ||
| 475 | + OP_LOGE_FOR_INVALID_VALUES_WITH_REASON(contextKeyParams.opName, "input_layout, pse_shift", | ||
| 476 | + "TND with pse", "when layout is TND, pse is not supported"), | ||
| 477 | + return false); | ||
| 478 | + | ||
| 479 | + return true; | ||
| 480 | +} | ||
| 481 | + | ||
| 482 | +} // namespace arch38 | ||
| 483 | +} // namespace optiling | ||
| @@ -0,0 +1,447 @@ | |||
| 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 | +#include "fia_arch38_prompt_tiling.h" | ||
| 12 | + | ||
| 13 | +using namespace ge; | ||
| 14 | +using namespace AscendC; | ||
| 15 | +using namespace matmul_tiling; | ||
| 16 | +namespace optiling { | ||
| 17 | + | ||
| 18 | +namespace arch38 { | ||
| 19 | + | ||
| 20 | +bool FiaArch38PromptTiling::GetAndCheckPrefixShape(FiaArch38ContextParams& contextKeyParams, | ||
| 21 | + const PFAShapeInfo& queryShapeInfo, PFAShapeInfo& prefixShapeInfo, | ||
| 22 | + FiaArch38FlashAttentionTilingData& tilingData) const | ||
| 23 | +{ | ||
| 24 | + PFAShapeDims dims; | ||
| 25 | + if (!SetShape(contextKeyParams, contextKeyParams.queryInputShape, "query", dims)) { | ||
| 26 | + return false; | ||
| 27 | + } | ||
| 28 | + int64_t prefixSeqInnerSize = dims.s; | ||
| 29 | + int64_t bPrefix = dims.b; | ||
| 30 | + int64_t nPrefix = dims.n; | ||
| 31 | + int64_t dPrefix = dims.d; | ||
| 32 | + int64_t hPrefix = dims.h; | ||
| 33 | + int64_t tPrefix = dims.t; | ||
| 34 | + OP_CHECK_IF( | ||
| 35 | + (bPrefix != 1), | ||
| 36 | + OP_LOGE_FOR_INVALID_VALUE_WITH_REASON(contextKeyParams.opName, "prefix batch", std::to_string(bPrefix).c_str(), | ||
| 37 | + "prefix batch num only supports 1"), | ||
| 38 | + return false); | ||
| 39 | + if (inputLayout == InputLayout::BSH) { | ||
| 40 | + OP_CHECK_IF((hPrefix != queryShapeInfo.h / tilingData.fiaArch38AttentionBaseParams.get_headNumRatio()), | ||
| 41 | + OP_LOGE_FOR_INVALID_SHAPESIZES_WITH_REASON( | ||
| 42 | + contextKeyParams.opName, "key_shared_prefix, key", | ||
| 43 | + (std::to_string(hPrefix) + ", " + | ||
| 44 | + std::to_string(queryShapeInfo.h / tilingData.fiaArch38AttentionBaseParams.get_headNumRatio())) | ||
| 45 | + .c_str(), | ||
| 46 | + "prefix H must be the same as KV H"), | ||
| 47 | + return false); | ||
| 48 | + } else { | ||
| 49 | + OP_CHECK_IF((nPrefix != queryShapeInfo.n / tilingData.fiaArch38AttentionBaseParams.get_headNumRatio()) || | ||
| 50 | + (dPrefix != queryShapeInfo.h / queryShapeInfo.n), | ||
| 51 | + OP_LOGE_FOR_INVALID_SHAPESIZES_WITH_REASON( | ||
| 52 | + contextKeyParams.opName, "key_shared_prefix, key", | ||
| 53 | + (std::to_string(nPrefix) + ", " + std::to_string(dPrefix) + ", " + | ||
| 54 | + std::to_string(queryShapeInfo.n / tilingData.fiaArch38AttentionBaseParams.get_headNumRatio()) + | ||
| 55 | + ", " + std::to_string(queryShapeInfo.h / queryShapeInfo.n)) | ||
| 56 | + .c_str(), | ||
| 57 | + "prefix N and D must be the same as KV N and D"), | ||
| 58 | + return false); | ||
| 59 | + } | ||
| 60 | + prefixShapeInfo.b = static_cast<uint32_t>(bPrefix); | ||
| 61 | + prefixShapeInfo.n = static_cast<uint64_t>(nPrefix); | ||
| 62 | + prefixShapeInfo.s = static_cast<uint32_t>(prefixSeqInnerSize); | ||
| 63 | + prefixShapeInfo.d = static_cast<uint32_t>(dPrefix); | ||
| 64 | + prefixShapeInfo.h = static_cast<uint64_t>(hPrefix); | ||
| 65 | + return true; | ||
| 66 | +} | ||
| 67 | + | ||
| 68 | +bool FiaArch38PromptTiling::CheckKeyValuePrefixConsistency(FiaArch38ContextParams& contextKeyParams, | ||
| 69 | + const gert::StorageShape* keyShape) const | ||
| 70 | +{ | ||
| 71 | + size_t prefixKeyDim = contextKeyParams.keySharedPrefix->GetStorageShape().GetDimNum(); | ||
| 72 | + size_t prefixValueDim = contextKeyParams.valueSharedPrefix->GetStorageShape().GetDimNum(); | ||
| 73 | + size_t KVDim = keyShape->GetStorageShape().GetDimNum(); | ||
| 74 | + OP_CHECK_IF( | ||
| 75 | + ((prefixKeyDim != KVDim) || (prefixKeyDim != prefixValueDim)), | ||
| 76 | + OP_LOGE_FOR_INVALID_SHAPEDIMS_WITH_REASON( | ||
| 77 | + contextKeyParams.opName, "key_shared_prefix, value_shared_prefix, key", | ||
| 78 | + (std::to_string(prefixKeyDim) + ", " + std::to_string(prefixValueDim) + ", " + std::to_string(KVDim)) | ||
| 79 | + .c_str(), | ||
| 80 | + "dim num of key_shared_prefix and value_shared_prefix must be the same as KV"), | ||
| 81 | + return false); | ||
| 82 | + for (uint32_t i = 0; i < prefixKeyDim; i++) { | ||
| 83 | + int64_t tmpPrefixKeyDim = contextKeyParams.keySharedPrefix->GetStorageShape().GetDim(i); | ||
| 84 | + int64_t tmpPrefixValueDim = contextKeyParams.valueSharedPrefix->GetStorageShape().GetDim(i); | ||
| 85 | + if ((tmpPrefixKeyDim == 0) || (tmpPrefixValueDim == 0)) { | ||
| 86 | + OP_LOGE_FOR_INVALID_SHAPESIZES_WITH_REASON( | ||
| 87 | + contextKeyParams.opName, "key_shared_prefix, value_shared_prefix", | ||
| 88 | + (std::to_string(tmpPrefixKeyDim) + ", " + std::to_string(tmpPrefixValueDim)).c_str(), | ||
| 89 | + "key_shared_prefix and value_shared_prefix do not support empty tensor"); | ||
| 90 | + return false; | ||
| 91 | + } | ||
| 92 | + if (tmpPrefixKeyDim != tmpPrefixValueDim) { | ||
| 93 | + OP_LOGE_FOR_INVALID_SHAPES_WITH_REASON( | ||
| 94 | + contextKeyParams.opName, "key_shared_prefix, value_shared_prefix", | ||
| 95 | + (std::to_string(tmpPrefixKeyDim) + ", " + std::to_string(tmpPrefixValueDim)).c_str(), | ||
| 96 | + "shape of key_shared_prefix must be the same as value_shared_prefix"); | ||
| 97 | + return false; | ||
| 98 | + } | ||
| 99 | + } | ||
| 100 | + return true; | ||
| 101 | +} | ||
| 102 | + | ||
| 103 | +bool FiaArch38PromptTiling::CheckActSharedPrefix(FiaArch38ContextParams& contextKeyParams, const uint32_t sPrefix, | ||
| 104 | + const uint32_t sKV) | ||
| 105 | +{ | ||
| 106 | + size_t prefixDimNum = contextKeyParams.actualSharedPrefixLen->GetStorageShape().GetDimNum(); | ||
| 107 | + OP_CHECK_IF((prefixDimNum != 1), | ||
| 108 | + OP_LOGE_FOR_INVALID_SHAPEDIM_WITH_REASON(contextKeyParams.opName, "actual_shared_prefix_len", | ||
| 109 | + std::to_string(prefixDimNum).c_str(), | ||
| 110 | + "actualSharedPrefixLen dim num must be 1"), | ||
| 111 | + return false); | ||
| 112 | + int64_t prefixShapeSize = contextKeyParams.actualSharedPrefixLen->GetStorageShape().GetShapeSize(); | ||
| 113 | + OP_CHECK_IF((prefixShapeSize != 1), | ||
| 114 | + OP_LOGE_FOR_INVALID_SHAPESIZE_WITH_REASON(contextKeyParams.opName, "actual_shared_prefix_len", | ||
| 115 | + std::to_string(prefixShapeSize).c_str(), | ||
| 116 | + "actualSharedPrefixLen length must be 1"), | ||
| 117 | + return false); | ||
| 118 | + OP_CHECK_IF((contextKeyParams.actualSharedPrefixLen->GetData<int64_t>() == nullptr), | ||
| 119 | + OP_LOGE_WITHOUT_REPORT(contextKeyParams.opName, "input actualSharedPrefixLen GetData is nullptr!"), | ||
| 120 | + return false); | ||
| 121 | + actualSharedPrefixLen = contextKeyParams.actualSharedPrefixLen->GetData<int64_t>()[0]; | ||
| 122 | + OP_CHECK_IF((actualSharedPrefixLen > sPrefix) || (actualSharedPrefixLen < 0), | ||
| 123 | + OP_LOGE_FOR_INVALID_VALUE_WITH_REASON(contextKeyParams.opName, "actual_shared_prefix_len", | ||
| 124 | + std::to_string(actualSharedPrefixLen).c_str(), | ||
| 125 | + "actualSharedPrefixLen must be in range [0, sPrefix]"), | ||
| 126 | + return false); | ||
| 127 | + | ||
| 128 | + if (actualSharedPrefixLen + sKV > SLIMIT) { | ||
| 129 | + OP_LOGW(contextKeyParams.opName, "Seq size of actualSharedPrefixLen(%ld) + kv_s(%u) should <= 20m", | ||
| 130 | + actualSharedPrefixLen, sKV); | ||
| 131 | + } | ||
| 132 | + return true; | ||
| 133 | +} | ||
| 134 | + | ||
| 135 | +bool FiaArch38PromptTiling::CheckPAKeyValueDimConsistency(const FiaArch38ContextParams& contextKeyParams, | ||
| 136 | + const PACacheDimCtx& ctx) const | ||
| 137 | +{ | ||
| 138 | + int64_t keyDim2 = ctx.keyShape->GetStorageShape().GetDim(KV_CACHE_DIM_1); | ||
| 139 | + int64_t keyDim3 = ctx.keyShape->GetStorageShape().GetDim(KV_CACHE_DIM_2); | ||
| 140 | + int64_t keyDim4 = 0; | ||
| 141 | + int64_t valueDim1 = ctx.valueShape->GetStorageShape().GetDim(KV_CACHE_DIM_0); | ||
| 142 | + int64_t valueDim2 = ctx.valueShape->GetStorageShape().GetDim(KV_CACHE_DIM_1); | ||
| 143 | + int64_t valueDim3 = ctx.valueShape->GetStorageShape().GetDim(KV_CACHE_DIM_2); | ||
| 144 | + int64_t valueDim4 = 0; | ||
| 145 | + if (ctx.keyDim == KV_CACHE_DIM_NUMS_4) { | ||
| 146 | + keyDim4 = ctx.keyShape->GetStorageShape().GetDim(KV_CACHE_DIM_3); | ||
| 147 | + valueDim4 = ctx.valueShape->GetStorageShape().GetDim(KV_CACHE_DIM_3); | ||
| 148 | + } | ||
| 149 | + OP_CHECK_IF( | ||
| 150 | + (ctx.keyDim == KV_CACHE_DIM_NUMS_4) && ((ctx.keyDim1 != valueDim1) || (keyDim2 != valueDim2) || | ||
| 151 | + (keyDim3 != valueDim3) || ((keyDim4 != valueDim4) && (!enablePFAMLA))), | ||
| 152 | + OP_LOGE_FOR_INVALID_SHAPEDIMS_WITH_REASON( | ||
| 153 | + contextKeyParams.opName, "key, value", | ||
| 154 | + (std::to_string(ctx.keyDim1) + ", " + std::to_string(keyDim2) + ", " + std::to_string(keyDim3) + ", " + | ||
| 155 | + std::to_string(keyDim4) + ", " + std::to_string(valueDim1) + ", " + std::to_string(valueDim2) + ", " + | ||
| 156 | + std::to_string(valueDim3) + ", " + std::to_string(valueDim4)) | ||
| 157 | + .c_str(), | ||
| 158 | + "the dim num of key and value are inconsistent when PA enable"), | ||
| 159 | + return false); | ||
| 160 | + OP_CHECK_IF( | ||
| 161 | + (ctx.keyDim == KV_CACHE_DIM_NUMS_3) && | ||
| 162 | + ((ctx.keyDim1 != valueDim1) || (keyDim2 != valueDim2) || ((keyDim3 != valueDim3) && (!enablePFAMLA))), | ||
| 163 | + OP_LOGE_FOR_INVALID_SHAPEDIMS_WITH_REASON( | ||
| 164 | + contextKeyParams.opName, "key, value", | ||
| 165 | + (std::to_string(ctx.keyDim1) + ", " + std::to_string(keyDim2) + ", " + std::to_string(keyDim3) + ", " + | ||
| 166 | + std::to_string(valueDim1) + ", " + std::to_string(valueDim2) + ", " + std::to_string(valueDim3)) | ||
| 167 | + .c_str(), | ||
| 168 | + "the dim num of key and value are inconsistent when PA enable"), | ||
| 169 | + return false); | ||
| 170 | + return true; | ||
| 171 | +} | ||
| 172 | + | ||
| 173 | +bool FiaArch38PromptTiling::CheckPAKeyValueLayout(const FiaArch38ContextParams& contextKeyParams, | ||
| 174 | + const PFAShapeInfo& queryShapeInfo, const PACacheDimCtx& ctx) const | ||
| 175 | +{ | ||
| 176 | + int64_t keyDim2 = ctx.keyShape->GetStorageShape().GetDim(KV_CACHE_DIM_1); | ||
| 177 | + int64_t keyDim3 = ctx.keyShape->GetStorageShape().GetDim(KV_CACHE_DIM_2); | ||
| 178 | + int64_t keyDim4 = (ctx.keyDim == KV_CACHE_DIM_NUMS_4) ? ctx.keyShape->GetStorageShape().GetDim(KV_CACHE_DIM_3) : 0; | ||
| 179 | + if (inputLayout == InputLayout::BNSD || inputLayout == InputLayout::TND) { | ||
| 180 | + OP_CHECK_IF( | ||
| 181 | + (ctx.keyDim != KV_CACHE_DIM_NUMS_3) && (ctx.keyDim != KV_CACHE_DIM_NUMS_4), | ||
| 182 | + OP_LOGE_FOR_INVALID_SHAPES_WITH_REASON(contextKeyParams.opName, "key, value", | ||
| 183 | + (std::to_string(ctx.keyDim1) + ", " + std::to_string(keyDim2) + | ||
| 184 | + ", " + std::to_string(keyDim3) + ", " + std::to_string(keyDim4)) | ||
| 185 | + .c_str(), | ||
| 186 | + "the layout of key and value is invalid when PA enable"), | ||
| 187 | + return false); | ||
| 188 | + } else if (inputLayout == InputLayout::BSH || inputLayout == InputLayout::BSND) { | ||
| 189 | + OP_CHECK_IF( | ||
| 190 | + ctx.keyDim != 3, | ||
| 191 | + OP_LOGE_FOR_INVALID_SHAPES_WITH_REASON(contextKeyParams.opName, "key, value", | ||
| 192 | + (std::to_string(ctx.keyDim1) + ", " + std::to_string(keyDim2) + | ||
| 193 | + ", " + std::to_string(keyDim3) + ", " + std::to_string(keyDim4)) | ||
| 194 | + .c_str(), | ||
| 195 | + "the layout of key and value is invalid when PA enable"), | ||
| 196 | + return false); | ||
| 197 | + } | ||
| 198 | + return true; | ||
| 199 | +} | ||
| 200 | + | ||
| 201 | +bool FiaArch38PromptTiling::CheckPACacheShape3D(const FiaArch38ContextParams& contextKeyParams, | ||
| 202 | + const PFAShapeInfo& shapeInfo, const gert::StorageShape* shape, | ||
| 203 | + const PACacheDimCtx& ctx, const std::string& sName) | ||
| 204 | +{ | ||
| 205 | + int64_t dim1 = shape->GetStorageShape().GetDim(KV_CACHE_DIM_0); | ||
| 206 | + int64_t dim2 = shape->GetStorageShape().GetDim(KV_CACHE_DIM_1); | ||
| 207 | + int64_t dim3 = shape->GetStorageShape().GetDim(KV_CACHE_DIM_2); | ||
| 208 | + int64_t tempBlockSize = dim2; | ||
| 209 | + int64_t tempH = dim3; | ||
| 210 | + paLayoutType = 1; // If it is three-dimensional, paLayoutType = 1 | ||
| 211 | + OP_CHECK_IF( | ||
| 212 | + ((dim1 < ctx.blockNumValid) || (tempBlockSize != *ctx.blockSize) || (tempH * ctx.headNumRatio != shapeInfo.h)), | ||
| 213 | + OP_LOGE_FOR_INVALID_SHAPE_WITH_REASON( | ||
| 214 | + contextKeyParams.opName, sName.c_str(), | ||
| 215 | + ("[" + std::to_string(dim1) + ", " + std::to_string(dim2) + ", " + std::to_string(dim3) + "]").c_str(), | ||
| 216 | + "the dim must be [>=blockNumValid, blockSize, h/headNumRatio] when PA enable"), | ||
| 217 | + return false); | ||
| 218 | + // In the BSH input of the PA scenario, the h of the KV matrix must not exceed 65535. | ||
| 219 | + OP_CHECK_IF(dim3 > HLIMIT, | ||
| 220 | + OP_LOGE_FOR_INVALID_SHAPESIZE_WITH_REASON( | ||
| 221 | + contextKeyParams.opName, sName.c_str(), std::to_string(dim3).c_str(), | ||
| 222 | + "layout of key/value is BSH, h must not be greater than 65535 when PA enable"), | ||
| 223 | + return false); | ||
| 224 | + return true; | ||
| 225 | +} | ||
| 226 | + | ||
| 227 | +bool FiaArch38PromptTiling::CheckPACacheShape4D(const FiaArch38ContextParams& contextKeyParams, | ||
| 228 | + const PFAShapeInfo& shapeInfo, const gert::StorageShape* shape, | ||
| 229 | + const PACacheDimCtx& ctx, const std::string& sName) | ||
| 230 | +{ | ||
| 231 | + int64_t dim1 = shape->GetStorageShape().GetDim(KV_CACHE_DIM_0); | ||
| 232 | + int64_t dim2 = shape->GetStorageShape().GetDim(KV_CACHE_DIM_1); | ||
| 233 | + int64_t dim3 = shape->GetStorageShape().GetDim(KV_CACHE_DIM_2); | ||
| 234 | + int64_t dim4 = shape->GetStorageShape().GetDim(KV_CACHE_DIM_3); | ||
| 235 | + int64_t tempN = dim2; | ||
| 236 | + int64_t tempBlockSize = dim3; | ||
| 237 | + int64_t tempD = dim4; | ||
| 238 | + paLayoutType = 0; // If it is four-dimensional, paLayoutType = 0 | ||
| 239 | + OP_CHECK_IF(((dim1 < ctx.blockNumValid) || (tempN * ctx.headNumRatio != shapeInfo.n) || | ||
| 240 | + (tempBlockSize != *ctx.blockSize) || (tempD != (shapeInfo.h / shapeInfo.n))), | ||
| 241 | + OP_LOGE_FOR_INVALID_SHAPE_WITH_REASON( | ||
| 242 | + contextKeyParams.opName, sName.c_str(), | ||
| 243 | + ("[" + std::to_string(dim1) + ", " + std::to_string(dim2) + ", " + std::to_string(dim3) + ", " + | ||
| 244 | + std::to_string(dim4) + "]") | ||
| 245 | + .c_str(), | ||
| 246 | + "the dim must be [>=blockNumValid, n/headNumRatio, blockSize, h/n] when PA enable"), | ||
| 247 | + return false); | ||
| 248 | + return true; | ||
| 249 | +} | ||
| 250 | + | ||
| 251 | +bool FiaArch38PromptTiling::CheckPACacheShape(const FiaArch38ContextParams& contextKeyParams, | ||
| 252 | + const PFAShapeInfo& shapeInfo, const gert::StorageShape* shape, | ||
| 253 | + const PACacheDimCtx& ctx, const std::string& sName) | ||
| 254 | +{ | ||
| 255 | + return (ctx.keyDim == KV_CACHE_DIM_NUMS_3) ? CheckPACacheShape3D(contextKeyParams, shapeInfo, shape, ctx, sName) : | ||
| 256 | + CheckPACacheShape4D(contextKeyParams, shapeInfo, shape, ctx, sName); | ||
| 257 | +} | ||
| 258 | + | ||
| 259 | +PABlockNumInfo FiaArch38PromptTiling::ComputePABlockNumInfo(const FiaArch38ContextParams& contextKeyParams, | ||
| 260 | + const PFAShapeInfo& queryShapeInfo) const | ||
| 261 | +{ | ||
| 262 | + const gert::Tensor* actSeqLenKV = contextKeyParams.actualSequenceLengthKV; | ||
| 263 | + const int64_t* blockSize = contextKeyParams.blockSize; | ||
| 264 | + PABlockNumInfo info; | ||
| 265 | + for (uint32_t i = 0; i < queryShapeInfo.b; i++) { | ||
| 266 | + int32_t actualSeqKVPerBatch = (actSeqLenKV->GetShapeSize() > 1) ? | ||
| 267 | + static_cast<int32_t>(actSeqLenKV->GetData<int64_t>()[i]) : | ||
| 268 | + static_cast<int32_t>(actSeqLenKV->GetData<int64_t>()[0]); | ||
| 269 | + int32_t blockNumPerBatch = static_cast<int32_t>((actualSeqKVPerBatch + *blockSize - 1) / *blockSize); | ||
| 270 | + info.blockNumValid += blockNumPerBatch; | ||
| 271 | + if (blockNumPerBatch > info.maxBlockNumPerBatch) { | ||
| 272 | + info.maxBlockNumPerBatch = blockNumPerBatch; | ||
| 273 | + } | ||
| 274 | + } | ||
| 275 | + return info; | ||
| 276 | +} | ||
| 277 | + | ||
| 278 | +bool FiaArch38PromptTiling::CheckBlockTableDims(const FiaArch38ContextParams& contextKeyParams, | ||
| 279 | + const PFAShapeInfo& queryShapeInfo, int32_t maxBlockNumPerBatch) const | ||
| 280 | +{ | ||
| 281 | + const gert::StorageShape* blockTableShape = contextKeyParams.blockTableShape; | ||
| 282 | + size_t blockTableDim = blockTableShape->GetStorageShape().GetDimNum(); | ||
| 283 | + int64_t blockTableDim1 = blockTableShape->GetStorageShape().GetDim(0); | ||
| 284 | + string blockTableStr = ""; | ||
| 285 | + for (size_t i = 0; i < blockTableDim; i++) { | ||
| 286 | + blockTableStr += std::to_string(blockTableShape->GetStorageShape().GetDim(i)); | ||
| 287 | + blockTableStr += ","; | ||
| 288 | + } | ||
| 289 | + blockTableStr.pop_back(); | ||
| 290 | + | ||
| 291 | + OP_CHECK_IF(blockTableDim != 2, // PA BlockTable dim size | ||
| 292 | + OP_LOGE_FOR_INVALID_SHAPEDIM_WITH_REASON(contextKeyParams.opName, "block_table", | ||
| 293 | + std::to_string(blockTableDim).c_str(), | ||
| 294 | + "the dim of block table must be 2 when PA enable"), | ||
| 295 | + return false); | ||
| 296 | + | ||
| 297 | + OP_CHECK_IF(((blockTableDim1 != queryShapeInfo.b) || (blockTableDim2 < maxBlockNumPerBatch)), | ||
| 298 | + OP_LOGE_FOR_INVALID_SHAPE_WITH_REASON( | ||
| 299 | + contextKeyParams.opName, "block_table", | ||
| 300 | + ("[" + std::to_string(blockTableDim1) + ", " + std::to_string(blockTableDim2) + "]").c_str(), | ||
| 301 | + "block table shape should be [b, >=maxBlockNumPerBatch] when PA enable"), | ||
| 302 | + return false); | ||
| 303 | + return true; | ||
| 304 | +} | ||
| 305 | + | ||
| 306 | +bool FiaArch38PromptTiling::CheckBlockTableShape(FiaArch38ContextParams& contextKeyParams, PFAShapeInfo& queryShapeInfo, | ||
| 307 | + PFAShapeInfo& queryRopeShapeInfo, | ||
| 308 | + FiaArch38FlashAttentionTilingData& tilingData) | ||
| 309 | +{ | ||
| 310 | + PABlockNumInfo blockNumInfo = ComputePABlockNumInfo(contextKeyParams, queryShapeInfo); | ||
| 311 | + | ||
| 312 | + const gert::StorageShape* keyShape = contextKeyParams.keyInputShape; | ||
| 313 | + const gert::StorageShape* valueShape = contextKeyParams.valueInputShape; | ||
| 314 | + const size_t keyDim = keyShape->GetStorageShape().GetDimNum(); | ||
| 315 | + PACacheDimCtx ctx; | ||
| 316 | + ctx.keyShape = keyShape; | ||
| 317 | + ctx.valueShape = valueShape; | ||
| 318 | + ctx.keyDim = keyDim; | ||
| 319 | + ctx.keyDim1 = keyShape->GetStorageShape().GetDim(KV_CACHE_DIM_0); | ||
| 320 | + ctx.blockSize = contextKeyParams.blockSize; | ||
| 321 | + ctx.blockNumValid = blockNumInfo.blockNumValid; | ||
| 322 | + ctx.headNumRatio = (enableIFAMLA || enableIFA) ? | ||
| 323 | + static_cast<int64_t>(gSize) : | ||
| 324 | + static_cast<int64_t>(tilingData.fiaArch38AttentionBaseParams.get_headNumRatio()); | ||
| 325 | + | ||
| 326 | + if (!CheckPAKeyValueDimConsistency(contextKeyParams, ctx) || | ||
| 327 | + !CheckPAKeyValueLayout(contextKeyParams, queryShapeInfo, ctx)) { | ||
| 328 | + return false; | ||
| 329 | + } | ||
| 330 | + if (!CheckPACacheShape(contextKeyParams, queryShapeInfo, keyShape, ctx, "key")) { | ||
| 331 | + return false; | ||
| 332 | + } | ||
| 333 | + if (!CheckPARopeShape(contextKeyParams, queryRopeShapeInfo, ctx)) { | ||
| 334 | + return false; | ||
| 335 | + } | ||
| 336 | + | ||
| 337 | + if (!CheckBlockTableDims(contextKeyParams, queryShapeInfo, blockNumInfo.maxBlockNumPerBatch)) { | ||
| 338 | + return false; | ||
| 339 | + } | ||
| 340 | + | ||
| 341 | + paBlockNumSum = ctx.keyDim1; | ||
| 342 | + S2 = static_cast<uint32_t>(blockNumInfo.maxBlockNumPerBatch * (*contextKeyParams.blockSize)); | ||
| 343 | + if (S2 > SLIMIT) { | ||
| 344 | + OP_LOGW(contextKeyParams.opName, "seq should <= 20m, but kv seq = %u when PA enable", S2); | ||
| 345 | + } | ||
| 346 | + return true; | ||
| 347 | +} | ||
| 348 | + | ||
| 349 | +bool FiaArch38PromptTiling::CheckPARopeShape(const FiaArch38ContextParams& contextKeyParams, | ||
| 350 | + PFAShapeInfo& queryRopeShapeInfo, const PACacheDimCtx& ctx) | ||
| 351 | +{ | ||
| 352 | + if (!enableIFAMLA) { | ||
| 353 | + return true; | ||
| 354 | + } | ||
| 355 | + const gert::StorageShape* keyRopeShape = contextKeyParams.keyRopeInputShape; | ||
| 356 | + const size_t keyRopeDim = keyRopeShape->GetStorageShape().GetDimNum(); | ||
| 357 | + OP_CHECK_IF((keyRopeDim != ctx.keyDim), | ||
| 358 | + OP_LOGE_FOR_INVALID_SHAPEDIMS_WITH_REASON( | ||
| 359 | + contextKeyParams.opName, "key_rope, key", | ||
| 360 | + (std::to_string(keyRopeDim) + ", " + std::to_string(ctx.keyDim)).c_str(), | ||
| 361 | + "the dim of keyRope must be equal to the dim of key when PA enable"), | ||
| 362 | + return false); | ||
| 363 | + if (!CheckPACacheShape(contextKeyParams, queryRopeShapeInfo, keyRopeShape, ctx, "keyRope")) { | ||
| 364 | + return false; | ||
| 365 | + } | ||
| 366 | + return true; | ||
| 367 | +} | ||
| 368 | + | ||
| 369 | +bool FiaArch38PromptTiling::CheckPATypeAndShape(FiaArch38ContextParams& contextKeyParams, PFAShapeInfo& queryShapeInfo, | ||
| 370 | + PFAShapeInfo& queryRopeShapeInfo, | ||
| 371 | + FiaArch38FlashAttentionTilingData& tilingData) | ||
| 372 | +{ | ||
| 373 | + // The interception that is mutually exclusive with the left padding has been implemented in FIA. | ||
| 374 | + OP_CHECK_IF(enableTensorList, | ||
| 375 | + OP_LOGE_FOR_INVALID_VALUES_WITH_REASON(contextKeyParams.opName, "block_table, tensorlist", | ||
| 376 | + "block_table present with tensorlist", | ||
| 377 | + "tensorlist is not supported when blockTable is not null"), | ||
| 378 | + return false); | ||
| 379 | + OP_CHECK_IF((!enableActSeqLenKV) && (contextKeyParams.fromTilingSink == 0), | ||
| 380 | + OP_LOGE_WITH_INVALID_INPUT(contextKeyParams.opName, "actual_seq_lengths_kv"), return false); | ||
| 381 | + | ||
| 382 | + if (!CheckPAType(contextKeyParams)) { | ||
| 383 | + return false; | ||
| 384 | + } | ||
| 385 | + if (!CheckPABlockSize(contextKeyParams, queryShapeInfo)) { | ||
| 386 | + return false; | ||
| 387 | + } | ||
| 388 | + | ||
| 389 | + if (contextKeyParams.fromTilingSink != 0) { | ||
| 390 | + S2 = static_cast<uint32_t>(blockTableDim2 * (*contextKeyParams.blockSize)); | ||
| 391 | + return true; | ||
| 392 | + } | ||
| 393 | + if (!CheckBlockTableShape(contextKeyParams, queryShapeInfo, queryRopeShapeInfo, tilingData)) { | ||
| 394 | + return false; | ||
| 395 | + } | ||
| 396 | + | ||
| 397 | + tilingData.fiaArch38AttentionBaseParams.set_PAlayoutType(paLayoutType); | ||
| 398 | + return true; | ||
| 399 | +} | ||
| 400 | + | ||
| 401 | +bool FiaArch38PromptTiling::CheckPAType(const FiaArch38ContextParams& contextKeyParams) | ||
| 402 | +{ | ||
| 403 | + const gert::StorageShape* blockTableShape = contextKeyParams.blockTableShape; | ||
| 404 | + OP_CHECK_IF(blockTableShape == nullptr, OP_LOGE_WITH_INVALID_INPUT(contextKeyParams.opName, "block_table"), | ||
| 405 | + return false); | ||
| 406 | + | ||
| 407 | + OP_CHECK_IF( | ||
| 408 | + blockTableShape->GetStorageShape().GetShapeSize() == 0, | ||
| 409 | + OP_LOGE_FOR_INVALID_SHAPESIZES_WITH_REASON(contextKeyParams.opName, "block_table", | ||
| 410 | + (std::to_string(blockTableShape->GetStorageShape().GetDim(0)) + | ||
| 411 | + ", " + std::to_string(blockTableShape->GetStorageShape().GetDim(1))) | ||
| 412 | + .c_str(), | ||
| 413 | + "blockTable each dim must not be 0"), | ||
| 414 | + return false); | ||
| 415 | + | ||
| 416 | + ge::DataType blockTableType = contextKeyParams.blockTableType; | ||
| 417 | + OP_CHECK_IF((blockTableType != ge::DT_INT32), | ||
| 418 | + OP_LOGE_FOR_INVALID_DTYPE_WITH_REASON(contextKeyParams.opName, "block_table", | ||
| 419 | + GetPfaDataTypeStr(blockTableType).c_str(), | ||
| 420 | + "blockTable only supports int32 when PA enable"), | ||
| 421 | + return false); | ||
| 422 | + return true; | ||
| 423 | +} | ||
| 424 | + | ||
| 425 | +bool FiaArch38PromptTiling::CheckPABlockSize(const FiaArch38ContextParams& contextKeyParams, | ||
| 426 | + const PFAShapeInfo& queryShapeInfo) | ||
| 427 | +{ | ||
| 428 | + const int64_t* blockSize = contextKeyParams.blockSize; | ||
| 429 | + blockTableDim2 = static_cast<int32_t>(contextKeyParams.blockTableShape->GetStorageShape().GetDim(1)); | ||
| 430 | + OP_CHECK_IF((!enableIFAMLA && !enableIFA && !(queryShapeInfo.s == 1 && enableAlibiPse) && | ||
| 431 | + (*blockSize % BLOCK_SIZE_BASE != 0 || *blockSize < BLOCK_SIZE_BASE || *blockSize > BLOCK_SIZE_MAX)), | ||
| 432 | + OP_LOGE_FOR_INVALID_VALUE_WITH_REASON( | ||
| 433 | + contextKeyParams.opName, "block_size", std::to_string(*blockSize).c_str(), | ||
| 434 | + "block size must be a multiple of BLOCK_SIZE_BASE and within the allowed range"), | ||
| 435 | + return false); | ||
| 436 | + ifaBlockSizeBase /= static_cast<int32_t>(dataTypeSize); | ||
| 437 | + OP_CHECK_IF(((enableIFAMLA || enableIFA || (queryShapeInfo.s == 1 && enableAlibiPse)) && | ||
| 438 | + (*blockSize % ifaBlockSizeBase != 0 || *blockSize < ifaBlockSizeBase || *blockSize > BLOCK_SIZE_MAX)), | ||
| 439 | + OP_LOGE_FOR_INVALID_VALUE_WITH_REASON( | ||
| 440 | + contextKeyParams.opName, "block_size", std::to_string(*blockSize).c_str(), | ||
| 441 | + "block size must be a multiple of ifaBlockSizeBase and within the allowed range"), | ||
| 442 | + return false); | ||
| 443 | + return true; | ||
| 444 | +} | ||
| 445 | + | ||
| 446 | +} // namespace arch38 | ||
| 447 | +} // namespace optiling | ||
| @@ -8,10 +8,6 @@ | |||
| 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 | -/*! | ||
| 12 | - * \file fia_arch38_prompt_tiling.h | ||
| 13 | - * \brief | ||
| 14 | - */ | ||
| 15 | #ifndef FIA_ARCH38_PROMPT_TILING_H_ | 11 | #ifndef FIA_ARCH38_PROMPT_TILING_H_ |
| 16 | #define FIA_ARCH38_PROMPT_TILING_H_ | 12 | #define FIA_ARCH38_PROMPT_TILING_H_ |
| 17 | #include "op_host/data_copy_transpose_tiling_def.h" | 13 | #include "op_host/data_copy_transpose_tiling_def.h" |
| @@ -21,6 +17,8 @@ | |||
| 21 | #include "fia_arch38_context.h" | 17 | #include "fia_arch38_context.h" |
| 22 | #include "fia_arch38_tiling_data.h" | 18 | #include "fia_arch38_tiling_data.h" |
| 23 | #include "fia_arch38_tiling_common.h" | 19 | #include "fia_arch38_tiling_common.h" |
| 20 | +#include "fia_arch38_tiling_structs.h" | ||
| 21 | +#include "fia_arch38_tiling_utils.h" | ||
| 24 | 22 | ||
| 25 | namespace optiling { | 23 | namespace optiling { |
| 26 | namespace arch38 { | 24 | namespace arch38 { |
| @@ -89,11 +87,29 @@ protected: | |||
| 89 | bool GetAndCheckShape(FiaArch38ContextParams& contextKeyParams, PFAShapeInfo& shapeInfo, | 87 | bool GetAndCheckShape(FiaArch38ContextParams& contextKeyParams, PFAShapeInfo& shapeInfo, |
| 90 | const gert::StorageShape* shape, const std::string& sName) const; | 88 | const gert::StorageShape* shape, const std::string& sName) const; |
| 91 | bool GetAndCheckRopeShape(FiaArch38ContextParams& contextKeyParams, PFAShapeInfo& shapeInfo, | 89 | bool GetAndCheckRopeShape(FiaArch38ContextParams& contextKeyParams, PFAShapeInfo& shapeInfo, |
| 92 | - PFAShapeInfo& ropeShapeInfo, const gert::StorageShape* shape, const std::string& sName, | 90 | + PFAShapeInfo& ropeShapeInfo, const gert::StorageShape* shape, |
| 93 | - const std::string& rName) const; | 91 | + const RopeNamePair& names) const; |
| 92 | + bool CheckRopeShapeDim(const FiaArch38ContextParams& contextKeyParams, const gert::StorageShape* shape, | ||
| 93 | + const std::string& sName, const PFAShapeInfo& shapeInfo) const; | ||
| 94 | + bool CheckRopeDimsD(const FiaArch38ContextParams& contextKeyParams, const std::string& rName, | ||
| 95 | + const PFAShapeDims& dims) const; | ||
| 94 | bool SetShape(FiaArch38ContextParams& contextKeyParams, const gert::StorageShape* shape, | 96 | bool SetShape(FiaArch38ContextParams& contextKeyParams, const gert::StorageShape* shape, |
| 95 | - const std::string inputName, int64_t& b, int64_t& n, int64_t& s, int64_t& d, int64_t& h, | 97 | + const std::string inputName, PFAShapeDims& dims) const; |
| 96 | - int64_t& t) const; | 98 | + bool SetShapePA(const FiaArch38ContextParams& contextKeyParams, const gert::StorageShape* shape, |
| 99 | + PFAShapeDims& dims) const; | ||
| 100 | + bool SetShapeBNSD(const gert::StorageShape* shape, PFAShapeDims& dims) const; | ||
| 101 | + bool SetShapeBSH(const FiaArch38ContextParams& contextKeyParams, const gert::StorageShape* shape, | ||
| 102 | + const std::string& inputName, PFAShapeDims& dims) const; | ||
| 103 | + bool SetShapeBSND(const gert::StorageShape* shape, PFAShapeDims& dims) const; | ||
| 104 | + bool SetShapeTND(const FiaArch38ContextParams& contextKeyParams, const gert::StorageShape* shape, | ||
| 105 | + const std::string& inputName, PFAShapeDims& dims) const; | ||
| 106 | + bool CheckShapeSizeLimits(const FiaArch38ContextParams& contextKeyParams, const std::string& sName, | ||
| 107 | + const PFAShapeDims& dims) const; | ||
| 108 | + bool CheckShapeDAndNLimits(const FiaArch38ContextParams& contextKeyParams, const std::string& sName, | ||
| 109 | + const PFAShapeDims& dims) const; | ||
| 110 | + bool CheckRopeShapeConsistency(const FiaArch38ContextParams& contextKeyParams, const std::string& sName, | ||
| 111 | + const std::string& rName, const PFAShapeInfo& shapeInfo, | ||
| 112 | + const PFAShapeDims& dims) const; | ||
| 97 | bool CheckQueryOutParamsConsistency(const FiaArch38ContextParams& contextKeyParams, | 113 | bool CheckQueryOutParamsConsistency(const FiaArch38ContextParams& contextKeyParams, |
| 98 | const gert::StorageShape* queryShape, const gert::StorageShape* outShape) const; | 114 | const gert::StorageShape* queryShape, const gert::StorageShape* outShape) const; |
| 99 | bool CheckKVDataType(FiaArch38ContextParams& contextKeyParams) const; | 115 | bool CheckKVDataType(FiaArch38ContextParams& contextKeyParams) const; |
| @@ -103,40 +119,52 @@ protected: | |||
| 103 | FiaArch38FlashAttentionTilingData& tilingData); | 119 | FiaArch38FlashAttentionTilingData& tilingData); |
| 104 | bool CheckInputDimAndHeadNum(FiaArch38ContextParams& contextKeyParams, const uint32_t nQAttr, | 120 | bool CheckInputDimAndHeadNum(FiaArch38ContextParams& contextKeyParams, const uint32_t nQAttr, |
| 105 | const uint32_t nKVAttr); | 121 | const uint32_t nKVAttr); |
| 106 | - bool CheckPostQuantShape(const FiaArch38ContextParams& contextKeyParams, | 122 | + bool ReadQkvShapeHeadNum(const FiaArch38ContextParams& contextKeyParams, uint32_t& queryShapeHeadNum, |
| 107 | - const gert::StorageShape* quantOffset2Shape, const ge::DataType quantScale2Type, | 123 | + uint32_t& keyShapeHeadNum, uint32_t& valueShapeHeadNum); |
| 108 | - int64_t quantScale2ShapeSize, const PFAShapeInfo& queryShapeInfo, | 124 | + bool CheckPostQuantDtype(const FiaArch38ContextParams& contextKeyParams, |
| 109 | - const PFAShapeInfo& valueShapeInfo) const; | 125 | + const gert::StorageShape* quantOffset2Shape, const ge::DataType quantScale2Type) const; |
| 126 | + bool CheckPostQuantShapeSize(const FiaArch38ContextParams& contextKeyParams, | ||
| 127 | + const gert::StorageShape* quantOffset2Shape, int64_t quantScale2ShapeSize, | ||
| 128 | + const PFAShapeInfo& queryShapeInfo, const PFAShapeInfo& valueShapeInfo) const; | ||
| 110 | bool CheckPostQuantParams(const FiaArch38ContextParams& contextKeyParams, const PFAShapeInfo& queryShapeInfo, | 129 | bool CheckPostQuantParams(const FiaArch38ContextParams& contextKeyParams, const PFAShapeInfo& queryShapeInfo, |
| 111 | const PFAShapeInfo& valueShapeInfo) const; | 130 | const PFAShapeInfo& valueShapeInfo) const; |
| 112 | bool CheckPerTensorQuantParams(const FiaArch38ContextParams& contextKeyParams) const; | 131 | bool CheckPerTensorQuantParams(const FiaArch38ContextParams& contextKeyParams) const; |
| 113 | bool CheckPerblockQuantParams(const FiaArch38ContextParams& contextKeyParams, const PFAShapeInfo& queryShapeInfo, | 132 | bool CheckPerblockQuantParams(const FiaArch38ContextParams& contextKeyParams, const PFAShapeInfo& queryShapeInfo, |
| 114 | const PFAShapeInfo& keyShapeInfo, const PFAShapeInfo& valueShapeInfo) const; | 133 | const PFAShapeInfo& keyShapeInfo, const PFAShapeInfo& valueShapeInfo) const; |
| 134 | + bool CheckPerblockQuantDtype(const FiaArch38ContextParams& contextKeyParams) const; | ||
| 135 | + bool CheckPerblockScaleShape(const FiaArch38ContextParams& contextKeyParams, const PFAShapeInfo& shapeInfo, | ||
| 136 | + const PerblockScaleCheckCtx& ctx) const; | ||
| 115 | bool CheckAntiquantParamsShape(FiaArch38ContextParams& contextKeyParams) const; | 137 | bool CheckAntiquantParamsShape(FiaArch38ContextParams& contextKeyParams) const; |
| 116 | bool GetAndCheckPrefixShape(FiaArch38ContextParams& contextKeyParams, const PFAShapeInfo& queryShapeInfo, | 138 | bool GetAndCheckPrefixShape(FiaArch38ContextParams& contextKeyParams, const PFAShapeInfo& queryShapeInfo, |
| 117 | PFAShapeInfo& prefixShapeInfo, FiaArch38FlashAttentionTilingData& tilingData) const; | 139 | PFAShapeInfo& prefixShapeInfo, FiaArch38FlashAttentionTilingData& tilingData) const; |
| 118 | bool CheckKeyValuePrefixConsistency(FiaArch38ContextParams& contextKeyParams, | 140 | bool CheckKeyValuePrefixConsistency(FiaArch38ContextParams& contextKeyParams, |
| 119 | const gert::StorageShape* keyShape) const; | 141 | const gert::StorageShape* keyShape) const; |
| 120 | bool CheckActSharedPrefix(FiaArch38ContextParams& contextKeyParams, const uint32_t sPrefix, const uint32_t sKV); | 142 | bool CheckActSharedPrefix(FiaArch38ContextParams& contextKeyParams, const uint32_t sPrefix, const uint32_t sKV); |
| 121 | - bool CheckPAKeyValueShape(FiaArch38ContextParams& contextKeyParams, int64_t& keyDim1, | 143 | + bool CheckPAKeyValueDimConsistency(const FiaArch38ContextParams& contextKeyParams, const PACacheDimCtx& ctx) const; |
| 122 | - const PFAShapeInfo& queryShapeInfo, const gert::StorageShape* keyShape, | 144 | + bool CheckPAKeyValueLayout(const FiaArch38ContextParams& contextKeyParams, const PFAShapeInfo& queryShapeInfo, |
| 123 | - const gert::StorageShape* valueShape, const size_t keyDim, const int32_t* blockSize, | 145 | + const PACacheDimCtx& ctx) const; |
| 124 | - int64_t blockNumValid, int32_t headNumRatio) const; | 146 | + bool CheckPACacheShape3D(const FiaArch38ContextParams& contextKeyParams, const PFAShapeInfo& shapeInfo, |
| 125 | - bool CheckPACacheShape(FiaArch38ContextParams& contextKeyParams, const size_t keyDim, const PFAShapeInfo& shapeInfo, | 147 | + const gert::StorageShape* shape, const PACacheDimCtx& ctx, const std::string& sName); |
| 126 | - const gert::StorageShape* shape, const int32_t* blockSize, int64_t blockNumValid, | 148 | + bool CheckPACacheShape4D(const FiaArch38ContextParams& contextKeyParams, const PFAShapeInfo& shapeInfo, |
| 127 | - int32_t headNumRatio, const std::string& sName); | 149 | + const gert::StorageShape* shape, const PACacheDimCtx& ctx, const std::string& sName); |
| 150 | + bool CheckPACacheShape(const FiaArch38ContextParams& contextKeyParams, const PFAShapeInfo& shapeInfo, | ||
| 151 | + const gert::StorageShape* shape, const PACacheDimCtx& ctx, const std::string& sName); | ||
| 152 | + PABlockNumInfo ComputePABlockNumInfo(const FiaArch38ContextParams& contextKeyParams, | ||
| 153 | + const PFAShapeInfo& queryShapeInfo) const; | ||
| 154 | + bool CheckBlockTableDims(const FiaArch38ContextParams& contextKeyParams, const PFAShapeInfo& queryShapeInfo, | ||
| 155 | + int32_t maxBlockNumPerBatch) const; | ||
| 128 | bool CheckBlockTableShape(FiaArch38ContextParams& contextKeyParams, PFAShapeInfo& queryShapeInfo, | 156 | bool CheckBlockTableShape(FiaArch38ContextParams& contextKeyParams, PFAShapeInfo& queryShapeInfo, |
| 129 | - PFAShapeInfo& queryRopeShapeInfo, const int32_t* blockSize, | 157 | + PFAShapeInfo& queryRopeShapeInfo, FiaArch38FlashAttentionTilingData& tilingData); |
| 130 | - const gert::StorageShape* blockTableShape, FiaArch38FlashAttentionTilingData& tilingData); | 158 | + bool CheckPARopeShape(const FiaArch38ContextParams& contextKeyParams, PFAShapeInfo& queryRopeShapeInfo, |
| 131 | - bool CheckMaskShape(FiaArch38ContextParams& contextKeyParams, const int32_t* sparseMode, int64_t& attenMaskBatch, | 159 | + const PACacheDimCtx& ctx); |
| 132 | - int64_t& attenMaskS1, int64_t& attenMaskS2, bool& checkMask, const uint32_t sQ, | 160 | + bool CheckMaskShape(FiaArch38ContextParams& contextKeyParams, MaskShapeInfo& info, const uint32_t sQ, |
| 133 | - const uint32_t sK, const uint32_t batchSize, std::string& strMaskShape) const; | 161 | + const uint32_t sK, const uint32_t batchSize) const; |
| 134 | - void SetSparseModeData(FiaArch38ContextParams& contextKeyParams, const gert::StorageShape* attenMaskShape, | 162 | + void SetSparseModeData(FiaArch38ContextParams& contextKeyParams, FiaArch38FlashAttentionTilingData& tilingData); |
| 135 | - FiaArch38FlashAttentionTilingData& tilingData, const int32_t* sparseMode, | 163 | + void SetAttenMaskBatch(const gert::StorageShape* attenMaskShape, FiaArch38FlashAttentionTilingData& tilingData); |
| 136 | - const int64_t* preTokens, const int64_t* nextTokens); | 164 | + void ClampSparseTokens(const int64_t* preTokens, const int64_t* nextTokens); |
| 137 | - bool CheckMaskShapeCrossSparse(FiaArch38ContextParams& contextKeyParams, const int32_t* sparseMode, uint64_t sQ, | 165 | + bool CheckMaskShapeCrossSparse(FiaArch38ContextParams& contextKeyParams, const int64_t* sparseMode, uint64_t sQ, |
| 138 | const uint64_t sK, const uint32_t batchSize); | 166 | const uint64_t sK, const uint32_t batchSize); |
| 139 | - bool CheckMaskCrossIFAMLA(FiaArch38ContextParams& contextKeyParams, const int32_t* sparseMode, uint64_t queryS); | 167 | + bool CheckMaskCrossIFAMLA(FiaArch38ContextParams& contextKeyParams, const int64_t* sparseMode, uint64_t queryS); |
| 140 | bool CheckIO(FiaArch38ContextParams& contextKeyParams, PFAShapeInfo& queryShapeInfo, PFAShapeInfo& valueShapeInfo); | 168 | bool CheckIO(FiaArch38ContextParams& contextKeyParams, PFAShapeInfo& queryShapeInfo, PFAShapeInfo& valueShapeInfo); |
| 141 | bool CheckKV(FiaArch38ContextParams& contextKeyParams, PFAShapeInfo& keyShapeInfo, | 169 | bool CheckKV(FiaArch38ContextParams& contextKeyParams, PFAShapeInfo& keyShapeInfo, |
| 142 | PFAShapeInfo& valueShapeInfo) const; | 170 | PFAShapeInfo& valueShapeInfo) const; |
| @@ -151,35 +179,69 @@ protected: | |||
| 151 | bool CheckPrefix(FiaArch38ContextParams& contextKeyParams, PFAShapeInfo& queryShapeInfo, PFAShapeInfo& keyShapeInfo, | 179 | bool CheckPrefix(FiaArch38ContextParams& contextKeyParams, PFAShapeInfo& queryShapeInfo, PFAShapeInfo& keyShapeInfo, |
| 152 | FiaArch38FlashAttentionTilingData& tilingData); | 180 | FiaArch38FlashAttentionTilingData& tilingData); |
| 153 | bool CheckActSeq(const FiaArch38ContextParams& contextKeyParams, const PFAShapeInfo& queryShapeInfo) const; | 181 | bool CheckActSeq(const FiaArch38ContextParams& contextKeyParams, const PFAShapeInfo& queryShapeInfo) const; |
| 182 | + bool CheckActSeqMonotonic(const FiaArch38ContextParams& contextKeyParams, const gert::Tensor* actSeqLen, | ||
| 183 | + const gert::Tensor* actSeqLenKV, uint32_t batchSize) const; | ||
| 154 | bool CheckActSeqLen(FiaArch38ContextParams& contextKeyParams, PFAShapeInfo& queryShapeInfo, | 184 | bool CheckActSeqLen(FiaArch38ContextParams& contextKeyParams, PFAShapeInfo& queryShapeInfo, |
| 155 | const PFAShapeInfo& keyShapeInfo); | 185 | const PFAShapeInfo& keyShapeInfo); |
| 186 | + bool CheckActSeqLenQ(FiaArch38ContextParams& contextKeyParams, PFAShapeInfo& queryShapeInfo); | ||
| 187 | + bool CheckActSeqLenKV(FiaArch38ContextParams& contextKeyParams, PFAShapeInfo& queryShapeInfo, | ||
| 188 | + const PFAShapeInfo& keyShapeInfo); | ||
| 156 | bool CheckPATypeAndShape(FiaArch38ContextParams& contextKeyParams, PFAShapeInfo& queryShapeInfo, | 189 | bool CheckPATypeAndShape(FiaArch38ContextParams& contextKeyParams, PFAShapeInfo& queryShapeInfo, |
| 157 | PFAShapeInfo& queryRopeShapeInfo, FiaArch38FlashAttentionTilingData& tilingData); | 190 | PFAShapeInfo& queryRopeShapeInfo, FiaArch38FlashAttentionTilingData& tilingData); |
| 191 | + bool CheckPAType(const FiaArch38ContextParams& contextKeyParams); | ||
| 192 | + bool CheckPABlockSize(const FiaArch38ContextParams& contextKeyParams, const PFAShapeInfo& queryShapeInfo); | ||
| 158 | bool CheckPseShiftTypeAndShape(FiaArch38ContextParams& contextKeyParams, uint32_t b, uint32_t n, uint32_t s1, | 193 | bool CheckPseShiftTypeAndShape(FiaArch38ContextParams& contextKeyParams, uint32_t b, uint32_t n, uint32_t s1, |
| 159 | uint32_t s2); | 194 | uint32_t s2); |
| 195 | + bool CheckPseShiftDtype(const FiaArch38ContextParams& contextKeyParams); | ||
| 196 | + bool CheckPseShiftShapeDim(const FiaArch38ContextParams& contextKeyParams, uint32_t b, uint32_t n, uint32_t s1, | ||
| 197 | + uint32_t s2); | ||
| 160 | bool CheckInnerPrecise(FiaArch38ContextParams& contextKeyParams, FiaArch38FlashAttentionTilingData& tilingData); | 198 | bool CheckInnerPrecise(FiaArch38ContextParams& contextKeyParams, FiaArch38FlashAttentionTilingData& tilingData); |
| 161 | bool CheckMaskTypeAndShape(FiaArch38ContextParams& contextKeyParams, FiaArch38FlashAttentionTilingData& tilingData); | 199 | bool CheckMaskTypeAndShape(FiaArch38ContextParams& contextKeyParams, FiaArch38FlashAttentionTilingData& tilingData); |
| 200 | + bool CheckMaskDtype(const FiaArch38ContextParams& contextKeyParams); | ||
| 162 | void SetSparseType(uint32_t qS); | 201 | void SetSparseType(uint32_t qS); |
| 163 | bool CheckSparseMode(FiaArch38ContextParams& contextKeyParams, uint32_t qS, | 202 | bool CheckSparseMode(FiaArch38ContextParams& contextKeyParams, uint32_t qS, |
| 164 | FiaArch38FlashAttentionTilingData& tilingData); | 203 | FiaArch38FlashAttentionTilingData& tilingData); |
| 204 | + bool CheckSparseTokensRange(const FiaArch38ContextParams& contextKeyParams, uint32_t qS) const; | ||
| 165 | bool CheckPACrossover(FiaArch38ContextParams& contextKeyParams, PFAShapeInfo& queryShapeInfo); | 205 | bool CheckPACrossover(FiaArch38ContextParams& contextKeyParams, PFAShapeInfo& queryShapeInfo); |
| 166 | bool CheckMaskCrossover(FiaArch38ContextParams& contextKeyParams, PFAShapeInfo& queryShapeInfo, | 206 | bool CheckMaskCrossover(FiaArch38ContextParams& contextKeyParams, PFAShapeInfo& queryShapeInfo, |
| 167 | FiaArch38FlashAttentionTilingData& tilingData); | 207 | FiaArch38FlashAttentionTilingData& tilingData); |
| 168 | bool CheckTNDLayoutCrossover(FiaArch38ContextParams& contextKeyParams); | 208 | bool CheckTNDLayoutCrossover(FiaArch38ContextParams& contextKeyParams); |
| 169 | bool ParseActualSeqLengths(FiaArch38ContextParams& contextKeyParams, PFAShapeInfo& queryShapeInfo, | 209 | bool ParseActualSeqLengths(FiaArch38ContextParams& contextKeyParams, PFAShapeInfo& queryShapeInfo, |
| 170 | std::vector<int64_t>& actualSeqLengths, std::vector<int64_t>& actualSeqLengthsKV); | 210 | std::vector<int64_t>& actualSeqLengths, std::vector<int64_t>& actualSeqLengthsKV); |
| 211 | + bool ParseActualSeqLengthsQueryNonTnd(FiaArch38ContextParams& contextKeyParams, PFAShapeInfo& queryShapeInfo, | ||
| 212 | + std::vector<int64_t>& actualSeqLengths, uint32_t i); | ||
| 213 | + void ParseActualSeqLengthsKvNonTnd(FiaArch38ContextParams& contextKeyParams, | ||
| 214 | + std::vector<int64_t>& actualSeqLengthsKV, uint32_t i); | ||
| 171 | bool CheckMultiFeatureCrossover(FiaArch38ContextParams& contextKeyParams, PFAShapeInfo& queryShapeInfo, | 215 | bool CheckMultiFeatureCrossover(FiaArch38ContextParams& contextKeyParams, PFAShapeInfo& queryShapeInfo, |
| 172 | std::vector<int64_t>& actualSeqLengths, std::vector<int64_t>& actualSeqLengthsKV, | 216 | std::vector<int64_t>& actualSeqLengths, std::vector<int64_t>& actualSeqLengthsKV, |
| 173 | FiaArch38FlashAttentionTilingData& tilingData); | 217 | FiaArch38FlashAttentionTilingData& tilingData); |
| 218 | + bool CheckMultiFeatureCrossoverPerBatch(FiaArch38ContextParams& contextKeyParams, | ||
| 219 | + std::vector<int64_t>& actualSeqLengths, | ||
| 220 | + std::vector<int64_t>& actualSeqLengthsKV, | ||
| 221 | + FiaArch38FlashAttentionTilingData& tilingData, uint32_t i); | ||
| 222 | + bool CheckBandModeTokens(const FiaArch38ContextParams& contextKeyParams, | ||
| 223 | + const std::vector<int64_t>& actualSeqLengths, | ||
| 224 | + const std::vector<int64_t>& actualSeqLengthsKV, uint32_t i) const; | ||
| 225 | + void CalcPerbatchTokens(const std::vector<int64_t>& actualSeqLengths, | ||
| 226 | + const std::vector<int64_t>& actualSeqLengthsKV, uint32_t i, int64_t& preTokensPerbatch, | ||
| 227 | + int64_t& nextTokensPerbatch); | ||
| 174 | bool CheckPerblockCrossover(FiaArch38ContextParams& contextKeyParams); | 228 | bool CheckPerblockCrossover(FiaArch38ContextParams& contextKeyParams); |
| 175 | - void SetTilingDataAttribute(FiaArch38ContextParams& contextKeyParams, | 229 | + bool CheckPerblockQuantCrossoverBase(const FiaArch38ContextParams& contextKeyParams) const; |
| 230 | + bool CheckPerblockQuantCrossoverFeature(const FiaArch38ContextParams& contextKeyParams) const; | ||
| 231 | + void SetTilingDataAttribute(const FiaArch38ContextParams& contextKeyParams, | ||
| 176 | FiaArch38FlashAttentionTilingData& tilingData); | 232 | FiaArch38FlashAttentionTilingData& tilingData); |
| 177 | - void GetEnableDN(FiaArch38FlashAttentionTilingData& tilingData, PFAShapeInfo& queryShapeInfo, | 233 | + void GetEnableDN(FiaArch38FlashAttentionTilingData& tilingData, const PFAShapeInfo& queryShapeInfo, |
| 178 | const PFAShapeInfo& valueShapeInfo, std::vector<int64_t>& actualSeqLengths, | 234 | const PFAShapeInfo& valueShapeInfo, std::vector<int64_t>& actualSeqLengths, |
| 179 | std::vector<int64_t>& actualSeqLengthsKV); | 235 | std::vector<int64_t>& actualSeqLengthsKV); |
| 180 | - void SetTilingData(FiaArch38ContextParams& contextKeyParams, PFAShapeInfo& queryShapeInfo, | 236 | + void SetTilingData(const FiaArch38ContextParams& contextKeyParams, const PFAShapeInfo& queryShapeInfo, |
| 181 | - PFAShapeInfo& queryRopeShapeInfo, PFAShapeInfo& valueShapeInfo, | 237 | + const PFAShapeInfo& queryRopeShapeInfo, const PFAShapeInfo& valueShapeInfo, |
| 182 | FiaArch38FlashAttentionTilingData& tilingData); | 238 | FiaArch38FlashAttentionTilingData& tilingData); |
| 239 | + void SetTilingDataTypeByteNum(FiaArch38FlashAttentionTilingData& tilingData); | ||
| 240 | + void SetTilingDataQuantParams(const FiaArch38ContextParams& contextKeyParams, | ||
| 241 | + FiaArch38FlashAttentionTilingData& tilingData); | ||
| 242 | + void SetTilingDataShapeParams(const FiaArch38ContextParams& contextKeyParams, const PFAShapeInfo& queryShapeInfo, | ||
| 243 | + const PFAShapeInfo& queryRopeShapeInfo, const PFAShapeInfo& valueShapeInfo, | ||
| 244 | + FiaArch38FlashAttentionTilingData& tilingData); | ||
| 183 | void InferTilingMod(const FiaArch38ContextParams& contextKeyParams, std::vector<int64_t>& actualSeqLengths, | 245 | void InferTilingMod(const FiaArch38ContextParams& contextKeyParams, std::vector<int64_t>& actualSeqLengths, |
| 184 | std::vector<int64_t>& actualSeqLengthsKV, uint32_t actualSeqArrayLen, uint32_t d); | 246 | std::vector<int64_t>& actualSeqLengthsKV, uint32_t actualSeqArrayLen, uint32_t d); |
| 185 | int64_t GetSInnerBlockNums(int64_t sInnerIndexStart, int64_t sInnerIndexEnd, int64_t innerBlockNums) const; | 247 | int64_t GetSInnerBlockNums(int64_t sInnerIndexStart, int64_t sInnerIndexEnd, int64_t innerBlockNums) const; |
| @@ -191,26 +253,35 @@ protected: | |||
| 191 | uint32_t sInnerSize, int64_t preTokensLeftUp, int64_t nextTokensLeftUp, | 253 | uint32_t sInnerSize, int64_t preTokensLeftUp, int64_t nextTokensLeftUp, |
| 192 | bool isAttenMaskUsed) const; | 254 | bool isAttenMaskUsed) const; |
| 193 | void ComputeSplitNBSeq(FiaArch38FlashAttentionTilingData& tilingData, uint32_t batchSize, | 255 | void ComputeSplitNBSeq(FiaArch38FlashAttentionTilingData& tilingData, uint32_t batchSize, |
| 194 | - const size_t tilingElementArrayLen, std::vector<int64_t>& actualSeqLengths, | 256 | + const size_t tilingElementArrayLen, SplitNBSeqCtx& ctx); |
| 195 | - std::vector<int64_t>& actualSeqLengthsKV, uint32_t sOuterSize, uint32_t sInnerSize, | 257 | + void SplitOneHeadBlock(FiaArch38FlashAttentionTilingData& tilingData, uint32_t sIdx, uint32_t headNum, |
| 196 | - double coreWightTarget, uint32_t& curCore); | 258 | + uint32_t headNumSize, SplitNBSeqCtx& ctx) const; |
| 197 | void FiaArch38SplitNBSeq(FiaArch38FlashAttentionTilingData& tilingData, std::vector<int64_t>& actualSeqLengths, | 259 | void FiaArch38SplitNBSeq(FiaArch38FlashAttentionTilingData& tilingData, std::vector<int64_t>& actualSeqLengths, |
| 198 | std::vector<int64_t>& actualSeqLengthsKV, bool isAttenMaskUsed); | 260 | std::vector<int64_t>& actualSeqLengthsKV, bool isAttenMaskUsed); |
| 199 | void InferSplitCoreMode(); | 261 | void InferSplitCoreMode(); |
| 200 | void InferConstantization(); | 262 | void InferConstantization(); |
| 201 | - bool AdjustCVTilingCVDiff(const FiaArch38ContextParams& contextKeyParams, uint32_t& sOuterFactor, | 263 | + bool AdjustCVTilingCVDiff(const FiaArch38ContextParams& contextKeyParams, CVFactorParams& cvFactors, |
| 202 | - uint32_t& sInnerFactor, uint32_t& softmaxSOuterFactor, | ||
| 203 | FiaArch38FlashAttentionTilingData& tilingData, const PFAShapeInfo& queryShapeInfo); | 264 | FiaArch38FlashAttentionTilingData& tilingData, const PFAShapeInfo& queryShapeInfo); |
| 265 | + bool AdjustCVFactorForSmallS(const FiaArch38ContextParams& contextKeyParams, const PFAShapeInfo& queryShapeInfo, | ||
| 266 | + FiaArch38FlashAttentionTilingData& tilingData, CVFactorParams& cvFactors) const; | ||
| 267 | + bool AdjustCVFactorForLargeD(CVFactorParams& cvFactors) const; | ||
| 268 | + bool AdjustCVFactorForIFAMLA(CVFactorParams& cvFactors) const; | ||
| 204 | void GetMatMulType(matmul_tiling::DataType& mmInputType, matmul_tiling::DataType& mmOutputType) const; | 269 | void GetMatMulType(matmul_tiling::DataType& mmInputType, matmul_tiling::DataType& mmOutputType) const; |
| 205 | bool EnableMTE2BmmPipe(FiaArch38FlashAttentionTilingData& tilingData, matmul_tiling::MatmulApiTiling& bmm, | 270 | bool EnableMTE2BmmPipe(FiaArch38FlashAttentionTilingData& tilingData, matmul_tiling::MatmulApiTiling& bmm, |
| 206 | TCubeTiling& bmmTilingData, uint32_t sOuterFactor, uint32_t sInnerFactor) const; | 271 | TCubeTiling& bmmTilingData, uint32_t sOuterFactor, uint32_t sInnerFactor) const; |
| 207 | void EnableBmmDoubleBuffer(TCubeTiling& bmmTilingData) const; | 272 | void EnableBmmDoubleBuffer(TCubeTiling& bmmTilingData) const; |
| 208 | bool FiaArch38CheckBmm1(FiaArch38FlashAttentionTilingData& tilingData, TCubeTiling& bmm1TilingData, | 273 | bool FiaArch38CheckBmm1(FiaArch38FlashAttentionTilingData& tilingData, TCubeTiling& bmm1TilingData, |
| 209 | - int64_t l1SizeRemain, int64_t l0CSize, uint32_t sOuterFactor, uint32_t sInnerFactor, | 274 | + BmmShapeCtx& bmmCtx, bool autoBaseMNK = false); |
| 210 | - bool autoBaseMNK = false); | 275 | + void SetBmm1OrgShape(matmul_tiling::MatmulApiTiling& bmm1, FiaArch38FlashAttentionTilingData& tilingData, |
| 276 | + int32_t strideQ, int32_t strideK) const; | ||
| 277 | + bool Bmm1SetTiling(matmul_tiling::MatmulApiTiling& bmm1, TCubeTiling& bmm1TilingData, const BmmShapeCtx& bmmCtx, | ||
| 278 | + bool autoBaseMNK) const; | ||
| 211 | bool FiaArch38CheckBmm2(FiaArch38FlashAttentionTilingData& tilingData, TCubeTiling& bmm2TilingData, | 279 | bool FiaArch38CheckBmm2(FiaArch38FlashAttentionTilingData& tilingData, TCubeTiling& bmm2TilingData, |
| 212 | - int64_t l1SizeRemain, int64_t l0CSize, uint32_t sOuterFactor, uint32_t sInnerFactor, | 280 | + BmmShapeCtx& bmmCtx); |
| 213 | - uint32_t dSplitFactor, bool autoBaseMNK = false); | 281 | + void SetBmm2OrgShape(matmul_tiling::MatmulApiTiling& bmm2, FiaArch38FlashAttentionTilingData& tilingData, |
| 282 | + int32_t strideV) const; | ||
| 283 | + bool Bmm2GetTiling(matmul_tiling::MatmulApiTiling& bmm2, TCubeTiling& bmm2TilingData, | ||
| 284 | + FiaArch38FlashAttentionTilingData& tilingData, const BmmShapeCtx& bmmCtx) const; | ||
| 214 | bool FiaArch38ComputeCVDiffParams(FiaArch38FlashAttentionTilingData& tilingData, int64_t l1Size, int64_t l0CSize, | 285 | bool FiaArch38ComputeCVDiffParams(FiaArch38FlashAttentionTilingData& tilingData, int64_t l1Size, int64_t l0CSize, |
| 215 | uint32_t& sOuterFactor, uint32_t& sInnerFactor); | 286 | uint32_t& sOuterFactor, uint32_t& sInnerFactor); |
| 216 | void GetPreNextTokensLeftUp(FiaArch38FlashAttentionTilingData& tilingData, int64_t actualSeqLength, | 287 | void GetPreNextTokensLeftUp(FiaArch38FlashAttentionTilingData& tilingData, int64_t actualSeqLength, |
| @@ -226,21 +297,36 @@ protected: | |||
| 226 | void UpdateTilingKeyFlag(const FiaArch38ContextParams& contextKeyParams, uint64_t& tilingKey) const; | 297 | void UpdateTilingKeyFlag(const FiaArch38ContextParams& contextKeyParams, uint64_t& tilingKey) const; |
| 227 | bool TilingGetTilingKeyAttentionAscendC(uint64_t& tilingKey, FiaArch38ContextParams& contextKeyParams, | 298 | bool TilingGetTilingKeyAttentionAscendC(uint64_t& tilingKey, FiaArch38ContextParams& contextKeyParams, |
| 228 | FiaArch38FlashAttentionTilingData& tilingData); | 299 | FiaArch38FlashAttentionTilingData& tilingData); |
| 300 | + void AddTilingKeyDtypeAndLayout(const FiaArch38ContextParams& contextKeyParams, uint64_t& tilingKey) const; | ||
| 301 | + void AddTilingKeyFeatureFlag(uint64_t& tilingKey) const; | ||
| 229 | size_t GetPFAWorkSpaceSize(FiaArch38FlashAttentionTilingData& tilingData); | 302 | size_t GetPFAWorkSpaceSize(FiaArch38FlashAttentionTilingData& tilingData); |
| 303 | + uint32_t ComputeDvBasicBlock(uint32_t dSize) const; | ||
| 230 | ge::graphStatus SetPlatMemoryInfo(FiaArch38ContextParams& contextKeyParams); | 304 | ge::graphStatus SetPlatMemoryInfo(FiaArch38ContextParams& contextKeyParams); |
| 231 | ge::graphStatus SetAttributeInfo(FiaArch38ContextParams& contextKeyParams); | 305 | ge::graphStatus SetAttributeInfo(FiaArch38ContextParams& contextKeyParams); |
| 306 | + ge::graphStatus SetAntiquantAndQuantInfo(FiaArch38ContextParams& contextKeyParams); | ||
| 307 | + ge::graphStatus SetPrefixAndSeqInfo(FiaArch38ContextParams& contextKeyParams); | ||
| 308 | + ge::graphStatus SetPseAndMaskInfo(FiaArch38ContextParams& contextKeyParams); | ||
| 232 | ge::graphStatus CheckTensorInvalid(const FiaArch38ContextParams& contextKeyParams) const; | 309 | ge::graphStatus CheckTensorInvalid(const FiaArch38ContextParams& contextKeyParams) const; |
| 233 | ge::graphStatus CheckRopeInvalid(const FiaArch38ContextParams& contextKeyParams) const; | 310 | ge::graphStatus CheckRopeInvalid(const FiaArch38ContextParams& contextKeyParams) const; |
| 234 | - ge::graphStatus CheckSingleAttribute(FiaArch38ContextParams& contextKeyParams, PFAShapeInfo& queryShapeInfo, | 311 | + ge::graphStatus CheckSingleAttribute(FiaArch38ContextParams& contextKeyParams, PFAShapeSet& shapes, |
| 235 | - PFAShapeInfo& keyShapeInfo, PFAShapeInfo& valueShapeInfo, | ||
| 236 | - PFAShapeInfo& queryRopeShapeInfo, | ||
| 237 | FiaArch38FlashAttentionTilingData& tilingData); | 312 | FiaArch38FlashAttentionTilingData& tilingData); |
| 313 | + ge::graphStatus CheckShapeStage(FiaArch38ContextParams& contextKeyParams, PFAShapeSet& shapes, | ||
| 314 | + FiaArch38FlashAttentionTilingData& tilingData); | ||
| 315 | + ge::graphStatus CheckFeatureStage(FiaArch38ContextParams& contextKeyParams, PFAShapeSet& shapes, | ||
| 316 | + FiaArch38FlashAttentionTilingData& tilingData); | ||
| 317 | + ge::graphStatus CheckBaseFeatureStage(FiaArch38ContextParams& contextKeyParams, PFAShapeSet& shapes, | ||
| 318 | + FiaArch38FlashAttentionTilingData& tilingData); | ||
| 319 | + ge::graphStatus CheckPAStage(FiaArch38ContextParams& contextKeyParams, PFAShapeSet& shapes, | ||
| 320 | + FiaArch38FlashAttentionTilingData& tilingData); | ||
| 321 | + ge::graphStatus CheckPseShiftStage(FiaArch38ContextParams& contextKeyParams, PFAShapeSet& shapes); | ||
| 238 | ge::graphStatus CheckCrossoverAttribute(FiaArch38ContextParams& contextKeyParams, PFAShapeInfo& queryShapeInfo, | 322 | ge::graphStatus CheckCrossoverAttribute(FiaArch38ContextParams& contextKeyParams, PFAShapeInfo& queryShapeInfo, |
| 239 | std::vector<int64_t>& actualSeqLengths, | 323 | std::vector<int64_t>& actualSeqLengths, |
| 240 | std::vector<int64_t>& actualSeqLengthsKV, | 324 | std::vector<int64_t>& actualSeqLengthsKV, |
| 241 | FiaArch38FlashAttentionTilingData& tilingData); | 325 | FiaArch38FlashAttentionTilingData& tilingData); |
| 242 | ge::graphStatus AdjustTilingData(FiaArch38ContextParams& contextKeyParams, | 326 | ge::graphStatus AdjustTilingData(FiaArch38ContextParams& contextKeyParams, |
| 243 | FiaArch38FlashAttentionTilingData& tilingData, const PFAShapeInfo& queryShapeInfo); | 327 | FiaArch38FlashAttentionTilingData& tilingData, const PFAShapeInfo& queryShapeInfo); |
| 328 | + ge::graphStatus CheckFp16TilingKeySupport(const FiaArch38ContextParams& contextKeyParams, uint32_t sOuterFactor, | ||
| 329 | + const PFAShapeInfo& queryShapeInfo) const; | ||
| 244 | ge::graphStatus ComputeTilingData(FiaArch38ContextParams& contextKeyParams, std::vector<int64_t>& actualSeqLengths, | 330 | ge::graphStatus ComputeTilingData(FiaArch38ContextParams& contextKeyParams, std::vector<int64_t>& actualSeqLengths, |
| 245 | std::vector<int64_t>& actualSeqLengthsKV, | 331 | std::vector<int64_t>& actualSeqLengthsKV, |
| 246 | FiaArch38FlashAttentionTilingData& tilingData); | 332 | FiaArch38FlashAttentionTilingData& tilingData); |
| @@ -249,6 +335,10 @@ protected: | |||
| 249 | void SetAttenMaskCompressMode(); | 335 | void SetAttenMaskCompressMode(); |
| 250 | void SetLayoutType(); | 336 | void SetLayoutType(); |
| 251 | void PFATilingDataconvert(FiaArch38FlashAttentionTilingData& tilingData); | 337 | void PFATilingDataconvert(FiaArch38FlashAttentionTilingData& tilingData); |
| 338 | + void SetInputParamsShape(FiaArch38FlashAttentionTilingData& tilingData); | ||
| 339 | + void SetInputParamsPseAndMask(FiaArch38FlashAttentionTilingData& tilingData); | ||
| 340 | + void SetInputParamsPA(FiaArch38FlashAttentionTilingData& tilingData); | ||
| 341 | + void SetInitOutputParams(FiaArch38FlashAttentionTilingData& tilingData); | ||
| 252 | void SetMultiCoreParamsRegbase(int64_t totalSize, int64_t actualUsedCoreNum); | 342 | void SetMultiCoreParamsRegbase(int64_t totalSize, int64_t actualUsedCoreNum); |
| 253 | bool IsFlashDecode() const; | 343 | bool IsFlashDecode() const; |
| 254 | ge::graphStatus SplitBNS(FiaArch38FlashAttentionTilingData& tilingData, uint64_t bng); | 344 | ge::graphStatus SplitBNS(FiaArch38FlashAttentionTilingData& tilingData, uint64_t bng); |
| @@ -260,6 +350,11 @@ protected: | |||
| 260 | const PFAShapeInfo& queryShapeInfo, const PFAShapeInfo& valueShapeInfo) const; | 350 | const PFAShapeInfo& queryShapeInfo, const PFAShapeInfo& valueShapeInfo) const; |
| 261 | bool CheckArch38ScenarioSupported(const FiaArch38ContextParams& contextKeyParams, | 351 | bool CheckArch38ScenarioSupported(const FiaArch38ContextParams& contextKeyParams, |
| 262 | const PFAShapeInfo& queryShapeInfo, const PFAShapeInfo& valueShapeInfo) const; | 352 | const PFAShapeInfo& queryShapeInfo, const PFAShapeInfo& valueShapeInfo) const; |
| 353 | + bool CheckArch38UnsupportedFeatures(const FiaArch38ContextParams& contextKeyParams) const; | ||
| 354 | + bool CheckArch38ShapeConstraints(const FiaArch38ContextParams& contextKeyParams, | ||
| 355 | + const PFAShapeInfo& queryShapeInfo) const; | ||
| 356 | + bool CheckArch38HeadAndSparse(const FiaArch38ContextParams& contextKeyParams) const; | ||
| 357 | + bool CheckArch38MaskConstraints(const FiaArch38ContextParams& contextKeyParams) const; | ||
| 263 | 358 | ||
| 264 | protected: | 359 | protected: |
| 265 | gert::TilingContext* context_ = nullptr; | 360 | gert::TilingContext* context_ = nullptr; |
| @@ -304,9 +399,9 @@ protected: | |||
| 304 | ge::DataType queryType{ge::DT_FLOAT}; | 399 | ge::DataType queryType{ge::DT_FLOAT}; |
| 305 | ge::DataType keyType{ge::DT_FLOAT}; | 400 | ge::DataType keyType{ge::DT_FLOAT}; |
| 306 | ge::DataType valueType{ge::DT_FLOAT}; | 401 | ge::DataType valueType{ge::DT_FLOAT}; |
| 307 | - uint32_t dataTypeSize = FLOAT32SIZE; | 402 | + uint32_t dataTypeSize = FLOAT32_SIZE; |
| 308 | - uint32_t outputDataTypeSize = FLOAT32SIZE; | 403 | + uint32_t outputDataTypeSize = FLOAT32_SIZE; |
| 309 | - uint32_t maskElemSize = FLOAT32SIZE; | 404 | + uint32_t maskElemSize = FLOAT32_SIZE; |
| 310 | int32_t ifaBlockSizeBase = 32; | 405 | int32_t ifaBlockSizeBase = 32; |
| 311 | uint32_t coreNum = 0; | 406 | uint32_t coreNum = 0; |
| 312 | uint32_t aivNum = 0; | 407 | uint32_t aivNum = 0; |
| @@ -331,7 +426,7 @@ protected: | |||
| 331 | int32_t blockTableDim2 = 1; | 426 | int32_t blockTableDim2 = 1; |
| 332 | int32_t paBlockNumSum = 1; | 427 | int32_t paBlockNumSum = 1; |
| 333 | uint32_t maskTypeByteNum = 0; | 428 | uint32_t maskTypeByteNum = 0; |
| 334 | - uint32_t softmaxDataTypeSize = FLOAT32SIZE; // BF16 calculates through FP32 | 429 | + uint32_t softmaxDataTypeSize = FLOAT32_SIZE; // BF16 calculates through FP32 |
| 335 | platform_ascendc::SocVersion curShortSocName; | 430 | platform_ascendc::SocVersion curShortSocName; |
| 336 | uint32_t layoutType = 0; | 431 | uint32_t layoutType = 0; |
| 337 | uint32_t paLayoutType = 0; | 432 | uint32_t paLayoutType = 0; |
| @@ -340,6 +435,7 @@ protected: | |||
| 340 | int32_t sparseModeVal = 0; | 435 | int32_t sparseModeVal = 0; |
| 341 | int64_t maxActualseqKV = 0; | 436 | int64_t maxActualseqKV = 0; |
| 342 | SplitCoreMode splitCoreMode = SplitCoreMode::SPLIT_NBS_VECTOR; | 437 | SplitCoreMode splitCoreMode = SplitCoreMode::SPLIT_NBS_VECTOR; |
| 438 | + uint32_t cvRatio = 2U; // Vector : Cube | ||
| 343 | bool isConstantization = false; | 439 | bool isConstantization = false; |
| 344 | uint32_t splitS2 = 1; // It can only be 0 when the D axis is split | 440 | uint32_t splitS2 = 1; // It can only be 0 when the D axis is split |
| 345 | int32_t innerPrecise = HIGH_PERFORMANCE; | 441 | int32_t innerPrecise = HIGH_PERFORMANCE; |
| @@ -0,0 +1,238 @@ | |||
| 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 | +#include "fia_arch38_prompt_tiling.h" | ||
| 12 | + | ||
| 13 | +using namespace ge; | ||
| 14 | +using namespace AscendC; | ||
| 15 | +using namespace matmul_tiling; | ||
| 16 | +namespace optiling { | ||
| 17 | + | ||
| 18 | +namespace arch38 { | ||
| 19 | + | ||
| 20 | +bool FiaArch38PromptTiling::CheckPostQuantDtype(const FiaArch38ContextParams& contextKeyParams, | ||
| 21 | + const gert::StorageShape* quantOffset2Shape, | ||
| 22 | + const ge::DataType quantScale2Type) const | ||
| 23 | +{ | ||
| 24 | + OP_CHECK_IF( | ||
| 25 | + (quantOffset2Shape != nullptr) && (quantScale2Type != contextKeyParams.quantOffset2Type), | ||
| 26 | + OP_LOGE_FOR_INVALID_DTYPES_WITH_REASON( | ||
| 27 | + contextKeyParams.opName, "quant_scale2, quant_offset2", | ||
| 28 | + (GetPfaDataTypeStr(quantScale2Type) + ", " + GetPfaDataTypeStr(contextKeyParams.quantOffset2Type)).c_str(), | ||
| 29 | + "post quant scale and offset must have the same data type"), | ||
| 30 | + return false); | ||
| 31 | + | ||
| 32 | + OP_CHECK_IF((quantScale2Type != ge::DT_BF16) && (quantScale2Type != ge::DT_FLOAT), | ||
| 33 | + OP_LOGE_FOR_INVALID_DTYPE_WITH_REASON(contextKeyParams.opName, "quant_scale2", | ||
| 34 | + GetPfaDataTypeStr(quantScale2Type).c_str(), | ||
| 35 | + "post quant scale dtype only supports bf16 and fp32"), | ||
| 36 | + return false); | ||
| 37 | + | ||
| 38 | + if (inputType == ge::DT_BF16) { | ||
| 39 | + OP_CHECK_IF(quantScale2Type != ge::DT_FLOAT && quantScale2Type != ge::DT_BF16, | ||
| 40 | + OP_LOGE_FOR_INVALID_DTYPE_WITH_REASON( | ||
| 41 | + contextKeyParams.opName, "quant_scale2", GetPfaDataTypeStr(quantScale2Type).c_str(), | ||
| 42 | + "post quant scale dtype only supports float32 and bf16 for this query dtype"), | ||
| 43 | + return false); | ||
| 44 | + } else { | ||
| 45 | + OP_CHECK_IF(quantScale2Type != ge::DT_FLOAT, | ||
| 46 | + OP_LOGE_FOR_INVALID_DTYPE_WITH_REASON( | ||
| 47 | + contextKeyParams.opName, "quant_scale2", GetPfaDataTypeStr(quantScale2Type).c_str(), | ||
| 48 | + "post quant scale dtype only supports float32 for this query dtype"), | ||
| 49 | + return false); | ||
| 50 | + } | ||
| 51 | + return true; | ||
| 52 | +} | ||
| 53 | + | ||
| 54 | +bool FiaArch38PromptTiling::CheckPostQuantShapeSize(const FiaArch38ContextParams& contextKeyParams, | ||
| 55 | + const gert::StorageShape* quantOffset2Shape, | ||
| 56 | + int64_t quantScale2ShapeSize, const PFAShapeInfo& queryShapeInfo, | ||
| 57 | + const PFAShapeInfo& valueShapeInfo) const | ||
| 58 | +{ | ||
| 59 | + if (quantOffset2Shape != nullptr) { | ||
| 60 | + int64_t quantOffset2ShapeSize = quantOffset2Shape->GetStorageShape().GetShapeSize(); | ||
| 61 | + OP_CHECK_IF(quantScale2ShapeSize != quantOffset2ShapeSize, | ||
| 62 | + OP_LOGE_FOR_INVALID_SHAPESIZES_WITH_REASON( | ||
| 63 | + contextKeyParams.opName, "quant_scale2, quant_offset2", | ||
| 64 | + (std::to_string(quantScale2ShapeSize) + ", " + std::to_string(quantOffset2ShapeSize)).c_str(), | ||
| 65 | + "quant_scale2 and quant_offset2 dimension multiply results must be equal"), | ||
| 66 | + return false); | ||
| 67 | + } | ||
| 68 | + | ||
| 69 | + uint64_t quantScale2ShapeSizePerChannel = | ||
| 70 | + static_cast<uint64_t>(queryShapeInfo.n) * static_cast<uint64_t>(valueShapeInfo.d); | ||
| 71 | + OP_CHECK_IF( | ||
| 72 | + quantScale2ShapeSize < 0 || ((static_cast<uint64_t>(quantScale2ShapeSize) != 1U) && | ||
| 73 | + (static_cast<uint64_t>(quantScale2ShapeSize) != quantScale2ShapeSizePerChannel)), | ||
| 74 | + OP_LOGE_FOR_INVALID_SHAPESIZE_WITH_REASON( | ||
| 75 | + contextKeyParams.opName, "quant_scale2, quant_offset2", std::to_string(quantScale2ShapeSize).c_str(), | ||
| 76 | + "post quant scale2/offset2 dimension multiply result only supports 1 or qN * vD"), | ||
| 77 | + return false); | ||
| 78 | + return true; | ||
| 79 | +} | ||
| 80 | + | ||
| 81 | +bool FiaArch38PromptTiling::CheckPerTensorQuantParams(const FiaArch38ContextParams& contextKeyParams) const | ||
| 82 | +{ | ||
| 83 | + const gert::StorageShape* deqScale1Shape = contextKeyParams.deqScale1Shape; | ||
| 84 | + const gert::StorageShape* quantScale1Shape = contextKeyParams.scale1Shape; | ||
| 85 | + const gert::StorageShape* deqScale2Shape = contextKeyParams.deqScale2Shape; | ||
| 86 | + const ge::DataType inputParamsType = contextKeyParams.inputDataType; | ||
| 87 | + OP_CHECK_IF((inputParamsType != ge::DT_INT8) && (inputParamsType != ge::DT_HIFLOAT8) && | ||
| 88 | + (inputParamsType != ge::DT_FLOAT8_E4M3FN), | ||
| 89 | + OP_LOGE_FOR_INVALID_DTYPE_WITH_REASON( | ||
| 90 | + contextKeyParams.opName, "query", GetPfaDataTypeStr(contextKeyParams.inputDataType).c_str(), | ||
| 91 | + "input type must be INT8, HIFLOAT8 or FLOAT8_E4M3FN in per-tensor quant scenario"), | ||
| 92 | + return false); | ||
| 93 | + OP_CHECK_IF((deqScale1Shape == nullptr) || (quantScale1Shape == nullptr) || (deqScale2Shape == nullptr), | ||
| 94 | + OP_LOGE_WITH_INVALID_INPUT(contextKeyParams.opName, "deq_scale1, quant_scale1, deq_scale2"), | ||
| 95 | + return false); | ||
| 96 | + OP_CHECK_IF( | ||
| 97 | + (deqScale1Shape != nullptr && deqScale1Shape->GetStorageShape().GetShapeSize() == 0) || | ||
| 98 | + (quantScale1Shape != nullptr && quantScale1Shape->GetStorageShape().GetShapeSize() == 0) || | ||
| 99 | + (deqScale2Shape != nullptr && deqScale2Shape->GetStorageShape().GetShapeSize() == 0), | ||
| 100 | + OP_LOGE_FOR_INVALID_SHAPESIZES_WITH_REASON(contextKeyParams.opName, "deq_scale1, quant_scale1, deq_scale2", "0", | ||
| 101 | + "quant scale inputs must not be empty in per-tensor quant scenario"), | ||
| 102 | + return false); | ||
| 103 | + return true; | ||
| 104 | +} | ||
| 105 | + | ||
| 106 | +bool FiaArch38PromptTiling::CheckPerblockQuantParams(const FiaArch38ContextParams& contextKeyParams, | ||
| 107 | + const PFAShapeInfo& queryShapeInfo, | ||
| 108 | + const PFAShapeInfo& keyShapeInfo, | ||
| 109 | + const PFAShapeInfo& valueShapeInfo) const | ||
| 110 | +{ | ||
| 111 | + if (!CheckPerblockQuantDtype(contextKeyParams)) { | ||
| 112 | + return false; | ||
| 113 | + } | ||
| 114 | + PerblockScaleCheckCtx dequantCtx = { | ||
| 115 | + contextKeyParams.dequantScaleQueryShape, 128U, "dequant_scale_query", | ||
| 116 | + "dequantScaleQueryShape must be (b, n1, ceil(s1/128), 1) in per-block quant scenario"}; | ||
| 117 | + if (!CheckPerblockScaleShape(contextKeyParams, queryShapeInfo, dequantCtx)) { | ||
| 118 | + return false; | ||
| 119 | + } | ||
| 120 | + PerblockScaleCheckCtx keyCtx = { | ||
| 121 | + contextKeyParams.keyAntiquantScaleShape, 256U, "key_antiquant_scale", | ||
| 122 | + "keyAntiquantScaleShape must be (b, n2, ceil(s2/256), 1) in per-block quant scenario"}; | ||
| 123 | + if (!CheckPerblockScaleShape(contextKeyParams, keyShapeInfo, keyCtx)) { | ||
| 124 | + return false; | ||
| 125 | + } | ||
| 126 | + PerblockScaleCheckCtx valueCtx = { | ||
| 127 | + contextKeyParams.valueAntiquantScaleShape, 256U, "value_antiquant_scale", | ||
| 128 | + "valueAntiquantScaleShape must be (b, n2, ceil(s2/256), 1) when enablePertensorQuant is true"}; | ||
| 129 | + if (!CheckPerblockScaleShape(contextKeyParams, valueShapeInfo, valueCtx)) { | ||
| 130 | + return false; | ||
| 131 | + } | ||
| 132 | + return true; | ||
| 133 | +} | ||
| 134 | + | ||
| 135 | +bool FiaArch38PromptTiling::CheckPerblockQuantDtype(const FiaArch38ContextParams& contextKeyParams) const | ||
| 136 | +{ | ||
| 137 | + const ge::DataType dequantScaleQueryType = contextKeyParams.dequantScaleQueryType; | ||
| 138 | + const ge::DataType keyAntiquantScaleType = contextKeyParams.keyAntiquantScaleType; | ||
| 139 | + const ge::DataType valueAntiquantScaleType = contextKeyParams.valueAntiquantScaleType; | ||
| 140 | + OP_CHECK_IF( | ||
| 141 | + (contextKeyParams.inputDataType != ge::DT_HIFLOAT8) && (contextKeyParams.inputDataType != ge::DT_FLOAT8_E4M3FN), | ||
| 142 | + OP_LOGE_FOR_INVALID_DTYPE_WITH_REASON( | ||
| 143 | + contextKeyParams.opName, "query", GetPfaDataTypeStr(inputType).c_str(), | ||
| 144 | + "input type must be HIFLOAT8 or FLOAT8_E4M3FN in per-block quant scenario"), | ||
| 145 | + return false); | ||
| 146 | + OP_CHECK_IF((dequantScaleQueryType != ge::DT_FLOAT) || (keyAntiquantScaleType != ge::DT_FLOAT) || | ||
| 147 | + (valueAntiquantScaleType != ge::DT_FLOAT), | ||
| 148 | + OP_LOGE_FOR_INVALID_DTYPES_WITH_REASON( | ||
| 149 | + contextKeyParams.opName, "dequant_scale_query, key_antiquant_scale, value_antiquant_scale", | ||
| 150 | + (GetPfaDataTypeStr(dequantScaleQueryType) + ", " + GetPfaDataTypeStr(keyAntiquantScaleType) + ", " + | ||
| 151 | + GetPfaDataTypeStr(valueAntiquantScaleType)) | ||
| 152 | + .c_str(), | ||
| 153 | + "dequant scale type must be DT_FLOAT in per-block quant scenario"), | ||
| 154 | + return false); | ||
| 155 | + OP_CHECK_IF((contextKeyParams.dequantScaleQueryShape == nullptr) || | ||
| 156 | + (contextKeyParams.keyAntiquantScaleShape == nullptr) || | ||
| 157 | + (contextKeyParams.valueAntiquantScaleShape == nullptr), | ||
| 158 | + OP_LOGE_WITH_INVALID_INPUT(contextKeyParams.opName, | ||
| 159 | + "dequant_scale_query, key_antiquant_scale, value_antiquant_scale"), | ||
| 160 | + return false); | ||
| 161 | + return true; | ||
| 162 | +} | ||
| 163 | + | ||
| 164 | +bool FiaArch38PromptTiling::CheckPerblockScaleShape(const FiaArch38ContextParams& contextKeyParams, | ||
| 165 | + const PFAShapeInfo& shapeInfo, | ||
| 166 | + const PerblockScaleCheckCtx& ctx) const | ||
| 167 | +{ | ||
| 168 | + OP_CHECK_IF( | ||
| 169 | + (ctx.scaleShape->GetStorageShape().GetDim(0) != shapeInfo.b) || | ||
| 170 | + (ctx.scaleShape->GetStorageShape().GetDim(1) != shapeInfo.n) || | ||
| 171 | + (ctx.scaleShape->GetStorageShape().GetDim(2) != | ||
| 172 | + CeilDivision(shapeInfo.s, static_cast<uint64_t>(ctx.blockSize))) || // 2 is the dim of scale along s. | ||
| 173 | + (ctx.scaleShape->GetStorageShape().GetDim(3) != 1), // 3 is the dim of scale along d. | ||
| 174 | + OP_LOGE_FOR_INVALID_SHAPE_WITH_REASON(contextKeyParams.opName, ctx.paramName.c_str(), | ||
| 175 | + ("[" + std::to_string(ctx.scaleShape->GetStorageShape().GetDim(0)) + | ||
| 176 | + ", " + std::to_string(ctx.scaleShape->GetStorageShape().GetDim(1)) + | ||
| 177 | + ", " + std::to_string(ctx.scaleShape->GetStorageShape().GetDim(2)) + | ||
| 178 | + ", " + std::to_string(ctx.scaleShape->GetStorageShape().GetDim(3)) + "]") | ||
| 179 | + .c_str(), | ||
| 180 | + ctx.expectShape.c_str()), | ||
| 181 | + return false); | ||
| 182 | + return true; | ||
| 183 | +} | ||
| 184 | + | ||
| 185 | +bool FiaArch38PromptTiling::CheckPostQuantParams(const FiaArch38ContextParams& contextKeyParams, | ||
| 186 | + const PFAShapeInfo& queryShapeInfo, | ||
| 187 | + const PFAShapeInfo& valueShapeInfo) const | ||
| 188 | +{ | ||
| 189 | + const gert::StorageShape* quantScale2Shape = contextKeyParams.scale2Shape; | ||
| 190 | + const gert::StorageShape* quantOffset2Shape = contextKeyParams.offset2Shape; | ||
| 191 | + const ge::DataType quantScale2Type = contextKeyParams.quantScale2Type; | ||
| 192 | + uint64_t h = queryShapeInfo.h; | ||
| 193 | + uint64_t n = queryShapeInfo.n; | ||
| 194 | + | ||
| 195 | + int64_t quantScale2ShapeSize = 0; | ||
| 196 | + uint32_t quantD = 0; | ||
| 197 | + uint64_t queryD = h / n; | ||
| 198 | + | ||
| 199 | + OP_CHECK_IF(outputType != ge::DT_INT8 && outputType != ge::DT_FLOAT8_E4M3FN && outputType != ge::DT_HIFLOAT8, | ||
| 200 | + OP_LOGE_FOR_INVALID_DTYPE_WITH_REASON(contextKeyParams.opName, "attention_out", | ||
| 201 | + GetPfaDataTypeStr(outputType).c_str(), | ||
| 202 | + "output type only supports int8, fp8_e4m3fn or hifloat8"), | ||
| 203 | + return false); | ||
| 204 | + | ||
| 205 | + // Basic verification: quantScale2 must be inputted and not an empty tensor | ||
| 206 | + OP_CHECK_IF(quantScale2Shape == nullptr, OP_LOGE_WITH_INVALID_INPUT(contextKeyParams.opName, "quant_scale2"), | ||
| 207 | + return false); | ||
| 208 | + quantScale2ShapeSize = quantScale2Shape->GetStorageShape().GetShapeSize(); | ||
| 209 | + quantD = quantScale2ShapeSize / n; | ||
| 210 | + OP_CHECK_IF(quantScale2ShapeSize == 0, | ||
| 211 | + OP_LOGE_FOR_INVALID_SHAPESIZE_WITH_REASON(contextKeyParams.opName, "quant_scale2", "0", | ||
| 212 | + "quant_scale2 must not be empty in post quant scenario"), | ||
| 213 | + return false); | ||
| 214 | + | ||
| 215 | + // altert unsupported situation(post quant per-tensor + BF16 + BSH + D unalign) | ||
| 216 | + if ((contextKeyParams.inputDataType == ge::DT_BF16) && (quantScale2ShapeSize == 1) && | ||
| 217 | + (inputLayout == InputLayout::BSH) && (queryD % BYTE_BLOCK != 0)) { | ||
| 218 | + OP_LOGW(contextKeyParams.opName, | ||
| 219 | + "post quant per-tensor doesn't support D unaligned(%u), when qkv is bf16 and layout is BSH.", queryD); | ||
| 220 | + } | ||
| 221 | + OP_CHECK_IF(!CheckPostQuantDtype(contextKeyParams, quantOffset2Shape, quantScale2Type) || | ||
| 222 | + !CheckPostQuantShapeSize(contextKeyParams, quantOffset2Shape, quantScale2ShapeSize, queryShapeInfo, | ||
| 223 | + valueShapeInfo), | ||
| 224 | + OP_LOGE_WITHOUT_REPORT(contextKeyParams.opName, "post quant params check failed!"), return false); | ||
| 225 | + return true; | ||
| 226 | +} | ||
| 227 | + | ||
| 228 | +bool FiaArch38PromptTiling::CheckAntiquantParamsShape(FiaArch38ContextParams& contextKeyParams) const | ||
| 229 | +{ | ||
| 230 | + const gert::StorageShape* antiquantScaleShape = contextKeyParams.antiquantScaleShape; | ||
| 231 | + // 伪量化收编至ifa tiling,pfa接口kv若传入伪量化dtype均会在此被拒绝 | ||
| 232 | + OP_CHECK_IF(contextKeyParams.antiquantScale == nullptr || antiquantScaleShape == nullptr, | ||
| 233 | + OP_LOGE_WITH_INVALID_INPUT(contextKeyParams.opName, "antiquant_scale"), return false); | ||
| 234 | + return true; | ||
| 235 | +} | ||
| 236 | + | ||
| 237 | +} // namespace arch38 | ||
| 238 | +} // namespace optiling | ||
| @@ -0,0 +1,696 @@ | |||
| 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 | +#include "fia_arch38_prompt_tiling.h" | ||
| 12 | + | ||
| 13 | +using namespace ge; | ||
| 14 | +using namespace AscendC; | ||
| 15 | +using namespace matmul_tiling; | ||
| 16 | +namespace optiling { | ||
| 17 | + | ||
| 18 | +namespace arch38 { | ||
| 19 | + | ||
| 20 | +void FiaArch38PromptTiling::FiaArch38InitOutputSplit(int64_t totalSize, FiaArch38FlashAttentionTilingData& tilingData) | ||
| 21 | +{ | ||
| 22 | + FiaArch38AttentionInitOutputParams* initParams = &tilingData.fiaArch38AttentionInitOutputParams; | ||
| 23 | + // Upward rounding, coreNum has been verified to be non-zero when obtained. | ||
| 24 | + uint32_t singleCoreSize = (totalSize + coreNum - 1) / (coreNum); | ||
| 25 | + | ||
| 26 | + if (enablePostQuant) { | ||
| 27 | + // requiring that the number of points allocated to each kernel must be even. | ||
| 28 | + singleCoreSize = ((singleCoreSize + 1) / 2) * 2; // 2 : fill in 0 | ||
| 29 | + } | ||
| 30 | + | ||
| 31 | + initParams->set_singleCoreSize(singleCoreSize); | ||
| 32 | + initParams->set_totalOutputSize(totalSize); | ||
| 33 | +} | ||
| 34 | + | ||
| 35 | +bool FiaArch38PromptTiling::CheckEmptyTensor(FiaArch38ContextParams& contextKeyParams) const | ||
| 36 | +{ | ||
| 37 | + return (contextKeyParams.keyInputShape->GetStorageShape().GetShapeSize() == 0) || | ||
| 38 | + (contextKeyParams.valueInputShape->GetStorageShape().GetShapeSize() == 0) || | ||
| 39 | + (contextKeyParams.outputShape->GetStorageShape().GetShapeSize() == 0) || | ||
| 40 | + (contextKeyParams.emptyTensor == 1U); | ||
| 41 | +} | ||
| 42 | + | ||
| 43 | +ge::graphStatus FiaArch38PromptTiling::SetEmptyTensor(FiaArch38ContextParams& contextKeyParams, uint64_t& tilingKey, | ||
| 44 | + uint32_t& numBlocksToBeSet, | ||
| 45 | + FiaArch38FlashAttentionTilingData& tilingData) | ||
| 46 | +{ | ||
| 47 | + tilingKey = EMPTY_KV_TILING_KEY; | ||
| 48 | + FiaArch38InitOutputSplit(contextKeyParams.outputShape->GetStorageShape().GetShapeSize(), tilingData); | ||
| 49 | + tilingData.fiaArch38AttentionInitOutputParams.set_needInit(1); | ||
| 50 | + | ||
| 51 | + auto platformInfoPtr = context_->GetPlatformInfo(); | ||
| 52 | + OP_CHECK_IF(platformInfoPtr == nullptr, ReportNullPointer(context_, __func__, "platformInfoPtr"), | ||
| 53 | + return ge::GRAPH_FAILED); | ||
| 54 | + auto ascendcPlatform = platform_ascendc::PlatformAscendC(platformInfoPtr); | ||
| 55 | + numBlocksToBeSet = ascendcPlatform.CalcTschBlockDim(coreNum, aicNum, coreNum); | ||
| 56 | + | ||
| 57 | + size_t* workspace = contextKeyParams.workspaceSize; | ||
| 58 | + const size_t sysWorkspaceSize = 16 * 1024 * 1024; // minimum size required by workspace | ||
| 59 | + workspace[0] = sysWorkspaceSize; | ||
| 60 | + return ge::GRAPH_SUCCESS; | ||
| 61 | +} | ||
| 62 | + | ||
| 63 | +bool FiaArch38PromptTiling::CheckIODataType(FiaArch38ContextParams& contextKeyParams) | ||
| 64 | +{ | ||
| 65 | + outputType = contextKeyParams.outputDataType; | ||
| 66 | + inputType = contextKeyParams.inputDataType; | ||
| 67 | + | ||
| 68 | + std::vector<ge::DataType> allowedDtypes = {ge::DT_FLOAT16, ge::DT_BF16, ge::DT_INT8, ge::DT_HIFLOAT8, | ||
| 69 | + ge::DT_FLOAT8_E4M3FN}; | ||
| 70 | + std::vector<uint32_t> dataTypeSizeArray = {FLOAT16_SIZE, BFLOAT16_SIZE, INT8_SIZE, | ||
| 71 | + FLOAT8_SIZE, FLOAT8_SIZE, FLOAT8_SIZE}; | ||
| 72 | + | ||
| 73 | + auto inputTypeCheck = std::find(allowedDtypes.begin(), allowedDtypes.end(), inputType); | ||
| 74 | + auto outputTypeCheck = std::find(allowedDtypes.begin(), allowedDtypes.end(), outputType); | ||
| 75 | + if (inputTypeCheck != allowedDtypes.end()) { | ||
| 76 | + uint32_t inputTypeIndex = std::distance(allowedDtypes.begin(), inputTypeCheck); | ||
| 77 | + dataTypeSize = dataTypeSizeArray[inputTypeIndex]; | ||
| 78 | + } | ||
| 79 | + maskElemSize = dataTypeSize; | ||
| 80 | + if (outputTypeCheck != allowedDtypes.end()) { | ||
| 81 | + uint32_t outputTypeIndex = std::distance(allowedDtypes.begin(), outputTypeCheck); | ||
| 82 | + outputDataTypeSize = dataTypeSizeArray[outputTypeIndex]; | ||
| 83 | + } | ||
| 84 | + | ||
| 85 | + if (inputType != ge::DT_FLOAT16) { | ||
| 86 | + OP_LOGW(contextKeyParams.opName, "innerprecise will not take effect when input type is %s!", | ||
| 87 | + GetPfaDataTypeStr(inputType).c_str()); | ||
| 88 | + } | ||
| 89 | + OP_CHECK_IF((inputTypeCheck == allowedDtypes.end() || outputTypeCheck == allowedDtypes.end()), | ||
| 90 | + OP_LOGE_FOR_INVALID_DTYPES_WITH_REASON( | ||
| 91 | + contextKeyParams.opName, "inputType, outputType", | ||
| 92 | + (GetPfaDataTypeStr(inputType) + ", " + GetPfaDataTypeStr(outputType)).c_str(), | ||
| 93 | + "inputType and outputType should be DT_FLOAT16, DT_BF16 or DT_INT8"), | ||
| 94 | + return false); | ||
| 95 | + return true; | ||
| 96 | +} | ||
| 97 | + | ||
| 98 | +bool FiaArch38PromptTiling::SetInputLayout(const char* layout) | ||
| 99 | +{ | ||
| 100 | + if (layout == nullptr) { | ||
| @@ -10,7 +10,13 @@ | |||
| 10 | 10 | ||
| 11 | /*! | 11 | /*! |
| 12 | * \file fia_arch38_tiling_common.h | 12 | * \file fia_arch38_tiling_common.h |
| 13 | - * \brief FIA arch38 本地化的 tiling 常量/枚举/中间数据结构(从 PFA 解耦,自持定义,不共用 common 头) | 13 | + * \brief FIA arch38 的 tiling 数据序列化契约定义 + 基础类型/精度常量与 InputLayout/TilingMod/SplitCoreMode 枚举 |
| 14 | + * | ||
| 15 | + * 本头文件承载算子 tiling 数据的序列化契约:通过 BEGIN_TILING_DATA_DEF/TILING_DATA_FIELD_DEF 宏 | ||
| 16 | + * 声明的 FiaArch38AttentionBaseParams / FiaArch38FlashAttentionTilingData 等结构,是 host 与 kernel | ||
| 17 | + * 之间传递切分参数的唯一 schema(参与序列化,字段顺序不能随意变动)。 | ||
| 18 | + * 其余内容(类型 size 常量、维度常量、精度枚举、InputLayout/TilingMod/SplitCoreMode)为该 schema 的 | ||
| 19 | + * 基础支撑定义。从 PFA 解耦自持,不共用通用 common 头。 | ||
| 14 | */ | 20 | */ |
| 15 | 21 | ||
| 16 | #ifndef FIA_ARCH38_TILING_COMMON_H | 22 | #ifndef FIA_ARCH38_TILING_COMMON_H |
| @@ -26,13 +32,13 @@ | |||
| 26 | namespace optiling { | 32 | namespace optiling { |
| 27 | namespace arch38 { | 33 | namespace arch38 { |
| 28 | 34 | ||
| 29 | -constexpr uint32_t INT8SIZE = 1; | 35 | +constexpr uint32_t INT8_SIZE = 1; |
| 30 | -constexpr uint32_t UINT8SIZE = 1; | 36 | +constexpr uint32_t UINT8_SIZE = 1; |
| 31 | -constexpr uint32_t FLOAT16SIZE = 2; | 37 | +constexpr uint32_t FLOAT16_SIZE = 2; |
| 32 | -constexpr uint32_t FLOAT8SIZE = 1; | 38 | +constexpr uint32_t FLOAT8_SIZE = 1; |
| 33 | -constexpr uint32_t BFLOAT16SIZE = 2; | 39 | +constexpr uint32_t BFLOAT16_SIZE = 2; |
| 34 | -constexpr uint32_t FLOAT32SIZE = 4; | 40 | +constexpr uint32_t FLOAT32_SIZE = 4; |
| 35 | -constexpr uint32_t BOOLSIZE = 1; | 41 | +constexpr uint32_t BOOL_SIZE = 1; |
| 36 | 42 | ||
| 37 | constexpr uint32_t NUM_16 = 16; | 43 | constexpr uint32_t NUM_16 = 16; |
| 38 | constexpr uint32_t NUM_32 = 32; | 44 | constexpr uint32_t NUM_32 = 32; |
| @@ -171,7 +177,6 @@ TILING_DATA_FIELD_DEF(uint32_t, gOfMla); | |||
| 171 | END_TILING_DATA_DEF; | 177 | END_TILING_DATA_DEF; |
| 172 | 178 | ||
| 173 | BEGIN_TILING_DATA_DEF(FiaArch38AttentionSeqParams) | 179 | BEGIN_TILING_DATA_DEF(FiaArch38AttentionSeqParams) |
| 174 | -// Temporary reuse | ||
| 175 | TILING_DATA_FIELD_DEF_ARR(uint32_t, 64, CoreHeadNumTail); // coreNStart | 180 | TILING_DATA_FIELD_DEF_ARR(uint32_t, 64, CoreHeadNumTail); // coreNStart |
| 176 | TILING_DATA_FIELD_DEF_ARR(uint32_t, 64, actualS1); // coreNEnd | 181 | TILING_DATA_FIELD_DEF_ARR(uint32_t, 64, actualS1); // coreNEnd |
| 177 | TILING_DATA_FIELD_DEF_ARR(uint32_t, 64, actualCoreNums); // coreSidStart | 182 | TILING_DATA_FIELD_DEF_ARR(uint32_t, 64, actualCoreNums); // coreSidStart |
| @@ -8,22 +8,27 @@ | |||
| 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 | +/*! | ||
| 12 | + * \file fia_arch38_tiling_data.h | ||
| 13 | + * \brief FIA arch38 的 kernel regbase 输入序列化契约定义(PFATilingDataconvert 的最终输出) | ||
| 14 | + * | ||
| 15 | + * 本头文件声明 kernel 直调(`<<<>>>`)接收的 regbase 输入参数序列化结构: | ||
| 16 | + * InputParamsRegbase / MultiCoreParamsRegbase / DropmaskParamsRegbase / InitOutputParams, | ||
| 17 | + * 以及由它们组合成的 FlashAttentionScoreSimplifiedTilingData(即 faTilingAdapter 的类型)。 | ||
| 18 | + * host 侧先在 fia_arch38_tiling_common.h 的 FiaArch38AttentionBaseParams 等中间态上计算, | ||
| 19 | + * 再经 PFATilingDataconvert 转换写入本文件的 InputParamsRegbase 等最终态,最后落盘到 tiling context。 | ||
| 20 | + * 注意:这些结构参与序列化,字段顺序不能随意变动。 | ||
| 21 | + */ | ||
| 22 | + | ||
| 11 | #ifndef FIA_ARCH38_TILING_DATA_H | 23 | #ifndef FIA_ARCH38_TILING_DATA_H |
| 12 | #define FIA_ARCH38_TILING_DATA_H | 24 | #define FIA_ARCH38_TILING_DATA_H |
| 13 | 25 | ||
| 14 | #include "register/tilingdata_base.h" | 26 | #include "register/tilingdata_base.h" |
| 15 | 27 | ||
| 16 | namespace optiling { | 28 | namespace optiling { |
| 29 | +namespace arch38 { | ||
| 17 | 30 | ||
| 18 | -struct PFAShapeInfo { | 31 | +// kernel 直调的输入参数(regbase):由 PFATilingDataconvert 从 host 中间态转换而来 |
| 19 | - uint32_t b = 0; | ||
| 20 | - uint64_t n = 0; | ||
| 21 | - uint64_t s = 0; | ||
| 22 | - uint32_t d = 0; | ||
| 23 | - uint64_t h = 0; | ||
| 24 | - uint32_t t = 0; | ||
| 25 | -}; | ||
| 26 | - | ||
| 27 | BEGIN_TILING_DATA_DEF(InputParamsRegbase) | 32 | BEGIN_TILING_DATA_DEF(InputParamsRegbase) |
| 28 | TILING_DATA_FIELD_DEF(int64_t, bSize); | 33 | TILING_DATA_FIELD_DEF(int64_t, bSize); |
| 29 | TILING_DATA_FIELD_DEF(int64_t, n2Size); | 34 | TILING_DATA_FIELD_DEF(int64_t, n2Size); |
| @@ -109,6 +114,7 @@ TILING_DATA_FIELD_DEF(uint16_t, antiquantPerHeadFlag); | |||
| 109 | TILING_DATA_FIELD_DEF(uint32_t, antiquantParaSeqSize); | 114 | TILING_DATA_FIELD_DEF(uint32_t, antiquantParaSeqSize); |
| 110 | END_TILING_DATA_DEF; | 115 | END_TILING_DATA_DEF; |
| 111 | 116 | ||
| 117 | +// 多核切分参数(各 core 的 s1 切分区间与负载) | ||
| 112 | BEGIN_TILING_DATA_DEF(MultiCoreParamsRegbase) | 118 | BEGIN_TILING_DATA_DEF(MultiCoreParamsRegbase) |
| 113 | TILING_DATA_FIELD_DEF(int32_t, coreNum); | 119 | TILING_DATA_FIELD_DEF(int32_t, coreNum); |
| 114 | TILING_DATA_FIELD_DEF(int64_t, totalSize); | 120 | TILING_DATA_FIELD_DEF(int64_t, totalSize); |
| @@ -119,6 +125,7 @@ TILING_DATA_FIELD_DEF_ARR(uint32_t, 48, bnStartIdx); | |||
| 119 | TILING_DATA_FIELD_DEF_ARR(int64_t, 48, sparseStartIdx); | 125 | TILING_DATA_FIELD_DEF_ARR(int64_t, 48, sparseStartIdx); |
| 120 | END_TILING_DATA_DEF; | 126 | END_TILING_DATA_DEF; |
| 121 | 127 | ||
| 128 | +// dropmask 场景的切分/UB 计算参数 | ||
| 122 | BEGIN_TILING_DATA_DEF(DropmaskParamsRegbase) | 129 | BEGIN_TILING_DATA_DEF(DropmaskParamsRegbase) |
| 123 | TILING_DATA_FIELD_DEF(int32_t, multiCoreFactorSize); | 130 | TILING_DATA_FIELD_DEF(int32_t, multiCoreFactorSize); |
| 124 | TILING_DATA_FIELD_DEF(int32_t, baseUbCalSize); | 131 | TILING_DATA_FIELD_DEF(int32_t, baseUbCalSize); |
| @@ -126,6 +133,7 @@ TILING_DATA_FIELD_DEF(int64_t, multiCoreTotalSize); | |||
| 126 | TILING_DATA_FIELD_DEF(int64_t, shapeTotalSize); | 133 | TILING_DATA_FIELD_DEF(int64_t, shapeTotalSize); |
| 127 | END_TILING_DATA_DEF; | 134 | END_TILING_DATA_DEF; |
| 128 | 135 | ||
| 136 | +// InitOutput 分支(needInit 路径)的输出尺寸参数 | ||
| 129 | BEGIN_TILING_DATA_DEF(InitOutputParams) | 137 | BEGIN_TILING_DATA_DEF(InitOutputParams) |
| 130 | TILING_DATA_FIELD_DEF(uint32_t, singleCoreSize); | 138 | TILING_DATA_FIELD_DEF(uint32_t, singleCoreSize); |
| 131 | TILING_DATA_FIELD_DEF(uint8_t, needInit); | 139 | TILING_DATA_FIELD_DEF(uint8_t, needInit); |
| @@ -135,11 +143,13 @@ TILING_DATA_FIELD_DEF(int64_t, totalOutputSize); | |||
| 135 | TILING_DATA_FIELD_DEF(int64_t, totalSoftMaxLseOutputSize); | 143 | TILING_DATA_FIELD_DEF(int64_t, totalSoftMaxLseOutputSize); |
| 136 | END_TILING_DATA_DEF; | 144 | END_TILING_DATA_DEF; |
| 137 | 145 | ||
| 146 | +// 注册 regbase 序列化类(供 tiling framework 反射/序列化) | ||
| 138 | REGISTER_TILING_DATA_CLASS(InputParamsRegbaseOp, InputParamsRegbase) | 147 | REGISTER_TILING_DATA_CLASS(InputParamsRegbaseOp, InputParamsRegbase) |
| 139 | REGISTER_TILING_DATA_CLASS(MultiCoreParamsRegbaseOp, MultiCoreParamsRegbase) | 148 | REGISTER_TILING_DATA_CLASS(MultiCoreParamsRegbaseOp, MultiCoreParamsRegbase) |
| 140 | REGISTER_TILING_DATA_CLASS(DropmaskParamsRegbaseOp, DropmaskParamsRegbase) | 149 | REGISTER_TILING_DATA_CLASS(DropmaskParamsRegbaseOp, DropmaskParamsRegbase) |
| 141 | REGISTER_TILING_DATA_CLASS(InitOutputParamsOp, InitOutputParams) | 150 | REGISTER_TILING_DATA_CLASS(InitOutputParamsOp, InitOutputParams) |
| 142 | 151 | ||
| 152 | +// kernel 直调输入参数的聚合体(faTilingAdapter 的类型),组合上述四个 regbase struct | ||
| 143 | BEGIN_TILING_DATA_DEF(FlashAttentionScoreSimplifiedTilingData) | 153 | BEGIN_TILING_DATA_DEF(FlashAttentionScoreSimplifiedTilingData) |
| 144 | TILING_DATA_FIELD_DEF_STRUCT(InputParamsRegbase, inputParamsRegbase); | 154 | TILING_DATA_FIELD_DEF_STRUCT(InputParamsRegbase, inputParamsRegbase); |
| 145 | TILING_DATA_FIELD_DEF_STRUCT(MultiCoreParamsRegbase, multiCoreParamsRegbase); | 155 | TILING_DATA_FIELD_DEF_STRUCT(MultiCoreParamsRegbase, multiCoreParamsRegbase); |
| @@ -147,6 +157,7 @@ TILING_DATA_FIELD_DEF_STRUCT(DropmaskParamsRegbase, dropmaskParamsRegbase); | |||
| 147 | TILING_DATA_FIELD_DEF_STRUCT(InitOutputParams, initOutputParams); | 157 | TILING_DATA_FIELD_DEF_STRUCT(InitOutputParams, initOutputParams); |
| 148 | END_TILING_DATA_DEF; | 158 | END_TILING_DATA_DEF; |
| 149 | 159 | ||
| 160 | +} // namespace arch38 | ||
| 150 | } // namespace optiling | 161 | } // namespace optiling |
| 151 | 162 | ||
| 152 | #endif // FIA_ARCH38_TILING_DATA_H | 163 | #endif // FIA_ARCH38_TILING_DATA_H |
| @@ -0,0 +1,143 @@ | |||
| 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 fia_arch38_tiling_structs.h | ||
| 13 | + * \brief FIA arch38 tiling 传参打包结构(内部工具层,不参与序列化) | ||
| 14 | + * | ||
| 15 | + * 本头文件声明仅用于在校验函数之间传递/聚合入参的纯结构体(PACacheDimCtx/PFAShapeDims/BmmShapeCtx/ | ||
| 16 | + * SplitNBSeqCtx/PFAShapeSet 等),目的是减少成员函数形参个数,属于 host 实现内部的数据交换, | ||
| 17 | + * 不参与算子 tiling 数据的序列化(序列化契约见 fia_arch38_tiling_common.h)。 | ||
| 18 | + */ | ||
| 19 | + | ||
| 20 | +#ifndef FIA_ARCH38_TILING_STRUCTS_H | ||
| 21 | +#define FIA_ARCH38_TILING_STRUCTS_H | ||
| 22 | + | ||
| 23 | +#include <cstddef> | ||
| 24 | +#include <cstdint> | ||
| 25 | +#include <string> | ||
| 26 | +#include <vector> | ||
| 27 | +#include "exe_graph/runtime/storage_shape.h" | ||
| 28 | + | ||
| 29 | +namespace optiling { | ||
| 30 | +namespace arch38 { | ||
| 31 | + | ||
| 32 | +// PA(page attention)形状校验入参:打包 key/value 维度与 block 参数 | ||
| 33 | +struct PACacheDimCtx { | ||
| 34 | + const gert::StorageShape* keyShape = nullptr; | ||
| 35 | + const gert::StorageShape* valueShape = nullptr; | ||
| 36 | + size_t keyDim = 0; | ||
| 37 | + int64_t keyDim1 = 0; | ||
| 38 | + const int64_t* blockSize = nullptr; | ||
| 39 | + int64_t blockNumValid = 0; | ||
| 40 | + int64_t headNumRatio = 0; | ||
| 41 | +}; | ||
| 42 | + | ||
| 43 | +// PA 每 batch 的 block 数统计结果 | ||
| 44 | +struct PABlockNumInfo { | ||
| 45 | + int64_t blockNumValid = 0; | ||
| 46 | + int32_t maxBlockNumPerBatch = 0; | ||
| 47 | +}; | ||
| 48 | + | ||
| 49 | +// 形状信息六元组(uint 最终态):SetShape 布局解析 + 校验后得到的 batch/head/seq/headSize/... 值, | ||
| 50 | +// 是 host 侧形状校验与 tiling 计算的最终入参(与 int64 中间态 PFAShapeDims 配套使用) | ||
| 51 | +struct PFAShapeInfo { | ||
| 52 | + uint32_t b = 0; | ||
| 53 | + uint64_t n = 0; | ||
| 54 | + uint64_t s = 0; | ||
| 55 | + uint32_t d = 0; | ||
| 56 | + uint64_t h = 0; | ||
| 57 | + uint32_t t = 0; | ||
| 58 | +}; | ||
| 59 | + | ||
| 60 | +// SetShape 布局解析的 int64 中间态六元组:保留 int64 以承载中间/负值,校验后再 cast 到 PFAShapeInfo | ||
| 61 | +struct PFAShapeDims { | ||
| 62 | + int64_t b = 0; | ||
| 63 | + int64_t n = 0; | ||
| 64 | + int64_t s = 0; | ||
| 65 | + int64_t d = 0; | ||
| 66 | + int64_t h = 0; | ||
| 67 | + int64_t t = 0; | ||
| 68 | +}; | ||
| 69 | + | ||
| 70 | +// CV diff 场景的 s_outer/s_inner/softmax_s_outer 因子集 | ||
| 71 | +struct CVFactorParams { | ||
| 72 | + uint32_t sOuterFactor = 0; | ||
| 73 | + uint32_t sInnerFactor = 0; | ||
| 74 | + uint32_t softmaxSOuterFactor = 0; | ||
| 75 | +}; | ||
| 76 | + | ||
| 77 | +// BMM1/BMM2 计算中的共享维度/空间入参 | ||
| 78 | +struct BmmShapeCtx { | ||
| 79 | + int64_t l1SizeRemain = 0; | ||
| 80 | + int64_t l0CSize = 0; | ||
| 81 | + uint32_t sOuterFactor = 0; | ||
| 82 | + uint32_t sInnerFactor = 0; | ||
| 83 | + uint32_t dSplitFactor = 0; | ||
| 84 | + bool autoBaseMNK = false; | ||
| 85 | +}; | ||
| 86 | + | ||
| 87 | +// NBS 按核切分时的分段索引与循环推进状态(数组长度在调用方按 tilingElementArrayLen 初始化) | ||
| 88 | +struct SplitNBSeqCtx { | ||
| 89 | + std::vector<uint32_t> coreSposEnd; | ||
| 90 | + std::vector<uint32_t> coreSposStart; | ||
| 91 | + std::vector<uint32_t> coreSidEnd; | ||
| 92 | + std::vector<uint32_t> coreSidStart; | ||
| 93 | + std::vector<uint32_t> coreNidEnd; | ||
| 94 | + std::vector<uint32_t> coreNidStart; | ||
| 95 | + std::vector<uint32_t> bnStartIdx; | ||
| 96 | + std::vector<int64_t> gS1StartIdx; | ||
| 97 | + int64_t curWight = 0; | ||
| 98 | + uint32_t curCore = 0; | ||
| 99 | + uint32_t tmpCoreNidEnd = 0; | ||
| 100 | + uint32_t tmpCoreSidEnd = 0; | ||
| 101 | + uint32_t tmpCoreSposEnd = 0; | ||
| 102 | + uint32_t sOuterSize = 0; | ||
| 103 | + uint32_t sInnerSize = 0; | ||
| 104 | + double coreWightTarget = 0; | ||
| 105 | + const std::vector<int64_t>* actualSeqLengths = nullptr; | ||
| 106 | + const std::vector<int64_t>* actualSeqLengthsKV = nullptr; | ||
| 107 | +}; | ||
| 108 | + | ||
| 109 | +// 单属性校验阶段使用的四类 shape 引用集合 | ||
| 110 | +struct PFAShapeSet { | ||
| 111 | + PFAShapeInfo* query = nullptr; | ||
| 112 | + PFAShapeInfo* key = nullptr; | ||
| 113 | + PFAShapeInfo* value = nullptr; | ||
| 114 | + PFAShapeInfo* queryRope = nullptr; | ||
| 115 | +}; | ||
| 116 | + | ||
| 117 | +// CheckMaskShape 的掩码形状解析结果(5 个输出打包,减少形参个数) | ||
| 118 | +struct MaskShapeInfo { | ||
| 119 | + int64_t attenMaskBatch = 1; | ||
| 120 | + int64_t attenMaskS1 = 0; | ||
| 121 | + int64_t attenMaskS2 = 0; | ||
| 122 | + bool checkMask = false; | ||
| 123 | + std::string strMaskShape; | ||
| 124 | +}; | ||
| 125 | + | ||
| 126 | +// CheckPerblockScaleShape 的 scale 形状校验入参打包 | ||
| 127 | +struct PerblockScaleCheckCtx { | ||
| 128 | + const gert::StorageShape* scaleShape = nullptr; | ||
| 129 | + uint32_t blockSize = 0; | ||
| 130 | + std::string paramName; | ||
| 131 | + std::string expectShape; | ||
| 132 | +}; | ||
| 133 | + | ||
| 134 | +// GetAndCheckRopeShape 的源/rope 名称对(仅用于错误消息) | ||
| 135 | +struct RopeNamePair { | ||
| 136 | + std::string srcName; | ||
| 137 | + std::string ropeName; | ||
| 138 | +}; | ||
| 139 | + | ||
| 140 | +} // namespace arch38 | ||
| 141 | +} // namespace optiling | ||
| 142 | + | ||
| 143 | +#endif // FIA_ARCH38_TILING_STRUCTS_H | ||
拆出的 8 个 .cpp 还需同步加入 common/src/tiling_sink/CMakeLists.txt。该独立目标仍只编译原来的两个 arch38 文件,也未链接这些实现;开启 ENABLE_TILING_SINK 后会缺少相关函数定义,影响链接或加载。