已合并
fix: fix from blob bug #33191
fix: fix from blob bug #33191
已合并
luochao60创建于 4月7日
luochao60成员
4月7日

【合入来源】

(如有)请关联需求文档/issue链接

【修改方案】

请描述修改内容的具体实现,涉及哪些组件之间进行交互,可以用1、2、3、...进行罗列
如果是需求或者重构类的PR,需要补充详细设计文档(说明上下游组件关系、时序图、类图、DFX能力等内容)

  1. 修复 torch_npu/csrc/aten/common/from_blob.cppTensorMaker::computeStorageSize() 计算 storage size 时对 storage_offset_ 的处理 bug:原代码将 storage_offset_(按元素数计)直接累加到字节大小上,缺少 * itemsize,导致非 float32 类型或带偏移的 tensor 计算出的 storage size 偏小,from_blob 创建的 tensor storage 不足。修复后两处分支均改为 storage_size += storage_offset_.value() * itemsize;

  2. 修复 torch_npu/csrc/npu/Module.cpp_weak_ref_tensor 实现:原实现通过 t.data_ptr() + t.sizes() + t.strides() 调用 from_blob 构造新 tensor,丢失了原 tensor 的 storage_offset 信息,且当原 tensor 是 view(带 offset 或非平凡 stride)时,新 tensor 的 storage 会被错误地按视图形状重新计算,与原 storage 不一致。修复方式:基于原 tensor 的完整 storage().mutable_data()storage().nbytes() / element_size() 调用 from_blob 构造新 tensor(覆盖完整 storage),随后通过 set_sizes_and_stridesset_storage_offset 还原原 tensor 的视图信息,确保弱引用 tensor 的 storage、sizes、strides、offset 与原 tensor 完全一致。

  3. 测试用例重构与新增:

    • 将原本散落在 test/cpp_extensions/extension.cpp 中的 from_blob 相关测试(check_from_blob/check_from_blob_strides/check_from_blob_delete)迁移到独立的 test/cpp_extensions/test_from_blob.cpp,按 at_npu::native::from_blob 接口能力分门别类组织。
    • test/cpp_extensions/setup.py 注册新的扩展模块 torch_test_cpp_extension.npu_from_blob
    • test/cpp_extensions/test/test_cpp_extensions_aot.py 新增 TestFromBlob 测试类,覆盖 basic / deleter / strides / storage_offset / storage_offset_2d / storage_offset_dtype / storage_offset_contiguous / non_owning / clone 等场景;test_storage_sizes 增加 @SupportedDevices(['Ascend910B', 'Ascend910C']) 限制。
    • test/npu/test_npu_format.py 新增 test_weak_ref_tensor_with_storage_offset 用例,构造带非平凡 stride 与 storage_offset 的 view,验证 _weak_ref_tensor 返回的 tensor 在 size、stride、storage_offset、storage().nbytes() 与数值上都与原 tensor 一致。

【资料变更】

请确认是否涉及资料变更。如涉及,需要在PR中体现,并简要说明修改内容。如不涉及,需填写"不涉及"

不涉及

【接口变更】

请确认是否涉及跨代码仓或者客户面可见的接口变更。如涉及,需要详细说明接口以及对应的变更内容,同时需要在资料中体现。如不涉及,需填写"不涉及"

不涉及

【功能验证】

说明测试场景,测试方法。如果本次测试方式与常规单元测试不同,请详细说明您的测试步骤
新增/变更内容是否已新增/适配UT测试用例看护,并补充测试自验证截图

  1. C++ 扩展用例 test/cpp_extensions/test/test_cpp_extensions_aot.py::TestFromBlob:覆盖 at_npu::native::from_blob 在 basic、自定义 deleter、显式 strides、带 storage_offset、二维带 offset、不同 dtype、contiguous 标志、non-owning 语义、clone 后数据正确性等场景,验证修复后 storage 计算正确。
  2. Python 用例 test/npu/test_npu_format.py::TestNPUFormat::test_weak_ref_tensor_with_storage_offset:构造 view_shape=[2,1,8,64]view_strides=[1536,0,192,1]view_offset=128 的 strided view,验证 _weak_ref_tensor 保持 size/stride/offset/storage 字节数一致且数值相等。
  3. UT 已随 PR 一同提交,本地自验证通过。

