合并受阻
变更摘要
本 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_batchmatmul的x,在 CPU 环境验证算子输入张量的连续性。 - 测试用例覆盖核心行为:
test_forward_input_to_op_is_contiguous用 K 维切片 3D 输入断言传入算子的张量连续(移除修复后该用例失败);test_forward_output_shape_restored验证输出形状能正确恢复为原始前导维度形状。


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.


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
/approveor/lgtm- Commenting
/approveimplies 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.


描述
业务背景:
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
如何测试
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 等)全部通过。
文档更新
无。
类型标签