Closed
[Bug-Report|缺陷反馈]: QuantBatchMatmulInplaceAdd HiFP8 T-T scale 视图触发 tiling 失败 #8119
panzhijaAdd on  17 days agoClosed at  16 days ago
panzhija
panzhijaCollaborators
17 days ago Create

Describe the current behavior / 问题描述 (Mandatory / 必填)

QuantBatchMatmulInplaceAdd 的 HiFP8 T-T(两路 per-tensor)量化场景中,x1/x2 scale 的 view shape 均为 [1],但其中一路或两路引用较大的底层 storage(例如 [2])时,torch_npu.npu_add_quant_matmul_ 在 tiling 阶段报量化模式与 shape 不匹配。相同输入和 scale 视图调用 torch_npu.npu_quant_matmul 可以正常执行。

该问题也影响带非零 storage offset 的单元素切片。两路 scale 都 clone() 成独立单元素 storage 后可以执行;对已经连续的单元素视图调用 PyTorch contiguous() 通常不产生副本,因此不能规避问题。

Environment Information / 环境信息 (Mandatory / 必填)

  • 昇腾硬件型号:Ascend950DT_9592。
  • CANN:9.3.0。
  • Python:3.12.9。
  • PyTorch:2.7.1+cpu;torch_npu:2.7.1.post11。
  • 软件路径:npu_add_quant_matmul_ → aclnnQuantBatchMatmulInplaceAdd,HiFP8 T-T,FP32 输出。

Steps to reproduce the issue / 重现步骤 (Mandatory / 必填)

  1. 构造 HiF8 输入,逻辑矩阵尺寸 M=72, N=128, K=256,矩阵转置组合为 true/false。T-T 在本文中表示两路 per-tensor 量化,与矩阵转置组合无关。
  2. 从两个元素的 FP32 storage 切出 shape 为 [1] 的 x1/x2 scale,分别使用 offset 0 和 1。
  3. 调用 npu_add_quant_matmul_。使用同一数据对照 npu_quant_matmul,或者将两路 scale 都替换为 .clone() 后重试。

下面的示例使用 en_dtypes 构造 HiF8 的 uint8 存储载体:

import numpy as np
import en_dtypes
import torch
import torch_npu

torch_npu.npu.set_device(0)
torch_npu.npu.set_compile_mode(jit_compile=False)
m, n, k = 72, 128, 256
a = ((np.arange(k * m).reshape(k, m) % 7) - 3).astype(np.float32)
b = ((np.arange(k * n).reshape(k, n) % 5) - 2).astype(np.float32)
qa, qb = a.astype(en_dtypes.hifloat8), b.astype(en_dtypes.hifloat8)
x1 = torch.from_numpy(qa.view(np.uint8).copy()).npu().transpose(0, 1)
x2 = torch.from_numpy(qb.view(np.uint8).copy()).npu()
scales = torch.tensor([0.5, 1.5], dtype=torch.float32, device="npu")
s1, s2 = scales[:1], scales[1:2]
y = torch.full((m, n), 1.25, dtype=torch.float32, device="npu")

torch_npu.npu_add_quant_matmul_(
    y, x1, x2, s2, x1_scale=s1,
    x1_dtype=torch_npu.hifloat8, x2_dtype=torch_npu.hifloat8,
    group_sizes=[0, 0, 0],
)
torch.npu.synchronize()
expected = (qa.astype(np.float32).T @ qb.astype(np.float32)) * 0.75 + 1.25
np.testing.assert_array_equal(y.cpu().numpy(), expected)

Describe the expected behavior / 预期结果 (Mandatory / 必填)

合法的单元素 scale 视图应按 view 和 offset 指向的数值参与计算,不应因底层 storage 大于一个元素而被误判为不支持的量化模式。其行为应与 npu_quant_matmul 保持一致。

典型 x1 scale 报错(仅 x2 使用较大 storage 时会在对应的 x2 检查中失败):

Unexpected input, for the dtype of x1: DT_HIFLOAT8, the shape of x1 and x1Scale(pertokenScale) not match any of quantification: [pertensor, pertoken, perblock].

修复补丁的真机前后对照:

回归范围 修复前 修复后
既有 HiF8、MX ACLNN ST 2/2 通过 2/2 通过
新增 4 个 scale storage/offset ACLNN ST 4 个 tiling 失败 4/4 精度通过
PyTorch 10 种 scale 布局的 inplace-add 2/10 通过 10/10 通过,max_abs_error=0

ACLNN ST 前后均使用 ops-test-kit --compare=close --seed=42 --pc=1 --run=1,未修改 golden 或精度阈值。对照使用同一套已安装 tiling/kernel,只切换 API 动态库。另有 API UT 41/41 通过;UT 不执行设备 tiling,缺陷修复以真机结果为依据。

Special notes for this issue/备注 (Optional / 选填)

根因是 inplace-add 对 HiF8 scale 复用了 MX 专用预处理:MxScaleContiguousProcess 对 rank < 3 直接返回,导致较大的 storage/origin shape 被传入后续量化模式检查。QBMMV3 的一维 scale 会经过 l0op::Contiguous 处理。

修复在 HiFloat8 输入分支对两路 scale 调用 l0op::Contiguous,使有效视图及 offset 得到正确处理。新增回归用例合入既有 UT/ST CSV。

修复提交:5aa543f332。

关联 PR:!13646。

likedislike
panzhijapanzhijaCollaborators
17 days ago Added label: bug-report
panzhijapanzhijaCollaborators
17 days ago Associated pull request
panzhijapanzhijaCollaborators
17 days ago Modify description of issue
panzhijapanzhijaCollaborators
17 days ago Assign smdbha as responsible person
CANN-robotCANN-robotCollaborators
16 days ago Close issue
CANN-robotCANN-robotCollaborators
16 days ago Added label: resolved