【CheckList】

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

likedislike
Pull Request已成功合入, 合并人@ascend-robot
(感谢 luochao60 的贡献)
Lluochao60成员
4月7日 创建了 pull request,commit 5c33efb9
ascend-robot
ascend-robot成员
4月7日 评论:

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 liujunzhu, chujinjin (2/2) liujunzhu (1/1)
test liujunzhu, chujinjin (2/2) liujunzhu (1/1)

💡 Tip:

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

CLA Signature Pass

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

likedislike
ascend-robotascend-robot成员
4月7日 添加了label:ascend-cla/yes
Lluochao60成员
4月9日 update merge request[project id: 7404318, iid: 33191, commit_id: 1922ac5988d335b702ba54cece78ee6ead5bb900] virtual merging success
Lluochao60成员
4月9日 强制推送  1 个提交:1d812510-fix from blob bug
Lluochao60成员
4月9日 update merge request[project id: 7404318, iid: 33191, commit_id: c292aaf69045a195b0b730a20a591e236e7521f8] virtual merging success
AtlasAccountAtlasAccount成员
4月9日 添加了label:ci-pipeline-failed
luochao60成员
4月9日 评论:

compile

likedislike
ascend-robotascend-robot成员
4月9日 删除了label:ci-pipeline-failed
ascend-robotascend-robot成员
4月9日 添加了label:ci-pipeline-running
ascend-robotascend-robot成员
4月9日 删除了label:ci-pipeline-running
ascend-robotascend-robot成员
4月9日 添加了label:ci-pipeline-passed
ascend-robot
ascend-robot成员
4月9日 评论:
流水线 PR-pipeline_pytorch#14815 已完成
阶段 任务名 状态 详情
编译构建 Build_X86 >>>
Build_ARM >>>
Build_LibTorch_x86 >>>
Build_X86_torchair 🛑 >>>
Build_ARM_torchair 🛑 >>>
Build_ARM_inductor 🛑 >>>
恶意代码检查 Antipoison >>>
编码安全与规范检查 CodeCheck >>>
check_error >>>
开源片段检查 SCA >>>
开发者测试 UT_X86_Part_01 >>>
UT_X86_Part_02 >>>
UT_ARM_A3_Part_01 >>>
UT_ARM_A3_Part_02 >>>
UT_DIST_X86_Part_01 🛑 >>>
UT_DIST_X86_Part_02 🛑 >>>
UT_DIST_X86_Part_03 🛑 >>>
UT_DIST_X86_Part_04 🛑 >>>
UT_inductor_Part_01 🛑 >>>
UT_inductor_Part_02 🛑 >>>
UT_inductor_Part_03 🛑 >>>
UT_inductor_Part_04 🛑 >>>
UT_ARM_A2_Part_01 >>>
UT_ARM_A2_Part_02 >>>
流水线 PR-pipeline_pytorch >>>
likedislike
liujunzhu
liujunzhu成员
4月14日 评论:

/approve

likedislike
ascend-robotascend-robot成员
4月14日 添加了label:approved
chujinjin成员
4月14日 评论:

/lgtm

likedislike
ascend-robotascend-robot成员
4月14日 添加了label:lgtm
ascend-robot
ascend-robot成员
4月14日 评论:

Review Guide

This pull-request passes review.
Committers who wrote a comment of /approve are: liujunzhu.
Reviewers who wrote a comment of /lgtm are: liujunzhu, chujinjin.

likedislike
ascend-robotascend-robot成员
4月14日 合入了pull request