已开启
fix: 修复非连续输入触发npu_weight_quant_batchmatmul的EZ1001报错(#184) #261
fix: 修复非连续输入触发npu_weight_quant_batchmatmul的EZ1001报错(#184) #261
已开启
niwang66创建于 16 天前
niwang66
niwang66
16 天前

描述

业务背景NpuWeightQuantizedLinear 是 GPTQ/AWQ/MinMax/OFMR 等算法 Weight-only
压缩的 deploy 算子,其 forward 将输入 reshape 为 2D 后调用
npu_weight_quant_batchmatmul

问题inputs.reshape(-1, K) 仅当输入张量本身连续时结果才连续。当输入为
K 维切片等非连续布局(例如 QKV 联合投影拆分出的 hidden_states)时,reshape 后
is_contiguous() == False,aclnn 抛出:
EZ1001: only support x tensor is contiguous or transpose last two dims
已在本机 910B4 + torch_npu 2.7.1.post4 稳定复现(普通 2D/3D 连续输入不会触发,
因此此前未被发现)。

方案:在 reshape 后显式调用 .contiguous()。对已连续张量该方法返回
原 view(零拷贝),仅在非连续时执行一次拷贝,不影响常规路径性能。

关联 issue:#184

如何测试

  • 真机复现:构造 K 维切片 3D 输入(randn(2,16,512)[:, :, :256])调用
    该模块,修复前报 EZ1001,修复后输出正常;正常连续输入、transpose 输入、
    awq scale_factor 分支均回归通过。
  • 新增单元测试 tests/unit_test/classic/test_weight_npu_quant_module.py
    CPU 环境 mock torch_npu,断言传入算子的张量连续;移除修复后该用例失败,
    可持续守护回归。
  • 回归验证:tests/unit_test/classic/ 共 41 个用例全部通过;
    pre-commit(ruff / codespell / oat-check 等)全部通过。

文档更新

无。

类型标签

likedislike
合并受阻
niwang66niwang66
16 天前 创建了 pull request,commit 05351b5f
niwang66niwang66
16 天前 关联了issue:[Bug-Report|缺陷反馈]: NpuWeightQuantizedLinear 非连续输入触发 npu_weight_quant_batchmatmul EZ1001 报错
atomgit-bot
atomgit-bot
16 天前 评论:

变更摘要

本 PR 修复 NpuWeightQuantizedLinear.forward 在输入为非连续张量时调用 npu_weight_quant_batchmatmul 触发 aclnn EZ1001 报错的问题。inputs.reshape(-1, K) 的结果仅在输入张量本身连续时才连续,当输入来自 K 维切片(如 QKV 联合投影拆分出的 hidden_states)等非连续布局时,传给算子的 2D 张量 is_contiguous() == False,从而抛出 EZ1001: only support x tensor is contiguous or transpose last two dims。修复方案是在 reshape 后显式调用 .contiguous(),对已连续张量该方法零拷贝返回原 view,不影响常规路径性能,并同步新增 CPU 环境 mock torch_npu 的单元测试守护该回归场景。

主要改动

  • 修复 NpuWeightQuantizedLinear.forward 输入连续性: 在 amct_pytorch/classic/deploy_op/weight_npu_quant_module.py 中将 inputs.reshape(-1, inputs.shape[-1]) 改为 inputs.reshape(-1, inputs.shape[-1]).contiguous(),确保传给 npu_weight_quant_batchmatmul 的 2D 张量连续,消除非连续输入触发的 EZ1001 报错。
  • 新增单元测试文件: 添加 tests/unit_test/classic/test_weight_npu_quant_module.py,通过 _install_fake_torch_npu 注册 mock 的 torch_npu 模块并记录传入 npu_weight_quant_batchmatmulx,在 CPU 环境验证算子输入张量的连续性。
  • 测试用例覆盖核心行为: test_forward_input_to_op_is_contiguous 用 K 维切片 3D 输入断言传入算子的张量连续(移除修复后该用例失败);test_forward_output_shape_restored 验证输出形状能正确恢复为原始前导维度形状。
likedislike
atomgit-bot
atomgit-bot
16 天前 评论:

代码审查

✅ 未发现问题

likedislike
CANN-robot
CANN-robot成员
16 天前 评论:

Hi @niwang66, welcome to submitting your first PR to amct!

PR Merge Steps

1. CLA Signing

If the current PR label includes cann-cla/yes, it means you have signed the CLA and can proceed to the next step. If the label includes cann-cla/no, please sign the CLA first. If you have any questions, please refer to the FAQ.

2. CI Check

Please comment /compile to trigger the CI pipeline check. If the CI run is successful, the PR will be tagged with ci-pipeline-passed and you can proceed to the next step. If the CI run fails, the PR will be tagged with ci-pipeline-failed, please check the CI logs to fix the issues in the PR. If you have any questions, please refer to the FAQ.

3. Code Review

After CI passes, please refer to the PR Approval Progress and proactively @ the committers in the table to review the code. After approval, committers will comment /lgtm and /approve. Once the lgtm and approved labels are successfully added, the PR will be merged automatically.

likedislike
CANN-robotCANN-robot成员
16 天前 添加了label:cann-cla/no
CANN-robot
CANN-robot成员
16 天前 评论:

Thanks for your pull-request.
The full list of commands accepted by me can be found at here.
You can get sig-info at here.
For more, you also can visit HICANN.


PR Approval Progress

⚠️ This PR does not yet meet the following requirements:lgtm (requires ≥ 2 person(s) per module)、approve (requires ≥ 1 person(s) per module)

Module Approval Details

module lgtm status approve status
repo-cann/amct ❌ (0/2)(You can also ask: 金瑛琪, li_ting, 姚广修, sophia1213, 梁横溢) ❌ (0/1)(You can also ask: wulinyu, 金瑛琪, li_ting, sophia1213, 袁江)

💡 Tip:

  • Committer can comment /approve or /lgtm
  • Commenting /approve implies both code review (lgtm) and intent to merge (approve)

CLA Signature Guide

@niwang66 , thanks for your pull request.

The following commit(s) have not signed Contributor License Agreement (CLA).

Commit Reason
f7b733b4 fix: 修复非连续输入触发... the email is not signed for CLA! please check if it is correct and the same as your signed email.

If you need to sign CLA, you can click here to sign the CLA.

If you need to check if the email is set up correctly, you can click here to do it in the FAQs.

After signing the CLA or updating the email, you must comment /check-cla to check the CLA status again.

likedislike
CANN-robotCANN-robot成员
16 天前 将wulinyu,zhuliangying,yaoguangxiu,lianghengyi,fujun19,sophia1213,li_ting,jinyingqi,yuanjiang_设为评审人
CANN-robotCANN-robot成员
16 天前 将wulinyu,zhuliangying,yaoguangxiu,lianghengyi,fujun19,sophia1213,li_ting,jinyingqi,yuanjiang_设为审查人
fujun19成员
11 天前 评论:

@niwang66 您好,当前 PR 仍显示 cann-cla/no,暂时无法进入后续合入流程。

请先按机器人提示完成 CLA 签署,并重点确认本次提交使用的 commit email 与 CLA 签署邮箱一致。签署或修正邮箱后,请在本 PR 下评论 /check-cla 触发 CLA 状态复检。CLA 通过后,再评论 /compile 触发 CI 检查。

参考链接:

likedislike