Merged
fix: moe_token_permute_with_ep_grad长尾算子加固 #15613
klein8793Add on 19 days ago
fix: moe_token_permute_with_ep_grad长尾算子加固 #15613
Merged
Total: 9 files changed+112-28
| @@ -136,7 +136,9 @@ | |||
| 136 | 136 | ||
| 137 | ## 约束说明 | 137 | ## 约束说明 |
| 138 | 138 | ||
| 139 | - - numTopk <= 512。 | 139 | + - 1 <= numTopk <= 512。 |
| 140 | + - permutedTokensOutputGrad、permutedProbsOutputGradOptional、tokenGradOut、probsGradOut 的数据类型必须一致,同时为 BFLOAT16、FLOAT16 或 FLOAT32 之一。 | ||
| 141 | + - sortedIndices 的元素取值表示全局 permuted 位置,取值范围为 [0, num_tokens * numTopk)。 | ||
| 140 | - 不支持paddedMode为`True`。 | 142 | - 不支持paddedMode为`True`。 |
| 141 | - 当rangeOptional为空时,忽略permutedProbsOutputGradOptional和probsGradOut,执行逻辑回退到[aclnnMoeTokenPermuteGrad](../moe_token_permute_grad/docs/aclnnMoeTokenPermuteGrad.md)。 | 143 | - 当rangeOptional为空时,忽略permutedProbsOutputGradOptional和probsGradOut,执行逻辑回退到[aclnnMoeTokenPermuteGrad](../moe_token_permute_grad/docs/aclnnMoeTokenPermuteGrad.md)。 |
| 142 | 144 | ||
| @@ -138,7 +138,9 @@ Computation formula: | |||
| 138 | 138 | ||
| 139 | ## Constraints | 139 | ## Constraints |
| 140 | 140 | ||
| 141 | - - numTopk <= 512. | 141 | + - 1 <= numTopk <= 512. |
| 142 | + - The data types of permutedTokensOutputGrad, permutedProbsOutputGradOptional, tokenGradOut, and probsGradOut must be the same, being one of BFLOAT16, FLOAT16, or FLOAT32. | ||
| 143 | + - The values of sortedIndices represent global permuted positions, ranging from [0, num_tokens * numTopk). | ||
| 142 | - paddedMode set to `True` is not supported. | 144 | - paddedMode set to `True` is not supported. |
| 143 | - When rangeOptional is empty, permutedProbsOutputGradOptional and probsGradOut are ignored, and the execution logic falls back to [aclnnMoeTokenPermuteGrad](../moe_token_permute_grad/docs/aclnnMoeTokenPermuteGrad_en.md). | 145 | - When rangeOptional is empty, permutedProbsOutputGradOptional and probsGradOut are ignored, and the execution logic falls back to [aclnnMoeTokenPermuteGrad](../moe_token_permute_grad/docs/aclnnMoeTokenPermuteGrad_en.md). |
| 144 | 146 | ||
| @@ -253,7 +253,7 @@ aclnnStatus aclnnMoeTokenPermuteWithEpGrad( | |||
| 253 | <tr> | 253 | <tr> |
| 254 | <td> ACLNN_ERR_PARAM_INVALID </td> | 254 | <td> ACLNN_ERR_PARAM_INVALID </td> |
| 255 | <td> 161002 </td> | 255 | <td> 161002 </td> |
| 256 | - <td>输入和输出的数据类型和数据格式不在支持的范围之内。</td> | 256 | + <td>输入和输出的数据类型和数据格式不在支持的范围之内,或参数 shape 不满足约束。</td> |
| 257 | </tr> | 257 | </tr> |
| 258 | </tbody> | 258 | </tbody> |
| 259 | </table> | 259 | </table> |
| @@ -306,7 +306,9 @@ aclnnStatus aclnnMoeTokenPermuteWithEpGrad( | |||
| 306 | - 确定性计算: | 306 | - 确定性计算: |
| 307 | - aclnnMoeTokenPermuteWithEpGrad默认确定性实现。 | 307 | - aclnnMoeTokenPermuteWithEpGrad默认确定性实现。 |
| 308 | 308 | ||
| 309 | -- numTopk <= 512。 | 309 | +- 1 <= numTopk <= 512。 |
| 310 | +- permutedTokensOutputGrad、permutedProbsOutputGradOptional、tokenGradOut、probsGradOut 的数据类型必须一致,同时为 BFLOAT16、FLOAT16 或 FLOAT32 之一。 | ||
| 311 | +- sortedIndices 的元素取值表示全局 permuted 位置,取值范围为 [0, num_tokens * numTopk),仅 rangeOptional[0] <= 值 < rangeOptional[1] 的元素参与计算。 | ||
| 310 | - 不支持paddedMode为`True`。 | 312 | - 不支持paddedMode为`True`。 |
| 311 | - 当rangeOptional为空时,忽略permutedProbsOutputGradOptional和probsGradOut,执行逻辑回退到[aclnnMoeTokenPermuteGrad](../../moe_token_permute_grad/docs/aclnnMoeTokenPermuteGrad.md)。 | 313 | - 当rangeOptional为空时,忽略permutedProbsOutputGradOptional和probsGradOut,执行逻辑回退到[aclnnMoeTokenPermuteGrad](../../moe_token_permute_grad/docs/aclnnMoeTokenPermuteGrad.md)。 |
| 312 | 314 | ||
| @@ -254,7 +254,7 @@ aclnnStatus aclnnMoeTokenPermuteWithEpGrad( | |||
| 254 | <tr> | 254 | <tr> |
| 255 | <td> ACLNN_ERR_PARAM_INVALID </td> | 255 | <td> ACLNN_ERR_PARAM_INVALID </td> |
| 256 | <td> 161002 </td> | 256 | <td> 161002 </td> |
| 257 | - <td>The data type or data format of the input or output is not within the supported range.</td> | 257 | + <td>The data type or data format of the input or output is not within the supported range, or the shape of a parameter does not meet the constraint.</td> |
| 258 | </tr> | 258 | </tr> |
| 259 | </tbody> | 259 | </tbody> |
| 260 | </table> | 260 | </table> |
| @@ -307,7 +307,9 @@ aclnnStatus aclnnMoeTokenPermuteWithEpGrad( | |||
| 307 | - deterministic computation: | 307 | - deterministic computation: |
| 308 | - aclnnMoeTokenPermuteWithEpGrad defaults to a deterministic implementation. | 308 | - aclnnMoeTokenPermuteWithEpGrad defaults to a deterministic implementation. |
| 309 | 309 | ||
| 310 | -- numTopk <= 512. | 310 | +- 1 <= numTopk <= 512. |
| 311 | +- The data types of permutedTokensOutputGrad, permutedProbsOutputGradOptional, tokenGradOut, and probsGradOut must be the same, being one of BFLOAT16, FLOAT16, or FLOAT32. | ||
| 312 | +- The values of sortedIndices represent global permuted positions, ranging from [0, num_tokens * numTopk); only elements with rangeOptional[0] <= value < rangeOptional[1] participate in the computation. | ||
| 311 | - paddedMode being `True` is not supported. | 313 | - paddedMode being `True` is not supported. |
| 312 | - If rangeOptional is empty, permutedProbsOutputGradOptional and probsGradOut are ignored, and the execution logic falls back to [aclnnMoeTokenPermuteGrad](../../moe_token_permute_grad/docs/aclnnMoeTokenPermuteGrad_en.md). | 314 | - If rangeOptional is empty, permutedProbsOutputGradOptional and probsGradOut are ignored, and the execution logic falls back to [aclnnMoeTokenPermuteGrad](../../moe_token_permute_grad/docs/aclnnMoeTokenPermuteGrad_en.md). |
| 313 | 315 | ||
| @@ -61,7 +61,7 @@ int Init(int32_t deviceId, aclrtStream* stream) | |||
| 61 | template <typename T> | 61 | template <typename T> |
| 62 | int CreateAclIntArray(const std::vector<T>& hostData, void** deviceAddr, aclIntArray** intArray) | 62 | int CreateAclIntArray(const std::vector<T>& hostData, void** deviceAddr, aclIntArray** intArray) |
| 63 | { | 63 | { |
| 64 | - auto size = GetShapeSize(hostData) * sizeof(T); | 64 | + auto size = hostData.size() * sizeof(T); |
| 65 | // Call aclrtMalloc to allocate memory on the device. | 65 | // Call aclrtMalloc to allocate memory on the device. |
| 66 | auto ret = aclrtMalloc(deviceAddr, size, ACL_MEM_MALLOC_HUGE_FIRST); | 66 | auto ret = aclrtMalloc(deviceAddr, size, ACL_MEM_MALLOC_HUGE_FIRST); |
| 67 | CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtMalloc failed. ERROR: %d\n", ret); return ret); | 67 | CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtMalloc failed. ERROR: %d\n", ret); return ret); |
| @@ -121,11 +121,11 @@ int main() | |||
| 121 | aclTensor* permuted_prob_output_grad = nullptr; | 121 | aclTensor* permuted_prob_output_grad = nullptr; |
| 122 | 122 | ||
| 123 | ret = CreateAclTensor(permuted_token_output_grad_Data, permuted_token_output_grad_Shape, | 123 | ret = CreateAclTensor(permuted_token_output_grad_Data, permuted_token_output_grad_Shape, |
| 124 | - &permuted_token_output_grad_Addr, aclDataType::ACL_BF16, &permuted_token_output_grad); | 124 | + &permuted_token_output_grad_Addr, aclDataType::ACL_FLOAT, &permuted_token_output_grad); |
| 125 | CHECK_RET(ret == ACL_SUCCESS, return ret); | 125 | CHECK_RET(ret == ACL_SUCCESS, return ret); |
| 126 | 126 | ||
| 127 | ret = CreateAclTensor(permuted_prob_output_grad_Data, permuted_prob_output_grad_Shape, | 127 | ret = CreateAclTensor(permuted_prob_output_grad_Data, permuted_prob_output_grad_Shape, |
| 128 | - &permuted_prob_output_grad_Addr, aclDataType::ACL_BF16, &permuted_prob_output_grad); | 128 | + &permuted_prob_output_grad_Addr, aclDataType::ACL_FLOAT, &permuted_prob_output_grad); |
| 129 | CHECK_RET(ret == ACL_SUCCESS, return ret); | 129 | CHECK_RET(ret == ACL_SUCCESS, return ret); |
| 130 | 130 | ||
| 131 | std::vector<int> sortedIndicesData = {2, 0, 4, 1, 5, 3}; | 131 | std::vector<int> sortedIndicesData = {2, 0, 4, 1, 5, 3}; |
| @@ -148,7 +148,7 @@ int main() | |||
| 148 | void* tokenOutAddr = nullptr; | 148 | void* tokenOutAddr = nullptr; |
| 149 | aclTensor* tokenOut = nullptr; | 149 | aclTensor* tokenOut = nullptr; |
| 150 | 150 | ||
| 151 | - ret = CreateAclTensor(tokenOutData, tokenOutShape, &tokenOutAddr, aclDataType::ACL_BF16, &tokenOut); | 151 | + ret = CreateAclTensor(tokenOutData, tokenOutShape, &tokenOutAddr, aclDataType::ACL_FLOAT, &tokenOut); |
| 152 | CHECK_RET(ret == ACL_SUCCESS, return ret); | 152 | CHECK_RET(ret == ACL_SUCCESS, return ret); |
| 153 | 153 | ||
| 154 | std::vector<float> probOutData = {0, 0, 0, 0, 0, 0}; | 154 | std::vector<float> probOutData = {0, 0, 0, 0, 0, 0}; |
| @@ -156,7 +156,7 @@ int main() | |||
| 156 | void* probOutAddr = nullptr; | 156 | void* probOutAddr = nullptr; |
| 157 | aclTensor* probOut = nullptr; | 157 | aclTensor* probOut = nullptr; |
| 158 | 158 | ||
| 159 | - ret = CreateAclTensor(probOutData, probOutShape, &probOutAddr, aclDataType::ACL_BF16, &probOut); | 159 | + ret = CreateAclTensor(probOutData, probOutShape, &probOutAddr, aclDataType::ACL_FLOAT, &probOut); |
| 160 | CHECK_RET(ret == ACL_SUCCESS, return ret); | 160 | CHECK_RET(ret == ACL_SUCCESS, return ret); |
| 161 | 161 | ||
| 162 | // 3. 调用CANN算子库API,需要修改为具体的Api名称 | 162 | // 3. 调用CANN算子库API,需要修改为具体的Api名称 |
| @@ -45,7 +45,7 @@ public: | |||
| 45 | .Format({ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND}) | 45 | .Format({ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND}) |
| 46 | .UnknownShapeFormat({ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND}); | 46 | .UnknownShapeFormat({ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND}); |
| 47 | this->Output("probs_grad") | 47 | this->Output("probs_grad") |
| 48 | - .ParamType(REQUIRED) | 48 | + .ParamType(OPTIONAL) |
| 49 | .DataType({ge::DT_BF16, ge::DT_FLOAT16, ge::DT_FLOAT}) | 49 | .DataType({ge::DT_BF16, ge::DT_FLOAT16, ge::DT_FLOAT}) |
| 50 | .Format({ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND}) | 50 | .Format({ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND}) |
| 51 | .UnknownShapeFormat({ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND}); | 51 | .UnknownShapeFormat({ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND}); |
| @@ -19,6 +19,9 @@ | |||
| 19 | 19 | ||
| 20 | using namespace ge; | 20 | using namespace ge; |
| 21 | namespace ops { | 21 | namespace ops { |
| 22 | +static constexpr size_t ATTR_INDEX_TOPK = 0; | ||
| 23 | +static constexpr size_t ATTR_INDEX_RANGE = 1; | ||
| 24 | + | ||
| 22 | static ge::graphStatus InferShapeForMoeTokenPermuteWithEpGrad(gert::InferShapeContext* context) | 25 | static ge::graphStatus InferShapeForMoeTokenPermuteWithEpGrad(gert::InferShapeContext* context) |
| 23 | { | 26 | { |
| 24 | const gert::Shape* permuted_inputs_shape = context->GetInputShape(0); | 27 | const gert::Shape* permuted_inputs_shape = context->GetInputShape(0); |
| @@ -28,14 +31,28 @@ static ge::graphStatus InferShapeForMoeTokenPermuteWithEpGrad(gert::InferShapeCo | |||
| 28 | const gert::Shape* indices_shape = context->GetInputShape(1); | 31 | const gert::Shape* indices_shape = context->GetInputShape(1); |
| 29 | OP_CHECK_IF(indices_shape == nullptr, OP_LOGE_WITH_INVALID_INPUT(context->GetNodeName(), "sorted_indices"), | 32 | OP_CHECK_IF(indices_shape == nullptr, OP_LOGE_WITH_INVALID_INPUT(context->GetNodeName(), "sorted_indices"), |
| 30 | return ge::GRAPH_FAILED); | 33 | return ge::GRAPH_FAILED); |
| 31 | - const int64_t* top_k = context->GetAttrs()->GetAttrPointer<int64_t>(0); | 34 | + const int64_t* top_k = context->GetAttrs()->GetAttrPointer<int64_t>(ATTR_INDEX_TOPK); |
| 32 | OP_CHECK_IF(top_k == nullptr, OP_LOGE_WITH_INVALID_INPUT(context->GetNodeName(), "num_topk"), | 35 | OP_CHECK_IF(top_k == nullptr, OP_LOGE_WITH_INVALID_INPUT(context->GetNodeName(), "num_topk"), |
| 33 | return ge::GRAPH_FAILED); | 36 | return ge::GRAPH_FAILED); |
| 34 | - OP_CHECK_IF(*top_k == 0, | 37 | + OP_CHECK_IF( |
| 35 | - OP_LOGE_FOR_INVALID_ARGUMENT_WITH_REASON(context->GetNodeName(), "num_topk", "num_topk cannot be 0."), | 38 | + *top_k < 1, |
| 39 | + OP_LOGE_FOR_INVALID_ARGUMENT_WITH_REASON(context->GetNodeName(), "num_topk", "num_topk must be positive."), | ||
| 40 | + return ge::GRAPH_FAILED); | ||
| 41 | + OP_CHECK_IF( | ||
| 42 | + *top_k > 512, | ||
| 43 | + OP_LOGE_FOR_INVALID_ARGUMENT_WITH_REASON(context->GetNodeName(), "num_topk", "num_topk cannot exceed 512."), | ||
| 44 | + return ge::GRAPH_FAILED); | ||
| 45 | + OP_CHECK_IF(indices_shape->GetDimNum() != 1, | ||
| 46 | + OP_LOGE_FOR_INVALID_SHAPEDIM_WITH_REASON(context->GetNodeName(), "sorted_indices", | ||
| 47 | + std::to_string(indices_shape->GetDimNum()), | ||
| 48 | + "sorted_indices must be 1D."), | ||
| 36 | return ge::GRAPH_FAILED); | 49 | return ge::GRAPH_FAILED); |
| 37 | int64_t topk = *top_k; | 50 | int64_t topk = *top_k; |
| 38 | int64_t indices_dim0 = indices_shape->GetDim(0); | 51 | int64_t indices_dim0 = indices_shape->GetDim(0); |
| 52 | + OP_CHECK_IF(indices_dim0 != -1 && indices_dim0 % topk != 0, | ||
| 53 | + OP_LOGE_FOR_INVALID_ARGUMENT_WITH_REASON(context->GetNodeName(), "sorted_indices, num_topk", | ||
| 54 | + "sorted_indices length must be a multiple of num_topk."), | ||
| 55 | + return ge::GRAPH_FAILED); | ||
| 39 | int64_t tokens_num = (indices_dim0 == -1) ? -1 : indices_dim0 / topk; | 56 | int64_t tokens_num = (indices_dim0 == -1) ? -1 : indices_dim0 / topk; |
| 40 | 57 | ||
| 41 | gert::Shape* out_shape = context->GetOutputShape(0); | 58 | gert::Shape* out_shape = context->GetOutputShape(0); |
| @@ -49,16 +66,24 @@ static ge::graphStatus InferShapeForMoeTokenPermuteWithEpGrad(gert::InferShapeCo | |||
| 49 | out_shape->SetDim(0, tokens_num); | 66 | out_shape->SetDim(0, tokens_num); |
| 50 | out_shape->SetDim(1, permuted_inputs_shape->GetDim(1)); | 67 | out_shape->SetDim(1, permuted_inputs_shape->GetDim(1)); |
| 51 | 68 | ||
| 69 | + // probs_grad 为可选输出:图调用省略该输出时 GetOutputShape(1) 返回 nullptr,跳过推导即可,不报错; | ||
| 70 | + // 输出存在时,仅当 range 提供且 probs 输入非空才推导 2D shape,否则为空 shape。 | ||
| 52 | gert::Shape* probs_out_shape = context->GetOutputShape(1); | 71 | gert::Shape* probs_out_shape = context->GetOutputShape(1); |
| 53 | - OP_CHECK_IF( | 72 | + if (probs_out_shape != nullptr) { |
| 54 | - probs_out_shape == nullptr, | 73 | + auto attrs = context->GetAttrs(); |
| 55 | - OP_LOGE_FOR_INVALID_ARGUMENT_WITH_REASON( | 74 | + const gert::ContinuousVector* rangePtr = attrs->GetAttrPointer<gert::ContinuousVector>(ATTR_INDEX_RANGE); |
| 56 | - context->GetNodeName(), "probs_out_shape", | 75 | + bool rangeProvided = (rangePtr != nullptr && rangePtr->GetSize() == 2); |
| 57 | - "Required probs_out_shape metadata is null; context->GetOutputShape(1) must provide a valid object."), | 76 | + const gert::Shape* probs_input_shape = context->GetInputShape(2); |
| 58 | - return ge::GRAPH_FAILED); | 77 | + bool hasProbs = rangeProvided && probs_input_shape != nullptr && probs_input_shape->GetDimNum() > 0; |
| 59 | - probs_out_shape->SetDimNum(out_dim_num); | 78 | + if (hasProbs) { |
| 60 | - probs_out_shape->SetDim(0, tokens_num); | 79 | + probs_out_shape->SetDimNum(out_dim_num); |
| 61 | - probs_out_shape->SetDim(1, topk); | 80 | + probs_out_shape->SetDim(0, tokens_num); |
| 81 | + probs_out_shape->SetDim(1, topk); | ||
| 82 | + } else { | ||
| 83 | + // range 为空(回退)或 permuted_probs_output_grad 为空/缺失时,probs_grad 不输出(空 shape) | ||
| 84 | + probs_out_shape->SetDimNum(0); | ||
| 85 | + } | ||
| 86 | + } | ||
| 62 | 87 | ||
| 63 | return GRAPH_SUCCESS; | 88 | return GRAPH_SUCCESS; |
| 64 | } | 89 | } |
| @@ -126,6 +126,9 @@ static inline ge::graphStatus MoeTokenUnpermuteWithEpInputParamCheck(const gert: | |||
| 126 | OP_CHECK_IF(inputTopK < 1, | 126 | OP_CHECK_IF(inputTopK < 1, |
| 127 | OP_LOGE_FOR_INVALID_ARGUMENT_WITH_REASON(nodeName, "num_topk", "input num TopK cannot less than 1."), | 127 | OP_LOGE_FOR_INVALID_ARGUMENT_WITH_REASON(nodeName, "num_topk", "input num TopK cannot less than 1."), |
| 128 | return ge::GRAPH_FAILED); | 128 | return ge::GRAPH_FAILED); |
| 129 | + OP_CHECK_IF(inputTopK > 512, | ||
| 130 | + OP_LOGE_FOR_INVALID_ARGUMENT_WITH_REASON(nodeName, "num_topk", "topK cannot larger than 512."), | ||
| 131 | + return ge::GRAPH_FAILED); | ||
| 129 | OP_CHECK_IF(tokensShape == nullptr || indicesShape == nullptr || dataTensor0 == nullptr || dataTensor1 == nullptr, | 132 | OP_CHECK_IF(tokensShape == nullptr || indicesShape == nullptr || dataTensor0 == nullptr || dataTensor1 == nullptr, |
| 130 | OP_LOGE_FOR_INVALID_ARGUMENT_WITH_REASON(Ops::Base::GetOpInfo(nodeName), | 133 | OP_LOGE_FOR_INVALID_ARGUMENT_WITH_REASON(Ops::Base::GetOpInfo(nodeName), |
| 131 | "tokensShape, indicesShape, dataTensor0, dataTensor1", | 134 | "tokensShape, indicesShape, dataTensor0, dataTensor1", |
| @@ -135,6 +138,27 @@ static inline ge::graphStatus MoeTokenUnpermuteWithEpInputParamCheck(const gert: | |||
| 135 | tokensShape->GetStorageShape().GetDimNum() != 2, | 138 | tokensShape->GetStorageShape().GetDimNum() != 2, |
| 136 | OP_LOGE_FOR_INVALID_ARGUMENT_WITH_REASON(nodeName, "permuted_tokens", "permutedTokens's shape is not 2D."), | 139 | OP_LOGE_FOR_INVALID_ARGUMENT_WITH_REASON(nodeName, "permuted_tokens", "permutedTokens's shape is not 2D."), |
| 137 | return ge::GRAPH_FAILED); | 140 | return ge::GRAPH_FAILED); |
| 141 | + OP_CHECK_IF(indicesShape->GetStorageShape().GetDimNum() != 1, | ||
| 142 | + OP_LOGE_FOR_INVALID_SHAPEDIM_WITH_REASON(nodeName, "sorted_indices", | ||
| 143 | + std::to_string(indicesShape->GetStorageShape().GetDimNum()), | ||
| 144 | + "sorted_indices must be 1D."), | ||
| 145 | + return ge::GRAPH_FAILED); | ||
| 146 | + int64_t totalLength = indicesShape->GetStorageShape().GetDim(0); | ||
| 147 | + OP_CHECK_IF( | ||
| 148 | + totalLength <= 0, | ||
| 149 | + OP_LOGE_FOR_INVALID_ARGUMENT_WITH_REASON(nodeName, "sorted_indices", "sorted_indices length must be positive."), | ||
| 150 | + return ge::GRAPH_FAILED); | ||
| 151 | + OP_CHECK_IF(totalLength % inputTopK != 0, | ||
| 152 | + OP_LOGE_FOR_INVALID_ARGUMENT_WITH_REASON(nodeName, "sorted_indices, num_topk", | ||
| 153 | + "sorted_indices length must be a multiple of num_topk."), | ||
| 154 | + return ge::GRAPH_FAILED); | ||
| 155 | + auto attrPtr = context->GetAttrs(); | ||
| 156 | + OP_CHECK_IF(attrPtr == nullptr, OP_LOGE_FOR_INVALID_ARGUMENT_WITH_REASON(nodeName, "attrPtr", "attrPtr is null."), | ||
| 157 | + return ge::GRAPH_FAILED); | ||
| 158 | + const bool* paddedModePtr = attrPtr->GetAttrPointer<bool>(2); | ||
| 159 | + OP_CHECK_IF(paddedModePtr != nullptr && *paddedModePtr, | ||
| 160 | + OP_LOGE_FOR_INVALID_ARGUMENT_WITH_REASON(nodeName, "padded_mode", "padded_mode only supports False."), | ||
| 161 | + return ge::GRAPH_FAILED); | ||
| 138 | 162 | ||
| 139 | if (probsShape != nullptr) { | 163 | if (probsShape != nullptr) { |
| 140 | int64_t probsDimNum = probsShape->GetStorageShape().GetDimNum(); | 164 | int64_t probsDimNum = probsShape->GetStorageShape().GetDimNum(); |
| @@ -197,8 +221,10 @@ static inline ge::graphStatus Init(const gert::TilingContext* context, const int | |||
| 197 | if (GetTopKAttr(context, inputTopK) != ge::GRAPH_SUCCESS) { | 221 | if (GetTopKAttr(context, inputTopK) != ge::GRAPH_SUCCESS) { |
| 198 | return ge::GRAPH_FAILED; | 222 | return ge::GRAPH_FAILED; |
| 199 | } | 223 | } |
| 224 | + bool rangeProvided = false; | ||
| 200 | auto rangePtr = attrPtr->GetAttrPointer<gert::ContinuousVector>(UNPERMUTE_WITH_EP_ARRT_RANGE); | 225 | auto rangePtr = attrPtr->GetAttrPointer<gert::ContinuousVector>(UNPERMUTE_WITH_EP_ARRT_RANGE); |
| 201 | if (rangePtr != nullptr) { | 226 | if (rangePtr != nullptr) { |
| 227 | + rangeProvided = true; | ||
| 202 | OP_CHECK_IF(rangePtr->GetSize() != RANGE_SIZE, | 228 | OP_CHECK_IF(rangePtr->GetSize() != RANGE_SIZE, |
| 203 | OP_LOGE_FOR_INVALID_ARGUMENT_WITH_REASON(context->GetNodeName(), "range", | 229 | OP_LOGE_FOR_INVALID_ARGUMENT_WITH_REASON(context->GetNodeName(), "range", |
| 204 | "the size of range only support 2"), | 230 | "the size of range only support 2"), |
| @@ -212,8 +238,19 @@ static inline ge::graphStatus Init(const gert::TilingContext* context, const int | |||
| 212 | param.input.start, param.input.end); | 238 | param.input.start, param.input.end); |
| 213 | } | 239 | } |
| 214 | } else { | 240 | } else { |
| 241 | + // range 未提供时回退全量范围(等效 aclnnMoeTokenPermuteGrad:对全部 token 计算 permute-grad)。 | ||
| 242 | + // 结束索引取 sorted_indices 全长度,避免 start/end 同为 0 导致 kernel needCopyIn 恒为 false。 | ||
| 215 | param.input.start = 0; | 243 | param.input.start = 0; |
| 216 | - param.input.end = 0; | 244 | + param.input.end = sortedIndicesShape->GetStorageShape().GetDim(0); |
| 245 | + // range 为空时回退到全量,语义变为不进行 EP 切分,应接受全部 token 输入, | ||
| 246 | + // 所以 permuted_tokens_output_grad 的大小必须与 sorted_indices 大小相等。 | ||
| 247 | + if (!isUnpermute) { | ||
| 248 | + OP_CHECK_IF(tokensShape->GetStorageShape().GetDim(0) != sortedIndicesShape->GetStorageShape().GetDim(0), | ||
| 249 | + OP_LOGE_FOR_INVALID_ARGUMENT_WITH_REASON( | ||
| 250 | + context->GetNodeName(), "permuted_tokens_output_grad, sorted_indices", | ||
| 251 | + "permuted_tokens_output_grad rows must equal sorted_indices length when range is absent."), | ||
| 252 | + return ge::GRAPH_FAILED); | ||
| 253 | + } | ||
| 217 | } | 254 | } |
| 218 | 255 | ||
| 219 | param.input.numOutTokens = tokensShape->GetStorageShape().GetDim(0); // numOutTokens根据tokens第0维获取; | 256 | param.input.numOutTokens = tokensShape->GetStorageShape().GetDim(0); // numOutTokens根据tokens第0维获取; |
| @@ -236,7 +273,16 @@ static inline ge::graphStatus Init(const gert::TilingContext* context, const int | |||
| 236 | param.input.PermuteProbGradSize = 1; | 273 | param.input.PermuteProbGradSize = 1; |
| 237 | param.input.tokensNum = safeDiv(param.input.totalLength, param.input.topK); | 274 | param.input.tokensNum = safeDiv(param.input.totalLength, param.input.topK); |
| 238 | } | 275 | } |
| 239 | - param.input.haveProbs = (probsShape != nullptr); | 276 | + // permute-grad 场景(range 提供时)probs 输入存在时必须有 probs_grad 输出,否则 op2 会向空输出地址写导致越界; |
| 277 | + // probs 缺失时允许 probs_grad 输出存在但被忽略(haveProbs 为 false,不写 probs_grad)。 | ||
| 278 | + if (!isUnpermute && rangeProvided && probsShape != nullptr && context->GetOutputShape(1) == nullptr) { | ||
| 279 | + OP_LOGE_FOR_INVALID_ARGUMENT_WITH_REASON( | ||
| 280 | + context->GetNodeName(), "probs_grad", | ||
| 281 | + "probs_grad output is required when permuted_probs_output_grad is provided."); | ||
| 282 | + return ge::GRAPH_FAILED; | ||
| 283 | + } | ||
| 284 | + // range 为空时回退到aclnnMoeTokenPermuteGrad,忽略 probs | ||
| 285 | + param.input.haveProbs = (probsShape != nullptr) && (isUnpermute || rangeProvided); | ||
| 240 | return ge::GRAPH_SUCCESS; | 286 | return ge::GRAPH_SUCCESS; |
| 241 | } | 287 | } |
| 242 | 288 | ||
| @@ -161,8 +161,13 @@ __aicore__ inline void KernelMoeTokenUnpermuteWithEp<T1, T2, T3, PROBS, ISTOKEN> | |||
| 161 | out_block_offset = out_block_offset * this->top_k; | 161 | out_block_offset = out_block_offset * this->top_k; |
| 162 | } | 162 | } |
| 163 | 163 | ||
| 164 | - this->outGM.SetGlobalBuffer((__gm__ T1*)unpermuted_tokens + out_block_offset, | 164 | + // probs 分支(ISTOKEN=false)每个 token 写 top_k 个元素,缓冲区跨度须按 top_k 计; |
| 165 | - this->tokens_core_length * this->hidden_size); | 165 | + // hidden_size 在该模板实例下恒为 1,仅乘以 hidden_size 会低估 probs_grad 的写入跨度。 |
| 166 | + int64_t out_block_length = this->tokens_core_length * this->hidden_size; | ||
| 167 | + if constexpr (!ISTOKEN) { | ||
| 168 | + out_block_length = this->tokens_core_length * this->top_k; | ||
| 169 | + } | ||
| 170 | + this->outGM.SetGlobalBuffer((__gm__ T1*)unpermuted_tokens + out_block_offset, out_block_length); | ||
| 166 | if constexpr (ISTOKEN) { | 171 | if constexpr (ISTOKEN) { |
| 167 | this->pipe->InitBuffer(tokens_inque, tiling_data->buffer_num, hidden_splited_length_align512 * sizeof(T1)); | 172 | this->pipe->InitBuffer(tokens_inque, tiling_data->buffer_num, hidden_splited_length_align512 * sizeof(T1)); |
| 168 | } else { | 173 | } else { |
【review】 问题分类:正确性 位置:moe/moe_token_permute_with_ep_grad/op_host/moe_token_permute_with_ep_grad_tiling.cpp:242,代码:param.input.end = sortedIndicesShape->GetStorageShape().GetDim(0);同文件:245,代码:param.input.numOutTokens = tokensShape->GetStorageShape().GetDim(0);moe/moe_token_permute_with_ep_grad/op_kernel/moe_token_permute_with_ep_grad.h:296,代码:needCopyIn = acl_token_idx >= this->start && acl_token_idx < this->end;同文件:340,代码:offset = (in_token_index - this->start) * this->hidden_size + ... 问题:新增的 range 缺省分支把 end 设为索引表长度 L,但这个值表示映射条目数,并不保证梯度输入实际有 L 行。当前参数检查只验证 tokens 为二维、indices 为一维及 L 能被 topK 整除,没有检查缺省全量范围与输入行数一致;numOutTokens 虽记录了真实行数,内核读入条件却未使用它。例如 range 属性读取为 nullptr、概率梯度输入缺失、topK=2、tokens shape=(4,2)、indices shape=(6,) 且值为 [0,1,2,3,4,5] 时,该组合能通过现有 tiling 检查,新分支得到 [start,end)=[0,6)。索引 4、5 均被判为可读,但 tokens 只有 4 行。 可能导致的结果:CopyTokenIn 会从第 4、5 行执行 DMA 读取,超出输入张量范围,造成非法内存访问或将其他内存数据累加到梯度。此问题来自本次将缺省空范围改为全量范围后新增的实际读路径;上述结论已核对参数检查和设备读址公式,未进行设备越界测试。 建议的修改方法:在启用缺省全量范围前,校验梯度输入首维与 L 相等,不满足时返回参数错误;若要支持截断输入,应明确其范围语义并按真实可读行数限制读入。增加 range 缺省下输入行数不足的失败用例,以及合法全量输入的数值用例,防止新的 fallback 绕过输入边界。