Files
TorchNPU/test/cpp_extensions/extension.cpp
T
luochao60 5e97f55469 fix: fix from blob bug
Co-authored-by: luochao60<luochao60@huawei.com>



# message auto-generated for no-merge-commit merge:
!30841 merge pta_fix_from_blob_20260212 into master

fix: fix from blob bug

Created-by: luochao60
Commit-by: luochao60
Merged-by: ascend-robot
Description: <!--
PR描述模板更新日期:20260203
-->

# 【合入来源】
> (如有)请关联需求文档/issue链接

- [ ] 需求
- [x] 问题单
- [ ] issue/工单
- [ ] 重构优化
- [ ] 资料更新

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

1. 修复 `torch_npu/csrc/aten/common/from_blob.cpp` 中 `TensorMaker::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_strides` 与 `set_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]

- [x] 代码注释完备,正确记录错误日志
- [x] 代码实现进行了返回值、空指针等校验
- [x] PR标题正确使用类型标签,如:feat、fix、refactor、docs、test等
- [x] PR持续集成流水线(CI)执行通过,代码检查无异常


See merge request: Ascend/pytorch!30841
2026-04-14 09:58:50 +08:00

89 lines
2.5 KiB
C++

#include <thread>
#include <chrono>
#include <torch/extension.h>
#include "torch_npu/csrc/core/npu/NPUFormat.h"
#include "torch_npu/csrc/framework/OpCommand.h"
#include "torch_npu/csrc/framework/OpHook.h"
// test in .setup with relative path
#include <tmp.h>
using namespace at;
static int g_op_hook_call_count = 0; // test op_hook
Tensor tanh_add(Tensor x, Tensor y)
{
return x.tanh() + y.tanh();
}
Tensor npu_add(const Tensor &self_, const Tensor &other_)
{
TORCH_INTERNAL_ASSERT(self_.device().type() == c10::DeviceType::PrivateUse1);
TORCH_INTERNAL_ASSERT(other_.device().type() == c10::DeviceType::PrivateUse1);
return at::add(self_, other_, 1);
}
bool check_storage_sizes(const Tensor &tensor, const c10::IntArrayRef &sizes)
{
auto tensor_sizes = at_npu::native::get_npu_storage_sizes(tensor);
if (tensor_sizes.size() == sizes.size()) {
return std::equal(tensor_sizes.begin(), tensor_sizes.end(), sizes.begin());
}
return false;
}
Tensor blocking_ops(Tensor x)
{
auto blocking_call = []() -> int {
std::this_thread::sleep_for(std::chrono::seconds(180));
return 0;
};
at_npu::native::OpCommand::RunOpApi("blocking_ops", blocking_call);
return x;
}
void register_op_hook()
{
at_npu::native::RegisterOpHookBeginFn(
[](const std::string &op_name) -> void {
g_op_hook_call_count++;
});
at_npu::native::RegisterOpHookPreFn([](const at::Tensor &at_tensor) -> void {
if (!at_tensor.defined()) {
return;
}
g_op_hook_call_count++;
});
at_npu::native::RegisterOpHookPostFn([](const at::Tensor &at_tensor) -> void {
if (!at_tensor.defined()) {
return;
}
g_op_hook_call_count++;
});
at_npu::native::RegisterOpHookEndFn([]() -> void {
g_op_hook_call_count++;
});
}
int get_op_hook_call_count()
{
return g_op_hook_call_count;
}
void reset_op_hook_call_count()
{
g_op_hook_call_count = 0;
}
PYBIND11_MODULE(TORCH_EXTENSION_NAME, m)
{
m.def("tanh_add", &tanh_add, "tanh(x) + tanh(y)");
m.def("npu_add", &npu_add, "x + y");
m.def("check_storage_sizes", &check_storage_sizes, "check_storage_sizes");
m.def("blocking_ops", &blocking_ops, "blocking_ops");
m.def("register_op_hook", &register_op_hook, "register_op_hook");
m.def("get_op_hook_call_count", &get_op_hook_call_count, "get_op_hook_call_count");
m.def("reset_op_hook_call_count", &reset_op_hook_call_count, "reset_op_hook_call_count");
}