Merged
fix: 修复 QuantMatmulActivationQuant golden #13974
panzhijaAdd on 12 days ago
fix: 修复 QuantMatmulActivationQuant golden #13974
Merged
panzhijaAdd on 12 days ago
Total: 3 files changed+74-37
@@ -49,6 +49,32 @@ def mx_dtype(dtype):
49 return fp8_dtype(dtype)49 return fp8_dtype(dtype)
50 50 
51 51 
52+def _mx_operand_dtypes(
53+ x1, x2, output_dtype, input_dtypes=(), x1_dtype=None, x2_dtype=None
54+):
55+ """Resolve logical MX types before promoted inputs lose their native dtype."""
56+ input_dtypes = input_dtypes or ()
57+ declared = (x1_dtype, x2_dtype)
58+ operands = []
59+ for index, value in enumerate((x1, x2)):
60+ if value is None:
61+ raise ValueError("MX inputs require x1 and x2")
62+ dtype = declared[index]
63+ if dtype is None:
64+ dtype = input_dtypes[index] if index < len(input_dtypes) else value.dtype
65+ if str(dtype).removeprefix("torch.") in ("float32", "float64"):
66+ dtype = str(value.dtype).removeprefix("torch.")
67+ # ACLNN may promote both the values and their dtype metadata.
68+ if dtype in ("float32", "float64"):
69+ dtype = output_dtype
atomgit-bot
atomgit-botatomgit-bot12 days ago

🟡 Medium Priority

新增的 _mx_operand_dtypes 在检测到 TTK 把“输入值 + dtype 元数据”同时提升为 FP32 时(第 65-69 行),对 x1 和 x2 都 回退到 output_dtype。

证据链:

  1. 循环体 for index, value in enumerate((x1, x2)) 共用同一段兜底逻辑,dtype = output_dtype 与 index 无关;
  2. output_dtype 只代表输出(= x1)的逻辑类型,代码后面第 71-72 行也明说 “MX output dtype must match x1”。因此对 index=1 用 output_dtype 是一个未经声明的猜测;
  3. 新 CSV 与既有 CSV 里存在 x1/x2 逻辑类型不同的合法用例,例如 e2e 表 53 行 silu_nz_optional_defaults_e5m2_e4m3(x1=e5m2、x2=e4m3)、18/19/21/35/44/51 行等。一旦这些用例在 TTK 提升元数据的路径上运行,b_dtype 会被判成 e5m2 而不是 e4m3。

失败模式:

触发条件:TTK 同时提升值与 dtype 元数据到 FP32,且该用例 x1 与 x2 逻辑类型不同(CSV 中已有多条此类用例)。

建议:只在 index==0(x1,与输出同类型)时回退到 output_dtype;对 index==1 仅在输出为 FP4(此时两个输入必须同为 FP4)时才允许回退,其余情况抛出明确错误(如 "x2 logical dtype is ambiguous after FP32 promotion, please declare x2_dtype"),避免静默按错误类型量化。同时建议在 FP4 CPU 分支保持"输入必须是 packed uint8 载体"的断言,防止 FP32 值被当成 nibble 解包。

