Merged
fix(dequant_situ_quant): aclnn第一段补齐输入shape/组合约束校验 #14701
demoaugusteAdd on 4 days ago
fix(dequant_situ_quant): aclnn第一段补齐输入shape/组合约束校验 #14701
Merged
demoaugusteAdd on 4 days ago
Total: 2 files changed+874-7
@@ -82,7 +82,7 @@ static bool CheckNotNull(const aclTensor* x, const aclTensor* yOut, const aclTen
82}82}
83 83 
84// 2. 检查条件必选输入是否为空指针:INT8/INT32必选weightScaleOptional,INT32必选activationScaleOptional,84// 2. 检查条件必选输入是否为空指针:INT8/INT32必选weightScaleOptional,INT32必选activationScaleOptional,
85-// static量化必选quantScaleOptional85+// INT8+static量化必选quantScaleOptional
86static bool CheckConditionalInputNotNull(const aclTensor* x, const aclTensor* weightScaleOptional,86static bool CheckConditionalInputNotNull(const aclTensor* x, const aclTensor* weightScaleOptional,
87 const aclTensor* activationScaleOptional, const aclTensor* quantScaleOptional,87 const aclTensor* activationScaleOptional, const aclTensor* quantScaleOptional,
88 QuantType quantType)88 QuantType quantType)
@@ -94,7 +94,7 @@ static bool CheckConditionalInputNotNull(const aclTensor* x, const aclTensor* we
94 if (xDtype == op::DataType::DT_INT32) {94 if (xDtype == op::DataType::DT_INT32) {
95 OP_CHECK_NULL(activationScaleOptional, return false);95 OP_CHECK_NULL(activationScaleOptional, return false);
96 }96 }
97- if (quantType == QuantType::STATIC) {97+ if (quantType == QuantType::STATIC && xDtype == op::DataType::DT_INT8) {
98 OP_CHECK_NULL(quantScaleOptional, return false);98 OP_CHECK_NULL(quantScaleOptional, return false);
99 }99 }
100 return true;100 return true;
@@ -248,8 +248,8 @@ static bool CheckXShape(const aclTensor* x)
248 return true;248 return true;
249}249}
250 250 
251-// 9. 检查输出shape的维度与x匹配251+// 9. 检查输出shape的维度与x匹配(含未知维(-1)时无法精确推导,仅校验rank)
252-static bool CheckOutShape(const aclTensor* x, const aclTensor* yOut, const aclTensor* yScaleOut)252+static bool CheckOutShapeRank(const aclTensor* x, const aclTensor* yOut, const aclTensor* yScaleOut)
253{253{
254 const size_t xDimNum = x->GetViewShape().GetDimNum();254 const size_t xDimNum = x->GetViewShape().GetDimNum();
255 const size_t expectedYDimNum = (x->GetDataType() == op::DataType::DT_INT8) ? xDimNum : DIM_NUM_NOT_INT8;255 const size_t expectedYDimNum = (x->GetDataType() == op::DataType::DT_INT8) ? xDimNum : DIM_NUM_NOT_INT8;
@@ -270,6 +270,284 @@ static bool CheckOutShape(const aclTensor* x, const aclTensor* yOut, const aclTe
270 return true;270 return true;
271}271}
272 272 
273+// 10. 检查可选输入是否应缺省(当前数据类型/量化模式下不使用)
274+static bool IsTensorAbsent(const aclTensor* tensor, const char* paramName, const char* reason)
275+{
276+ if (tensor == nullptr) {
277+ return true;
278+ }
279+ ReportInvalidValue(paramName, "not null", reason);
280+ return false;
281+}
282+ 
283+// 11. 检查tensor的shape size是否为指定取值之一
284+static bool CheckShapeSizeOneOf(const aclTensor* tensor, const char* paramName, int64_t firstSize, int64_t secondSize,
285+ const char* expectedRule)
286+{
287+ const int64_t size = tensor->GetViewShape().GetShapeSize();
288+ if (size == firstSize || size == secondSize) {
289+ return true;
290+ }
291+ const std::string reason = (std::string(paramName) + " shape size must be " + expectedRule + ", but got " +
292+ std::to_string(size) + ".");
293+ ReportInvalidShape(paramName, op::ToString(tensor->GetViewShape()).GetString(), reason);
294+ return false;
295+}
296+ 
297+// 12. 检查tensor的shape是否与期望shape一致
298+static bool CheckShapeExpected(const aclTensor* tensor, const char* paramName, const op::Shape& expectedShape,
299+ const char* expectedRule)
300+{
301+ if (tensor->GetViewShape() == expectedShape) {
302+ return true;
303+ }
304+ const std::string reason = (std::string(paramName) + " shape must be " + expectedRule + ", but got " +
305+ std::string(op::ToString(tensor->GetViewShape()).GetString()) + ".");
306+ ReportInvalidShape(paramName, op::ToString(tensor->GetViewShape()).GetString(), reason);
307+ return false;
308+}
309+ 
310+// 13. 检查1维非空tensor(groupIndexOptional)
311+static bool CheckNonEmptyOneDim(const aclTensor* tensor, const char* paramName)
312+{
313+ const op::Shape& shape = tensor->GetViewShape();
314+ if (shape.GetDimNum() == 1 && shape.GetDim(0) > 0) {
315+ return true;
316+ }
317+ const std::string reason = (std::string(paramName) + " must be a non-empty 1-D tensor, but got " +
318+ std::string(op::ToString(shape).GetString()) + ".");
319+ ReportInvalidShape(paramName, op::ToString(shape).GetString(), reason);
320+ return false;
321+}
322+ 
323+// 14. 检查INT32路径weightScaleOptional/biasOptional的shape:无group_index时为[2H]/[1]/[1,2H],
324+// 有group_index时为[expertNum, 2H]
325+static bool CheckMoeScaleShape(const aclTensor* tensor, const char* paramName, int64_t expertNum, int64_t inDimy,
326+ bool hasGroupIndex)
327+{
328+ const op::Shape& shape = tensor->GetViewShape();
329+ const size_t dimNum = shape.GetDimNum();
330+ bool valid = false;
331+ if (hasGroupIndex) {
332+ valid = (dimNum == static_cast<size_t>(NUM_TWO) && shape.GetDim(0) == expertNum && shape.GetDim(1) == inDimy);
333+ } else {
334+ valid = (dimNum == 1 && (shape.GetDim(0) == inDimy || shape.GetDim(0) == 1)) ||
335+ (dimNum == static_cast<size_t>(NUM_TWO) && shape.GetDim(0) == 1 && shape.GetDim(1) == inDimy);
336+ }
337+ if (valid) {
338+ return true;
339+ }
340+ const std::string reason = (std::string(paramName) + " shape is invalid, expertNum=" + std::to_string(expertNum) +
341+ ", inputWidth=" + std::to_string(inDimy) + ".");
342+ ReportInvalidShape(paramName, op::ToString(shape).GetString(), reason);
343+ return false;
344+}
345+ 
346+// 15. 检查INT32路径activationScaleOptional的shape:(1,)或每行一个值
347+static bool CheckInt32ActivationScaleShape(const aclTensor* tensor, int64_t rows)
348+{
349+ const op::Shape& shape = tensor->GetViewShape();
350+ const bool rankValid = (shape.GetDimNum() == 1) ||
351+ (shape.GetDimNum() == static_cast<size_t>(NUM_TWO) && shape.GetDim(1) == 1);
352+ const bool sizeValid = (shape.GetShapeSize() == rows) || (shape.GetShapeSize() == 1);
353+ if (rankValid && sizeValid) {
354+ return true;
355+ }
356+ const std::string reason = (std::string("activationScaleOptional shape must be (1,) or contain one FP32 value "
357+ "per row, but got ") +
358+ std::string(op::ToString(shape).GetString()) + ".");
359+ ReportInvalidShape("activationScaleOptional", op::ToString(shape).GetString(), reason);
360+ return false;
361+}
362+ 
363+// 16. INT8路径的shape约束(与tiling侧入参校验保持一致)
364+static bool CheckInt8ShapeConstraints(const aclTensor* x, const aclTensor* weightScaleOptional,
365+ const aclTensor* activationScaleOptional, const aclTensor* biasOptional,
366+ const aclTensor* quantScaleOptional, const aclTensor* quantOffsetOptional,
367+ const aclTensor* groupIndexOptional, QuantType quantType, const aclTensor* yOut,
368+ const aclTensor* yScaleOut)
369+{
370+ const op::Shape& xShape = x->GetViewShape();
371+ const size_t dimNum = xShape.GetDimNum();
372+ const int64_t inDimy = xShape.GetDim(dimNum - 1);
373+ const int64_t outDimy = inDimy / NUM_TWO;
374+ 
375+ if (!CheckShapeSizeOneOf(weightScaleOptional, "weightScaleOptional", inDimy, 1, "(2H,) or (1,)")) {
376+ return false;
377+ }
378+ if (!IsTensorAbsent(activationScaleOptional, "activationScaleOptional",
379+ "activationScaleOptional is not used when the data type of x is INT8.")) {
380+ return false;
381+ }
382+ if (biasOptional != nullptr && !CheckShapeSizeOneOf(biasOptional, "biasOptional", inDimy, 1, "(2H,) or (1,)")) {
383+ return false;
384+ }
385+ if (quantScaleOptional != nullptr &&
386+ !CheckShapeSizeOneOf(quantScaleOptional, "quantScaleOptional", outDimy, 1, "(H,) or (1,)")) {
387+ return false;
388+ }
389+ if (quantType == QuantType::STATIC) {
390+ if (quantOffsetOptional != nullptr &&
391+ !CheckShapeSizeOneOf(quantOffsetOptional, "quantOffsetOptional", outDimy, 1, "(H,) or (1,)")) {
392+ return false;
393+ }
394+ } else if (!IsTensorAbsent(quantOffsetOptional, "quantOffsetOptional",
395+ "quantOffsetOptional is only supported when quantTypeOptional is static.")) {
396+ return false;
397+ }
398+ if (!IsTensorAbsent(groupIndexOptional, "groupIndexOptional",
399+ "groupIndexOptional is not used when the data type of x is INT8.")) {
400+ return false;
401+ }
402+ 
403+ op::Shape expectedYShape;
404+ op::Shape expectedYScaleShape;
405+ for (size_t i = 0; i + 1 < dimNum; i++) {
406+ expectedYShape.AppendDim(xShape.GetDim(i));
407+ expectedYScaleShape.AppendDim(xShape.GetDim(i));
408+ }
409+ expectedYShape.AppendDim(outDimy);
410+ if (!CheckShapeExpected(yOut, "yOut", expectedYShape, "x.shape[:-1] + [H] (H = x.shape[-1] / 2)")) {
411+ return false;
412+ }
413+ return CheckShapeExpected(yScaleOut, "yScaleOut", expectedYScaleShape, "x.shape[:-1]");
414+}
415+ 
416+// 17. INT32路径的shape约束(与tiling侧入参校验保持一致)
417+static bool CheckInt32ShapeConstraints(const aclTensor* x, const aclTensor* weightScaleOptional,
418+ const aclTensor* activationScaleOptional, const aclTensor* biasOptional,
419+ const aclTensor* quantScaleOptional, const aclTensor* quantOffsetOptional,
420+ const aclTensor* groupIndexOptional, QuantType quantType, const aclTensor* yOut,
421+ const aclTensor* yScaleOut)
422+{
423+ const op::Shape& xShape = x->GetViewShape();
424+ const int64_t rows = xShape.GetDim(0);
425+ const int64_t inDimy = xShape.GetDim(1);
426+ const int64_t outDimy = inDimy / NUM_TWO;
427+ 
428+ if (quantType != QuantType::DYNAMIC) {
429+ ReportInvalidValue("quantTypeOptional", "static",
430+ "quantTypeOptional must be dynamic when the data type of x is INT32.");
431+ return false;
432+ }
433+ if (!IsTensorAbsent(quantScaleOptional, "quantScaleOptional",
434+ "quantScaleOptional is not used when the data type of x is INT32.")) {
435+ return false;
436+ }
437+ if (!IsTensorAbsent(quantOffsetOptional, "quantOffsetOptional",
438+ "quantOffsetOptional is not used when the data type of x is INT32.")) {
439+ return false;
440+ }
441+ const bool hasGroupIndex = (groupIndexOptional != nullptr);
442+ int64_t expertNum = 1;
443+ if (hasGroupIndex) {
444+ if (!CheckNonEmptyOneDim(groupIndexOptional, "groupIndexOptional")) {
445+ return false;
446+ }
447+ expertNum = groupIndexOptional->GetViewShape().GetDim(0);
448+ }
449+ if (!CheckMoeScaleShape(weightScaleOptional, "weightScaleOptional", expertNum, inDimy, hasGroupIndex)) {
450+ return false;
451+ }
452+ if (!CheckInt32ActivationScaleShape(activationScaleOptional, rows)) {
453+ return false;
454+ }
455+ if (biasOptional != nullptr &&
456+ !CheckMoeScaleShape(biasOptional, "biasOptional", expertNum, inDimy, hasGroupIndex)) {
457+ return false;
458+ }
459+ 
460+ op::Shape expectedYShape;
461+ expectedYShape.AppendDim(rows);
462+ expectedYShape.AppendDim(outDimy);
463+ if (!CheckShapeExpected(yOut, "yOut", expectedYShape, "[rows, input_width / 2]")) {
464+ return false;
465+ }
466+ op::Shape expectedYScaleShape;
467+ expectedYScaleShape.AppendDim(rows);
468+ return CheckShapeExpected(yScaleOut, "yScaleOut", expectedYScaleShape, "[rows]");
469+}
470+ 
471+// 18. BFLOAT16/FLOAT16(预反量化)路径的shape约束(与tiling侧入参校验保持一致)
472+static bool CheckPreDequantShapeConstraints(const aclTensor* x, const aclTensor* weightScaleOptional,
473+ const aclTensor* activationScaleOptional, const aclTensor* biasOptional,
474+ const aclTensor* quantScaleOptional, const aclTensor* quantOffsetOptional,
475+ const aclTensor* groupIndexOptional, QuantType quantType,
476+ const aclTensor* yOut, const aclTensor* yScaleOut)
477+{
478+ const op::Shape& xShape = x->GetViewShape();
479+ const int64_t rows = xShape.GetDim(0);
480+ const int64_t inDimy = xShape.GetDim(1);
481+ const int64_t outDimy = inDimy / NUM_TWO;
482+ 
483+ if (quantType != QuantType::DYNAMIC) {
484+ ReportInvalidValue("quantTypeOptional", "static",
485+ "quantTypeOptional must be dynamic when the data type of x is BFLOAT16/FLOAT16.");
486+ return false;
487+ }
488+ if (!IsTensorAbsent(weightScaleOptional, "weightScaleOptional",
489+ "weightScaleOptional is not used when the data type of x is BFLOAT16/FLOAT16.")) {
490+ return false;
491+ }
492+ if (!IsTensorAbsent(activationScaleOptional, "activationScaleOptional",
493+ "activationScaleOptional is not used when the data type of x is BFLOAT16/FLOAT16.")) {
494+ return false;
495+ }
496+ if (!IsTensorAbsent(biasOptional, "biasOptional",
497+ "biasOptional is not used when the data type of x is BFLOAT16/FLOAT16.")) {
498+ return false;
499+ }
500+ if (!IsTensorAbsent(quantScaleOptional, "quantScaleOptional",
501+ "quantScaleOptional is not used when the data type of x is BFLOAT16/FLOAT16.")) {
502+ return false;
503+ }
504+ if (!IsTensorAbsent(quantOffsetOptional, "quantOffsetOptional",
505+ "quantOffsetOptional is not used when the data type of x is BFLOAT16/FLOAT16.")) {
506+ return false;
507+ }
508+ if (groupIndexOptional != nullptr && !CheckNonEmptyOneDim(groupIndexOptional, "groupIndexOptional")) {
509+ return false;
510+ }
511+ 
512+ op::Shape expectedYShape;
513+ expectedYShape.AppendDim(rows);
514+ expectedYShape.AppendDim(outDimy);
515+ if (!CheckShapeExpected(yOut, "yOut", expectedYShape, "[rows, input_width / 2]")) {
516+ return false;
517+ }
518+ op::Shape expectedYScaleShape;
519+ expectedYScaleShape.AppendDim(rows);
520+ return CheckShapeExpected(yScaleOut, "yScaleOut", expectedYScaleShape, "[rows]");
521+}
522+ 
523+// 19. 检查输入/输出shape约束(含输入之间的shape关系),与tiling侧入参校验口径一致:
524+// 第一段接口即可判定的约束在此返回161002,避免落到tiling返回561002
525+static bool CheckShapeConstraints(const aclTensor* x, const aclTensor* weightScaleOptional,
526+ const aclTensor* activationScaleOptional, const aclTensor* biasOptional,
527+ const aclTensor* quantScaleOptional, const aclTensor* quantOffsetOptional,
528+ const aclTensor* groupIndexOptional, QuantType quantType, const aclTensor* yOut,
529+ const aclTensor* yScaleOut)
530+{
531+ const op::Shape& xShape = x->GetViewShape();
532+ // 含未知维(-1)的动态shape无法在host侧精确推导,仅校验输出rank,交由执行阶段校验
533+ if (HasUnknownDim(xShape) || HasUnknownDim(yOut->GetViewShape()) || HasUnknownDim(yScaleOut->GetViewShape())) {
johan_lee
johan_leejohan_lee4 days ago

【review】 问题分类:正确性 位置:quant/dequant_situ_quant/op_host/op_api/aclnn_dequant_situ_quant.cpp:533,代码:if (HasUnknownDim(xShape) || HasUnknownDim(yOut->GetViewShape()) || HasUnknownDim(yScaleOut->GetViewShape())) { 问题:这里发现未知维就只验输出 rank,跳过了后面与维度无关的 dtype 和量化模式组合校验。例如 INT32 x=[-1,64]、weightScale=[64]、activationScale=[1]、INT8 y=[16,32]、FLOAT yScale=[16]、quantType=static,且不传 quantScale。此前第一段会拦截静态量化缺少 quantScale,本 PR 将该前置约束限于 INT8 后,这组参数会通过 CheckParams;INT32 本身仍只支持 dynamic。BF16/FP16 路径也有同一问题。 可能导致的结果:无论未知维最终取什么值,静态模式都无法执行。调用方会在更晚的内部处理或 tiling 阶段才遇到失败,无法在第一段输入校验得到明确的参数错误。 建议的修改方法:先校验 dtype 与量化模式、以及与尺寸无关的可选输入组合,再根据未知维决定是否跳过依赖具体尺寸的检查。补一条 INT32 未知维且传 static 的负向用例。

likedislike
534+ return CheckOutShapeRank(x, yOut, yScaleOut);
535+ }
536+ if (x->GetDataType() == op::DataType::DT_INT8) {
537+ return CheckInt8ShapeConstraints(x, weightScaleOptional, activationScaleOptional, biasOptional,
538+ quantScaleOptional, quantOffsetOptional, groupIndexOptional, quantType, yOut,
539+ yScaleOut);
540+ }
541+ if (x->GetDataType() == op::DataType::DT_INT32) {
542+ return CheckInt32ShapeConstraints(x, weightScaleOptional, activationScaleOptional, biasOptional,
543+ quantScaleOptional, quantOffsetOptional, groupIndexOptional, quantType, yOut,
544+ yScaleOut);
545+ }
546+ return CheckPreDequantShapeConstraints(x, weightScaleOptional, activationScaleOptional, biasOptional,
547+ quantScaleOptional, quantOffsetOptional, groupIndexOptional, quantType, yOut,
548+ yScaleOut);
549+}
550+ 
273static aclnnStatus CheckParams(const aclTensor* x, const aclTensor* weightScaleOptional,551static aclnnStatus CheckParams(const aclTensor* x, const aclTensor* weightScaleOptional,
274 const aclTensor* activationScaleOptional, const aclTensor* biasOptional,552 const aclTensor* activationScaleOptional, const aclTensor* biasOptional,
275 const aclTensor* quantScaleOptional, const aclTensor* quantOffsetOptional,553 const aclTensor* quantScaleOptional, const aclTensor* quantOffsetOptional,
@@ -302,9 +580,11 @@ static aclnnStatus CheckParams(const aclTensor* x, const aclTensor* weightScaleO
302 // 7. 检查format是否符合要求580 // 7. 检查format是否符合要求
303 CHECK_RET(CheckFormat(x, yOut, yScaleOut), ACLNN_ERR_PARAM_INVALID);581 CHECK_RET(CheckFormat(x, yOut, yScaleOut), ACLNN_ERR_PARAM_INVALID);
304 582 
305- // 8. 检查x和输出的shape维度是否符合约束583+ // 8. 检查shape约束(含x维度、输入之间的shape关系及输出shape)
306 CHECK_RET(CheckXShape(x), ACLNN_ERR_PARAM_INVALID);584 CHECK_RET(CheckXShape(x), ACLNN_ERR_PARAM_INVALID);
307- CHECK_RET(CheckOutShape(x, yOut, yScaleOut), ACLNN_ERR_PARAM_INVALID);585+ CHECK_RET(CheckShapeConstraints(x, weightScaleOptional, activationScaleOptional, biasOptional, quantScaleOptional,
586+ quantOffsetOptional, groupIndexOptional, quantType, yOut, yScaleOut),
587+ ACLNN_ERR_PARAM_INVALID);
308 return ACLNN_SUCCESS;588 return ACLNN_SUCCESS;
309}589}
310} // namespace590} // namespace
@@ -487,7 +487,7 @@ TEST_F(l2_dequant_situ_quant_test, valid_int8_static_without_quant_offset)
487TEST_F(l2_dequant_situ_quant_test, valid_int32_dynamic_params)487TEST_F(l2_dequant_situ_quant_test, valid_int32_dynamic_params)
488{488{
489 auto x_desc = TensorDesc({16, 64}, ACL_INT32, ACL_FORMAT_ND).ValueRange(-2, 2);489 auto x_desc = TensorDesc({16, 64}, ACL_INT32, ACL_FORMAT_ND).ValueRange(-2, 2);
490- auto weight_scale_desc = TensorDesc({64}, ACL_FLOAT, ACL_FORMAT_ND);490+ auto weight_scale_desc = TensorDesc({2, 64}, ACL_FLOAT, ACL_FORMAT_ND);
491 auto activation_scale_desc = TensorDesc({1}, ACL_FLOAT, ACL_FORMAT_ND);491 auto activation_scale_desc = TensorDesc({1}, ACL_FLOAT, ACL_FORMAT_ND);
492 auto group_index_desc = TensorDesc({2}, ACL_INT64, ACL_FORMAT_ND);492 auto group_index_desc = TensorDesc({2}, ACL_INT64, ACL_FORMAT_ND);
493 auto y_desc = TensorDesc({16, 32}, ACL_INT8, ACL_FORMAT_ND);493 auto y_desc = TensorDesc({16, 32}, ACL_INT8, ACL_FORMAT_ND);