已合并
fix(npu): register QuantizedPrivateUse1 view/ravel/flatten for test_view_ops. #36700
Margaret_wangrui创建于 5月26日
fix(npu): register QuantizedPrivateUse1 view/ravel/flatten for test_view_ops. #36700
已合并
Margaret_wangrui创建于 5月26日
Margaret_wangrui
Margaret_wangrui成员
5月26日

【合入来源】

如有社区issue,请关联issue链接
请勿携带内部流程信息(需求链接、问题单、内部issue等)

https://gitcode.com/Ascend/pytorch/issues/2082

问题简述: 将 PyTorch 上游 test/test_view_ops.py 切换到 torch_npu 执行时,test_ravel_nputest_flatten_npu 在处理 torch._empty_affine_quantized(..., quint8, device=npu) 量化张量时报错:

NotImplementedError: Could not run 'aten::view' with arguments from the 'Quantizednpu' backend

浮点 NPU 张量(PrivateUse1)可正常 view;量化 NPU 张量(QuantizedPrivateUse1)缺少对应 kernel 注册。


【修改方案】

本 PR 为 QuantizedPrivateUse1 补齐 shape / copy 相关 dispatch,并与 op-plugin stride_copy 修复配合,使量化 NPU 张量上的 view / ravel / flatten 语义与上游用例一致。

组件交互关系:

test_ravel_npu / test_flatten_npu(量化 quint8 on NPU)
        │
        ▼
TensorShape.cpp(torch_npu)
  QuantizedPrivateUse1 注册:view / ravel / contiguous / clone / empty* / _copy_from
        │
        ├─► MetaData 不匹配时 clone 物化(NPUStorageDesc 与 view shape 对齐)
        │
        ▼
TensorFactories.cpp(torch_npu)
  NPUNativeFunctions::clone(quant):int_repr + npu_stride_copy_out
        │
        ▼
AsStridedKernelNpu.cpp(op-plugin,子模块 071a046)
  小 nbytes int_repr stride_copy 稳定走 AsStrided

具体实现:

  1. torch_npu/csrc/aten/common/TensorShape.cpp(+312 行)

    • 通过 TORCH_LIBRARY_IMPL(aten, QuantizedPrivateUse1, m) 注册:viewas_stridedravelcontiguouscloneempty_likeempty.memory_formatempty_strided_copy_from
    • 实现 npu_quantized_view_symint:基于 infer_size + computeStride,经 alias_with_sizes_and_strides_npu 构造 QTensorImpl view;stride 不兼容且可展平为 rank-1 时走 clone + view
    • 实现 npu_quantized_ravelaten::contiguousview(-1),避免 composite reshape 在 NPU 量化张量上短路。
    • 实现 npu_quantized_contiguous:除 is_contiguous() 外检查行主 stride,必要时 clone 物化。
    • npu_quantized_view_materialize_if_storage_desc_mismatch:当 MetaDataAreMatch(q)==false 时对 view 结果 clone(Contiguous)
    • NPUNativeFunctions::as_strided 增加量化分支,构造 QTensorImpl view。
  2. torch_npu/csrc/aten/common/TensorFactories.cpp(+202 行)

    • 增强 NPUNativeFunctions::clone 量化路径:empty_like + SetDesc + int_repr stride_copy。
    • MetaDataAreMatch 异常(如 transpose、view 后 storage desc 不一致)时,CPU int_repr staging + H2D 物化。
    • stride_copy 后 int_repr slab 传播与 canonical 缓冲对齐(npu_quantized_clone_write_int_repr_payload)。
  3. test/test_view_ops.py

    • 量化 NPU 张量不再强制 flat._base is src(允许 NPUStorageDesc 不匹配时的物化拷贝路径)。
    • 修正 is_quantized / is_npu 为属性访问(非可调用方法)。

变更规模:

 test/test_view_ops.py                          |   5 +-
 torch_npu/csrc/aten/common/TensorFactories.cpp | 202 +++
 torch_npu/csrc/aten/common/TensorShape.cpp     | 312 +++

【资料变更】

不涉及。


【接口变更】

不涉及跨代码仓或客户面可见的公开 API 变更。

内部说明(ATen dispatch 层,非 Python 公开接口):

算子 Dispatch Key 变更类型
view / as_strided / ravel / contiguous / clone QuantizedPrivateUse1 新增 kernel 注册
empty_like / empty.memory_format / empty_strided / _copy_from QuantizedPrivateUse1 新增 kernel 注册

用户可见行为变化:NPU 上 quint8 等量化张量可正常执行 viewravelflatten(此前为 NotImplementedError)。


【功能验证】

环境要求: 已编译安装 torch_npu;NPU 设备可用。

测试步骤:

python test/test_view_ops.py -v -k test_ravel_npu
python test/test_view_ops.py -v -k test_flatten_npu

覆盖场景:

场景 验证点
连续量化张量 5×5×5×5 ravel() / flatten(0,-1)view(-1) 一致
空 shape 0×2×33×0×2 numel=0 时 shape 与 view 链正确
转置 + ravel(nc=True 非连续路径 contiguous().view(-1)ravel() 一致
多组 flatten 维度 (0,2)(1,2)(-2,-1) 等与 view(-1) 展开顺序一致
浮点 NPU 回归 同一用例文件中浮点张量路径不受影响

UT 看护:

自验证: 请在合入前补充 NPU 环境执行上述命令的通过截图或 CI 流水线链接。

image.png


image.png

【CheckList】

PR提交人对以下CheckList自检项进行全量自检,自检通过或不涉及,均修改 [ ] 为 [x]

likedislike
Pull Request已成功合入, 合并人@ascend-robot
(感谢 Margaret_wangrui 的贡献)
Margaret_wangruiMargaret_wangrui成员
5月26日 创建了 pull request,commit d03ddaa2
Margaret_wangruiMargaret_wangrui成员
5月26日 关联了issue:torch_npu 未实现量化tensor的aten::view算子
ascend-robot
ascend-robot成员
5月26日 评论:

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


PR Approval Progress

Congratulations! All modules have met the lgtm and approve requirements.

Module Approval Details

module lgtm status approve status
repo-Ascend/pytorch wangmin0104, liangsongwei (2/2) wangmin0104 (1/1)
test wangmin0104, liangsongwei (2/2) wangmin0104 (1/1)

💡 Tip:

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

CLA Signature Pass

Margaret_wangrui, thanks for your pull request. All authors of the commits have signed the CLA. 👍

likedislike
ascend-robotascend-robot成员
5月26日 添加了label:ascend-cla/yes
此处折叠了90条消息 查看更多
ascend-robotascend-robot成员
5月30日 添加了label:approved
梁松伟
梁松伟成员
5月30日 评论:

/lgtm

likedislike
ascend-robotascend-robot成员
5月30日 添加了label:lgtm
ascend-robotascend-robot成员
5月30日 合入了pull request
ascend-robot
ascend-robot成员
5月30日 评论:
流水线 pytorch_gitcode_PR_multiVersion#9730 [ commitID:969df520 ] 已完成
likedislike