Merged
fix(mse_loss): MseLossOut空tensor输入按reduction语义填充输出 #14080
佐助大王Add on 11 days ago
fix(mse_loss): MseLossOut空tensor输入按reduction语义填充输出 #14080
Merged
Total: 4 files changed+141-2
| @@ -247,6 +247,10 @@ aclnnStatus aclnnMseLossOut( | |||
| 247 | 247 | ||
| 248 | - 确定性计算: | 248 | - 确定性计算: |
| 249 | - aclnnMseLossOut默认确定性实现。 | 249 | - aclnnMseLossOut默认确定性实现。 |
| 250 | +- 空 tensor 输入(元素个数为0)的输出语义: | ||
| 251 | + - reduction为0('none')时,输出为空 tensor,不写入任何元素; | ||
| 252 | + - reduction为1('mean')时,输出填充为 NaN; | ||
| 253 | + - reduction为2('sum')时,输出填充为 0。 | ||
| 250 | 254 | ||
| 251 | ## 调用示例 | 255 | ## 调用示例 |
| 252 | 256 | ||
| @@ -249,6 +249,10 @@ aclnnStatus aclnnMseLossOut( | |||
| 249 | 249 | ||
| 250 | - Deterministic computation: | 250 | - Deterministic computation: |
| 251 | - aclnnMseLossOut defaults to a deterministic implementation. | 251 | - aclnnMseLossOut defaults to a deterministic implementation. |
| 252 | +- Output semantics for empty tensor inputs (with 0 elements): | ||
| 253 | + - When reduction is 0 ('none'), the output is an empty tensor and no element is written. | ||
| 254 | + - When reduction is 1 ('mean'), the output is filled with NaN. | ||
| 255 | + - When reduction is 2 ('sum'), the output is filled with 0. | ||
| 252 | 256 | ||
| 253 | ## Example | 257 | ## Example |
| 254 | 258 | ||
| @@ -102,6 +102,31 @@ inline static const std::string& GetReductionStr(int64_t reduction) | |||
| 102 | } | 102 | } |
| 103 | } | 103 | } |
| 104 | 104 | ||
| 105 | +// 空tensor场景按0轴归约语义填充out:mean时0轴ReduceMean结果逐元素为NaN,sum时0轴ReduceSum结果逐元素为0, | ||
| 106 | +// 需按out的实际view shape整体填充,out为空tensor时无需写入 | ||
| 107 | +static aclnnStatus FillOutForEmptyInput(aclTensor* out, float val, aclOpExecutor* executor) | ||
| 108 | +{ | ||
| 109 | + if (out->GetViewShape().GetShapeSize() == 0) { | ||
| 110 | + return ACLNN_SUCCESS; | ||
| 111 | + } | ||
| 112 | + auto outShape = op::ToShapeVector(out->GetViewShape()); | ||
| 113 | + auto dims = executor->ConvertToTensor(outShape.data(), outShape.size(), op::DataType::DT_INT64); | ||
| 114 | + CHECK_RET(dims != nullptr, ACLNN_ERR_INNER_NULLPTR); | ||
| 115 | + auto shapeArray = executor->AllocIntArray(outShape.data(), outShape.size()); | ||
| 116 | + CHECK_RET(shapeArray != nullptr, ACLNN_ERR_INNER_NULLPTR); | ||
| 117 | + | ||
| 118 | + FVector<float> valVector = {val}; | ||
| 119 | + auto valTensor = executor->ConvertToTensor(valVector.data(), valVector.size(), out->GetDataType()); | ||
| 120 | + CHECK_RET(valTensor != nullptr, ACLNN_ERR_INNER_NULLPTR); | ||
| 121 | + | ||
| 122 | + auto fillOut = l0op::Fill(dims, valTensor, shapeArray, executor); | ||
| 123 | + CHECK_RET(fillOut != nullptr, ACLNN_ERR_INNER_NULLPTR); | ||
| 124 | + // 固定写法,将填充结果拷贝到输出out上,out可能是非连续的tensor | ||
| 125 | + auto viewCopyResult = l0op::ViewCopy(fillOut, out, executor); | ||
| 126 | + CHECK_RET(viewCopyResult != nullptr, ACLNN_ERR_INNER_NULLPTR); | ||
| 127 | + return ACLNN_SUCCESS; | ||
| 128 | +} | ||
| 129 | + | ||
| 105 | aclnnStatus aclnnMseLossOutGetWorkspaceSize(const aclTensor* self, const aclTensor* target, int64_t reduction, | 130 | aclnnStatus aclnnMseLossOutGetWorkspaceSize(const aclTensor* self, const aclTensor* target, int64_t reduction, |
| 106 | aclTensor* out, uint64_t* workspaceSize, aclOpExecutor** executor) | 131 | aclTensor* out, uint64_t* workspaceSize, aclOpExecutor** executor) |
| 107 | { | 132 | { |
| @@ -117,8 +142,14 @@ aclnnStatus aclnnMseLossOutGetWorkspaceSize(const aclTensor* self, const aclTens | |||
| 117 | CHECK_RET(ret == ACLNN_SUCCESS, ret); | 142 | CHECK_RET(ret == ACLNN_SUCCESS, ret); |
| 118 | 143 | ||
| 119 | if (self->IsEmpty() || target->IsEmpty()) { | 144 | if (self->IsEmpty() || target->IsEmpty()) { |
| 120 | - // 根据实际支持情况补充 | 145 | + // reduction为mean时对0轴ReduceMean结果逐元素为NaN,为sum时对0轴ReduceSum结果逐元素为0,为none时不写out |
| 121 | - *workspaceSize = 0; | 146 | + if (reduction == REDUCTION_MEAN_NUM) { |
| 147 | + ret = FillOutForEmptyInput(out, NAN, uniqueExecutor.get()); | ||
| 148 | + } else if (reduction == REDUCTION_SUM_NUM) { | ||
| 149 | + ret = FillOutForEmptyInput(out, 0, uniqueExecutor.get()); | ||
| 150 | + } | ||
| 151 | + CHECK_RET(ret == ACLNN_SUCCESS, ret); | ||
| 152 | + *workspaceSize = uniqueExecutor->GetWorkspaceSize(); | ||
| 122 | uniqueExecutor.ReleaseTo(executor); | 153 | uniqueExecutor.ReleaseTo(executor); |
| 123 | return ACLNN_SUCCESS; | 154 | return ACLNN_SUCCESS; |
| 124 | } | 155 | } |
| @@ -164,6 +164,66 @@ TEST_F(l2_mse_loss_out_test, aclnnMseLossOut_07_float_nd_empty_tensor_sum) | |||
| 164 | ut.TestPrecision(); | 164 | ut.TestPrecision(); |
| 165 | } | 165 | } |
| 166 | 166 | ||
| 167 | +// 空输入+非空out场景: self/target为[0,4]空tensor, reduction=mean时out按标量广播填充NaN, 验证填充路径真实执行 | ||
| 168 | +TEST_F(l2_mse_loss_out_test, aclnnMseLossOut_08_float_nd_empty_input_mean_out_not_empty) | ||
| 169 | +{ | ||
| 170 | + auto selfDesc = TensorDesc({0, 4}, ACL_FLOAT, ACL_FORMAT_ND); | ||
| 171 | + auto targetDesc = TensorDesc({0, 4}, ACL_FLOAT, ACL_FORMAT_ND); | ||
| 172 | + int64_t reduction = 1; | ||
| 173 | + | ||
| 174 | + auto outDesc = TensorDesc({4}, ACL_FLOAT, ACL_FORMAT_ND).Precision(0.0001, 0.0001); | ||
| 175 | + | ||
| 176 | + auto ut = OP_API_UT(aclnnMseLossOut, INPUT(selfDesc, targetDesc, reduction), OUTPUT(outDesc)); | ||
| 177 | + | ||
| 178 | + // SAMPLE: only test GetWorkspaceSize | ||
| 179 | + uint64_t workspace_size = 0; | ||
| 180 | + aclnnStatus aclRet = ut.TestGetWorkspaceSize(&workspace_size); | ||
| 181 | + EXPECT_EQ(aclRet, ACL_SUCCESS); | ||
| 182 | + | ||
| 183 | + // SAMPLE: precision simulate | ||
| 184 | + ut.TestPrecision(); | ||
| 185 | +} | ||
| 186 | + | ||
| 187 | +// 空输入+非空out场景: self/target为[0,4]空tensor, reduction=sum时out按标量广播填充0, 验证填充路径真实执行 | ||
| 188 | +TEST_F(l2_mse_loss_out_test, aclnnMseLossOut_09_float16_nd_empty_input_sum_out_not_empty) | ||
| 189 | +{ | ||
| 190 | + auto selfDesc = TensorDesc({0, 4}, ACL_FLOAT16, ACL_FORMAT_ND); | ||
| 191 | + auto targetDesc = TensorDesc({0, 4}, ACL_FLOAT16, ACL_FORMAT_ND); | ||
| 192 | + int64_t reduction = 2; | ||
| 193 | + | ||
| 194 | + auto outDesc = TensorDesc({4}, ACL_FLOAT16, ACL_FORMAT_ND).Precision(0.001, 0.001); | ||
| 195 | + | ||
| 196 | + auto ut = OP_API_UT(aclnnMseLossOut, INPUT(selfDesc, targetDesc, reduction), OUTPUT(outDesc)); | ||
| 197 | + | ||
| 198 | + // SAMPLE: only test GetWorkspaceSize | ||
| 199 | + uint64_t workspace_size = 0; | ||
| 200 | + aclnnStatus aclRet = ut.TestGetWorkspaceSize(&workspace_size); | ||
| 201 | + EXPECT_EQ(aclRet, ACL_SUCCESS); | ||
| 202 | + | ||
| 203 | + // SAMPLE: precision simulate | ||
| 204 | + ut.TestPrecision(); | ||
| 205 | +} | ||
| 206 | + | ||
| 207 | +// 空输入+非空out场景: reduction=none时空输入不写out, 非空out合法场景补充覆盖 | ||
| 208 | +TEST_F(l2_mse_loss_out_test, aclnnMseLossOut_10_float_nd_empty_input_none_out_empty) | ||
| 209 | +{ | ||
| 210 | + auto selfDesc = TensorDesc({0, 4}, ACL_FLOAT, ACL_FORMAT_ND); | ||
| 211 | + auto targetDesc = TensorDesc({0, 4}, ACL_FLOAT, ACL_FORMAT_ND); | ||
| 212 | + int64_t reduction = 0; | ||
| 213 | + | ||
| 214 | + auto outDesc = TensorDesc({0, 4}, ACL_FLOAT, ACL_FORMAT_ND).Precision(0.0001, 0.0001); | ||
| 215 | + | ||
| 216 | + auto ut = OP_API_UT(aclnnMseLossOut, INPUT(selfDesc, targetDesc, reduction), OUTPUT(outDesc)); | ||
| 217 | + | ||
| 218 | + // SAMPLE: only test GetWorkspaceSize | ||
| 219 | + uint64_t workspace_size = 0; | ||
| 220 | + aclnnStatus aclRet = ut.TestGetWorkspaceSize(&workspace_size); | ||
| 221 | + EXPECT_EQ(aclRet, ACL_SUCCESS); | ||
| 222 | + | ||
| 223 | + // SAMPLE: precision simulate | ||
| 224 | + ut.TestPrecision(); | ||
| 225 | +} | ||
| 226 | + | ||
| 167 | TEST_F(l2_mse_loss_out_test, aclnnMseLossOut_11_float_hwcn_input_not_contiguous) | 227 | TEST_F(l2_mse_loss_out_test, aclnnMseLossOut_11_float_hwcn_input_not_contiguous) |
| 168 | { | 228 | { |
| 169 | auto selfDesc = TensorDesc({5, 4}, ACL_FLOAT, ACL_FORMAT_HWCN, {1, 5}, 0, {4, 5}).ValueRange(-2, 2); | 229 | auto selfDesc = TensorDesc({5, 4}, ACL_FLOAT, ACL_FORMAT_HWCN, {1, 5}, 0, {4, 5}).ValueRange(-2, 2); |
| @@ -368,3 +428,43 @@ TEST_F(l2_mse_loss_out_test, ascend910B2_aclnnMseLossOut_bf16_float_nhwc_mean) | |||
| 368 | aclnnStatus aclRet = ut.TestGetWorkspaceSize(&workspaceSize); | 428 | aclnnStatus aclRet = ut.TestGetWorkspaceSize(&workspaceSize); |
| 369 | EXPECT_EQ(aclRet, ACLNN_SUCCESS); | 429 | EXPECT_EQ(aclRet, ACLNN_SUCCESS); |
| 370 | } | 430 | } |
| 431 | + | ||
| 432 | +// 空输入+非空out场景: self/target为[0,4]空tensor, reduction=mean时out按标量广播填充NaN, bf16覆盖 | ||
| 433 | +TEST_F(l2_mse_loss_out_test, ascend910B2_aclnnMseLossOut_bf16_nd_empty_input_mean_out_not_empty) | ||
| 434 | +{ | ||
| 435 | + auto selfDesc = TensorDesc({0, 4}, ACL_BF16, ACL_FORMAT_ND); | ||
| 436 | + auto targetDesc = TensorDesc({0, 4}, ACL_BF16, ACL_FORMAT_ND); | ||
| 437 | + int64_t reduction = 1; | ||
| 438 | + | ||
| 439 | + auto outDesc = TensorDesc({4}, ACL_BF16, ACL_FORMAT_ND).Precision(0.0001, 0.0001); | ||
| 440 | + | ||
| 441 | + auto ut = OP_API_UT(aclnnMseLossOut, INPUT(selfDesc, targetDesc, reduction), OUTPUT(outDesc)); | ||
| 442 | + | ||
| 443 | + // SAMPLE: only test GetWorkspaceSize | ||
| 444 | + uint64_t workspace_size = 0; | ||
| 445 | + aclnnStatus aclRet = ut.TestGetWorkspaceSize(&workspace_size); | ||
| 446 | + EXPECT_EQ(aclRet, ACL_SUCCESS); | ||
| 447 | + | ||
| 448 | + // SAMPLE: precision simulate | ||
| 449 | + ut.TestPrecision(); | ||
| 450 | +} | ||
| 451 | + | ||
| 452 | +// 空输入+非空out场景: self/target为[0,4]空tensor, reduction=sum时out按标量广播填充0, bf16覆盖 | ||
| 453 | +TEST_F(l2_mse_loss_out_test, ascend910B2_aclnnMseLossOut_bf16_nd_empty_input_sum_out_not_empty) | ||
| 454 | +{ | ||
| 455 | + auto selfDesc = TensorDesc({0, 4}, ACL_BF16, ACL_FORMAT_ND); | ||
| 456 | + auto targetDesc = TensorDesc({0, 4}, ACL_BF16, ACL_FORMAT_ND); | ||
| 457 | + int64_t reduction = 2; | ||
| 458 | + | ||
| 459 | + auto outDesc = TensorDesc({4}, ACL_BF16, ACL_FORMAT_ND).Precision(0.0001, 0.0001); | ||
| 460 | + | ||
| 461 | + auto ut = OP_API_UT(aclnnMseLossOut, INPUT(selfDesc, targetDesc, reduction), OUTPUT(outDesc)); | ||
| 462 | + | ||
| 463 | + // SAMPLE: only test GetWorkspaceSize | ||
| 464 | + uint64_t workspace_size = 0; | ||
| 465 | + aclnnStatus aclRet = ut.TestGetWorkspaceSize(&workspace_size); | ||
| 466 | + EXPECT_EQ(aclRet, ACL_SUCCESS); | ||
| 467 | + | ||
| 468 | + // SAMPLE: precision simulate | ||
| 469 | + ut.TestPrecision(); | ||
| 470 | +} | ||