Merged
kv_quant_sparse_flash_attention: kv_quant_sparse_flash_attention 算子性能优化 #10428
kdy18482276080Add on Aug 22
kv_quant_sparse_flash_attention: kv_quant_sparse_flash_attention 算子性能优化 #10428
Merged
Total: 8 files changed+707-102
| @@ -278,6 +278,35 @@ ge::graphStatus QSFAMlaTiling::GetPlatformInfo() | |||
| 278 | return ge::GRAPH_SUCCESS; | 278 | return ge::GRAPH_SUCCESS; |
| 279 | } | 279 | } |
| 280 | 280 | ||
| 281 | +void QSFAMlaTiling::CalcVectorizeFlag() | ||
| 282 | +{ | ||
| 283 | + // 稀疏kv物理地址向量化:仅950(DAV_3510/arch35)平台kernel支持该模板参数, | ||
| 284 | + // 910b(arch22)不支持,vectorizeFlag_保持为0。 | ||
| 285 | + // UB 计算与 kernel 侧 GetKVPhyAddr 的 InitBuffer 保持一致: | ||
| 286 | + // blkTableBuf 对齐 512B,sparseIdxBuf / kvPhyAddrBuf 对齐 32B。 | ||
| 287 | + vectorizeFlag_ = 0U; | ||
| 288 | + if (!qsfaInfo_->isA5) { | ||
| 289 | + return; | ||
| 290 | + } | ||
| 291 | + if (qsfaInfo_->kvLayout == QSFALayout::PA_BSND) { | ||
| 292 | + constexpr uint32_t S2_NUM_PER_LOOP = 128U; | ||
| 293 | + int64_t blockSize = qsfaInfo_->blockSize; | ||
| 294 | + uint32_t blocksizeFlag = static_cast<uint32_t>((blockSize & (blockSize - 1)) == 0); | ||
| 295 | + uint32_t sparseBlockCountFlag = | ||
| 296 | + static_cast<uint32_t>((static_cast<uint32_t>(qsfaInfo_->sparseBlockCount) & (S2_NUM_PER_LOOP - 1U)) == 0U); | ||
| 297 | + uint32_t sparseModeFlag = static_cast<uint32_t>(qsfaInfo_->sparseMode == 3U); | ||
| 298 | + constexpr uint32_t UB_SIZE = 248 * 1024; // UB共256KB,预留8KB | ||
| 299 | + uint32_t alignedSparseBlockCount = (static_cast<uint32_t>(qsfaInfo_->sparseBlockCount) + S2_NUM_PER_LOOP - 1U) / | ||
| 300 | + S2_NUM_PER_LOOP * S2_NUM_PER_LOOP; | ||
| 301 | + uint32_t vectorizeUbSize = | ||
| 302 | + Align<uint32_t>(qsfaInfo_->maxBlockNumPerBatch * sizeof(int32_t), 512U) + | ||
| 303 | + Align<uint32_t>(static_cast<uint32_t>(qsfaInfo_->sparseBlockCount) * sizeof(int32_t), BYTE_BLOCK) + | ||
| 304 | + alignedSparseBlockCount * sizeof(int64_t); | ||
| 305 | + vectorizeFlag_ = static_cast<uint32_t>(vectorizeUbSize <= UB_SIZE && blocksizeFlag && sparseBlockCountFlag && | ||
| 306 | + sparseModeFlag); | ||
| 307 | + } | ||
| 308 | +} | ||
| 309 | + | ||
| 281 | void QSFAMlaTiling::GenTilingKey() | 310 | void QSFAMlaTiling::GenTilingKey() |
| 282 | { | 311 | { |
| 283 | uint32_t layoutQuery = static_cast<uint32_t>(qsfaInfo_->qLayout); | 312 | uint32_t layoutQuery = static_cast<uint32_t>(qsfaInfo_->qLayout); |
| @@ -289,8 +318,7 @@ void QSFAMlaTiling::GenTilingKey() | |||
| 289 | 318 | ||
| 290 | tilingKey_ = | 319 | tilingKey_ = |
| 291 | GET_TPL_TILING_KEY(0U, pageAttention, layoutQuery, layoutKV, perfMode_ == QSFAPerfMode::V_TEMPLATE_MODE, | 320 | GET_TPL_TILING_KEY(0U, pageAttention, layoutQuery, layoutKV, perfMode_ == QSFAPerfMode::V_TEMPLATE_MODE, |
| 292 | - static_cast<uint32_t>(qsfaInfo_->gSize > 64)); // G大于64时核间切G | 321 | + static_cast<uint32_t>(qsfaInfo_->gSize > 64), vectorizeFlag_); // G大于64时核间切G |
| 293 | - | ||
| 294 | OP_LOGI(qsfaInfo_->opName, "QSFA tilingKey_: %lu.", tilingKey_); | 322 | OP_LOGI(qsfaInfo_->opName, "QSFA tilingKey_: %lu.", tilingKey_); |
| 295 | } | 323 | } |
| 296 | 324 | ||
| @@ -328,7 +356,10 @@ void QSFAMlaTiling::CalcUbBmm() | |||
| 328 | qPreSizeMla_ = qsfaInfo_->gSize * (headDimAlign_ + 64U) * qsfaInfo_->s1Size; | 356 | qPreSizeMla_ = qsfaInfo_->gSize * (headDimAlign_ + 64U) * qsfaInfo_->s1Size; |
| 329 | } | 357 | } |
| 330 | 358 | ||
| 331 | -void QSFAMlaTiling::CheckUbSpace() { CalcUbBmm(); } | 359 | +void QSFAMlaTiling::CheckUbSpace() |
| 360 | +{ | ||
| 361 | + CalcUbBmm(); | ||
| 362 | +} | ||
| 332 | 363 | ||
| 333 | void QSFAMlaTiling::CalcInnerSize(uint32_t qsfaS2Size) | 364 | void QSFAMlaTiling::CalcInnerSize(uint32_t qsfaS2Size) |
| 334 | { | 365 | { |
| @@ -364,7 +395,10 @@ void QSFAMlaTiling::SplitBalanced() | |||
| 364 | usedCoreNum_ = aicNum_; | 395 | usedCoreNum_ = aicNum_; |
| 365 | } | 396 | } |
| 366 | 397 | ||
| 367 | -void QSFAMlaTiling::Split() { SplitBalanced(); } | 398 | +void QSFAMlaTiling::Split() |
| 399 | +{ | ||
| 400 | + SplitBalanced(); | ||
| 401 | +} | ||
| 368 | 402 | ||
| 369 | void QSFAMlaTiling::FillTilingBaseParamsMla() | 403 | void QSFAMlaTiling::FillTilingBaseParamsMla() |
| 370 | { | 404 | { |
| @@ -402,7 +436,10 @@ void QSFAMlaTiling::FillTilingSplitKVMla() | |||
| 402 | } | 436 | } |
| 403 | } | 437 | } |
| 404 | 438 | ||
| 405 | -void QSFAMlaTiling::FillTilingSingleCoreParamsMla() { tilingData_.singleCoreParams.set_usedCoreNum(usedCoreNum_); } | 439 | +void QSFAMlaTiling::FillTilingSingleCoreParamsMla() |
| 440 | +{ | ||
| 441 | + tilingData_.singleCoreParams.set_usedCoreNum(usedCoreNum_); | ||
| 442 | +} | ||
| 406 | 443 | ||
| 407 | void QSFAMlaTiling::FillTilingSingleCoreTensorSizeMla() | 444 | void QSFAMlaTiling::FillTilingSingleCoreTensorSizeMla() |
| 408 | { | 445 | { |
| @@ -439,7 +476,10 @@ void QSFAMlaTiling::NormalCalcFDWorkSpace(const uint32_t actCoreNum) | |||
| 439 | } | 476 | } |
| 440 | } | 477 | } |
| 441 | 478 | ||
| 442 | -void QSFAMlaTiling::CalcFDWorkSpace(const uint32_t actCoreNum) { NormalCalcFDWorkSpace(actCoreNum); } | 479 | +void QSFAMlaTiling::CalcFDWorkSpace(const uint32_t actCoreNum) |
| 480 | +{ | ||
| 481 | + NormalCalcFDWorkSpace(actCoreNum); | ||
| 482 | +} | ||
| 443 | 483 | ||
| 444 | void QSFAMlaTiling::GetWorkspaceSize() | 484 | void QSFAMlaTiling::GetWorkspaceSize() |
| 445 | { | 485 | { |
| @@ -455,6 +495,12 @@ void QSFAMlaTiling::GetWorkspaceSize() | |||
| 455 | workspaceSize_ += | 495 | workspaceSize_ += |
| 456 | (S2_BASE_SIZE * D_SIZE * GetTypeSize(qsfaInfo_->inputQType) * TRIPLE_BUFFER_NUM * (aicNum >> 1)); | 496 | (S2_BASE_SIZE * D_SIZE * GetTypeSize(qsfaInfo_->inputQType) * TRIPLE_BUFFER_NUM * (aicNum >> 1)); |
| 457 | } | 497 | } |
| 498 | + // 稀疏kv物理地址向量化:仅向量化开启时预计算物理地址表(挂在V0结果区之后) | ||
| 499 | + if (vectorizeFlag_) { | ||
| 500 | + uint32_t totalBS1 = | ||
| 501 | + (qsfaInfo_->qLayout == QSFALayout::TND) ? qsfaInfo_->s1Size : (qsfaInfo_->bSize * qsfaInfo_->s1Size); | ||
| 502 | + workspaceSize_ += totalBS1 * static_cast<uint32_t>(qsfaInfo_->sparseBlockCount) * sizeof(int64_t); | ||
| 503 | + } | ||
| 458 | } else { | 504 | } else { |
| 459 | uint32_t mmResElemSize = 4; // 4:fp32 | 505 | uint32_t mmResElemSize = 4; // 4:fp32 |
| 460 | uint32_t vec1ResElemSize = 2; // 2:fp16/bf16 | 506 | uint32_t vec1ResElemSize = 2; // 2:fp16/bf16 |
| @@ -509,6 +555,7 @@ ge::graphStatus QSFAMlaTiling::DoOpTiling(QSFATilingInfo *qsfaInfo) | |||
| 509 | Split(); | 555 | Split(); |
| 510 | FillTiling(); | 556 | FillTiling(); |
| 511 | CalcBlockDim(); | 557 | CalcBlockDim(); |
| 558 | + CalcVectorizeFlag(); | ||
| 512 | GetWorkspaceSize(); | 559 | GetWorkspaceSize(); |
| 513 | GenTilingKey(); | 560 | GenTilingKey(); |
| 514 | 561 | ||
| @@ -726,9 +773,15 @@ ge::graphStatus QSFATilingCheck::CheckSingleParaKey() const | |||
| 726 | return ge::GRAPH_SUCCESS; | 773 | return ge::GRAPH_SUCCESS; |
| 727 | } | 774 | } |
| 728 | 775 | ||
| 729 | -ge::graphStatus QSFATilingCheck::CheckSingleParaNumHeads() const { return ge::GRAPH_SUCCESS; } | 776 | +ge::graphStatus QSFATilingCheck::CheckSingleParaNumHeads() const |
| 777 | +{ | ||
| 778 | + return ge::GRAPH_SUCCESS; | ||
| 779 | +} | ||
| 730 | 780 | ||
| 731 | -ge::graphStatus QSFATilingCheck::CheckSingleParaKvHeadNums() const { return ge::GRAPH_SUCCESS; } | 781 | +ge::graphStatus QSFATilingCheck::CheckSingleParaKvHeadNums() const |
| 782 | +{ | ||
| 783 | + return ge::GRAPH_SUCCESS; | ||
| 784 | +} | ||
| 732 | 785 | ||
| 733 | ge::graphStatus QSFATilingCheck::CheckSingleParaSparseMode() const | 786 | ge::graphStatus QSFATilingCheck::CheckSingleParaSparseMode() const |
| 734 | { | 787 | { |
| @@ -866,7 +919,10 @@ ge::graphStatus QSFATilingCheck::CheckParaExistenceMlaAntiquant() const | |||
| 866 | return ge::GRAPH_SUCCESS; | 919 | return ge::GRAPH_SUCCESS; |
| 867 | } | 920 | } |
| 868 | 921 | ||
| 869 | -ge::graphStatus QSFATilingCheck::CheckParaExistenceMla() const { return CheckParaExistenceMlaAntiquant(); } | 922 | +ge::graphStatus QSFATilingCheck::CheckParaExistenceMla() const |
| 923 | +{ | ||
| 924 | + return CheckParaExistenceMlaAntiquant(); | ||
| 925 | +} | ||
| 870 | 926 | ||
| 871 | ge::graphStatus QSFATilingCheck::CheckParaExistence() | 927 | ge::graphStatus QSFATilingCheck::CheckParaExistence() |
| 872 | { | 928 | { |
| @@ -1381,9 +1437,15 @@ ge::graphStatus QSFATilingCheck::CheckFeatureMlaAntiquant() const | |||
| 1381 | return ge::GRAPH_SUCCESS; | 1437 | return ge::GRAPH_SUCCESS; |
| 1382 | } | 1438 | } |
| 1383 | 1439 | ||
| 1384 | -ge::graphStatus QSFATilingCheck::CheckFeatureMla() const { return CheckFeatureMlaAntiquant(); } | 1440 | +ge::graphStatus QSFATilingCheck::CheckFeatureMla() const |
| 1441 | +{ | ||
| 1442 | + return CheckFeatureMlaAntiquant(); | ||
| 1443 | +} | ||
| 1385 | 1444 | ||
| 1386 | -ge::graphStatus QSFATilingCheck::CheckFeature() const { return CheckFeatureMla(); } | 1445 | +ge::graphStatus QSFATilingCheck::CheckFeature() const |
| 1446 | +{ | ||
| 1447 | + return CheckFeatureMla(); | ||
| 1448 | +} | ||
| 1387 | 1449 | ||
| 1388 | void QSFATilingCheck::Init() | 1450 | void QSFATilingCheck::Init() |
| 1389 | { | 1451 | { |
| @@ -66,7 +66,11 @@ constexpr uint32_t BYTE_BLOCK = 32; | |||
| 66 | const uint32_t QSFA_MAX_AIC_CORE_NUM = 26; // 25 + 1 保证数组8字节对齐 | 66 | const uint32_t QSFA_MAX_AIC_CORE_NUM = 26; // 25 + 1 保证数组8字节对齐 |
| 67 | 67 | ||
| 68 | // ------------------公共定义-------------------------- | 68 | // ------------------公共定义-------------------------- |
| 69 | -enum class QSFALayout : uint32_t { BSND = 0, TND = 1, PA_BSND = 2 }; | 69 | +enum class QSFALayout : uint32_t { |
| 70 | + BSND = 0, | ||
| 71 | + TND = 1, | ||
| 72 | + PA_BSND = 2 | ||
| 73 | +}; | ||
| 70 | 74 | ||
| 71 | struct QSFATilingShapeCompareParam { | 75 | struct QSFATilingShapeCompareParam { |
| 72 | int64_t B = 1; | 76 | int64_t B = 1; |
| @@ -79,9 +83,15 @@ struct QSFATilingShapeCompareParam { | |||
| 79 | int64_t Bn = 1; | 83 | int64_t Bn = 1; |
| 80 | }; | 84 | }; |
| 81 | 85 | ||
| 82 | -enum class KvStorageMode : uint32_t { BATCH_CONTINUOUS = 0, PAGE_ATTENTION = 1 }; | 86 | +enum class KvStorageMode : uint32_t { |
| 87 | + BATCH_CONTINUOUS = 0, | ||
| 88 | + PAGE_ATTENTION = 1 | ||
| 89 | +}; | ||
| 83 | 90 | ||
| 84 | -enum class QSFAPerfMode : uint32_t { C_TEMPLATE_MODE = 0, V_TEMPLATE_MODE }; | 91 | +enum class QSFAPerfMode : uint32_t { |
| 92 | + C_TEMPLATE_MODE = 0, | ||
| 93 | + V_TEMPLATE_MODE | ||
| 94 | +}; | ||
| 85 | 95 | ||
| 86 | enum class QSFAAxis : uint32_t { | 96 | enum class QSFAAxis : uint32_t { |
| 87 | B = 0, | 97 | B = 0, |
| @@ -288,7 +298,9 @@ struct QSFATilingInfo { | |||
| 288 | // ---------------算子Tiling类--------------- | 298 | // ---------------算子Tiling类--------------- |
| 289 | class QSFAMlaTiling { | 299 | class QSFAMlaTiling { |
| 290 | public: | 300 | public: |
| 291 | - explicit QSFAMlaTiling(gert::TilingContext *context) : context_(context) {} | 301 | + explicit QSFAMlaTiling(gert::TilingContext *context) |
| 302 | + : context_(context) | ||
| 303 | + {} | ||
| 292 | ge::graphStatus DoOpTiling(QSFATilingInfo *qsfaInfo); | 304 | ge::graphStatus DoOpTiling(QSFATilingInfo *qsfaInfo); |
| 293 | 305 | ||
| 294 | private: | 306 | private: |
| @@ -298,6 +310,7 @@ private: | |||
| 298 | ge::graphStatus SetTilingData(TilingDef &tilingData) const; | 310 | ge::graphStatus SetTilingData(TilingDef &tilingData) const; |
| 299 | gert::TilingContext *context_ = nullptr; | 311 | gert::TilingContext *context_ = nullptr; |
| 300 | ge::graphStatus GetPlatformInfo(); | 312 | ge::graphStatus GetPlatformInfo(); |
| 313 | + void CalcVectorizeFlag(); | ||
| 301 | void GenTilingKey(); | 314 | void GenTilingKey(); |
| 302 | bool DealSameSeqEachBatch(); | 315 | bool DealSameSeqEachBatch(); |
| 303 | 316 | ||
| @@ -331,6 +344,7 @@ private: | |||
| 331 | 344 | ||
| 332 | bool balanceModeFlag_ = false; | 345 | bool balanceModeFlag_ = false; |
| 333 | bool splitKVFlag_ = false; | 346 | bool splitKVFlag_ = false; |
| 347 | + uint32_t vectorizeFlag_ = 0; | ||
| 334 | 348 | ||
| 335 | uint32_t coreNum_ = 0; | 349 | uint32_t coreNum_ = 0; |
| 336 | QSFAPerfMode perfMode_ = QSFAPerfMode::V_TEMPLATE_MODE; | 350 | QSFAPerfMode perfMode_ = QSFAPerfMode::V_TEMPLATE_MODE; |
| @@ -367,7 +381,8 @@ private: | |||
| 367 | // -----------算子Tiling入参信息解析及Check类--------------- | 381 | // -----------算子Tiling入参信息解析及Check类--------------- |
| 368 | class QSFATilingCheck { | 382 | class QSFATilingCheck { |
| 369 | public: | 383 | public: |
| 370 | - explicit QSFATilingCheck(const QSFATilingInfo &qsfaInfo) : qsfaInfo_(qsfaInfo) {}; | 384 | + explicit QSFATilingCheck(const QSFATilingInfo &qsfaInfo) |
| 385 | + : qsfaInfo_(qsfaInfo) {}; | ||
| 371 | ~QSFATilingCheck() = default; | 386 | ~QSFATilingCheck() = default; |
| 372 | ge::graphStatus Process(); | 387 | ge::graphStatus Process(); |
| 373 | 388 | ||
| @@ -502,7 +517,9 @@ private: | |||
| 502 | 517 | ||
| 503 | class QSFAInfoParser { | 518 | class QSFAInfoParser { |
| 504 | public: | 519 | public: |
| 505 | - explicit QSFAInfoParser(const gert::TilingContext *context) : context_(context) {} | 520 | + explicit QSFAInfoParser(const gert::TilingContext *context) |
| 521 | + : context_(context) | ||
| 522 | + {} | ||
| 506 | ~QSFAInfoParser() = default; | 523 | ~QSFAInfoParser() = default; |
| 507 | 524 | ||
| 508 | ge::graphStatus CheckRequiredInOutExistence() const; | 525 | ge::graphStatus CheckRequiredInOutExistence() const; |
| @@ -60,7 +60,11 @@ enum class QSFA_LAYOUT { | |||
| 60 | PA_BSND = 2, | 60 | PA_BSND = 2, |
| 61 | }; | 61 | }; |
| 62 | 62 | ||
| 63 | -enum class QSFATemplateMode { SWA_TEMPLATE_MODE = 0, CFA_TEMPLATE_MODE = 1, SCFA_TEMPLATE_MODE = 2 }; | 63 | +enum class QSFATemplateMode { |
| 64 | + SWA_TEMPLATE_MODE = 0, | ||
| 65 | + CFA_TEMPLATE_MODE = 1, | ||
| 66 | + SCFA_TEMPLATE_MODE = 2 | ||
| 67 | +}; | ||
| 64 | 68 | ||
| 65 | namespace BaseApi { | 69 | namespace BaseApi { |
| 66 | __aicore__ constexpr uint64_t Align2Func(uint64_t data) | 70 | __aicore__ constexpr uint64_t Align2Func(uint64_t data) |
| @@ -86,9 +90,10 @@ __aicore__ constexpr uint64_t Align64Func(uint64_t data) | |||
| 86 | 90 | ||
| 87 | #define TEMPLATE_INTF \ | 91 | #define TEMPLATE_INTF \ |
| 88 | template <typename Q_T, typename KV_T, typename T, typename OUTPUT_T, bool isFd, bool isPa, QSFA_LAYOUT LAYOUT_T, \ | 92 | template <typename Q_T, typename KV_T, typename T, typename OUTPUT_T, bool isFd, bool isPa, QSFA_LAYOUT LAYOUT_T, \ |
| 89 | - QSFA_LAYOUT KV_LAYOUT_T, QSFATemplateMode TEMPLATE_MODE, bool IS_SPLIT_G> | 93 | + QSFA_LAYOUT KV_LAYOUT_T, QSFATemplateMode TEMPLATE_MODE, bool IS_SPLIT_G, bool IS_VEC_S2PHYADDR> |
| 90 | 94 | ||
| 91 | -#define TEMPLATE_INTF_ARGS Q_T, KV_T, T, OUTPUT_T, isFd, isPa, LAYOUT_T, KV_LAYOUT_T, TEMPLATE_MODE, IS_SPLIT_G | 95 | +#define TEMPLATE_INTF_ARGS \ |
| 96 | + Q_T, KV_T, T, OUTPUT_T, isFd, isPa, LAYOUT_T, KV_LAYOUT_T, TEMPLATE_MODE, IS_SPLIT_G, IS_VEC_S2PHYADDR | ||
| 92 | 97 | ||
| 93 | #define QSFA_CUBE_BLOCK_TRAITS_TYPE_FIELDS(X) \ | 98 | #define QSFA_CUBE_BLOCK_TRAITS_TYPE_FIELDS(X) \ |
| 94 | X(Q_T) \ | 99 | X(Q_T) \ |
| @@ -102,7 +107,8 @@ __aicore__ constexpr uint64_t Align64Func(uint64_t data) | |||
| 102 | X(LAYOUT_T, QSFA_LAYOUT, QSFA_LAYOUT::BSND) \ | 107 | X(LAYOUT_T, QSFA_LAYOUT, QSFA_LAYOUT::BSND) \ |
| 103 | X(KV_LAYOUT_T, QSFA_LAYOUT, QSFA_LAYOUT::PA_BSND) \ | 108 | X(KV_LAYOUT_T, QSFA_LAYOUT, QSFA_LAYOUT::PA_BSND) \ |
| 104 | X(TEMPLATE_MODE, QSFATemplateMode, QSFATemplateMode::SCFA_TEMPLATE_MODE) \ | 109 | X(TEMPLATE_MODE, QSFATemplateMode, QSFATemplateMode::SCFA_TEMPLATE_MODE) \ |
| 105 | - X(IS_SPLIT_G, bool, false) | 110 | + X(IS_SPLIT_G, bool, false) \ |
| 111 | + X(IS_VEC_S2PHYADDR, bool, false) | ||
| 106 | 112 | ||
| 107 | /* 1. 生成带默认值的模版Template */ | 113 | /* 1. 生成带默认值的模版Template */ |
| 108 | #define GEN_TYPE_PARAM(name) typename name, | 114 | #define GEN_TYPE_PARAM(name) typename name, |
| @@ -60,8 +60,8 @@ public: | |||
| 60 | __aicore__ inline void Init(__gm__ uint8_t *query, __gm__ uint8_t *key, __gm__ uint8_t *value, | 60 | __aicore__ inline void Init(__gm__ uint8_t *query, __gm__ uint8_t *key, __gm__ uint8_t *value, |
| 61 | __gm__ uint8_t *sparseIndices, __gm__ uint8_t *keyScale, __gm__ uint8_t *valueScale, | 61 | __gm__ uint8_t *sparseIndices, __gm__ uint8_t *keyScale, __gm__ uint8_t *valueScale, |
| 62 | __gm__ uint8_t *blockTable, __gm__ uint8_t *actualSeqLengthsQ, | 62 | __gm__ uint8_t *blockTable, __gm__ uint8_t *actualSeqLengthsQ, |
| 63 | - __gm__ uint8_t *actualSeqLengths, __gm__ uint8_t *sinks, | 63 | + __gm__ uint8_t *actualSeqLengths, __gm__ uint8_t *sinks, __gm__ uint8_t *attentionOut, |
| 64 | - __gm__ uint8_t *attentionOut, __gm__ uint8_t *workspace, | 64 | + __gm__ uint8_t *workspace, |
| 65 | const KvQuantSparseFlashAttentionTilingDataMla *__restrict tiling, TPipe *tPipe); | 65 | const KvQuantSparseFlashAttentionTilingDataMla *__restrict tiling, TPipe *tPipe); |
| 66 | __aicore__ inline void Process(); | 66 | __aicore__ inline void Process(); |
| 67 | 67 | ||
| @@ -74,6 +74,8 @@ private: | |||
| 74 | const KvQuantSparseFlashAttentionTilingDataMla *__restrict tiling, | 74 | const KvQuantSparseFlashAttentionTilingDataMla *__restrict tiling, |
| 75 | TPipe *tPipe); | 75 | TPipe *tPipe); |
| 76 | __aicore__ inline void InitLocalBuffer(); | 76 | __aicore__ inline void InitLocalBuffer(); |
| 77 | + __aicore__ inline void InitMMResBufUb(); | ||
| 78 | + __aicore__ inline void InitMMResBufGm(__gm__ uint8_t *workspace); | ||
| 77 | __aicore__ inline void ComputeConstexpr(); | 79 | __aicore__ inline void ComputeConstexpr(); |
| 78 | __aicore__ inline void InitMMResBuf(__gm__ uint8_t *workspace); | 80 | __aicore__ inline void InitMMResBuf(__gm__ uint8_t *workspace); |
| 79 | __aicore__ inline void SetRunInfo(RunInfo &runInfo, RunParamStr &runParam, int64_t taskId, int64_t s2LoopCount, | 81 | __aicore__ inline void SetRunInfo(RunInfo &runInfo, RunParamStr &runParam, int64_t taskId, int64_t s2LoopCount, |
| @@ -176,6 +178,24 @@ __aicore__ inline void KvQuantSparseFlashAttentionMla<CubeBlockType, VecBlockTyp | |||
| 176 | this->InitGlobalBuffer(query, key, value, sparseIndices, blockTable, actualSeqLengthsQ, actualSeqLengths, sinks, | 178 | this->InitGlobalBuffer(query, key, value, sparseIndices, blockTable, actualSeqLengthsQ, actualSeqLengths, sinks, |
| 177 | workspace, tiling, tPipe); // gm设置 | 179 | workspace, tiling, tPipe); // gm设置 |
| 178 | this->InitCalcParamsEach(); | 180 | this->InitCalcParamsEach(); |
| 181 | + if ASCEND_IS_AIV { | ||
| 182 | + if constexpr (IS_VEC_S2PHYADDR) { | ||
| 183 | + uint32_t hasLoad = (aicIdx < usedCoreNum) ? 1U : 0U; | ||
| 184 | + // 适配分核左闭右开(与 ProcessMainLoop 中 gS1End/bN2End 的转换保持一致) | ||
| 185 | + uint32_t bN2EndIdx = constInfo.bN2End + 1; | ||
| 186 | + uint32_t nextGs1Idx = 0; | ||
| 187 | + uint32_t gS1EndBIdx = constInfo.bN2End / constInfo.n2Size; | ||
| 188 | + uint32_t gS1max = GetBalanceActualSeqLengths(actualSeqLengthsQGm, gS1EndBIdx); | ||
| 189 | + if (constInfo.gS1End + 1 < gS1max) { | ||
| 190 | + nextGs1Idx = constInfo.gS1End + 1; | ||
| 191 | + } | ||
| 192 | + this->vecBlock.GetKVPhyAddr(hasLoad, constInfo.bN2Start, bN2EndIdx, constInfo.gS1Start, nextGs1Idx, | ||
| 193 | + workspace, constInfo); | ||
| 194 | + } | ||
| 195 | + } | ||
| 196 | + /* UB buffer 在 GetKVPhyAddr(含 tPipe->Reset)之后初始化,避免 buffer 池被重置导致重叠 */ | ||
| 197 | + this->InitMMResBufUb(); | ||
| 198 | + this->InitMMResBufGm(workspace); | ||
| 179 | this->InitLocalBuffer(); | 199 | this->InitLocalBuffer(); |
| 180 | } | 200 | } |
| 181 | 201 | ||
| @@ -319,8 +339,8 @@ template <typename CubeBlockType, typename VecBlockType> | |||
| 319 | __aicore__ inline void KvQuantSparseFlashAttentionMla<CubeBlockType, VecBlockType>::InitGlobalBuffer( | 339 | __aicore__ inline void KvQuantSparseFlashAttentionMla<CubeBlockType, VecBlockType>::InitGlobalBuffer( |
| 320 | __gm__ uint8_t *query, __gm__ uint8_t *key, __gm__ uint8_t *value, __gm__ uint8_t *sparseIndices, | 340 | __gm__ uint8_t *query, __gm__ uint8_t *key, __gm__ uint8_t *value, __gm__ uint8_t *sparseIndices, |
| 321 | __gm__ uint8_t *blockTable, __gm__ uint8_t *actualSeqLengthsQ, __gm__ uint8_t *actualSeqLengths, | 341 | __gm__ uint8_t *blockTable, __gm__ uint8_t *actualSeqLengthsQ, __gm__ uint8_t *actualSeqLengths, |
| 322 | - __gm__ uint8_t *sinks, __gm__ uint8_t *workspace, | 342 | + __gm__ uint8_t *sinks, __gm__ uint8_t *workspace, const KvQuantSparseFlashAttentionTilingDataMla *__restrict tiling, |
| 323 | - const KvQuantSparseFlashAttentionTilingDataMla *__restrict tiling, TPipe *tPipe) | 343 | + TPipe *tPipe) |
| 324 | { | 344 | { |
| 325 | if (actualSeqLengthsQ != nullptr) { | 345 | if (actualSeqLengthsQ != nullptr) { |
| 326 | actualSeqQlenAddr = (__gm__ int32_t *)actualSeqLengthsQ; | 346 | actualSeqQlenAddr = (__gm__ int32_t *)actualSeqLengthsQ; |
| @@ -353,6 +373,11 @@ __aicore__ inline void KvQuantSparseFlashAttentionMla<CubeBlockType, VecBlockTyp | |||
| 353 | l1RightBuffers.Get().SetCrossCore(); | 373 | l1RightBuffers.Get().SetCrossCore(); |
| 354 | l1RightBuffers.Get().SetCrossCore(); | 374 | l1RightBuffers.Get().SetCrossCore(); |
| 355 | } | 375 | } |
| 376 | +} | ||
| 377 | + | ||
| 378 | +template <typename CubeBlockType, typename VecBlockType> | ||
| 379 | +__aicore__ inline void KvQuantSparseFlashAttentionMla<CubeBlockType, VecBlockType>::InitMMResBufUb() | ||
| 380 | +{ | ||
| 356 | uint32_t mm1ResultSize = constInfo.s1BaseSize / CV_RATIO * constInfo.s2BaseSize * sizeof(T); | 381 | uint32_t mm1ResultSize = constInfo.s1BaseSize / CV_RATIO * constInfo.s2BaseSize * sizeof(T); |
| 357 | uint32_t mm2ResultSize = constInfo.s1BaseSize / CV_RATIO * 512 * sizeof(T); | 382 | uint32_t mm2ResultSize = constInfo.s1BaseSize / CV_RATIO * 512 * sizeof(T); |
| 358 | ubBufferManager.Init(pipe, mm1ResultSize * 2 + mm2ResultSize); | 383 | ubBufferManager.Init(pipe, mm1ResultSize * 2 + mm2ResultSize); |
| @@ -374,7 +399,12 @@ __aicore__ inline void KvQuantSparseFlashAttentionMla<CubeBlockType, VecBlockTyp | |||
| 374 | if ASCEND_IS_AIV { | 399 | if ASCEND_IS_AIV { |
| 375 | bmm2Buffers.Get().SetCrossCore(); | 400 | bmm2Buffers.Get().SetCrossCore(); |
| 376 | } | 401 | } |
| 402 | +} | ||
| 377 | 403 | ||
| 404 | +template <typename CubeBlockType, typename VecBlockType> | ||
| 405 | +__aicore__ inline void KvQuantSparseFlashAttentionMla<CubeBlockType, VecBlockType>::InitMMResBufGm( | ||
| 406 | + __gm__ uint8_t *workspace) | ||
| 407 | +{ | ||
| 378 | if constexpr (IS_SPLIT_G) { | 408 | if constexpr (IS_SPLIT_G) { |
| 379 | uint32_t v0ResSize = constInfo.s2BaseSize * 576U * sizeof(Q_T); | 409 | uint32_t v0ResSize = constInfo.s2BaseSize * 576U * sizeof(Q_T); |
| 380 | int64_t totalOffset = v0ResSize * 3 * (aicIdx >> 1U); | 410 | int64_t totalOffset = v0ResSize * 3 * (aicIdx >> 1U); |
| @@ -480,6 +510,7 @@ __aicore__ inline void KvQuantSparseFlashAttentionMla<CubeBlockType, VecBlockTyp | |||
| 480 | SyncAll<false>(); | 510 | SyncAll<false>(); |
| 481 | } | 511 | } |
| 482 | 512 | ||
| 513 | + ICachePreLoad(6); | ||
| 483 | ProcessMainLoop(); | 514 | ProcessMainLoop(); |
| 484 | } | 515 | } |
| 485 | 516 | ||
| @@ -89,6 +89,9 @@ public: | |||
| 89 | RunInfo &runInfo, ConstInfo &constInfo); | 89 | RunInfo &runInfo, ConstInfo &constInfo); |
| 90 | using mm2ResPos = Buffer<BufferType::UB, SyncType::CROSS_CORE_SYNC_BOTH>; | 90 | using mm2ResPos = Buffer<BufferType::UB, SyncType::CROSS_CORE_SYNC_BOTH>; |
| 91 | __aicore__ inline void ProcessVec2(mm2ResPos &bmm2ResBuf, RunInfo &runInfo, ConstInfo &constInfo); | 91 | __aicore__ inline void ProcessVec2(mm2ResPos &bmm2ResBuf, RunInfo &runInfo, ConstInfo &constInfo); |
| 92 | + __aicore__ inline void GetKVPhyAddr(uint32_t hasLoad, uint32_t bN2StartIdx, uint32_t bN2EndIdx, | ||
| 93 | + uint32_t gS1StartIdx, uint32_t nextGs1Idx, __gm__ uint8_t *workspace, | ||
| 94 | + ConstInfo &constInfo); | ||
| 92 | 95 | ||
| 93 | private: | 96 | private: |
| 94 | __aicore__ inline void ProcessVec1SoftmaxDispatchQSFA(LocalTensor<Q_T> &stage1CastTensor, LocalTensor<T> &mmRes, | 97 | __aicore__ inline void ProcessVec1SoftmaxDispatchQSFA(LocalTensor<Q_T> &stage1CastTensor, LocalTensor<T> &mmRes, |
| @@ -1,12 +1,12 @@ | |||
| 1 | /** | 1 | /** |
| 2 | - * Copyright (c) 2025 Huawei Technologies Co., Ltd. | 2 | + * Copyright (c) 2025 Huawei Technologies Co., Ltd. |
| 3 | - * This program is free software, you can redistribute it and/or modify it under the terms and conditions of | 3 | + * This program is free software, you can redistribute it and/or modify it under the terms and conditions of |
| 4 | - * CANN Open Software License Agreement Version 2.0 (the "License"). | 4 | + * CANN Open Software License Agreement Version 2.0 (the "License"). |
| 5 | - * Please refer to the License for details. You may not use this file except in compliance with the License. | 5 | + * Please refer to the License for details. You may not use this file except in compliance with the License. |
| 6 | - * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, | 6 | + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, |
| 7 | - * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. | 7 | + * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. |
| 8 | - * See LICENSE in the root of the software repository for the full text of the License. | 8 | + * See LICENSE in the root of the software repository for the full text of the License. |
| 9 | - */ | 9 | + */ |
| 10 | 10 | ||
| 11 | /*! | 11 | /*! |
| 12 | * \file kv_quant_sparse_flash_attention.cpp | 12 | * \file kv_quant_sparse_flash_attention.cpp |
| @@ -72,7 +72,8 @@ using namespace AscendC; | |||
| 72 | #endif | 72 | #endif |
| 73 | 73 | ||
| 74 | #if (__CCE_AICORE__ == 310) | 74 | #if (__CCE_AICORE__ == 310) |
| 75 | -template <int FLASH_DECODE, int PAGE_ATTENTION, int LAYOUT_T, int KV_LAYOUT_T, int TEMPLATE_MODE, int IS_SPLIT_G> | 75 | +template <int FLASH_DECODE, int PAGE_ATTENTION, int LAYOUT_T, int KV_LAYOUT_T, int TEMPLATE_MODE, int IS_SPLIT_G, |
| 76 | + int IS_VEC_S2PHYADDR> | ||
| 76 | __aicore__ inline void DispatchKernelDtype310(__gm__ uint8_t *query, __gm__ uint8_t *key, __gm__ uint8_t *value, | 77 | __aicore__ inline void DispatchKernelDtype310(__gm__ uint8_t *query, __gm__ uint8_t *key, __gm__ uint8_t *value, |
| 77 | __gm__ uint8_t *sparseIndices, __gm__ uint8_t *keyScale, | 78 | __gm__ uint8_t *sparseIndices, __gm__ uint8_t *keyScale, |
| 78 | __gm__ uint8_t *valueScale, __gm__ uint8_t *blocktable, | 79 | __gm__ uint8_t *valueScale, __gm__ uint8_t *blocktable, |
| @@ -84,37 +85,44 @@ __aicore__ inline void DispatchKernelDtype310(__gm__ uint8_t *query, __gm__ uint | |||
| 84 | ORIG_DTYPE_ATTENTION_OUT == DT_BF16) { | 85 | ORIG_DTYPE_ATTENTION_OUT == DT_BF16) { |
| 85 | QSFA_OP_IMPL(BaseApi::KvQuantSparseFlashAttentionMla, KvQuantSparseFlashAttentionTilingDataMla, bfloat16_t, | 86 | QSFA_OP_IMPL(BaseApi::KvQuantSparseFlashAttentionMla, KvQuantSparseFlashAttentionTilingDataMla, bfloat16_t, |
| 86 | fp8_e4m3fn_t, float, bfloat16_t, FLASH_DECODE, PAGE_ATTENTION, static_cast<QSFA_LAYOUT>(LAYOUT_T), | 87 | fp8_e4m3fn_t, float, bfloat16_t, FLASH_DECODE, PAGE_ATTENTION, static_cast<QSFA_LAYOUT>(LAYOUT_T), |
| 87 | - static_cast<QSFA_LAYOUT>(KV_LAYOUT_T), static_cast<QSFATemplateMode>(TEMPLATE_MODE), IS_SPLIT_G); | 88 | + static_cast<QSFA_LAYOUT>(KV_LAYOUT_T), static_cast<QSFATemplateMode>(TEMPLATE_MODE), IS_SPLIT_G, |
| 89 | + IS_VEC_S2PHYADDR); | ||
| 88 | } else if constexpr (ORIG_DTYPE_QUERY == DT_BF16 && ORIG_DTYPE_KEY == DT_HIFLOAT8 && | 90 | } else if constexpr (ORIG_DTYPE_QUERY == DT_BF16 && ORIG_DTYPE_KEY == DT_HIFLOAT8 && |
| 89 | ORIG_DTYPE_ATTENTION_OUT == DT_BF16) { | 91 | ORIG_DTYPE_ATTENTION_OUT == DT_BF16) { |
| 90 | QSFA_OP_IMPL(BaseApi::KvQuantSparseFlashAttentionMla, KvQuantSparseFlashAttentionTilingDataMla, bfloat16_t, | 92 | QSFA_OP_IMPL(BaseApi::KvQuantSparseFlashAttentionMla, KvQuantSparseFlashAttentionTilingDataMla, bfloat16_t, |
| 91 | hifloat8_t, float, bfloat16_t, FLASH_DECODE, PAGE_ATTENTION, static_cast<QSFA_LAYOUT>(LAYOUT_T), | 93 | hifloat8_t, float, bfloat16_t, FLASH_DECODE, PAGE_ATTENTION, static_cast<QSFA_LAYOUT>(LAYOUT_T), |
| 92 | - static_cast<QSFA_LAYOUT>(KV_LAYOUT_T), static_cast<QSFATemplateMode>(TEMPLATE_MODE), IS_SPLIT_G); | 94 | + static_cast<QSFA_LAYOUT>(KV_LAYOUT_T), static_cast<QSFATemplateMode>(TEMPLATE_MODE), IS_SPLIT_G, |
| 95 | + IS_VEC_S2PHYADDR); | ||
| 93 | } else if constexpr (ORIG_DTYPE_QUERY == DT_BF16 && ORIG_DTYPE_KEY == DT_INT8 && | 96 | } else if constexpr (ORIG_DTYPE_QUERY == DT_BF16 && ORIG_DTYPE_KEY == DT_INT8 && |
| 94 | ORIG_DTYPE_ATTENTION_OUT == DT_BF16) { | 97 | ORIG_DTYPE_ATTENTION_OUT == DT_BF16) { |
| 95 | QSFA_OP_IMPL(BaseApi::KvQuantSparseFlashAttentionMla, KvQuantSparseFlashAttentionTilingDataMla, bfloat16_t, | 98 | QSFA_OP_IMPL(BaseApi::KvQuantSparseFlashAttentionMla, KvQuantSparseFlashAttentionTilingDataMla, bfloat16_t, |
| 96 | int8_t, float, bfloat16_t, FLASH_DECODE, PAGE_ATTENTION, static_cast<QSFA_LAYOUT>(LAYOUT_T), | 99 | int8_t, float, bfloat16_t, FLASH_DECODE, PAGE_ATTENTION, static_cast<QSFA_LAYOUT>(LAYOUT_T), |
| 97 | - static_cast<QSFA_LAYOUT>(KV_LAYOUT_T), static_cast<QSFATemplateMode>(TEMPLATE_MODE), IS_SPLIT_G); | 100 | + static_cast<QSFA_LAYOUT>(KV_LAYOUT_T), static_cast<QSFATemplateMode>(TEMPLATE_MODE), IS_SPLIT_G, |
| 101 | + IS_VEC_S2PHYADDR); | ||
| 98 | } else if constexpr (ORIG_DTYPE_QUERY == DT_FLOAT16 && ORIG_DTYPE_KEY == DT_FLOAT8_E4M3FN && | 102 | } else if constexpr (ORIG_DTYPE_QUERY == DT_FLOAT16 && ORIG_DTYPE_KEY == DT_FLOAT8_E4M3FN && |
| 99 | ORIG_DTYPE_ATTENTION_OUT == DT_FLOAT16) { | 103 | ORIG_DTYPE_ATTENTION_OUT == DT_FLOAT16) { |
| 100 | QSFA_OP_IMPL(BaseApi::KvQuantSparseFlashAttentionMla, KvQuantSparseFlashAttentionTilingDataMla, half, | 104 | QSFA_OP_IMPL(BaseApi::KvQuantSparseFlashAttentionMla, KvQuantSparseFlashAttentionTilingDataMla, half, |
| 101 | fp8_e4m3fn_t, float, half, FLASH_DECODE, PAGE_ATTENTION, static_cast<QSFA_LAYOUT>(LAYOUT_T), | 105 | fp8_e4m3fn_t, float, half, FLASH_DECODE, PAGE_ATTENTION, static_cast<QSFA_LAYOUT>(LAYOUT_T), |
| 102 | - static_cast<QSFA_LAYOUT>(KV_LAYOUT_T), static_cast<QSFATemplateMode>(TEMPLATE_MODE), IS_SPLIT_G); | 106 | + static_cast<QSFA_LAYOUT>(KV_LAYOUT_T), static_cast<QSFATemplateMode>(TEMPLATE_MODE), IS_SPLIT_G, |
| 107 | + IS_VEC_S2PHYADDR); | ||
| 103 | } else if constexpr (ORIG_DTYPE_QUERY == DT_FLOAT16 && ORIG_DTYPE_KEY == DT_HIFLOAT8 && | 108 | } else if constexpr (ORIG_DTYPE_QUERY == DT_FLOAT16 && ORIG_DTYPE_KEY == DT_HIFLOAT8 && |
| 104 | ORIG_DTYPE_ATTENTION_OUT == DT_FLOAT16) { | 109 | ORIG_DTYPE_ATTENTION_OUT == DT_FLOAT16) { |
| 105 | QSFA_OP_IMPL(BaseApi::KvQuantSparseFlashAttentionMla, KvQuantSparseFlashAttentionTilingDataMla, half, | 110 | QSFA_OP_IMPL(BaseApi::KvQuantSparseFlashAttentionMla, KvQuantSparseFlashAttentionTilingDataMla, half, |
| 106 | hifloat8_t, float, half, FLASH_DECODE, PAGE_ATTENTION, static_cast<QSFA_LAYOUT>(LAYOUT_T), | 111 | hifloat8_t, float, half, FLASH_DECODE, PAGE_ATTENTION, static_cast<QSFA_LAYOUT>(LAYOUT_T), |
| 107 | - static_cast<QSFA_LAYOUT>(KV_LAYOUT_T), static_cast<QSFATemplateMode>(TEMPLATE_MODE), IS_SPLIT_G); | 112 | + static_cast<QSFA_LAYOUT>(KV_LAYOUT_T), static_cast<QSFATemplateMode>(TEMPLATE_MODE), IS_SPLIT_G, |
| 113 | + IS_VEC_S2PHYADDR); | ||
| 108 | } else if constexpr (ORIG_DTYPE_QUERY == DT_FLOAT16 && ORIG_DTYPE_KEY == DT_INT8 && | 114 | } else if constexpr (ORIG_DTYPE_QUERY == DT_FLOAT16 && ORIG_DTYPE_KEY == DT_INT8 && |
| 109 | ORIG_DTYPE_ATTENTION_OUT == DT_FLOAT16) { | 115 | ORIG_DTYPE_ATTENTION_OUT == DT_FLOAT16) { |
| 110 | QSFA_OP_IMPL(BaseApi::KvQuantSparseFlashAttentionMla, KvQuantSparseFlashAttentionTilingDataMla, half, int8_t, | 116 | QSFA_OP_IMPL(BaseApi::KvQuantSparseFlashAttentionMla, KvQuantSparseFlashAttentionTilingDataMla, half, int8_t, |
| 111 | float, half, FLASH_DECODE, PAGE_ATTENTION, static_cast<QSFA_LAYOUT>(LAYOUT_T), | 117 | float, half, FLASH_DECODE, PAGE_ATTENTION, static_cast<QSFA_LAYOUT>(LAYOUT_T), |
| 112 | - static_cast<QSFA_LAYOUT>(KV_LAYOUT_T), static_cast<QSFATemplateMode>(TEMPLATE_MODE), IS_SPLIT_G); | 118 | + static_cast<QSFA_LAYOUT>(KV_LAYOUT_T), static_cast<QSFATemplateMode>(TEMPLATE_MODE), IS_SPLIT_G, |
| 119 | + IS_VEC_S2PHYADDR); | ||
| 113 | } | 120 | } |
| 114 | } | 121 | } |
| 115 | #endif | 122 | #endif |
| 116 | 123 | ||
| 117 | -template <int FLASH_DECODE, int PAGE_ATTENTION, int LAYOUT_T, int KV_LAYOUT_T, int TEMPLATE_MODE, int IS_SPLIT_G> | 124 | +template <int FLASH_DECODE, int PAGE_ATTENTION, int LAYOUT_T, int KV_LAYOUT_T, int TEMPLATE_MODE, int IS_SPLIT_G, |
| 125 | + int IS_VEC_S2PHYADDR> | ||
| 118 | __global__ __aicore__ void kv_quant_sparse_flash_attention( | 126 | __global__ __aicore__ void kv_quant_sparse_flash_attention( |
| 119 | __gm__ uint8_t *query, __gm__ uint8_t *key, __gm__ uint8_t *value, __gm__ uint8_t *sparseIndices, | 127 | __gm__ uint8_t *query, __gm__ uint8_t *key, __gm__ uint8_t *value, __gm__ uint8_t *sparseIndices, |
| 120 | __gm__ uint8_t *keyScale, __gm__ uint8_t *valueScale, __gm__ uint8_t *blocktable, | 128 | __gm__ uint8_t *keyScale, __gm__ uint8_t *valueScale, __gm__ uint8_t *blocktable, |
| @@ -126,9 +134,10 @@ __global__ __aicore__ void kv_quant_sparse_flash_attention( | |||
| 126 | TPipe tPipe; | 134 | TPipe tPipe; |
| 127 | __gm__ uint8_t *user = GetUserWorkspace(workspace); | 135 | __gm__ uint8_t *user = GetUserWorkspace(workspace); |
| 128 | #if (__CCE_AICORE__ == 310) | 136 | #if (__CCE_AICORE__ == 310) |
| 129 | - DispatchKernelDtype310<FLASH_DECODE, PAGE_ATTENTION, LAYOUT_T, KV_LAYOUT_T, TEMPLATE_MODE, IS_SPLIT_G>( | 137 | + DispatchKernelDtype310<FLASH_DECODE, PAGE_ATTENTION, LAYOUT_T, KV_LAYOUT_T, TEMPLATE_MODE, IS_SPLIT_G, |
| 130 | - query, key, value, sparseIndices, keyScale, valueScale, blocktable, actualSeqLengthsQuery, actualSeqLengthsKV, | 138 | + IS_VEC_S2PHYADDR>(query, key, value, sparseIndices, keyScale, valueScale, blocktable, |
| 131 | - sinks, attentionOut, user, tiling, tPipe); | 139 | + actualSeqLengthsQuery, actualSeqLengthsKV, sinks, attentionOut, user, |
| 140 | + tiling, tPipe); | ||
| 132 | #else | 141 | #else |
| 133 | if constexpr (ORIG_DTYPE_QUERY == DT_FLOAT16 && ORIG_DTYPE_KEY == DT_INT8 && | 142 | if constexpr (ORIG_DTYPE_QUERY == DT_FLOAT16 && ORIG_DTYPE_KEY == DT_INT8 && |
| 134 | ORIG_DTYPE_ATTENTION_OUT == DT_FLOAT16) { | 143 | ORIG_DTYPE_ATTENTION_OUT == DT_FLOAT16) { |
| @@ -1,12 +1,12 @@ | |||
| 1 | /** | 1 | /** |
| 2 | - * Copyright (c) 2026 Huawei Technologies Co., Ltd. | 2 | + * Copyright (c) 2026 Huawei Technologies Co., Ltd. |
| 3 | - * This program is free software, you can redistribute it and/or modify it under the terms and conditions of | 3 | + * This program is free software, you can redistribute it and/or modify it under the terms and conditions of |
| 4 | - * CANN Open Software License Agreement Version 2.0 (the "License"). | 4 | + * CANN Open Software License Agreement Version 2.0 (the "License"). |
| 5 | - * Please refer to the License for details. You may not use this file except in compliance with the License. | 5 | + * Please refer to the License for details. You may not use this file except in compliance with the License. |
| 6 | - * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, | 6 | + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, |
| 7 | - * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. | 7 | + * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. |
| 8 | - * See LICENSE in the root of the software repository for the full text of the License. | 8 | + * See LICENSE in the root of the software repository for the full text of the License. |
| 9 | - */ | 9 | + */ |
| 10 | 10 | ||
| 11 | /*! | 11 | /*! |
| 12 | * \file kv_quant_sparse_flash_attention_template_tiling_key.h | 12 | * \file kv_quant_sparse_flash_attention_template_tiling_key.h |
| @@ -36,32 +36,33 @@ ASCENDC_TPL_ARGS_DECL(KvQuantSparseFlashAttention, // 算子OpType | |||
| 36 | QSFA_LAYOUT_TND, QSFA_LAYOUT_PA_BSND), | 36 | QSFA_LAYOUT_TND, QSFA_LAYOUT_PA_BSND), |
| 37 | ASCENDC_TPL_UINT_DECL(TEMPLATE_MODE, ASCENDC_TPL_4_BW, ASCENDC_TPL_UI_LIST, C_TEMPLATE, | 37 | ASCENDC_TPL_UINT_DECL(TEMPLATE_MODE, ASCENDC_TPL_4_BW, ASCENDC_TPL_UI_LIST, C_TEMPLATE, |
| 38 | V_TEMPLATE), | 38 | V_TEMPLATE), |
| 39 | - ASCENDC_TPL_BOOL_DECL(IS_SPLIT_G, 0, 1), ); | 39 | + ASCENDC_TPL_BOOL_DECL(IS_SPLIT_G, 0, 1), ASCENDC_TPL_BOOL_DECL(IS_VEC_S2PHYADDR, 0, 1), ); |
| 40 | 40 | ||
| 41 | // 支持的模板参数组合 | 41 | // 支持的模板参数组合 |
| 42 | // 用于调用GET_TPL_TILING_KEY获取TilingKey时,接口内部校验TilingKey是否合法 | 42 | // 用于调用GET_TPL_TILING_KEY获取TilingKey时,接口内部校验TilingKey是否合法 |
| 43 | -ASCENDC_TPL_SEL(ASCENDC_TPL_ARGS_SEL(ASCENDC_TPL_BOOL_SEL(FLASH_DECODE, 0), ASCENDC_TPL_BOOL_SEL(PAGE_ATTENTION, 0), | 43 | +ASCENDC_TPL_SEL( |
| 44 | - ASCENDC_TPL_UINT_SEL(LAYOUT_T, ASCENDC_TPL_UI_LIST, QSFA_LAYOUT_BSND), | 44 | + ASCENDC_TPL_ARGS_SEL(ASCENDC_TPL_BOOL_SEL(FLASH_DECODE, 0), ASCENDC_TPL_BOOL_SEL(PAGE_ATTENTION, 0), |
| 45 | - ASCENDC_TPL_UINT_SEL(KV_LAYOUT_T, ASCENDC_TPL_UI_LIST, QSFA_LAYOUT_BSND), | 45 | + ASCENDC_TPL_UINT_SEL(LAYOUT_T, ASCENDC_TPL_UI_LIST, QSFA_LAYOUT_BSND), |
| 46 | - ASCENDC_TPL_UINT_SEL(TEMPLATE_MODE, ASCENDC_TPL_UI_LIST, V_TEMPLATE), | 46 | + ASCENDC_TPL_UINT_SEL(KV_LAYOUT_T, ASCENDC_TPL_UI_LIST, QSFA_LAYOUT_BSND), |
| 47 | - ASCENDC_TPL_BOOL_SEL(IS_SPLIT_G, 0, 1), ), | 47 | + ASCENDC_TPL_UINT_SEL(TEMPLATE_MODE, ASCENDC_TPL_UI_LIST, V_TEMPLATE), |
| 48 | + ASCENDC_TPL_BOOL_SEL(IS_SPLIT_G, 0, 1), ASCENDC_TPL_BOOL_SEL(IS_VEC_S2PHYADDR, 0, 1), ), | ||
| 48 | 49 | ||
| 49 | - ASCENDC_TPL_ARGS_SEL(ASCENDC_TPL_BOOL_SEL(FLASH_DECODE, 0), ASCENDC_TPL_BOOL_SEL(PAGE_ATTENTION, 0), | 50 | + ASCENDC_TPL_ARGS_SEL(ASCENDC_TPL_BOOL_SEL(FLASH_DECODE, 0), ASCENDC_TPL_BOOL_SEL(PAGE_ATTENTION, 0), |
| 50 | - ASCENDC_TPL_UINT_SEL(LAYOUT_T, ASCENDC_TPL_UI_LIST, QSFA_LAYOUT_TND), | 51 | + ASCENDC_TPL_UINT_SEL(LAYOUT_T, ASCENDC_TPL_UI_LIST, QSFA_LAYOUT_TND), |
| 51 | - ASCENDC_TPL_UINT_SEL(KV_LAYOUT_T, ASCENDC_TPL_UI_LIST, QSFA_LAYOUT_TND), | 52 | + ASCENDC_TPL_UINT_SEL(KV_LAYOUT_T, ASCENDC_TPL_UI_LIST, QSFA_LAYOUT_TND), |
| 52 | - ASCENDC_TPL_UINT_SEL(TEMPLATE_MODE, ASCENDC_TPL_UI_LIST, V_TEMPLATE), | 53 | + ASCENDC_TPL_UINT_SEL(TEMPLATE_MODE, ASCENDC_TPL_UI_LIST, V_TEMPLATE), |
| 53 | - ASCENDC_TPL_BOOL_SEL(IS_SPLIT_G, 0, 1), ), | 54 | + ASCENDC_TPL_BOOL_SEL(IS_SPLIT_G, 0, 1), ASCENDC_TPL_BOOL_SEL(IS_VEC_S2PHYADDR, 0, 1), ), |
| 54 | 55 | ||
| 55 | - ASCENDC_TPL_ARGS_SEL(ASCENDC_TPL_BOOL_SEL(FLASH_DECODE, 0), ASCENDC_TPL_BOOL_SEL(PAGE_ATTENTION, 1), | 56 | + ASCENDC_TPL_ARGS_SEL(ASCENDC_TPL_BOOL_SEL(FLASH_DECODE, 0), ASCENDC_TPL_BOOL_SEL(PAGE_ATTENTION, 1), |
| 56 | - ASCENDC_TPL_UINT_SEL(LAYOUT_T, ASCENDC_TPL_UI_LIST, QSFA_LAYOUT_BSND), | 57 | + ASCENDC_TPL_UINT_SEL(LAYOUT_T, ASCENDC_TPL_UI_LIST, QSFA_LAYOUT_BSND), |
| 57 | - ASCENDC_TPL_UINT_SEL(KV_LAYOUT_T, ASCENDC_TPL_UI_LIST, QSFA_LAYOUT_PA_BSND), | 58 | + ASCENDC_TPL_UINT_SEL(KV_LAYOUT_T, ASCENDC_TPL_UI_LIST, QSFA_LAYOUT_PA_BSND), |
| 58 | - ASCENDC_TPL_UINT_SEL(TEMPLATE_MODE, ASCENDC_TPL_UI_LIST, V_TEMPLATE), | 59 | + ASCENDC_TPL_UINT_SEL(TEMPLATE_MODE, ASCENDC_TPL_UI_LIST, V_TEMPLATE), |
| 59 | - ASCENDC_TPL_BOOL_SEL(IS_SPLIT_G, 0, 1), ), | 60 | + ASCENDC_TPL_BOOL_SEL(IS_SPLIT_G, 0, 1), ASCENDC_TPL_BOOL_SEL(IS_VEC_S2PHYADDR, 0, 1), ), |
| 60 | 61 | ||
| 61 | - ASCENDC_TPL_ARGS_SEL(ASCENDC_TPL_BOOL_SEL(FLASH_DECODE, 0), ASCENDC_TPL_BOOL_SEL(PAGE_ATTENTION, 1), | 62 | + ASCENDC_TPL_ARGS_SEL(ASCENDC_TPL_BOOL_SEL(FLASH_DECODE, 0), ASCENDC_TPL_BOOL_SEL(PAGE_ATTENTION, 1), |
| 62 | - ASCENDC_TPL_UINT_SEL(LAYOUT_T, ASCENDC_TPL_UI_LIST, QSFA_LAYOUT_TND), | 63 | + ASCENDC_TPL_UINT_SEL(LAYOUT_T, ASCENDC_TPL_UI_LIST, QSFA_LAYOUT_TND), |
| 63 | - ASCENDC_TPL_UINT_SEL(KV_LAYOUT_T, ASCENDC_TPL_UI_LIST, QSFA_LAYOUT_PA_BSND), | 64 | + ASCENDC_TPL_UINT_SEL(KV_LAYOUT_T, ASCENDC_TPL_UI_LIST, QSFA_LAYOUT_PA_BSND), |
| 64 | - ASCENDC_TPL_UINT_SEL(TEMPLATE_MODE, ASCENDC_TPL_UI_LIST, V_TEMPLATE), | 65 | + ASCENDC_TPL_UINT_SEL(TEMPLATE_MODE, ASCENDC_TPL_UI_LIST, V_TEMPLATE), |
| 65 | - ASCENDC_TPL_BOOL_SEL(IS_SPLIT_G, 0, 1), ), ); | 66 | + ASCENDC_TPL_BOOL_SEL(IS_SPLIT_G, 0, 1), ASCENDC_TPL_BOOL_SEL(IS_VEC_S2PHYADDR, 0, 1), ), ); |
| 66 | 67 | ||
| 67 | #endif // TEMPLATE_TILING_KEY | 68 | #endif // TEMPLATE_TILING_KEY |
| @@ -14,9 +14,15 @@ | |||
| 14 | 14 | ||
| 15 | class KvQuantSparseFlashAttentionTilingArch35 : public testing::Test { | 15 | class KvQuantSparseFlashAttentionTilingArch35 : public testing::Test { |
| 16 | protected: | 16 | protected: |
| 17 | - static void SetUpTestCase() { std::cout << "KvQuantSparseFlashAttentionTilingArch35 SetUp" << std::endl; } | 17 | + static void SetUpTestCase() |
| 18 | + { | ||
| 19 | + std::cout << "KvQuantSparseFlashAttentionTilingArch35 SetUp" << std::endl; | ||
| 20 | + } | ||
| 18 | 21 | ||
| 19 | - static void TearDownTestCase() { std::cout << "KvQuantSparseFlashAttentionTilingArch35 TearDown" << std::endl; } | 22 | + static void TearDownTestCase() |
| 23 | + { | ||
| 24 | + std::cout << "KvQuantSparseFlashAttentionTilingArch35 TearDown" << std::endl; | ||
| 25 | + } | ||
| 20 | }; | 26 | }; |
| 21 | 27 | ||
| 22 | // 0 | 28 | // 0 |
| @@ -53,7 +59,7 @@ TEST_F(KvQuantSparseFlashAttentionTilingArch35, KvQuantSparseFlashAttention_950_ | |||
| 53 | {"rope_head_dim", Ops::Transformer::AnyValue::CreateFrom<int64_t>(64)}}, | 59 | {"rope_head_dim", Ops::Transformer::AnyValue::CreateFrom<int64_t>(64)}}, |
| 54 | &compileInfo, "Ascend950", 64, 262144, 16384); | 60 | &compileInfo, "Ascend950", 64, 262144, 16384); |
| 55 | 61 | ||
| 56 | - int64_t expectTilingKey = 1154; | 62 | + int64_t expectTilingKey = 33922; |
| 57 | std::string expectTilingData = "2199023255554 64 16 8589934624 4410436851802832898 8 3 1 128 656 0 " | 63 | std::string expectTilingData = "2199023255554 64 16 8589934624 4410436851802832898 8 3 1 128 656 0 " |
| 58 | "10496 40532396646334464 262144 64 316659348865024 2199023255560 "; | 64 | "10496 40532396646334464 262144 64 316659348865024 2199023255560 "; |
| 59 | ExecuteTestCase(tilingContextPara, ge::GRAPH_SUCCESS, expectTilingKey, expectTilingData); | 65 | ExecuteTestCase(tilingContextPara, ge::GRAPH_SUCCESS, expectTilingKey, expectTilingData); |