Merged
FIA 伪量化 PA 场景支持 kv 及 kv_scale 0 轴 1 轴非连续 #10263
anakinSkywalkerAdd on Aug 19
FIA 伪量化 PA 场景支持 kv 及 kv_scale 0 轴 1 轴非连续 #10263
Merged
Total: 15 files changed+473-40
| @@ -231,6 +231,10 @@ public: | |||
| 231 | StridesParams keyStrides; | 231 | StridesParams keyStrides; |
| 232 | StridesParams valueStrides; | 232 | StridesParams valueStrides; |
| 233 | StridesParams kRopeStrides; | 233 | StridesParams kRopeStrides; |
| 234 | + StridesParams kScaleStrides; | ||
| 235 | + StridesParams vScaleStrides; | ||
| 236 | + StridesParams kOffsetStrides; | ||
| 237 | + StridesParams vOffsetStrides; | ||
| 234 | 238 | ||
| 235 | int64_t get_bSize() const | 239 | int64_t get_bSize() const |
| 236 | { | 240 | { |
| @@ -318,7 +318,15 @@ struct RunInfo<false> { | |||
| 318 | uint64_t valueBnStride; \ | 318 | uint64_t valueBnStride; \ |
| 319 | uint64_t valueN2Stride; \ | 319 | uint64_t valueN2Stride; \ |
| 320 | uint64_t keyRopeBnStride; \ | 320 | uint64_t keyRopeBnStride; \ |
| 321 | - uint64_t keyRopeN2Stride | 321 | + uint64_t keyRopeN2Stride; \ |
| 322 | + uint64_t kScaleBnStride; \ | ||
| 323 | + uint64_t kScaleN2Stride; \ | ||
| 324 | + uint64_t vScaleBnStride; \ | ||
| 325 | + uint64_t vScaleN2Stride; \ | ||
| 326 | + uint64_t kOffsetBnStride; \ | ||
| 327 | + uint64_t kOffsetN2Stride; \ | ||
| 328 | + uint64_t vOffsetBnStride; \ | ||
| 329 | + uint64_t vOffsetN2Stride | ||
| 322 | 330 | ||
| 323 | #define ROPE_INFO \ | 331 | #define ROPE_INFO \ |
| 324 | /* rope参数 */ \ | 332 | /* rope参数 */ \ |
| @@ -546,6 +554,14 @@ struct CVSharedParams<true, true> { | |||
| 546 | uint64_t valueN2Stride; | 554 | uint64_t valueN2Stride; |
| 547 | uint64_t keyRopeBnStride; | 555 | uint64_t keyRopeBnStride; |
| 548 | uint64_t keyRopeN2Stride; | 556 | uint64_t keyRopeN2Stride; |
| 557 | + uint64_t kScaleBnStride; | ||
| 558 | + uint64_t kScaleN2Stride; | ||
| 559 | + uint64_t vScaleBnStride; | ||
| 560 | + uint64_t vScaleN2Stride; | ||
| 561 | + uint64_t kOffsetBnStride; | ||
| 562 | + uint64_t kOffsetN2Stride; | ||
| 563 | + uint64_t vOffsetBnStride; | ||
| 564 | + uint64_t vOffsetN2Stride; | ||
| 549 | 565 | ||
| 550 | // prefix | 566 | // prefix |
| 551 | bool isActualSharedPrefixLenNull; | 567 | bool isActualSharedPrefixLenNull; |
| @@ -48,15 +48,30 @@ void TensorPreProcess(const aclTensorList *&tensorListKey, const aclTensorList * | |||
| 48 | return; | 48 | return; |
| 49 | } | 49 | } |
| 50 | auto tempKey = const_cast<aclTensorList *>(tensorListKey); | 50 | auto tempKey = const_cast<aclTensorList *>(tensorListKey); |
| 51 | + // 仅 INT4 输入场景(DT_INT32 打包存储):将 viewShape/StorageShape/viewStrides 换算到 int4 口径(末维 ×8) | ||
| 52 | + // SetViewShape 会把 viewStrides 重算成连续值,需用保存的原始 stride 换算后覆盖以保留非连续信息 | ||
| 51 | for (uint64_t i = 0; i < tempKey->Size(); i++) { | 53 | for (uint64_t i = 0; i < tempKey->Size(); i++) { |
| 52 | if ((*tempKey)[i] != nullptr) { | 54 | if ((*tempKey)[i] != nullptr) { |
| 53 | op::Shape viewShape = (*tempKey)[i]->GetViewShape(); | 55 | op::Shape viewShape = (*tempKey)[i]->GetViewShape(); |
| 54 | auto viewShapeDim = viewShape.GetDimNum(); | 56 | auto viewShapeDim = viewShape.GetDimNum(); |
| 57 | + auto origStrides = (*tempKey)[i]->GetViewStrides(); | ||
| 55 | if (viewShapeDim >= 1) { | 58 | if (viewShapeDim >= 1) { |
| 56 | viewShape[viewShapeDim - 1] = viewShape[viewShapeDim - 1] * INT4_NUMS_IN_INT32; | 59 | viewShape[viewShapeDim - 1] = viewShape[viewShapeDim - 1] * INT4_NUMS_IN_INT32; |
| 57 | } | 60 | } |
| 58 | (*tempKey)[i]->SetViewShape(viewShape); | 61 | (*tempKey)[i]->SetViewShape(viewShape); |
| 59 | (*tempKey)[i]->SetDataType(DataType::DT_INT4); | 62 | (*tempKey)[i]->SetDataType(DataType::DT_INT4); |
| 63 | + auto storageShape = (*tempKey)[i]->GetStorageShape(); | ||
| 64 | + if (storageShape.GetDimNum() >= 1) { | ||
| 65 | + storageShape[storageShape.GetDimNum() - 1] = | ||
| 66 | + storageShape[storageShape.GetDimNum() - 1] * INT4_NUMS_IN_INT32; | ||
| 67 | + (*tempKey)[i]->SetStorageShape(storageShape); | ||
| 68 | + } | ||
| 69 | + if (origStrides.size() > 0 && viewShapeDim >= 1) { | ||
| 70 | + for (uint64_t d = 0; d < origStrides.size() - 1; ++d) { | ||
| 71 | + origStrides[d] = origStrides[d] * static_cast<int64_t>(INT4_NUMS_IN_INT32); | ||
| 72 | + } | ||
| 73 | + (*tempKey)[i]->SetViewStrides(origStrides); | ||
| 74 | + } | ||
| 60 | } | 75 | } |
| 61 | } | 76 | } |
| 62 | 77 | ||
| @@ -65,11 +80,24 @@ void TensorPreProcess(const aclTensorList *&tensorListKey, const aclTensorList * | |||
| 65 | if ((*tempValue)[i] != nullptr) { | 80 | if ((*tempValue)[i] != nullptr) { |
| 66 | op::Shape viewShape = (*tempValue)[i]->GetViewShape(); | 81 | op::Shape viewShape = (*tempValue)[i]->GetViewShape(); |
| 67 | auto viewShapeDim = viewShape.GetDimNum(); | 82 | auto viewShapeDim = viewShape.GetDimNum(); |
| 83 | + auto origStrides = (*tempValue)[i]->GetViewStrides(); | ||
| 68 | if (viewShapeDim >= 1) { | 84 | if (viewShapeDim >= 1) { |
| 69 | viewShape[viewShapeDim - 1] = viewShape[viewShapeDim - 1] * INT4_NUMS_IN_INT32; | 85 | viewShape[viewShapeDim - 1] = viewShape[viewShapeDim - 1] * INT4_NUMS_IN_INT32; |
| 70 | } | 86 | } |
| 71 | (*tempValue)[i]->SetViewShape(viewShape); | 87 | (*tempValue)[i]->SetViewShape(viewShape); |
| 72 | (*tempValue)[i]->SetDataType(DataType::DT_INT4); | 88 | (*tempValue)[i]->SetDataType(DataType::DT_INT4); |
| 89 | + auto storageShapeV = (*tempValue)[i]->GetStorageShape(); | ||
| 90 | + if (storageShapeV.GetDimNum() >= 1) { | ||
| 91 | + storageShapeV[storageShapeV.GetDimNum() - 1] = | ||
| 92 | + storageShapeV[storageShapeV.GetDimNum() - 1] * INT4_NUMS_IN_INT32; | ||
| 93 | + (*tempValue)[i]->SetStorageShape(storageShapeV); | ||
| 94 | + } | ||
| 95 | + if (origStrides.size() > 0 && viewShapeDim >= 1) { | ||
| 96 | + for (uint64_t d = 0; d < origStrides.size() - 1; ++d) { | ||
| 97 | + origStrides[d] = origStrides[d] * static_cast<int64_t>(INT4_NUMS_IN_INT32); | ||
| 98 | + } | ||
| 99 | + (*tempValue)[i]->SetViewStrides(origStrides); | ||
| 100 | + } | ||
| 73 | } | 101 | } |
| 74 | } | 102 | } |
| 75 | 103 | ||
| @@ -97,16 +125,42 @@ void PrefixTensorPreProcess(const aclTensor *&tensorKey, const aclTensor *&tenso | |||
| 97 | auto tempKey = const_cast<aclTensor *>(tensorKey); | 125 | auto tempKey = const_cast<aclTensor *>(tensorKey); |
| 98 | op::Shape viewKeyShape = tempKey->GetViewShape(); | 126 | op::Shape viewKeyShape = tempKey->GetViewShape(); |
| 99 | auto viewKeyShapeDim = viewKeyShape.GetDimNum(); | 127 | auto viewKeyShapeDim = viewKeyShape.GetDimNum(); |
| 128 | + auto keyOrigStrides = tempKey->GetViewStrides(); | ||
| 100 | viewKeyShape[viewKeyShapeDim - 1] = viewKeyShape[viewKeyShapeDim - 1] * INT4_NUMS_IN_INT32; | 129 | viewKeyShape[viewKeyShapeDim - 1] = viewKeyShape[viewKeyShapeDim - 1] * INT4_NUMS_IN_INT32; |
| 101 | tempKey->SetViewShape(viewKeyShape); | 130 | tempKey->SetViewShape(viewKeyShape); |
| 102 | tempKey->SetDataType(DataType::DT_INT4); | 131 | tempKey->SetDataType(DataType::DT_INT4); |
| 132 | + auto keyStorageShape = tempKey->GetStorageShape(); | ||
| 133 | + if (keyStorageShape.GetDimNum() >= 1) { | ||
| 134 | + keyStorageShape[keyStorageShape.GetDimNum() - 1] = | ||
| 135 | + keyStorageShape[keyStorageShape.GetDimNum() - 1] * INT4_NUMS_IN_INT32; | ||
| 136 | + tempKey->SetStorageShape(keyStorageShape); | ||
| 137 | + } | ||
| 138 | + if (keyOrigStrides.size() > 0 && viewKeyShapeDim >= 1) { | ||
| 139 | + for (uint64_t d = 0; d < keyOrigStrides.size() - 1; ++d) { | ||
| 140 | + keyOrigStrides[d] = keyOrigStrides[d] * static_cast<int64_t>(INT4_NUMS_IN_INT32); | ||
| 141 | + } | ||
| 142 | + tempKey->SetViewStrides(keyOrigStrides); | ||
| 143 | + } | ||
| 103 | 144 | ||
| 104 | auto tempValue = const_cast<aclTensor *>(tensorValue); | 145 | auto tempValue = const_cast<aclTensor *>(tensorValue); |
| 105 | op::Shape viewValueShape = tempValue->GetViewShape(); | 146 | op::Shape viewValueShape = tempValue->GetViewShape(); |
| 106 | auto viewValueShapeDim = viewValueShape.GetDimNum(); | 147 | auto viewValueShapeDim = viewValueShape.GetDimNum(); |
| 148 | + auto valueOrigStrides = tempValue->GetViewStrides(); | ||
| 107 | viewValueShape[viewValueShapeDim - 1] = viewValueShape[viewValueShapeDim - 1] * INT4_NUMS_IN_INT32; | 149 | viewValueShape[viewValueShapeDim - 1] = viewValueShape[viewValueShapeDim - 1] * INT4_NUMS_IN_INT32; |
| 108 | tempValue->SetViewShape(viewValueShape); | 150 | tempValue->SetViewShape(viewValueShape); |
| 109 | tempValue->SetDataType(DataType::DT_INT4); | 151 | tempValue->SetDataType(DataType::DT_INT4); |
| 152 | + auto valueStorageShape = tempValue->GetStorageShape(); | ||
| 153 | + if (valueStorageShape.GetDimNum() >= 1) { | ||
| 154 | + valueStorageShape[valueStorageShape.GetDimNum() - 1] = | ||
| 155 | + valueStorageShape[valueStorageShape.GetDimNum() - 1] * INT4_NUMS_IN_INT32; | ||
| 156 | + tempValue->SetStorageShape(valueStorageShape); | ||
| 157 | + } | ||
| 158 | + if (valueOrigStrides.size() > 0 && viewValueShapeDim >= 1) { | ||
| 159 | + for (uint64_t d = 0; d < valueOrigStrides.size() - 1; ++d) { | ||
| 160 | + valueOrigStrides[d] = valueOrigStrides[d] * static_cast<int64_t>(INT4_NUMS_IN_INT32); | ||
| 161 | + } | ||
| 162 | + tempValue->SetViewStrides(valueOrigStrides); | ||
| 163 | + } | ||
| 110 | 164 | ||
| 111 | OP_LOGD("The conversion of kvPrefix from int32 to int4 is completed."); | 165 | OP_LOGD("The conversion of kvPrefix from int32 to int4 is completed."); |
| 112 | } | 166 | } |
| @@ -50,11 +50,10 @@ __attribute__((visibility("default"))) aclnnStatus aclnnFusedInferAttentionScore | |||
| 50 | int64_t valueAntiquantMode, int64_t queryQuantMode, int64_t pseType, const aclTensor *attentionOut, | 50 | int64_t valueAntiquantMode, int64_t queryQuantMode, int64_t pseType, const aclTensor *attentionOut, |
| 51 | const aclTensor *softmaxLse, uint64_t *workspaceSize, aclOpExecutor **executor); | 51 | const aclTensor *softmaxLse, uint64_t *workspaceSize, aclOpExecutor **executor); |
| 52 | 52 | ||
| 53 | -__attribute__((visibility("default"))) aclnnStatus CheckTensorContiguous(const aclTensorList *key, | 53 | +__attribute__((visibility("default"))) aclnnStatus |
| 54 | - const aclTensorList *value, | 54 | +CheckTensorContiguous(const aclTensorList *key, const aclTensorList *value, const aclTensor *keyAntiquantScaleOptional, |
| 55 | - const aclTensor *keyAntiquantScaleOptional, | 55 | + const aclTensor *valueAntiquantScaleOptional, const aclTensor *keyAntiquantOffsetOptional, |
| 56 | - const aclTensor *valueAntiquantScaleOptional, | 56 | + const aclTensor *valueAntiquantOffsetOptional, const aclTensor *keyRopeOptional); |
| 57 | - const aclTensor *keyRopeOptional); | ||
| 58 | 57 | ||
| 59 | aclnnStatus aclnnFusedInferAttentionScoreV5GetMaxWorkspaceSize( | 58 | aclnnStatus aclnnFusedInferAttentionScoreV5GetMaxWorkspaceSize( |
| 60 | const aclTensor *query, const aclTensorList *key, const aclTensorList *value, const aclTensor *pseShiftOptional, | 59 | const aclTensor *query, const aclTensorList *key, const aclTensorList *value, const aclTensor *pseShiftOptional, |
| @@ -164,7 +163,9 @@ aclnnStatus aclnnFusedInferAttentionScoreV5GetMaxWorkspaceSize( | |||
| 164 | 163 | ||
| 165 | aclnnStatus CheckTensorContiguous(const aclTensorList *key, const aclTensorList *value, | 164 | aclnnStatus CheckTensorContiguous(const aclTensorList *key, const aclTensorList *value, |
| 166 | const aclTensor *keyAntiquantScaleOptional, | 165 | const aclTensor *keyAntiquantScaleOptional, |
| 167 | - const aclTensor *valueAntiquantScaleOptional, const aclTensor *keyRopeOptional) | 166 | + const aclTensor *valueAntiquantScaleOptional, |
| 167 | + const aclTensor *keyAntiquantOffsetOptional, | ||
| 168 | + const aclTensor *valueAntiquantOffsetOptional, const aclTensor *keyRopeOptional) | ||
| 168 | { | 169 | { |
| 169 | if ((key != nullptr && !IsContiguous((*key)[0])) || (value != nullptr && !IsContiguous((*value)[0]))) { | 170 | if ((key != nullptr && !IsContiguous((*key)[0])) || (value != nullptr && !IsContiguous((*value)[0]))) { |
| 170 | return ACLNN_ERR_INNER_TILING_ERROR; | 171 | return ACLNN_ERR_INNER_TILING_ERROR; |
| @@ -173,7 +174,9 @@ aclnnStatus CheckTensorContiguous(const aclTensorList *key, const aclTensorList | |||
| 173 | return ACLNN_ERR_INNER_TILING_ERROR; | 174 | return ACLNN_ERR_INNER_TILING_ERROR; |
| 174 | } | 175 | } |
| 175 | if ((keyAntiquantScaleOptional != nullptr && !IsContiguous(keyAntiquantScaleOptional)) || | 176 | if ((keyAntiquantScaleOptional != nullptr && !IsContiguous(keyAntiquantScaleOptional)) || |
| 176 | - (valueAntiquantScaleOptional != nullptr && !IsContiguous(valueAntiquantScaleOptional))) { | 177 | + (valueAntiquantScaleOptional != nullptr && !IsContiguous(valueAntiquantScaleOptional)) || |
| 178 | + (keyAntiquantOffsetOptional != nullptr && !IsContiguous(keyAntiquantOffsetOptional)) || | ||
| 179 | + (valueAntiquantOffsetOptional != nullptr && !IsContiguous(valueAntiquantOffsetOptional))) { | ||
| 177 | return ACLNN_ERR_INNER_TILING_ERROR; | 180 | return ACLNN_ERR_INNER_TILING_ERROR; |
| 178 | } | 181 | } |
| 179 | return ACLNN_SUCCESS; | 182 | return ACLNN_SUCCESS; |
| @@ -207,8 +210,8 @@ aclnnStatus aclnnFusedInferAttentionScoreV5GetWorkspaceSize( | |||
| 207 | const aclTensor *tensorValueSharedPrefixOptional = valueSharedPrefixOptional; | 210 | const aclTensor *tensorValueSharedPrefixOptional = valueSharedPrefixOptional; |
| 208 | PrefixTensorPreProcess(tensorKeySharedPrefixOptional, tensorValueSharedPrefixOptional); | 211 | PrefixTensorPreProcess(tensorKeySharedPrefixOptional, tensorValueSharedPrefixOptional); |
| 209 | 212 | ||
| 210 | - aclnnStatus ret = | 213 | + aclnnStatus ret = CheckTensorContiguous(key, value, keyAntiquantScaleOptional, valueAntiquantScaleOptional, |
| 211 | - CheckTensorContiguous(key, value, keyAntiquantScaleOptional, valueAntiquantScaleOptional, keyRopeOptional); | 214 | + keyAntiquantOffsetOptional, valueAntiquantOffsetOptional, keyRopeOptional); |
| 212 | if (ret != ACLNN_SUCCESS && NnopbaseSupportTensorV2 == nullptr) { | 215 | if (ret != ACLNN_SUCCESS && NnopbaseSupportTensorV2 == nullptr) { |
| 213 | OP_LOGE(ACLNN_ERR_INNER_TILING_ERROR, "When tensor is not contiguous, opbase package version check failed"); | 216 | OP_LOGE(ACLNN_ERR_INNER_TILING_ERROR, "When tensor is not contiguous, opbase package version check failed"); |
| 214 | return ret; | 217 | return ret; |
Mattention/fused_infer_attention_score/op_host/arch35/fused_infer_attention_score_tiling_impl.cpp+8-0
| @@ -1829,6 +1829,14 @@ ge::graphStatus FusedInferAttentionScoreTilingImpl::SetFATilingData(const FiaTil | |||
| 1829 | inputParams.kRopeStrides.set_bnStride(fiaInfo.kRopeBnStride); | 1829 | inputParams.kRopeStrides.set_bnStride(fiaInfo.kRopeBnStride); |
| 1830 | inputParams.kRopeStrides.set_n2Stride(fiaInfo.kRopeN2Stride); | 1830 | inputParams.kRopeStrides.set_n2Stride(fiaInfo.kRopeN2Stride); |
| 1831 | } | 1831 | } |
| 1832 | + inputParams.kScaleStrides.set_bnStride(fiaInfo.kScaleBnStride); | ||
| 1833 | + inputParams.kScaleStrides.set_n2Stride(fiaInfo.kScaleN2Stride); | ||
| 1834 | + inputParams.vScaleStrides.set_bnStride(fiaInfo.vScaleBnStride); | ||
| 1835 | + inputParams.vScaleStrides.set_n2Stride(fiaInfo.vScaleN2Stride); | ||
| 1836 | + inputParams.kOffsetStrides.set_bnStride(fiaInfo.kOffsetBnStride); | ||
| 1837 | + inputParams.kOffsetStrides.set_n2Stride(fiaInfo.kOffsetN2Stride); | ||
| 1838 | + inputParams.vOffsetStrides.set_bnStride(fiaInfo.vOffsetBnStride); | ||
| 1839 | + inputParams.vOffsetStrides.set_n2Stride(fiaInfo.vOffsetN2Stride); | ||
| 1832 | } | 1840 | } |
| 1833 | inputParams.set_dSizeRope(DSIZE_64); | 1841 | inputParams.set_dSizeRope(DSIZE_64); |
| 1834 | inputParams.set_scaleValue(fiaInfo.scaleValue); | 1842 | inputParams.set_scaleValue(fiaInfo.scaleValue); |
| @@ -2194,12 +2194,167 @@ ge::graphStatus DequantChecker::CheckFeatureForAntiquant(const FiaTilingInfo &fi | |||
| 2194 | ge::GRAPH_SUCCESS != CheckFeatureQuerySForAntiquant(fiaInfo) || | 2194 | ge::GRAPH_SUCCESS != CheckFeatureQuerySForAntiquant(fiaInfo) || |
| 2195 | ge::GRAPH_SUCCESS != CheckFeaturePAForAntiquant(fiaInfo) || | 2195 | ge::GRAPH_SUCCESS != CheckFeaturePAForAntiquant(fiaInfo) || |
| 2196 | ge::GRAPH_SUCCESS != CheckFeatureRopeForAntiquant(fiaInfo) || | 2196 | ge::GRAPH_SUCCESS != CheckFeatureRopeForAntiquant(fiaInfo) || |
| 2197 | - ge::GRAPH_SUCCESS != CheckFeatureD032ForAntiquant(fiaInfo)) { | 2197 | + ge::GRAPH_SUCCESS != CheckFeatureD032ForAntiquant(fiaInfo) || |
| 2198 | + ge::GRAPH_SUCCESS != CheckStrideForAntiquant(fiaInfo)) { | ||
需要打开已有拦截,位于paged_attention_checker.cpp的 if (enableAntiQuant_ || fiaInfo.fullQuantMode == FiaFullQuantMode::Q_PER_TOKEN_HEAD_KV_PER_TENSOR_FULL_QUANT) ![]() ![]() | |||
| 2198 | return ge::GRAPH_FAILED; | 2199 | return ge::GRAPH_FAILED; |
| 2199 | } | 2200 | } |
| 2200 | return ge::GRAPH_SUCCESS; | 2201 | return ge::GRAPH_SUCCESS; |
| 2201 | } | 2202 | } |
| 2202 | 2203 | ||
| 2204 | +ge::graphStatus DequantChecker::CheckStrideForAntiquant(const FiaTilingInfo &fiaInfo) | ||
| 2205 | +{ | ||
| 2206 | + if (!fiaInfo.hasViewStride) { | ||
| 2207 | + return ge::GRAPH_SUCCESS; | ||
| 2208 | + } | ||
| 2209 | + const gert::Shape keyShape = fiaInfo.opParamInfo.key.shape->GetStorageShape(); | ||
| 2210 | + const uint32_t keyDimNum = keyShape.GetDimNum(); | ||
| 2211 | + const gert::Shape valueShape = fiaInfo.opParamInfo.value.shape->GetStorageShape(); | ||
| 2212 | + const uint32_t valueDimNum = valueShape.GetDimNum(); | ||
| 2213 | + int32_t dimIndex = 0; | ||
| 2214 | + | ||
| 2215 | + if (!fiaInfo.pageAttentionFlag) { | ||
| 2216 | + OP_CHECK_IF(CheckTensorContiguous(keyDimNum, keyShape, fiaInfo.keyStrides, dimIndex) != ge::GRAPH_SUCCESS, | ||
| 2217 | + OP_LOGE_FOR_INVALID_ARGUMENT_WITH_REASON( | ||
| 2218 | + fiaInfo.opName, "key", "In non-PA antiquant scenarios, non-contiguous key is not supported"), | ||
| 2219 | + return ge::GRAPH_FAILED); | ||
| 2220 | + OP_CHECK_IF( | ||
| 2221 | + CheckTensorContiguous(valueDimNum, valueShape, fiaInfo.valueStrides, dimIndex) != ge::GRAPH_SUCCESS, | ||
| 2222 | + OP_LOGE_FOR_INVALID_ARGUMENT_WITH_REASON( | ||
| 2223 | + fiaInfo.opName, "value", "In non-PA antiquant scenarios, non-contiguous value is not supported"), | ||
| 2224 | + return ge::GRAPH_FAILED); | ||
| 2225 | + if (fiaInfo.opParamInfo.keyAntiquantScale.tensor != nullptr) { | ||
| 2226 | + const auto &scaleShape = fiaInfo.opParamInfo.keyAntiquantScale.tensor->GetStorageShape(); | ||
| 2227 | + OP_CHECK_IF(CheckTensorContiguous(scaleShape.GetDimNum(), scaleShape, fiaInfo.kScaleStrides, dimIndex) != | ||
| 2228 | + ge::GRAPH_SUCCESS, | ||
| 2229 | + OP_LOGE_FOR_INVALID_ARGUMENT_WITH_REASON( | ||
| 2230 | + fiaInfo.opName, "key_antiquant_scale", | ||
| 2231 | + "In non-PA antiquant scenarios, non-contiguous key_antiquant_scale is not supported"), | ||
| 2232 | + return ge::GRAPH_FAILED); | ||
| 2233 | + } | ||
| 2234 | + if (fiaInfo.opParamInfo.valueAntiquantScale.tensor != nullptr) { | ||
| 2235 | + const auto &scaleShape = fiaInfo.opParamInfo.valueAntiquantScale.tensor->GetStorageShape(); | ||
| 2236 | + OP_CHECK_IF(CheckTensorContiguous(scaleShape.GetDimNum(), scaleShape, fiaInfo.vScaleStrides, dimIndex) != | ||
| 2237 | + ge::GRAPH_SUCCESS, | ||
| 2238 | + OP_LOGE_FOR_INVALID_ARGUMENT_WITH_REASON( | ||
| 2239 | + fiaInfo.opName, "value_antiquant_scale", | ||
| 2240 | + "In non-PA antiquant scenarios, non-contiguous value_antiquant_scale is not supported"), | ||
| 2241 | + return ge::GRAPH_FAILED); | ||
| 2242 | + } | ||
| 2243 | + if (fiaInfo.opParamInfo.keyAntiquantOffset.tensor != nullptr) { | ||
| 2244 | + const auto &offsetShape = fiaInfo.opParamInfo.keyAntiquantOffset.tensor->GetStorageShape(); | ||
| 2245 | + OP_CHECK_IF(CheckTensorContiguous(offsetShape.GetDimNum(), offsetShape, fiaInfo.kOffsetStrides, dimIndex) != | ||
| 2246 | + ge::GRAPH_SUCCESS, | ||
| 2247 | + OP_LOGE_FOR_INVALID_ARGUMENT_WITH_REASON( | ||
| 2248 | + fiaInfo.opName, "key_antiquant_offset", | ||
| 2249 | + "In non-PA antiquant scenarios, non-contiguous key_antiquant_offset is not supported"), | ||
| 2250 | + return ge::GRAPH_FAILED); | ||
| 2251 | + } | ||
| 2252 | + if (fiaInfo.opParamInfo.valueAntiquantOffset.tensor != nullptr) { | ||
| 2253 | + const auto &offsetShape = fiaInfo.opParamInfo.valueAntiquantOffset.tensor->GetStorageShape(); | ||
| 2254 | + OP_CHECK_IF(CheckTensorContiguous(offsetShape.GetDimNum(), offsetShape, fiaInfo.vOffsetStrides, dimIndex) != | ||
| 2255 | + ge::GRAPH_SUCCESS, | ||
| 2256 | + OP_LOGE_FOR_INVALID_ARGUMENT_WITH_REASON( | ||
| 2257 | + fiaInfo.opName, "value_antiquant_offset", | ||
| 2258 | + "In non-PA antiquant scenarios, non-contiguous value_antiquant_offset is not supported"), | ||
| 2259 | + return ge::GRAPH_FAILED); | ||
| 2260 | + } | ||
| 2261 | + return ge::GRAPH_SUCCESS; | ||
| 2262 | + } | ||
| 2263 | + | ||
| 2264 | + const bool isBBHLayout = (fiaInfo.kvLayout == FiaLayout::BnBsH); | ||
| 2265 | + auto checkStrideAllowed = [&](const char *tensorName, uint32_t dimNum, const gert::Shape &shape, | ||
| 2266 | + const gert::Stride *strides) -> ge::graphStatus { | ||
| 2267 | + if (strides == nullptr || strides->GetDimNum() == 0) { | ||
| 2268 | + return ge::GRAPH_SUCCESS; | ||
| 2269 | + } | ||
| 2270 | + if (CheckTensorContiguous(dimNum, shape, strides, dimIndex) != ge::GRAPH_SUCCESS) { | ||
| 2271 | + if (isBBHLayout && dimIndex != 0) { | ||
| 2272 | + OP_LOGE_FOR_INVALID_ARGUMENT_WITH_REASON( | ||
| 2273 | + fiaInfo.opName, tensorName, | ||
| 2274 | + "In PA BBH antiquant scenarios, non-contiguous is only supported in dimension 0"); | ||
| 2275 | + return ge::GRAPH_FAILED; | ||
| 2276 | + } else if (!isBBHLayout && dimIndex != 0 && dimIndex != 1) { | ||
| 2277 | + OP_LOGE_FOR_INVALID_ARGUMENT_WITH_REASON( | ||
| 2278 | + fiaInfo.opName, tensorName, | ||
| 2279 | + "In PA BNBD/NZ antiquant scenarios, non-contiguous is only supported in dimensions 0/1"); | ||
| 2280 | + return ge::GRAPH_FAILED; | ||
| 2281 | + } | ||
| 2282 | + } | ||
| 2283 | + return ge::GRAPH_SUCCESS; | ||
| 2284 | + }; | ||
| 2285 | + | ||
| 2286 | + // 非连续约束: 仅 mode4/mode5 支持, mode4 仅 axis0, mode5 允许 axis0+axis1 | ||
| 2287 | + auto checkScaleStrideAllowed = [&](const char *tensorName, uint32_t dimNum, const gert::Shape &shape, | ||
| 2288 | + const gert::Stride *strides, uint32_t antiquantMode) -> ge::graphStatus { | ||
| 2289 | + if (strides == nullptr || strides->GetDimNum() == 0) { | ||
| 2290 | + return ge::GRAPH_SUCCESS; | ||
| 2291 | + } | ||
| 2292 | + if (CheckTensorContiguous(dimNum, shape, strides, dimIndex) != ge::GRAPH_SUCCESS) { | ||
| 2293 | + if (antiquantMode != PER_TOKEN_PA_MODE && antiquantMode != PER_TOKEN_HEAD_PA_MODE) { | ||
| 2294 | + OP_LOGE_FOR_INVALID_VALUE_WITH_REASON( | ||
| 2295 | + fiaInfo.opName, tensorName, std::to_string(antiquantMode).c_str(), | ||
| 2296 | + "In PA antiquant scenarios, non-contiguous is only supported in per-token-pa(mode4) " | ||
| 2297 | + "or per-token-head-pa(mode5) mode"); | ||
| 2298 | + return ge::GRAPH_FAILED; | ||
| 2299 | + } | ||
| 2300 | + if (antiquantMode == PER_TOKEN_PA_MODE) { | ||
| 2301 | + if (dimIndex != 0) { | ||
| 2302 | + OP_LOGE_FOR_INVALID_ARGUMENT_WITH_REASON( | ||
| 2303 | + fiaInfo.opName, tensorName, | ||
| 2304 | + "In PA mode4 antiquant scenarios, non-contiguous is only supported in dimension 0(Bn)"); | ||
| 2305 | + return ge::GRAPH_FAILED; | ||
| 2306 | + } | ||
| 2307 | + } else { | ||
| 2308 | + if (dimIndex != 0 && dimIndex != 1) { | ||
| 2309 | + OP_LOGE_FOR_INVALID_ARGUMENT_WITH_REASON( | ||
| 2310 | + fiaInfo.opName, tensorName, | ||
| 2311 | + "In PA mode5 antiquant scenarios, non-contiguous is only supported in dimensions 0(Bn)/1(N2)"); | ||
| 2312 | + return ge::GRAPH_FAILED; | ||
| 2313 | + } | ||
| 2314 | + } | ||
| 2315 | + } | ||
| 2316 | + return ge::GRAPH_SUCCESS; | ||
| 2317 | + }; | ||
| 2318 | + | ||
| 2319 | + if (checkStrideAllowed("key", keyDimNum, keyShape, fiaInfo.keyStrides) != ge::GRAPH_SUCCESS) { | ||
| 2320 | + return ge::GRAPH_FAILED; | ||
| 2321 | + } | ||
| 2322 | + if (checkStrideAllowed("value", valueDimNum, valueShape, fiaInfo.valueStrides) != ge::GRAPH_SUCCESS) { | ||
| 2323 | + return ge::GRAPH_FAILED; | ||
| 2324 | + } | ||
| 2325 | + if (fiaInfo.opParamInfo.keyAntiquantScale.tensor != nullptr) { | ||
| 2326 | + const auto &scaleShape = fiaInfo.opParamInfo.keyAntiquantScale.tensor->GetStorageShape(); | ||
| 2327 | + if (checkScaleStrideAllowed("k_descale", scaleShape.GetDimNum(), scaleShape, fiaInfo.kScaleStrides, | ||
| 2328 | + fiaInfo.keyAntiquantMode) != ge::GRAPH_SUCCESS) { | ||
| 2329 | + return ge::GRAPH_FAILED; | ||
| 2330 | + } | ||
| 2331 | + } | ||
| 2332 | + if (fiaInfo.opParamInfo.valueAntiquantScale.tensor != nullptr) { | ||
| 2333 | + const auto &scaleShape = fiaInfo.opParamInfo.valueAntiquantScale.tensor->GetStorageShape(); | ||
| 2334 | + if (checkScaleStrideAllowed("v_descale", scaleShape.GetDimNum(), scaleShape, fiaInfo.vScaleStrides, | ||
| 2335 | + fiaInfo.valueAntiquantMode) != ge::GRAPH_SUCCESS) { | ||
| 2336 | + return ge::GRAPH_FAILED; | ||
| 2337 | + } | ||
| 2338 | + } | ||
| 2339 | + // offset 与 scale 同构: offsetShape == scaleShape (CheckOffsetShapeForAntiquant 强制), | ||
| 2340 | + // 非连续约束两道门与 scale 完全一致, 复用 checkScaleStrideAllowed | ||
| 2341 | + if (fiaInfo.opParamInfo.keyAntiquantOffset.tensor != nullptr) { | ||
| 2342 | + const auto &offsetShape = fiaInfo.opParamInfo.keyAntiquantOffset.tensor->GetStorageShape(); | ||
| 2343 | + if (checkScaleStrideAllowed("k_offset", offsetShape.GetDimNum(), offsetShape, fiaInfo.kOffsetStrides, | ||
| 2344 | + fiaInfo.keyAntiquantMode) != ge::GRAPH_SUCCESS) { | ||
| 2345 | + return ge::GRAPH_FAILED; | ||
| 2346 | + } | ||
| 2347 | + } | ||
| 2348 | + if (fiaInfo.opParamInfo.valueAntiquantOffset.tensor != nullptr) { | ||
| 2349 | + const auto &offsetShape = fiaInfo.opParamInfo.valueAntiquantOffset.tensor->GetStorageShape(); | ||
| 2350 | + if (checkScaleStrideAllowed("v_offset", offsetShape.GetDimNum(), offsetShape, fiaInfo.vOffsetStrides, | ||
| 2351 | + fiaInfo.valueAntiquantMode) != ge::GRAPH_SUCCESS) { | ||
| 2352 | + return ge::GRAPH_FAILED; | ||
| 2353 | + } | ||
| 2354 | + } | ||
| 2355 | + return ge::GRAPH_SUCCESS; | ||
| 2356 | +} | ||
| 2357 | + | ||
| 2203 | ge::graphStatus DequantChecker::CheckFeatureLayoutForAntiquant(const FiaTilingInfo &fiaInfo) const | 2358 | ge::graphStatus DequantChecker::CheckFeatureLayoutForAntiquant(const FiaTilingInfo &fiaInfo) const |
| 2204 | { | 2359 | { |
| 2205 | // 伪量化场景校验支持的inputLayout | 2360 | // 伪量化场景校验支持的inputLayout |
| @@ -117,6 +117,7 @@ private: | |||
| 117 | ge::graphStatus CheckFeaturePAForAntiquant(const FiaTilingInfo &fiaInfo) const; | 117 | ge::graphStatus CheckFeaturePAForAntiquant(const FiaTilingInfo &fiaInfo) const; |
| 118 | ge::graphStatus CheckFeatureRopeForAntiquant(const FiaTilingInfo &fiaInfo) const; | 118 | ge::graphStatus CheckFeatureRopeForAntiquant(const FiaTilingInfo &fiaInfo) const; |
| 119 | ge::graphStatus CheckFeatureD032ForAntiquant(const FiaTilingInfo &fiaInfo) const; | 119 | ge::graphStatus CheckFeatureD032ForAntiquant(const FiaTilingInfo &fiaInfo) const; |
| 120 | + ge::graphStatus CheckStrideForAntiquant(const FiaTilingInfo &fiaInfo); | ||
| 120 | 121 | ||
| 121 | // MultiPara | 122 | // MultiPara |
| 122 | ge::graphStatus CheckMultiParaForAntiquant(const FiaTilingInfo &fiaInfo); | 123 | ge::graphStatus CheckMultiParaForAntiquant(const FiaTilingInfo &fiaInfo); |
| @@ -745,11 +745,11 @@ ge::graphStatus PagedAttentionChecker::CheckNonContiguousSupport(const FiaTiling | |||
| 745 | "Non-contiguous cache is not supported on the current architecture."), | 745 | "Non-contiguous cache is not supported on the current architecture."), |
| 746 | return ge::GRAPH_FAILED); | 746 | return ge::GRAPH_FAILED); |
| 747 | 747 | ||
| 748 | - if (enableAntiQuant_ || (fiaInfo.fullQuantMode == FiaFullQuantMode::Q_PER_TOKEN_HEAD_KV_PER_TENSOR_FULL_QUANT && | 748 | + if (fiaInfo.fullQuantMode == FiaFullQuantMode::Q_PER_TOKEN_HEAD_KV_PER_TENSOR_FULL_QUANT && |
| 749 | - !(inputLayout == "TND" && fiaInfo.inputQType == ge::DT_FLOAT8_E4M3FN))) { | 749 | + !(inputLayout == "TND" && fiaInfo.inputQType == ge::DT_FLOAT8_E4M3FN)) { |
| 750 | OP_CHECK_IF(keyDim != -1, | 750 | OP_CHECK_IF(keyDim != -1, |
| 751 | OP_LOGE_FOR_INVALID_ARGUMENT_WITH_REASON(fiaInfo.opName, "key", | 751 | OP_LOGE_FOR_INVALID_ARGUMENT_WITH_REASON(fiaInfo.opName, "key", |
| 752 | - ("In anti-quant or non-FP8 TND mla fullquant scenarios, " | 752 | + ("In non-FP8 TND mla fullquant scenarios, " |
| 753 | "PA does not support non-contiguous key tensors, " | 753 | "PA does not support non-contiguous key tensors, " |
| 754 | "but the first non-contiguous dimension is index " + | 754 | "but the first non-contiguous dimension is index " + |
| 755 | std::to_string(keyDim)) | 755 | std::to_string(keyDim)) |
| @@ -757,7 +757,7 @@ ge::graphStatus PagedAttentionChecker::CheckNonContiguousSupport(const FiaTiling | |||
| 757 | return ge::GRAPH_FAILED); | 757 | return ge::GRAPH_FAILED); |
| 758 | OP_CHECK_IF(valueDim != -1, | 758 | OP_CHECK_IF(valueDim != -1, |
| 759 | OP_LOGE_FOR_INVALID_ARGUMENT_WITH_REASON(fiaInfo.opName, "value", | 759 | OP_LOGE_FOR_INVALID_ARGUMENT_WITH_REASON(fiaInfo.opName, "value", |
| 760 | - ("In anti-quant or non-FP8 TND mla fullquant scenarios, " | 760 | + ("In non-FP8 TND mla fullquant scenarios, " |
| 761 | "PA does not support non-contiguous value tensors, " | 761 | "PA does not support non-contiguous value tensors, " |
| 762 | "but the first non-contiguous dimension is index " + | 762 | "but the first non-contiguous dimension is index " + |
| 763 | std::to_string(valueDim)) | 763 | std::to_string(valueDim)) |
| @@ -765,7 +765,7 @@ ge::graphStatus PagedAttentionChecker::CheckNonContiguousSupport(const FiaTiling | |||
| 765 | return ge::GRAPH_FAILED); | 765 | return ge::GRAPH_FAILED); |
| 766 | OP_CHECK_IF(keyRopeDim != -1, | 766 | OP_CHECK_IF(keyRopeDim != -1, |
| 767 | OP_LOGE_FOR_INVALID_ARGUMENT_WITH_REASON(fiaInfo.opName, "keyRope", | 767 | OP_LOGE_FOR_INVALID_ARGUMENT_WITH_REASON(fiaInfo.opName, "keyRope", |
| 768 | - ("In anti-quant or non-FP8 TND mla fullquant scenarios, " | 768 | + ("In non-FP8 TND mla fullquant scenarios, " |
| 769 | "PA does not support non-contiguous keyRope tensors, " | 769 | "PA does not support non-contiguous keyRope tensors, " |
| 770 | "but the first non-contiguous dimension is index " + | 770 | "but the first non-contiguous dimension is index " + |
| 771 | std::to_string(keyRopeDim)) | 771 | std::to_string(keyRopeDim)) |
| @@ -773,6 +773,17 @@ ge::graphStatus PagedAttentionChecker::CheckNonContiguousSupport(const FiaTiling | |||
| 773 | return ge::GRAPH_FAILED); | 773 | return ge::GRAPH_FAILED); |
| 774 | return ge::GRAPH_SUCCESS; | 774 | return ge::GRAPH_SUCCESS; |
| 775 | } | 775 | } |
| 776 | + // antiquant 不支持 keyRope 非连续(kRopeStrides 不处理),key/value 放行到下方布局级检查 | ||
| 777 | + if (enableAntiQuant_) { | ||
| 778 | + OP_CHECK_IF(keyRopeDim != -1, | ||
| 779 | + OP_LOGE_FOR_INVALID_ARGUMENT_WITH_REASON( | ||
| 780 | + fiaInfo.opName, "keyRope", | ||
| 781 | + ("In anti-quant scenarios, PA does not support non-contiguous keyRope tensors, " | ||
| 782 | + "but the first non-contiguous dimension is index " + | ||
| 783 | + std::to_string(keyRopeDim) + ".") | ||
| 784 | + .c_str()), | ||
| 785 | + return ge::GRAPH_FAILED); | ||
| 786 | + } | ||
| 776 | 787 | ||
| 777 | if (fiaInfo.kvLayout == FiaLayout::BnBsH) { | 788 | if (fiaInfo.kvLayout == FiaLayout::BnBsH) { |
| 778 | OP_CHECK_IF(keyDim > 0, | 789 | OP_CHECK_IF(keyDim > 0, |
| @@ -237,6 +237,8 @@ public: | |||
| 237 | const gert::Stride *kRopeStrides = nullptr; | 237 | const gert::Stride *kRopeStrides = nullptr; |
| 238 | const gert::Stride *kScaleStrides = nullptr; | 238 | const gert::Stride *kScaleStrides = nullptr; |
| 239 | const gert::Stride *vScaleStrides = nullptr; | 239 | const gert::Stride *vScaleStrides = nullptr; |
| 240 | + const gert::Stride *kOffsetStrides = nullptr; | ||
| 241 | + const gert::Stride *vOffsetStrides = nullptr; | ||
| 240 | bool hasViewStride = true; | 242 | bool hasViewStride = true; |
| 241 | uint64_t keyBnStride = 0; | 243 | uint64_t keyBnStride = 0; |
| 242 | uint64_t keyN2Stride = 0; | 244 | uint64_t keyN2Stride = 0; |
| @@ -244,6 +246,14 @@ public: | |||
| 244 | uint64_t valueN2Stride = 0; | 246 | uint64_t valueN2Stride = 0; |
| 245 | uint64_t kRopeBnStride = 0; | 247 | uint64_t kRopeBnStride = 0; |
| 246 | uint64_t kRopeN2Stride = 0; | 248 | uint64_t kRopeN2Stride = 0; |
| 249 | + uint64_t kScaleBnStride = 0; | ||
| 250 | + uint64_t kScaleN2Stride = 0; | ||
| 251 | + uint64_t vScaleBnStride = 0; | ||
| 252 | + uint64_t vScaleN2Stride = 0; | ||
| 253 | + uint64_t kOffsetBnStride = 0; | ||
| 254 | + uint64_t kOffsetN2Stride = 0; | ||
| 255 | + uint64_t vOffsetBnStride = 0; | ||
| 256 | + uint64_t vOffsetN2Stride = 0; | ||
| 247 | // empty Tensor | 257 | // empty Tensor |
| 248 | bool emptyTensorFlag = false; | 258 | bool emptyTensorFlag = false; |
| 249 | uint64_t totalOutputSize = 0; | 259 | uint64_t totalOutputSize = 0; |
| @@ -347,7 +357,7 @@ public: | |||
| 347 | FiaLayout outLayout = FiaLayout::BSND; | 357 | FiaLayout outLayout = FiaLayout::BSND; |
| 348 | FiaLayout kvLayout = FiaLayout::BSND; | 358 | FiaLayout kvLayout = FiaLayout::BSND; |
| 349 | 359 | ||
| 350 | - // NZ layout D0 from KV shape last dim (16 for fp16/bf16/fp8, 32 for int8) | 360 | + // NZ layout D0 from KV shape last dim |
| 351 | uint32_t kvCacheNzD0 = 0; | 361 | uint32_t kvCacheNzD0 = 0; |
| 352 | }; | 362 | }; |
| 353 | } // namespace optiling | 363 | } // namespace optiling |
| @@ -1731,7 +1731,7 @@ public: | |||
| 1731 | ge::DT_FLOAT16, ge::DT_FLOAT16, ge::DT_FLOAT16, ge::DT_FLOAT16, ge::DT_FLOAT16, | 1731 | ge::DT_FLOAT16, ge::DT_FLOAT16, ge::DT_FLOAT16, ge::DT_FLOAT16, ge::DT_FLOAT16, |
| 1732 | ge::DT_FLOAT16, ge::DT_FLOAT16, ge::DT_FLOAT16, ge::DT_FLOAT16}) | 1732 | ge::DT_FLOAT16, ge::DT_FLOAT16, ge::DT_FLOAT16, ge::DT_FLOAT16}) |
| 1733 | .FormatList({ge::FORMAT_ND}) | 1733 | .FormatList({ge::FORMAT_ND}) |
| 1734 | - .AutoContiguous(); | 1734 | + .IgnoreContiguous(); |
| 1735 | aicore_config_95.Input("value_antiquant_scale") | 1735 | aicore_config_95.Input("value_antiquant_scale") |
| 1736 | .ParamType(OPTIONAL) | 1736 | .ParamType(OPTIONAL) |
| 1737 | .DataType( | 1737 | .DataType( |
| @@ -1899,7 +1899,7 @@ public: | |||
| 1899 | ge::DT_FLOAT16, ge::DT_FLOAT16, ge::DT_FLOAT16, ge::DT_FLOAT16, ge::DT_FLOAT16, | 1899 | ge::DT_FLOAT16, ge::DT_FLOAT16, ge::DT_FLOAT16, ge::DT_FLOAT16, ge::DT_FLOAT16, |
| 1900 | ge::DT_FLOAT16, ge::DT_FLOAT16, ge::DT_FLOAT16, ge::DT_FLOAT16}) | 1900 | ge::DT_FLOAT16, ge::DT_FLOAT16, ge::DT_FLOAT16, ge::DT_FLOAT16}) |
| 1901 | .FormatList({ge::FORMAT_ND}) | 1901 | .FormatList({ge::FORMAT_ND}) |
| 1902 | - .AutoContiguous(); | 1902 | + .IgnoreContiguous(); |
| 1903 | aicore_config_95.Input("key_shared_prefix") | 1903 | aicore_config_95.Input("key_shared_prefix") |
| 1904 | .ParamType(OPTIONAL) | 1904 | .ParamType(OPTIONAL) |
| 1905 | .DataType({ge::DT_FLOAT16, ge::DT_FLOAT16, ge::DT_BF16, ge::DT_INT8, | 1905 | .DataType({ge::DT_FLOAT16, ge::DT_FLOAT16, ge::DT_BF16, ge::DT_INT8, |
Mattention/fused_infer_attention_score/op_host/fused_infer_attention_score_tiling_info_parser.cpp+93-0
| @@ -319,6 +319,11 @@ ge::graphStatus FiaInfoParser::GetStrides() | |||
| 319 | const bool keyScaleIsView = hasSingleKvTensor && hasKeyScale && context_->InputIsView(KEY_ANTIQUANT_SCALE_INDEX); | 319 | const bool keyScaleIsView = hasSingleKvTensor && hasKeyScale && context_->InputIsView(KEY_ANTIQUANT_SCALE_INDEX); |
| 320 | const bool valueScaleIsView = | 320 | const bool valueScaleIsView = |
| 321 | hasSingleKvTensor && hasValueScale && context_->InputIsView(VALUE_ANTIQUANT_SCALE_INDEX); | 321 | hasSingleKvTensor && hasValueScale && context_->InputIsView(VALUE_ANTIQUANT_SCALE_INDEX); |
| 322 | + const bool hasKeyOffset = opParamInfo_.keyAntiquantOffset.tensor != nullptr; | ||
| 323 | + const bool hasValueOffset = opParamInfo_.valueAntiquantOffset.tensor != nullptr; | ||
| 324 | + const bool keyOffsetIsView = hasSingleKvTensor && hasKeyOffset && context_->InputIsView(KEY_ANTIQUANT_OFFSET_INDEX); | ||
【一般】这个和下面的IsView的两个变量,没有参与到后面hasViewStrides的判决中,是否符合预期?下面的hasViewStrides判决为false之后就提前返回了,导致后续的许多变量不会读取 ![]() ![]() | |||
| 325 | + const bool valueOffsetIsView = | ||
| 326 | + hasSingleKvTensor && hasValueOffset && context_->InputIsView(VALUE_ANTIQUANT_OFFSET_INDEX); | ||
| 322 | 327 | ||
| 323 | if (hasKeyRope) { | 328 | if (hasKeyRope) { |
| 324 | kRopeStrides_ = context_->GetOptionalInputStride(KEY_ROPE_INDEX); | 329 | kRopeStrides_ = context_->GetOptionalInputStride(KEY_ROPE_INDEX); |
| @@ -352,6 +357,18 @@ ge::graphStatus FiaInfoParser::GetStrides() | |||
| 352 | if (!HasUsableStride(vScaleStrides_)) { | 357 | if (!HasUsableStride(vScaleStrides_)) { |
| 353 | vScaleStrides_ = nullptr; | 358 | vScaleStrides_ = nullptr; |
| 354 | } | 359 | } |
| 360 | + if (hasKeyOffset) { | ||
| 361 | + kOffsetStrides_ = context_->GetOptionalInputStride(KEY_ANTIQUANT_OFFSET_INDEX); | ||
| 362 | + } | ||
| 363 | + if (hasValueOffset) { | ||
| 364 | + vOffsetStrides_ = context_->GetOptionalInputStride(VALUE_ANTIQUANT_OFFSET_INDEX); | ||
| 365 | + } | ||
| 366 | + if (!HasUsableStride(kOffsetStrides_)) { | ||
| 367 | + kOffsetStrides_ = nullptr; | ||
| 368 | + } | ||
| 369 | + if (!HasUsableStride(vOffsetStrides_)) { | ||
| 370 | + vOffsetStrides_ = nullptr; | ||
| 371 | + } | ||
| 355 | 372 | ||
| 356 | OP_CHECK_IF(keyIsView && !HasStrideMetadata(kStrideCache_), | 373 | OP_CHECK_IF(keyIsView && !HasStrideMetadata(kStrideCache_), |
| 357 | OP_LOGE(opName_, "Failed to get stride metadata for view input key."), return ge::GRAPH_FAILED); | 374 | OP_LOGE(opName_, "Failed to get stride metadata for view input key."), return ge::GRAPH_FAILED); |
| @@ -365,6 +382,12 @@ ge::graphStatus FiaInfoParser::GetStrides() | |||
| 365 | OP_CHECK_IF(valueScaleIsView && vScaleStrides_ == nullptr, | 382 | OP_CHECK_IF(valueScaleIsView && vScaleStrides_ == nullptr, |
| 366 | OP_LOGE(opName_, "Failed to get stride metadata for view input valueAntiquantScale."), | 383 | OP_LOGE(opName_, "Failed to get stride metadata for view input valueAntiquantScale."), |
| 367 | return ge::GRAPH_FAILED); | 384 | return ge::GRAPH_FAILED); |
| 385 | + OP_CHECK_IF(keyOffsetIsView && kOffsetStrides_ == nullptr, | ||
| 386 | + OP_LOGE(opName_, "Failed to get stride metadata for view input keyAntiquantOffset."), | ||
| 387 | + return ge::GRAPH_FAILED); | ||
| 388 | + OP_CHECK_IF(valueOffsetIsView && vOffsetStrides_ == nullptr, | ||
| 389 | + OP_LOGE(opName_, "Failed to get stride metadata for view input valueAntiquantOffset."), | ||
| 390 | + return ge::GRAPH_FAILED); | ||
| 368 | 391 | ||
| 369 | if (HasUsableStride(kStrideCache_[0])) { | 392 | if (HasUsableStride(kStrideCache_[0])) { |
| 370 | keyStrides_ = kStrideCache_[0]; | 393 | keyStrides_ = kStrideCache_[0]; |
| @@ -420,6 +443,30 @@ ge::graphStatus FiaInfoParser::GetStrides() | |||
| 420 | return ge::GRAPH_FAILED; | 443 | return ge::GRAPH_FAILED; |
| 421 | } | 444 | } |
| 422 | } | 445 | } |
| 446 | + if (hasKeyOffset) { | ||
| 447 | + if (kOffsetStrides_ == nullptr) { | ||
| 448 | + BuildContiguousStride(opParamInfo_.keyAntiquantOffset.tensor->GetStorageShape(), | ||
| 449 | + keyOffsetContiguousStrides_); | ||
| 450 | + kOffsetStrides_ = &keyOffsetContiguousStrides_; | ||
| 451 | + } | ||
| 452 | + if (ValidateStrideLocal(opName_, "keyAntiquantOffset", | ||
| 453 | + opParamInfo_.keyAntiquantOffset.tensor->GetStorageShape(), | ||
| 454 | + kOffsetStrides_) != ge::GRAPH_SUCCESS) { | ||
| 455 | + return ge::GRAPH_FAILED; | ||
| 456 | + } | ||
| 457 | + } | ||
| 458 | + if (hasValueOffset) { | ||
| 459 | + if (vOffsetStrides_ == nullptr) { | ||
| 460 | + BuildContiguousStride(opParamInfo_.valueAntiquantOffset.tensor->GetStorageShape(), | ||
| 461 | + valueOffsetContiguousStrides_); | ||
| 462 | + vOffsetStrides_ = &valueOffsetContiguousStrides_; | ||
| 463 | + } | ||
| 464 | + if (ValidateStrideLocal(opName_, "valueAntiquantOffset", | ||
| 465 | + opParamInfo_.valueAntiquantOffset.tensor->GetStorageShape(), | ||
| 466 | + vOffsetStrides_) != ge::GRAPH_SUCCESS) { | ||
| 467 | + return ge::GRAPH_FAILED; | ||
| 468 | + } | ||
| 469 | + } | ||
| 423 | if (hasKeyRope) { | 470 | if (hasKeyRope) { |
| 424 | if (kRopeStrides_ == nullptr) { | 471 | if (kRopeStrides_ == nullptr) { |
| 425 | BuildContiguousStride(opParamInfo_.keyRope.tensor->GetStorageShape(), keyRopeContiguousStrides_); | 472 | BuildContiguousStride(opParamInfo_.keyRope.tensor->GetStorageShape(), keyRopeContiguousStrides_); |
| @@ -498,6 +545,42 @@ ge::graphStatus FiaInfoParser::GetKvStrideValues() | |||
| 498 | kRopeN2Stride_ = kRopeStrides_->GetStride(1); | 545 | kRopeN2Stride_ = kRopeStrides_->GetStride(1); |
| 499 | } | 546 | } |
| 500 | } | 547 | } |
| 548 | + // 仅 mode4/mode5 scale 及 offset 支持非连续 stride 传入: | ||
| 549 | + // mode4 shape [blockNum, bs], mode5 shape [blockNum, N2, bs] | ||
| 550 | + if (opParamInfo_.keyAntiquantMode != nullptr) { | ||
| 551 | + const auto mode = *opParamInfo_.keyAntiquantMode; | ||
| 552 | + if (mode == optiling::arch35FIA::PER_TOKEN_PA_MODE) { | ||
| 553 | + if (kScaleStrides_ != nullptr) { | ||
| 554 | + kScaleBnStride_ = kScaleStrides_->GetStride(0); | ||
| 555 | + } | ||
| 556 | + if (vScaleStrides_ != nullptr) { | ||
| 557 | + vScaleBnStride_ = vScaleStrides_->GetStride(0); | ||
| 558 | + } | ||
| 559 | + if (kOffsetStrides_ != nullptr) { | ||
| 560 | + kOffsetBnStride_ = kOffsetStrides_->GetStride(0); | ||
| 561 | + } | ||
| 562 | + if (vOffsetStrides_ != nullptr) { | ||
| 563 | + vOffsetBnStride_ = vOffsetStrides_->GetStride(0); | ||
| 564 | + } | ||
| 565 | + } else if (mode == optiling::arch35FIA::PER_TOKEN_HEAD_PA_MODE) { | ||
| 566 | + if (kScaleStrides_ != nullptr) { | ||
| 567 | + kScaleBnStride_ = kScaleStrides_->GetStride(0); | ||
| 568 | + kScaleN2Stride_ = kScaleStrides_->GetStride(1); | ||
| 569 | + } | ||
| 570 | + if (vScaleStrides_ != nullptr) { | ||
| 571 | + vScaleBnStride_ = vScaleStrides_->GetStride(0); | ||
| 572 | + vScaleN2Stride_ = vScaleStrides_->GetStride(1); | ||
| 573 | + } | ||
| 574 | + if (kOffsetStrides_ != nullptr) { | ||
| 575 | + kOffsetBnStride_ = kOffsetStrides_->GetStride(0); | ||
| 576 | + kOffsetN2Stride_ = kOffsetStrides_->GetStride(1); | ||
| 577 | + } | ||
| 578 | + if (vOffsetStrides_ != nullptr) { | ||
| 579 | + vOffsetBnStride_ = vOffsetStrides_->GetStride(0); | ||
| 580 | + vOffsetN2Stride_ = vOffsetStrides_->GetStride(1); | ||
| 581 | + } | ||
| 582 | + } | ||
| 583 | + } | ||
| 501 | return ge::GRAPH_SUCCESS; | 584 | return ge::GRAPH_SUCCESS; |
| 502 | } | 585 | } |
| 503 | 586 | ||
| @@ -1781,6 +1864,8 @@ void FiaInfoParser::GenerateInfo(FiaTilingInfo &fiaInfo) | |||
| 1781 | fiaInfo.kRopeStrides = kRopeStrides_; | 1864 | fiaInfo.kRopeStrides = kRopeStrides_; |
| 1782 | fiaInfo.kScaleStrides = kScaleStrides_; | 1865 | fiaInfo.kScaleStrides = kScaleStrides_; |
| 1783 | fiaInfo.vScaleStrides = vScaleStrides_; | 1866 | fiaInfo.vScaleStrides = vScaleStrides_; |
| 1867 | + fiaInfo.kOffsetStrides = kOffsetStrides_; | ||
| 1868 | + fiaInfo.vOffsetStrides = vOffsetStrides_; | ||
| 1784 | fiaInfo.hasViewStride = hasViewStride_; | 1869 | fiaInfo.hasViewStride = hasViewStride_; |
| 1785 | fiaInfo.keyBnStride = keyBnStride_; | 1870 | fiaInfo.keyBnStride = keyBnStride_; |
| 1786 | fiaInfo.keyN2Stride = keyN2Stride_; | 1871 | fiaInfo.keyN2Stride = keyN2Stride_; |
| @@ -1788,6 +1873,14 @@ void FiaInfoParser::GenerateInfo(FiaTilingInfo &fiaInfo) | |||
| 1788 | fiaInfo.valueN2Stride = valueN2Stride_; | 1873 | fiaInfo.valueN2Stride = valueN2Stride_; |
| 1789 | fiaInfo.kRopeBnStride = kRopeBnStride_; | 1874 | fiaInfo.kRopeBnStride = kRopeBnStride_; |
| 1790 | fiaInfo.kRopeN2Stride = kRopeN2Stride_; | 1875 | fiaInfo.kRopeN2Stride = kRopeN2Stride_; |
| 1876 | + fiaInfo.kScaleBnStride = kScaleBnStride_; | ||
| 1877 | + fiaInfo.kScaleN2Stride = kScaleN2Stride_; | ||
| 1878 | + fiaInfo.vScaleBnStride = vScaleBnStride_; | ||
| 1879 | + fiaInfo.vScaleN2Stride = vScaleN2Stride_; | ||
| 1880 | + fiaInfo.kOffsetBnStride = kOffsetBnStride_; | ||
| 1881 | + fiaInfo.kOffsetN2Stride = kOffsetN2Stride_; | ||
| 1882 | + fiaInfo.vOffsetBnStride = vOffsetBnStride_; | ||
| 1883 | + fiaInfo.vOffsetN2Stride = vOffsetN2Stride_; | ||
| 1791 | 1884 | ||
| 1792 | fiaInfo.totalOutputSize = opParamInfo_.attenOut.shape->GetStorageShape().GetShapeSize(); | 1885 | fiaInfo.totalOutputSize = opParamInfo_.attenOut.shape->GetStorageShape().GetShapeSize(); |
| 1793 | 1886 | ||
Mattention/fused_infer_attention_score/op_host/fused_infer_attention_score_tiling_info_parser.h+12-0
| @@ -154,8 +154,12 @@ public: | |||
| 154 | const gert::Stride *kRopeStrides_ = nullptr; | 154 | const gert::Stride *kRopeStrides_ = nullptr; |
| 155 | gert::Stride keyScaleContiguousStrides_; | 155 | gert::Stride keyScaleContiguousStrides_; |
| 156 | gert::Stride valueScaleContiguousStrides_; | 156 | gert::Stride valueScaleContiguousStrides_; |
| 157 | + gert::Stride keyOffsetContiguousStrides_; | ||
| 158 | + gert::Stride valueOffsetContiguousStrides_; | ||
| 157 | const gert::Stride *kScaleStrides_ = nullptr; | 159 | const gert::Stride *kScaleStrides_ = nullptr; |
| 158 | const gert::Stride *vScaleStrides_ = nullptr; | 160 | const gert::Stride *vScaleStrides_ = nullptr; |
| 161 | + const gert::Stride *kOffsetStrides_ = nullptr; | ||
| 162 | + const gert::Stride *vOffsetStrides_ = nullptr; | ||
| 159 | bool hasViewStride_ = true; | 163 | bool hasViewStride_ = true; |
| 160 | uint64_t keyBnStride_ = 0; | 164 | uint64_t keyBnStride_ = 0; |
| 161 | uint64_t keyN2Stride_ = 0; | 165 | uint64_t keyN2Stride_ = 0; |
| @@ -163,6 +167,14 @@ public: | |||
| 163 | uint64_t valueN2Stride_ = 0; | 167 | uint64_t valueN2Stride_ = 0; |
| 164 | uint64_t kRopeBnStride_ = 0; | 168 | uint64_t kRopeBnStride_ = 0; |
| 165 | uint64_t kRopeN2Stride_ = 0; | 169 | uint64_t kRopeN2Stride_ = 0; |
| 170 | + uint64_t kScaleBnStride_ = 0; | ||
| 171 | + uint64_t kScaleN2Stride_ = 0; | ||
| 172 | + uint64_t vScaleBnStride_ = 0; | ||
| 173 | + uint64_t vScaleN2Stride_ = 0; | ||
| 174 | + uint64_t kOffsetBnStride_ = 0; | ||
| 175 | + uint64_t kOffsetN2Stride_ = 0; | ||
| 176 | + uint64_t vOffsetBnStride_ = 0; | ||
| 177 | + uint64_t vOffsetN2Stride_ = 0; | ||
| 166 | int32_t keyNonContigDim_ = -1; | 178 | int32_t keyNonContigDim_ = -1; |
| 167 | int32_t valueNonContigDim_ = -1; | 179 | int32_t valueNonContigDim_ = -1; |
| 168 | int32_t keyRopeNonContigDim_ = -1; | 180 | int32_t keyRopeNonContigDim_ = -1; |
Mattention/fused_infer_attention_score/op_kernel/arch35/flash_attention_score_antiquant_block_vec.h+22-5
| @@ -123,7 +123,7 @@ public: | |||
| 123 | RunParamStr<isInfer> &runParam, Buffer<BufferType::L1> &outBufAntiKey, | 123 | RunParamStr<isInfer> &runParam, Buffer<BufferType::L1> &outBufAntiKey, |
| 124 | GlobalTensor<KV_T> &tempKeyGm, ConstInfo<isInfer, hasRope> &constInfo); | 124 | GlobalTensor<KV_T> &tempKeyGm, ConstInfo<isInfer, hasRope> &constInfo); |
| 125 | __aicore__ inline void SetAntiqParamCommon(const RunInfo<isInfer> &runInfo, int64_t kvOffset, | 125 | __aicore__ inline void SetAntiqParamCommon(const RunInfo<isInfer> &runInfo, int64_t kvOffset, |
| 126 | - ConstInfo<isInfer, hasRope> &constInfo); | 126 | + ConstInfo<isInfer, hasRope> &constInfo, bool isKey); |
| 127 | __aicore__ inline void AntiquantValue(const RunInfo<isInfer> &runInfo, int64_t &subTaskId, bool &first, | 127 | __aicore__ inline void AntiquantValue(const RunInfo<isInfer> &runInfo, int64_t &subTaskId, bool &first, |
| 128 | RunParamStr<isInfer> &runParam, Buffer<BufferType::L1> &outBufAntiValue, | 128 | RunParamStr<isInfer> &runParam, Buffer<BufferType::L1> &outBufAntiValue, |
| 129 | ConstInfo<isInfer, hasRope> &constInfo); | 129 | ConstInfo<isInfer, hasRope> &constInfo); |
| @@ -592,7 +592,8 @@ __aicore__ inline void FABlockVecAntiquant<ANTIQUANT_TEMPLATE_ARGS>::AntiquantKe | |||
| 592 | const RunInfo<isInfer> &runInfo, int64_t &subTaskId, bool &first, RunParamStr<isInfer> &runParam, | 592 | const RunInfo<isInfer> &runInfo, int64_t &subTaskId, bool &first, RunParamStr<isInfer> &runParam, |
| 593 | Buffer<BufferType::L1> &outBufAntiKey, GlobalTensor<KV_T> &tempKeyGm, ConstInfo<isInfer, hasRope> &constInfo) | 593 | Buffer<BufferType::L1> &outBufAntiKey, GlobalTensor<KV_T> &tempKeyGm, ConstInfo<isInfer, hasRope> &constInfo) |
| 594 | { | 594 | { |
| 595 | - SetAntiqParamCommon(runInfo, runInfo.keyOffset, constInfo); | 595 | + const bool isKey = true; // key 侧 stride (区别于 value 侧) |
| 596 | + SetAntiqParamCommon(runInfo, runInfo.keyOffset, constInfo, isKey); | ||
| 596 | if constexpr (KVFP4) { | 597 | if constexpr (KVFP4) { |
| 597 | taskParam.isLoadAntiqParam = true; | 598 | taskParam.isLoadAntiqParam = true; |
| 598 | taskParam.isFreeAntiqParam = false; | 599 | taskParam.isFreeAntiqParam = false; |
| @@ -615,7 +616,7 @@ __aicore__ inline void FABlockVecAntiquant<ANTIQUANT_TEMPLATE_ARGS>::AntiquantKe | |||
| 615 | 616 | ||
| 616 | ANTIQUANT_TEMPLATES_DEF_NO_DEFAULT | 617 | ANTIQUANT_TEMPLATES_DEF_NO_DEFAULT |
| 617 | __aicore__ inline void FABlockVecAntiquant<ANTIQUANT_TEMPLATE_ARGS>::SetAntiqParamCommon( | 618 | __aicore__ inline void FABlockVecAntiquant<ANTIQUANT_TEMPLATE_ARGS>::SetAntiqParamCommon( |
| 618 | - const RunInfo<isInfer> &runInfo, int64_t kvOffset, ConstInfo<isInfer, hasRope> &constInfo) | 619 | + const RunInfo<isInfer> &runInfo, int64_t kvOffset, ConstInfo<isInfer, hasRope> &constInfo, bool isKey) |
| 619 | { | 620 | { |
| 620 | if (isBeforeHalf) { | 621 | if (isBeforeHalf) { |
| 621 | taskParam.copyTotalS = GetRealDealSize(runInfo.s2RealSize); // 2 is Vec num | 622 | taskParam.copyTotalS = GetRealDealSize(runInfo.s2RealSize); // 2 is Vec num |
| @@ -634,10 +635,25 @@ __aicore__ inline void FABlockVecAntiquant<ANTIQUANT_TEMPLATE_ARGS>::SetAntiqPar | |||
| 634 | taskParam.kvGmOffset = kvOffset + constInfo.subBlockIdx * GetRealDealSize(runInfo.s2RealSize) * taskParam.kvStep; | 635 | taskParam.kvGmOffset = kvOffset + constInfo.subBlockIdx * GetRealDealSize(runInfo.s2RealSize) * taskParam.kvStep; |
| 635 | taskParam.s2BatchOffset = curSequence; | 636 | taskParam.s2BatchOffset = curSequence; |
| 636 | taskParam.kvPaddingBeginOffset = runInfo.kvLeftPaddingSize; | 637 | taskParam.kvPaddingBeginOffset = runInfo.kvLeftPaddingSize; |
| 637 | - taskParam.antiqParamOffset = runInfo.n2oIdx * taskParam.headDim; | ||
| 638 | taskParam.bIdx = runInfo.boIdx; | 638 | taskParam.bIdx = runInfo.boIdx; |
| 639 | taskParam.n2Idx = runInfo.n2oIdx; | 639 | taskParam.n2Idx = runInfo.n2oIdx; |
| 640 | taskParam.s2Idx = runInfo.s2LoopCount; | 640 | taskParam.s2Idx = runInfo.s2LoopCount; |
| 641 | + if (isKey) { | ||
| 642 | + taskParam.kvBnStride = constInfo.keyBnStride; | ||
| 643 | + taskParam.kvN2Stride = constInfo.keyN2Stride; | ||
| 644 | + taskParam.scaleBnStride = constInfo.kScaleBnStride; | ||
| 645 | + taskParam.scaleN2Stride = constInfo.kScaleN2Stride; | ||
| 646 | + taskParam.offsetBnStride = constInfo.kOffsetBnStride; | ||
| 647 | + taskParam.offsetN2Stride = constInfo.kOffsetN2Stride; | ||
| 648 | + } else { | ||
| 649 | + taskParam.kvBnStride = constInfo.valueBnStride; | ||
| 650 | + taskParam.kvN2Stride = constInfo.valueN2Stride; | ||
| 651 | + taskParam.scaleBnStride = constInfo.vScaleBnStride; | ||
| 652 | + taskParam.scaleN2Stride = constInfo.vScaleN2Stride; | ||
| 653 | + taskParam.offsetBnStride = constInfo.vOffsetBnStride; | ||
| 654 | + taskParam.offsetN2Stride = constInfo.vOffsetN2Stride; | ||
| 655 | + } | ||
| 656 | + taskParam.antiqParamOffset = runInfo.n2oIdx * taskParam.headDim; | ||
| 641 | if constexpr (isInfer) { | 657 | if constexpr (isInfer) { |
| 642 | taskParam.s2Idx += runInfo.s2StartIdx / constInfo.s2BaseSize; | 658 | taskParam.s2Idx += runInfo.s2StartIdx / constInfo.s2BaseSize; |
| 643 | if constexpr (enableKVPrefix) { | 659 | if constexpr (enableKVPrefix) { |
| @@ -939,7 +955,8 @@ __aicore__ inline void FABlockVecAntiquant<ANTIQUANT_TEMPLATE_ARGS>::AntiquantVa | |||
| 939 | tempValueGm = this->valueGm; | 955 | tempValueGm = this->valueGm; |
| 940 | GetKvByTensorList(runInfo, this->valueGm, tempValueGm, constInfo); | 956 | GetKvByTensorList(runInfo, this->valueGm, tempValueGm, constInfo); |
| 941 | } | 957 | } |
| 942 | - SetAntiqParamCommon(runInfo, runInfo.valueOffset, constInfo); | 958 | + const bool isKey = false; // value 侧 stride (区别于 key 侧) |
| 959 | + SetAntiqParamCommon(runInfo, runInfo.valueOffset, constInfo, isKey); | ||
| 943 | if constexpr (VALUE_ANTIQUANT_PER_TOKEN || ANTIQUANT_PER_GROUP) { | 960 | if constexpr (VALUE_ANTIQUANT_PER_TOKEN || ANTIQUANT_PER_GROUP) { |
| 944 | taskParam.isFreeAntiqParam = true; | 961 | taskParam.isFreeAntiqParam = true; |
| 945 | taskParam.isLoadAntiqParam = true; | 962 | taskParam.isLoadAntiqParam = true; |
Mattention/fused_infer_attention_score/op_kernel/arch35/flash_attention_score_antiquant_kernel.h+12-0
| @@ -379,6 +379,18 @@ FlashAttentionScoreAntiquantKernel<AntiquantCubeBlockType, AntiquantVecBlockType | |||
| 379 | this->constInfo.paBlockNumSum = inputParamsRegbase.paBlockNumSum; | 379 | this->constInfo.paBlockNumSum = inputParamsRegbase.paBlockNumSum; |
| 380 | this->constInfo.kvCacheNzD0 = inputParamsRegbase.kvCacheNzD0; | 380 | this->constInfo.kvCacheNzD0 = inputParamsRegbase.kvCacheNzD0; |
| 381 | } | 381 | } |
| 382 | + this->constInfo.keyBnStride = inputParamsRegbase.keyStrides.bnStride; | ||
| 383 | + this->constInfo.keyN2Stride = inputParamsRegbase.keyStrides.n2Stride; | ||
| 384 | + this->constInfo.valueBnStride = inputParamsRegbase.valueStrides.bnStride; | ||
| 385 | + this->constInfo.valueN2Stride = inputParamsRegbase.valueStrides.n2Stride; | ||
| 386 | + this->constInfo.kScaleBnStride = inputParamsRegbase.kScaleStrides.bnStride; | ||
| 387 | + this->constInfo.kScaleN2Stride = inputParamsRegbase.kScaleStrides.n2Stride; | ||
| 388 | + this->constInfo.vScaleBnStride = inputParamsRegbase.vScaleStrides.bnStride; | ||
| 389 | + this->constInfo.vScaleN2Stride = inputParamsRegbase.vScaleStrides.n2Stride; | ||
| 390 | + this->constInfo.kOffsetBnStride = inputParamsRegbase.kOffsetStrides.bnStride; | ||
| 391 | + this->constInfo.kOffsetN2Stride = inputParamsRegbase.kOffsetStrides.n2Stride; | ||
| 392 | + this->constInfo.vOffsetBnStride = inputParamsRegbase.vOffsetStrides.bnStride; | ||
| 393 | + this->constInfo.vOffsetN2Stride = inputParamsRegbase.vOffsetStrides.n2Stride; | ||
| 382 | 394 | ||
| 383 | this->constInfo.transposeLayout = inputParamsRegbase.transposeLayout; | 395 | this->constInfo.transposeLayout = inputParamsRegbase.transposeLayout; |
| 384 | if (this->constInfo.transposeLayout == static_cast<uint32_t>(TransposeLayoutEnum::BNSD_BSND)) { | 396 | if (this->constInfo.transposeLayout == static_cast<uint32_t>(TransposeLayoutEnum::BNSD_BSND)) { |
Mattention/fused_infer_attention_score/op_kernel/arch35/flash_attention_score_antiquant_processor.h+53-16
| @@ -85,6 +85,12 @@ struct AntiquantTaskParamBaseAPI { | |||
| 85 | uint64_t kvPaddingBeginOffset; | 85 | uint64_t kvPaddingBeginOffset; |
| 86 | 86 | ||
| 87 | bool isPrefixLoop = 0; | 87 | bool isPrefixLoop = 0; |
| 88 | + uint64_t kvBnStride = 0; | ||
| 89 | + uint64_t kvN2Stride = 0; | ||
| 90 | + uint64_t scaleBnStride = 0; | ||
| 91 | + uint64_t scaleN2Stride = 0; | ||
| 92 | + uint64_t offsetBnStride = 0; | ||
| 93 | + uint64_t offsetN2Stride = 0; | ||
| 88 | }; | 94 | }; |
| 89 | 95 | ||
| 90 | template <ANTIQUANT_PROCESSOR_TEMPLATE_DEF, const bool ANTIQUANT_PER_TOKEN> | 96 | template <ANTIQUANT_PROCESSOR_TEMPLATE_DEF, const bool ANTIQUANT_PER_TOKEN> |
| @@ -150,12 +156,10 @@ public: | |||
| 150 | GlobalTensor<int32_t> &blockTableGm, | 156 | GlobalTensor<int32_t> &blockTableGm, |
| 151 | const AntiquantTaskParamBaseAPI &taskParam, uint32_t curSequence, | 157 | const AntiquantTaskParamBaseAPI &taskParam, uint32_t curSequence, |
| 152 | uint32_t dealRowCount); | 158 | uint32_t dealRowCount); |
| 153 | - __aicore__ inline void CopyAntiquantParamsPageAttention(LocalTensor<ANTIQ_PARAMS_T> dstLocal, | 159 | + __aicore__ inline void CopyAntiquantParamsPageAttention( |
| 154 | - GlobalTensor<ANTIQ_PARAMS_T> &srcGm, | 160 | + LocalTensor<ANTIQ_PARAMS_T> dstLocal, GlobalTensor<ANTIQ_PARAMS_T> &srcGm, GlobalTensor<int32_t> &blockTableGm, |
| 155 | - GlobalTensor<int32_t> &blockTableGm, | 161 | + const AntiquantTaskParamBaseAPI &taskParam, DataCopyExtParams copyInParams, |
| 156 | - const AntiquantTaskParamBaseAPI &taskParam, | 162 | + DataCopyPadExtParams<ANTIQ_PARAMS_T> copyInPadParams, bool isOffset = false); |
| 157 | - DataCopyExtParams copyInParams, | ||
| 158 | - DataCopyPadExtParams<ANTIQ_PARAMS_T> copyInPadParams); | ||
| 159 | template <bool HAS_OFFSET, bool IS_PER_TOKEN> | 163 | template <bool HAS_OFFSET, bool IS_PER_TOKEN> |
| 160 | __aicore__ inline void CallAntiquantVF(LocalTensor<Q_T> &antiqResUb, LocalTensor<KV_T> &antiqInUb, | 164 | __aicore__ inline void CallAntiquantVF(LocalTensor<Q_T> &antiqResUb, LocalTensor<KV_T> &antiqInUb, |
| 161 | LocalTensor<ANTIQ_PARAMS_T> &offsetTensor, | 165 | LocalTensor<ANTIQ_PARAMS_T> &offsetTensor, |
| @@ -402,8 +406,9 @@ AntiquantProcessorBaseAPI<ANTIQUANT_TEMPLATE_ARGS, ANTIQUANT_PER_TOKEN>::LoadAnt | |||
| 402 | if (taskParam.isExistOffset) { | 406 | if (taskParam.isExistOffset) { |
| 403 | LocalTensor<ANTIQ_PARAMS_T> antiqOffsetTmpUb = antiqOffsetInputQue.template AllocTensor<ANTIQ_PARAMS_T>(); | 407 | LocalTensor<ANTIQ_PARAMS_T> antiqOffsetTmpUb = antiqOffsetInputQue.template AllocTensor<ANTIQ_PARAMS_T>(); |
| 404 | if (taskParam.isPageAttentionAntiquant) { | 408 | if (taskParam.isPageAttentionAntiquant) { |
| 409 | + const bool isOffset = true; // offset 用自己的 stride (与 scale 可不同), 区别于 scale 路径 | ||
| 405 | CopyAntiquantParamsPageAttention(antiqOffsetTmpUb, antiqOffsetGm, blockTableGm, taskParam, copyInParams, | 410 | CopyAntiquantParamsPageAttention(antiqOffsetTmpUb, antiqOffsetGm, blockTableGm, taskParam, copyInParams, |
| 406 | - copyInPadParams); | 411 | + copyInPadParams, isOffset); |
| 407 | } else { | 412 | } else { |
| 408 | DataCopyPad(antiqOffsetTmpUb, antiqOffsetGm[scaleOffset], copyInParams, copyInPadParams); | 413 | DataCopyPad(antiqOffsetTmpUb, antiqOffsetGm[scaleOffset], copyInParams, copyInPadParams); |
| 409 | } | 414 | } |
| @@ -527,7 +532,7 @@ __aicore__ inline void | |||
| 527 | AntiquantProcessorBaseAPI<ANTIQUANT_TEMPLATE_ARGS, ANTIQUANT_PER_TOKEN>::CopyAntiquantParamsPageAttention( | 532 | AntiquantProcessorBaseAPI<ANTIQUANT_TEMPLATE_ARGS, ANTIQUANT_PER_TOKEN>::CopyAntiquantParamsPageAttention( |
| 528 | LocalTensor<ANTIQ_PARAMS_T> dstLocal, GlobalTensor<ANTIQ_PARAMS_T> &srcGm, GlobalTensor<int32_t> &blockTableGm, | 533 | LocalTensor<ANTIQ_PARAMS_T> dstLocal, GlobalTensor<ANTIQ_PARAMS_T> &srcGm, GlobalTensor<int32_t> &blockTableGm, |
| 529 | const AntiquantTaskParamBaseAPI &taskParam, DataCopyExtParams copyInParams, | 534 | const AntiquantTaskParamBaseAPI &taskParam, DataCopyExtParams copyInParams, |
| 530 | - DataCopyPadExtParams<ANTIQ_PARAMS_T> copyInPadParams) | 535 | + DataCopyPadExtParams<ANTIQ_PARAMS_T> copyInPadParams, bool isOffset) |
| 531 | { | 536 | { |
| 532 | uint32_t useKvHeadNum = 1; | 537 | uint32_t useKvHeadNum = 1; |
| 533 | uint32_t useN2Idx = 0; | 538 | uint32_t useN2Idx = 0; |
| @@ -547,8 +552,18 @@ AntiquantProcessorBaseAPI<ANTIQUANT_TEMPLATE_ARGS, ANTIQUANT_PER_TOKEN>::CopyAnt | |||
| 547 | if (copyElmenCnt + copyFinishElmenCnt > taskParam.copyTotalS) { | 552 | if (copyElmenCnt + copyFinishElmenCnt > taskParam.copyTotalS) { |
| 548 | copyElmenCnt = taskParam.copyTotalS - copyFinishElmenCnt; | 553 | copyElmenCnt = taskParam.copyTotalS - copyFinishElmenCnt; |
| 549 | } | 554 | } |
| 550 | - uint64_t srcOffset = idInBlockTable * taskParam.kvCacheBlockSize * useKvHeadNum + | 555 | + uint64_t blockStride = taskParam.kvCacheBlockSize * useKvHeadNum; |
| 551 | - taskParam.kvCacheBlockSize * useN2Idx + remainElmenCnt; | 556 | + uint64_t headStride = taskParam.kvCacheBlockSize; |
| 557 | + // offset 用自己的 stride (与 scale 可不同), scale 用 scale 的 | ||
| 558 | + uint64_t bnStride = isOffset ? taskParam.offsetBnStride : taskParam.scaleBnStride; | ||
| 559 | + uint64_t n2Stride = isOffset ? taskParam.offsetN2Stride : taskParam.scaleN2Stride; | ||
| 560 | + if (bnStride != 0) { | ||
| 561 | + blockStride = bnStride; | ||
| 562 | + } | ||
| 563 | + if (n2Stride != 0) { | ||
| 564 | + headStride = n2Stride; | ||
| 565 | + } | ||
| 566 | + uint64_t srcOffset = idInBlockTable * blockStride + useN2Idx * headStride + remainElmenCnt; | ||
| 552 | copyInParams.blockLen = copyElmenCnt * sizeof(ANTIQ_PARAMS_T); | 567 | copyInParams.blockLen = copyElmenCnt * sizeof(ANTIQ_PARAMS_T); |
| 553 | DataCopyPad(dstLocal[dstOffset], srcGm[srcOffset], copyInParams, copyInPadParams); | 568 | DataCopyPad(dstLocal[dstOffset], srcGm[srcOffset], copyInParams, copyInPadParams); |
| 554 | dstOffset += copyElmenCnt; | 569 | dstOffset += copyElmenCnt; |
| @@ -641,19 +656,41 @@ __aicore__ inline void AntiquantProcessorBaseAPI<ANTIQUANT_TEMPLATE_ARGS, ANTIQU | |||
| 641 | } | 656 | } |
| 642 | uint64_t curOffset = 0; | 657 | uint64_t curOffset = 0; |
| 643 | if (taskParam.paKvShapeType == static_cast<uint32_t>(KvCacheLayout::KV_CACHE_NZ)) { // NZ | 658 | if (taskParam.paKvShapeType == static_cast<uint32_t>(KvCacheLayout::KV_CACHE_NZ)) { // NZ |
| 644 | - curOffset = idInBlockTable * taskParam.kvHeadNum * taskParam.headDim * taskParam.kvCacheBlockSize + | 659 | + uint64_t nzHeadStride = taskParam.headDim * taskParam.kvCacheBlockSize; |
| 645 | - (uint64_t)(taskParam.n2Idx * taskParam.headDim * taskParam.kvCacheBlockSize) + | 660 | + uint64_t nzBlockStride = taskParam.kvHeadNum * nzHeadStride; |
| 661 | + if (taskParam.kvN2Stride != 0) { | ||
| 662 | + nzHeadStride = taskParam.kvN2Stride; | ||
| 663 | + } | ||
| 664 | + if (taskParam.kvBnStride != 0) { | ||
| 665 | + nzBlockStride = taskParam.kvBnStride; | ||
| 666 | + } else if (taskParam.kvN2Stride != 0) { | ||
| 667 | + nzBlockStride = taskParam.kvHeadNum * nzHeadStride; | ||
| 668 | + } | ||
| 669 | + curOffset = idInBlockTable * nzBlockStride + (uint64_t)(taskParam.n2Idx * nzHeadStride) + | ||
| 646 | reaminRowCnt * typeElementSize; | 670 | reaminRowCnt * typeElementSize; |
| 647 | CopyKVPaNz(dstLocal[copyFinishRowCnt * typeElementSize], srcGm, curOffset, copyRowCnt, dealRowCount, | 671 | CopyKVPaNz(dstLocal[copyFinishRowCnt * typeElementSize], srcGm, curOffset, copyRowCnt, dealRowCount, |
| 648 | taskParam); | 672 | taskParam); |
| 649 | } else { | 673 | } else { |
| 650 | if (taskParam.paKvShapeType == static_cast<uint32_t>(KvCacheLayout::KV_CACHE_BSH)) { // BBH | 674 | if (taskParam.paKvShapeType == static_cast<uint32_t>(KvCacheLayout::KV_CACHE_BSH)) { // BBH |
| 651 | - curOffset = (idInBlockTable * taskParam.kvCacheBlockSize + reaminRowCnt) * taskParam.kvHeadNum * | 675 | + uint64_t bsStride = taskParam.kvHeadNum * taskParam.headDim; |
| 652 | - taskParam.headDim + | 676 | + uint64_t bnStride = taskParam.kvCacheBlockSize * bsStride; |
| 677 | + if (taskParam.kvBnStride != 0) { | ||
| 678 | + bnStride = taskParam.kvBnStride; | ||
| 679 | + } | ||
| 680 | + curOffset = idInBlockTable * bnStride + reaminRowCnt * bsStride + | ||
| 653 | (uint64_t)(taskParam.n2Idx * taskParam.headDim); | 681 | (uint64_t)(taskParam.n2Idx * taskParam.headDim); |
| 654 | } else { // BNBD | 682 | } else { // BNBD |
| 655 | - curOffset = idInBlockTable * taskParam.kvHeadNum * taskParam.kvCacheBlockSize * taskParam.headDim + | 683 | + uint64_t headStride = taskParam.kvCacheBlockSize * taskParam.headDim; |
| 656 | - (uint64_t)(taskParam.n2Idx * taskParam.kvCacheBlockSize * taskParam.headDim) + | 684 | + uint64_t blockStride = taskParam.kvHeadNum * headStride; |
| 685 | + if (taskParam.kvN2Stride != 0) { | ||
| 686 | + headStride = taskParam.kvN2Stride; | ||
| 687 | + } | ||
| 688 | + if (taskParam.kvBnStride != 0) { | ||
| 689 | + blockStride = taskParam.kvBnStride; | ||
| 690 | + } else if (taskParam.kvN2Stride != 0) { | ||
| 691 | + blockStride = taskParam.kvHeadNum * headStride; | ||
| 692 | + } | ||
| 693 | + curOffset = idInBlockTable * blockStride + (uint64_t)(taskParam.n2Idx * headStride) + | ||
| 657 | reaminRowCnt * taskParam.headDim; | 694 | reaminRowCnt * taskParam.headDim; |
| 658 | } | 695 | } |
| 659 | CopyKV(dstLocal[copyFinishRowCnt * dBaseSize], srcGm, curOffset, copyRowCnt, taskParam.headDim, | 696 | CopyKV(dstLocal[copyFinishRowCnt * dBaseSize], srcGm, curOffset, copyRowCnt, taskParam.headDim, |


补充PR描述