likedislike
Incorrect?
70+ operands.append(mx_dtype(dtype))
71+ if operands[0] != output_dtype:
72+ raise ValueError("MX output dtype must match x1")
73+ if (operands[0] == "float4_e2m1") != (operands[1] == "float4_e2m1"):
74+ raise ValueError("MXFP4 requires both inputs to be FP4")
75+ return tuple(operands)
76+ 
77+ 
52_FP32_TINY = np.float32(np.finfo(np.float32).tiny)78_FP32_TINY = np.float32(np.finfo(np.float32).tiny)
53_QUANTIZE_CHUNK_ELEMENTS = 1 << 2079_QUANTIZE_CHUNK_ELEMENTS = 1 << 20
54_MX_GROUP_SIZE = 3280_MX_GROUP_SIZE = 32
@@ -941,12 +967,9 @@ def _npu_activation_golden(
941 import torch.nn.functional as functional967 import torch.nn.functional as functional
942 import torch_npu968 import torch_npu
943 969 
944- a_dtype = mx_dtype(x1.dtype if x1_dtype is None else x1_dtype)970+ a_dtype, b_dtype = _mx_operand_dtypes(
945- b_dtype = mx_dtype(x2.dtype if x2_dtype is None else x2_dtype)971+ x1, x2, dtype, x1_dtype=x1_dtype, x2_dtype=x2_dtype
946- if dtype != a_dtype:972+ )
947- raise ValueError("MX output dtype must match x1")
948- if is_fp4 and b_dtype != dtype:
949- raise ValueError("MXFP4 requires both inputs to be FP4")
950 a = _to_npu_tensor(x1, a_dtype, device)973 a = _to_npu_tensor(x1, a_dtype, device)
951 b = (974 b = (
952 _to_npu_tensor(x2, b_dtype, device)975 _to_npu_tensor(x2, b_dtype, device)
@@ -1164,6 +1187,9 @@ class QuantMatmulActivationQuantTestSpec:
1164 dtype = mx_dtype(y_dtype)1187 dtype = mx_dtype(y_dtype)
1165 if quant_mode != "mx":1188 if quant_mode != "mx":
1166 raise ValueError("This TestSpec covers MX quantization")1189 raise ValueError("This TestSpec covers MX quantization")
1190+ a_dtype, b_dtype = _mx_operand_dtypes(
1191+ x1, x2, dtype, kwargs.get("input_dtypes", ())
1192+ )
1167 npu_device = _npu_golden_device(device)1193 npu_device = _npu_golden_device(device)
1168 weight_nz_shape = None1194 weight_nz_shape = None
1169 input_formats = kwargs.get("input_formats", ())1195 input_formats = kwargs.get("input_formats", ())
@@ -1175,9 +1201,8 @@ class QuantMatmulActivationQuantTestSpec:
1175 raise ValueError("WeightNZ kernel golden requires input_ori_shapes[1]")1201 raise ValueError("WeightNZ kernel golden requires input_ori_shapes[1]")
1176 weight_nz_shape = input_ori_shapes[1]1202 weight_nz_shape = input_ori_shapes[1]
1177 if npu_device is None:1203 if npu_device is None:
1178- x2 = weight_nz_to_nd(x2, weight_nz_shape, dtype)1204+ x2 = weight_nz_to_nd(x2, weight_nz_shape, b_dtype)
1179 if npu_device is not None:1205 if npu_device is not None:
1180- input_dtypes = kwargs.get("input_dtypes", ())
1181 return _npu_activation_golden(1206 return _npu_activation_golden(
1182 x1,1207 x1,
1183 x2,1208 x2,
@@ -1193,8 +1218,8 @@ class QuantMatmulActivationQuantTestSpec:
1193 dst_type_max=dst_type_max,1218 dst_type_max=dst_type_max,
1194 transpose_x1=transpose_x1,1219 transpose_x1=transpose_x1,
1195 transpose_x2=transpose_x2,1220 transpose_x2=transpose_x2,
1196- x1_dtype=input_dtypes[0] if input_dtypes else None,1221+ x1_dtype=a_dtype,
1197- x2_dtype=input_dtypes[1] if input_dtypes else None,1222+ x2_dtype=b_dtype,
1198 weight_nz_shape=weight_nz_shape,1223 weight_nz_shape=weight_nz_shape,
1199 )1224 )
1200 # TTK kernel path normalizes the first output dtype to float32, so the1225 # TTK kernel path normalizes the first output dtype to float32, so the
@@ -1251,6 +1276,9 @@ class QuantMatmulActivationQuantAclnnTestSpec:
1251 if dtype is None:1276 if dtype is None:
1252 dtype = str(yOut.dtype) if yOut is not None else str(x1.dtype)1277 dtype = str(yOut.dtype) if yOut is not None else str(x1.dtype)
1253 dtype = mx_dtype(dtype)1278 dtype = mx_dtype(dtype)
1279+ a_dtype, b_dtype = _mx_operand_dtypes(
1280+ x1, x2, dtype, kwargs.get("tensor_dtypes", ())
1281+ )
1254 npu_device = _npu_golden_device(device)1282 npu_device = _npu_golden_device(device)
1255 weight_nz_shape = None1283 weight_nz_shape = None
1256 if _is_fractal_nz(kwargs.get("tensor_formats", ())):1284 if _is_fractal_nz(kwargs.get("tensor_formats", ())):
@@ -1260,9 +1288,8 @@ class QuantMatmulActivationQuantAclnnTestSpec:
1260 # Recover and decode that storage before _numpy() copies the view.1288 # Recover and decode that storage before _numpy() copies the view.
1261 weight_nz_shape = tuple(x2.shape)1289 weight_nz_shape = tuple(x2.shape)
1262 if npu_device is None:1290 if npu_device is None:
1263- x2 = weight_nz_to_nd(x2, weight_nz_shape, dtype)1291+ x2 = weight_nz_to_nd(x2, weight_nz_shape, b_dtype)
1264 if npu_device is not None:1292 if npu_device is not None:
1265- tensor_dtypes = kwargs.get("tensor_dtypes", ())
1266 return _npu_activation_golden(1293 return _npu_activation_golden(
1267 x1,1294 x1,
1268 x2,1295 x2,
@@ -1278,8 +1305,8 @@ class QuantMatmulActivationQuantAclnnTestSpec:
1278 dst_type_max=dstTypeMax,1305 dst_type_max=dstTypeMax,
1279 transpose_x1=transposeX1,1306 transpose_x1=transposeX1,
1280 transpose_x2=transposeX2,1307 transpose_x2=transposeX2,
1281- x1_dtype=tensor_dtypes[0] if tensor_dtypes else None,1308+ x1_dtype=a_dtype,
1282- x2_dtype=tensor_dtypes[1] if tensor_dtypes else None,1309+ x2_dtype=b_dtype,
1283 weight_nz_shape=weight_nz_shape,1310 weight_nz_shape=weight_nz_shape,
1284 )1311 )
1285 result = reference(1312 result = reference(
@@ -1317,18 +1344,36 @@ class QuantMatmulActivationQuantE2ETestSpec:
1317 **kwargs,1344 **kwargs,
1318 ):1345 ):
1319 # PyTorch views already express the logical matrix: no second transpose.1346 # PyTorch views already express the logical matrix: no second transpose.
1347+ tensor_dtypes = kwargs.get("tensor_dtypes", ())
1320 dtype = output_dtype1348 dtype = output_dtype
1321 if dtype is None:1349 if dtype is None:
1322- dtype = x1_dtype if x1_dtype is not None else str(x1.dtype)1350+ dtype = x1_dtype
1351+ if dtype is None:
1352+ dtype = tensor_dtypes[0] if tensor_dtypes else x1.dtype
1323 dtype = mx_dtype(dtype)1353 dtype = mx_dtype(dtype)
1354+ a_dtype, b_dtype = _mx_operand_dtypes(
1355+ x1, x2, dtype, tensor_dtypes, x1_dtype, x2_dtype
1356+ )
1357+ quant_mode = kwargs.get("quant_mode", "mx")
1358+ if quant_mode != "mx":
1359+ raise ValueError("This TestSpec covers MX quantization")
1360+ group_sizes = kwargs.get("group_sizes")
1361+ if group_sizes is not None and tuple(group_sizes) not in (
1362+ (),
1363+ (0, 0, 0),
1364+ (1, 1, 32),
1365+ ):
1366+ raise ValueError("Invalid MX group_sizes")
1367+ scale_alg = 0 if scale_alg is None else scale_alg
1324 npu_device = _npu_golden_device(device)1368 npu_device = _npu_golden_device(device)
1325 if npu_device is not None:1369 if npu_device is not None:
1326- group_sizes = kwargs.get("group_sizes")1370+ if _is_fractal_nz(kwargs.get("tensor_formats", ())):
1327- if group_sizes is not None and tuple(group_sizes) not in (1371+ import torch_npu
1328- (0, 0, 0),1372+ 
1329- (1, 1, 32),1373+ # Match the NZ binding applied before the actual E2E operator call.
1330- ):1374+ x2 = torch_npu.npu_format_cast(
1331- raise ValueError("Invalid MX group_sizes")1375+ _to_npu_tensor(x2, b_dtype, npu_device), 29
1376+ )
1332 return _npu_activation_golden(1377 return _npu_activation_golden(
1333 x1,1378 x1,
1334 x2,1379 x2,
@@ -1338,23 +1383,21 @@ class QuantMatmulActivationQuantE2ETestSpec:
1338 device=npu_device,1383 device=npu_device,
1339 output_dtype=dtype,1384 output_dtype=dtype,
1340 activation_type=activation_type,1385 activation_type=activation_type,
1341- quant_mode=kwargs.get("quant_mode", "mx"),1386+ quant_mode=quant_mode,
1342 round_mode=round_mode,1387 round_mode=round_mode,
1343 scale_alg=scale_alg,1388 scale_alg=scale_alg,
1344 dst_type_max=dst_type_max,1389 dst_type_max=dst_type_max,
1345- x1_dtype=x1_dtype,1390+ x1_dtype=a_dtype,
1346- x2_dtype=x2_dtype,1391+ x2_dtype=b_dtype,
1347 pack_fp4_output=dtype == "float4_e2m1",1392 pack_fp4_output=dtype == "float4_e2m1",
1348 )1393 )
1349 is_fp4 = dtype == "float4_e2m1"1394 is_fp4 = dtype == "float4_e2m1"
1350 if is_fp4:1395 if is_fp4:
1351- if mx_dtype(x1_dtype) != dtype or mx_dtype(x2_dtype) != dtype:
1352- raise ValueError("MXFP4 E2E inputs require FP4 dtype attributes")
1353 x1_value = _unpack_fp4(x1)1396 x1_value = _unpack_fp4(x1)
1354 x2_value = _unpack_fp4(x2)1397 x2_value = _unpack_fp4(x2)
1355 else:1398 else:
1356- x1_value = _numpy_mx(x1, x1.dtype if x1_dtype is None else x1_dtype)1399+ x1_value = _numpy_mx(x1, a_dtype)
1357- x2_value = _numpy_mx(x2, x2.dtype if x2_dtype is None else x2_dtype)1400+ x2_value = _numpy_mx(x2, b_dtype)
1358 result = reference(1401 result = reference(
1359 x1_value,1402 x1_value,
1360 x2_value,1403 x2_value,
@@ -49,3 +49,6 @@ gelu_tanh_fp4_stride_tt_scale2_max7,cann_ops_nn.ops.quant_matmul_activation_quan
49gelu_tanh_fp4_byte_scales,cann_ops_nn.ops.quant_matmul_activation_quant,"((4, 32), (64, 33), (1, 66, 2), (4, 1, 2), (66,))","((32, 1), (33, 1), (132, 2, 1), (2, 2, 1), (1,))","('uint8', 'uint8', 'uint8', 'uint8', 'float32')","('ND', 'ND', 'ND', 'ND', 'ND')","{'output_dtype': 296, 'x1_dtype': 296, 'x2_dtype': 296, 'x1scale_dtype': 293, 'x2scale_dtype': 293, 'group_sizes': [1, 1, 32], 'activation_type': 'gelu_tanh', 'quant_mode': 'mx', 'round_mode': 'rint', 'scale_alg': 0, 'dst_type_max': 0.0}",(),"((0, 255), (0, 255), (126, 128), (126, 128), (-0.25, 0.25))","((0.0, 0.0), (0.0, 0.0))","(0.0, 0.0)",True49gelu_tanh_fp4_byte_scales,cann_ops_nn.ops.quant_matmul_activation_quant,"((4, 32), (64, 33), (1, 66, 2), (4, 1, 2), (66,))","((32, 1), (33, 1), (132, 2, 1), (2, 2, 1), (1,))","('uint8', 'uint8', 'uint8', 'uint8', 'float32')","('ND', 'ND', 'ND', 'ND', 'ND')","{'output_dtype': 296, 'x1_dtype': 296, 'x2_dtype': 296, 'x1scale_dtype': 293, 'x2scale_dtype': 293, 'group_sizes': [1, 1, 32], 'activation_type': 'gelu_tanh', 'quant_mode': 'mx', 'round_mode': 'rint', 'scale_alg': 0, 'dst_type_max': 0.0}",(),"((0, 255), (0, 255), (126, 128), (126, 128), (-0.25, 0.25))","((0.0, 0.0), (0.0, 0.0))","(0.0, 0.0)",True
50gelu_erf_fp4_scale2_max7_float32_attr,cann_ops_nn.ops.quant_matmul_activation_quant,"((4, 32), (64, 33), (1, 66, 2), (4, 1, 2), (66,))","((32, 1), (33, 1), (132, 2, 1), (2, 2, 1), (1,))","('uint8', 'uint8', 'float8_e8m0', 'float8_e8m0', 'float32')","('ND', 'ND', 'ND', 'ND', 'ND')","{'output_dtype': 296, 'x1_dtype': 296, 'x2_dtype': 296, 'x1scale_dtype': 293, 'x2scale_dtype': 293, 'group_sizes': [1, 1, 32], 'activation_type': 'gelu_erf', 'quant_mode': 'mx', 'round_mode': 'rint', 'scale_alg': 2, 'dst_type_max': 7.00000001}",(),"((0, 0), (0, 0), (1.0, 1.0), (1.0, 1.0), (1.1754943508222875e-38, 1.1754943508222875e-38))","((0.0, 0.0), (0.0, 0.0))","(0.0, 0.0)",True50gelu_erf_fp4_scale2_max7_float32_attr,cann_ops_nn.ops.quant_matmul_activation_quant,"((4, 32), (64, 33), (1, 66, 2), (4, 1, 2), (66,))","((32, 1), (33, 1), (132, 2, 1), (2, 2, 1), (1,))","('uint8', 'uint8', 'float8_e8m0', 'float8_e8m0', 'float32')","('ND', 'ND', 'ND', 'ND', 'ND')","{'output_dtype': 296, 'x1_dtype': 296, 'x2_dtype': 296, 'x1scale_dtype': 293, 'x2scale_dtype': 293, 'group_sizes': [1, 1, 32], 'activation_type': 'gelu_erf', 'quant_mode': 'mx', 'round_mode': 'rint', 'scale_alg': 2, 'dst_type_max': 7.00000001}",(),"((0, 0), (0, 0), (1.0, 1.0), (1.0, 1.0), (1.1754943508222875e-38, 1.1754943508222875e-38))","((0.0, 0.0), (0.0, 0.0))","(0.0, 0.0)",True
51gelu_tanh_fp4_ocp_ignores_dst_type_max,cann_ops_nn.ops.quant_matmul_activation_quant,"((4, 32), (64, 33), (1, 66, 2), (4, 1, 2), (66,))","((32, 1), (33, 1), (132, 2, 1), (2, 2, 1), (1,))","('uint8', 'uint8', 'float8_e8m0', 'float8_e8m0', 'float32')","('ND', 'ND', 'ND', 'ND', 'ND')","{'output_dtype': 296, 'x1_dtype': 296, 'x2_dtype': 296, 'x1scale_dtype': 293, 'x2scale_dtype': 293, 'group_sizes': [1, 1, 32], 'activation_type': 'gelu_tanh', 'quant_mode': 'mx', 'round_mode': 'rint', 'scale_alg': 0, 'dst_type_max': 2.0}",(),"((0, 255), (0, 255), (0.5, 2.0), (0.5, 2.0), (-0.25, 0.25))","((0.0, 0.0), (0.0, 0.0))","(0.0, 0.0)",True51gelu_tanh_fp4_ocp_ignores_dst_type_max,cann_ops_nn.ops.quant_matmul_activation_quant,"((4, 32), (64, 33), (1, 66, 2), (4, 1, 2), (66,))","((32, 1), (33, 1), (132, 2, 1), (2, 2, 1), (1,))","('uint8', 'uint8', 'float8_e8m0', 'float8_e8m0', 'float32')","('ND', 'ND', 'ND', 'ND', 'ND')","{'output_dtype': 296, 'x1_dtype': 296, 'x2_dtype': 296, 'x1scale_dtype': 293, 'x2scale_dtype': 293, 'group_sizes': [1, 1, 32], 'activation_type': 'gelu_tanh', 'quant_mode': 'mx', 'round_mode': 'rint', 'scale_alg': 0, 'dst_type_max': 2.0}",(),"((0, 255), (0, 255), (0.5, 2.0), (0.5, 2.0), (-0.25, 0.25))","((0.0, 0.0), (0.0, 0.0))","(0.0, 0.0)",True
52+silu_nz_transposed_view_e4m3,cann_ops_nn.ops.quant_matmul_activation_quant,"((3, 128), (128, 256), (2, 256, 2), (3, 2, 2), None)","((128, 1), (1, 128), (512, 2, 1), (4, 2, 1), None)","('float8_e4m3fn', 'float8_e4m3fn', 'float8_e8m0', 'float8_e8m0', 'float32')","('ND', 'FRACTAL_NZ', 'ND', 'ND', 'ND')","{'output_dtype': 24, 'group_sizes': [1, 1, 32], 'activation_type': 'silu', 'quant_mode': 'mx', 'round_mode': 'rint', 'scale_alg': 0}",(),"((-0.5, 0.5), (-0.25, 0.75), (0.5, 2.0), (0.5, 2.0), (None, None))","((0.0, 0.0), (0.0, 0.0))","(0.0, 0.0)",True
53+silu_nz_optional_defaults_e5m2_e4m3,cann_ops_nn.ops.quant_matmul_activation_quant,"((3, 64), (64, 128), (1, 128, 2), (3, 1, 2), None)","((64, 1), (128, 1), (256, 2, 1), (2, 2, 1), None)","('float8_e5m2', 'float8_e4m3fn', 'float8_e8m0', 'float8_e8m0', 'float32')","('ND', 'FRACTAL_NZ', 'ND', 'ND', 'ND')","{'output_dtype': None, 'group_sizes': [], 'activation_type': 'silu', 'quant_mode': 'mx', 'round_mode': 'rint', 'scale_alg': None}",(),"((-0.5, 0.5), (-0.25, 0.75), (0.5, 2.0), (0.5, 2.0), (None, None))","((0.0, 0.0), (0.0, 0.0))","(0.0, 0.0)",True
54+gelu_tanh_fp4_nz_e2e,cann_ops_nn.ops.quant_matmul_activation_quant,"((4, 32), (64, 64), (1, 128, 2), (4, 1, 2), None)","((32, 1), (64, 1), (256, 2, 1), (2, 2, 1), None)","('uint8', 'uint8', 'float8_e8m0', 'float8_e8m0', 'float32')","('ND', 'FRACTAL_NZ', 'ND', 'ND', 'ND')","{'output_dtype': 296, 'x1_dtype': 296, 'x2_dtype': 296, 'x1scale_dtype': 293, 'x2scale_dtype': 293, 'group_sizes': [1, 1, 32], 'activation_type': 'gelu_tanh', 'quant_mode': 'mx', 'round_mode': 'rint', 'scale_alg': 0, 'dst_type_max': 0.0}",(),"((0, 255), (0, 255), (0.5, 2.0), (0.5, 2.0), (None, None))","((0.0, 0.0), (0.0, 0.0))","(0.0, 0.0)",True