[Cpp API Compatibility] Fix empty() and from_blob() and Verify cuda()#78255
[Cpp API Compatibility] Fix empty() and from_blob() and Verify cuda()#78255youge325 wants to merge 1 commit intoPaddlePaddle:developfrom
Conversation
|
你的PR提交成功,感谢你对开源项目的贡献! |
There was a problem hiding this comment.
Pull request overview
This PR fixes several compatibility layer issues between the PyTorch (ATen) API and the Paddle backend: it refactors from_blob to use a builder-pattern TensorMaker/for_blob class that infers device placement from the data pointer (instead of defaulting to CPU), adds pin_memory support to the empty API, and adds a non-contiguous warning to abs. It also adds corresponding test files and renames the squeeze test to follow the ATen_* naming convention.
Changes:
from_blobfix: Replaces directpaddle::from_blobcalls with aTensorMakerbuilder class; when no device is specified, uses an UNDEFINED place sopaddle::from_blobcan auto-detect device from the pointer.emptyfix: Addspin_memorysupport by allocating on CPU and copying toGPUPinnedPlace/XPUPinnedPlace; removes the old error that rejectedpin_memory=true.- New tests & test rename: Adds
ATen_empty_test.cc,ATen_from_blob_test.cc,ATen_cuda_test.ccand renamescompat_squeeze_testtoATen_squeeze_test.cc.
Reviewed changes
Copilot reviewed 8 out of 9 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
paddle/phi/api/include/compat/ATen/ops/from_blob.h |
Introduces TensorMaker builder class and refactors all from_blob overloads to use it with UNDEFINED place for auto-detection |
paddle/phi/api/include/compat/ATen/ops/empty.h |
Adds pin_memory support for both TensorOptions and 6-arg overloads |
paddle/phi/api/include/compat/ATen/ops/abs.h |
Adds LOG(WARNING) for non-contiguous tensor inputs to abs and abs_ |
paddle/phi/api/include/compat/c10/core/TensorOptions.h |
Adds variadic device(Args&&...) template overload for constructing Device in-place |
test/cpp/compat/ATen_squeeze_test.cc |
New test file for squeeze/unsqueeze operations (replacing compat_squeeze_test) |
test/cpp/compat/ATen_from_blob_test.cc |
New tests for from_blob covering CPU/GPU place detection and deleter behavior |
test/cpp/compat/ATen_empty_test.cc |
New tests for at::empty covering basic shapes, dtype, and pinned memory |
test/cpp/compat/ATen_cuda_test.cc |
New tests verifying at::Tensor::cuda() transfer behavior |
test/cpp/compat/CMakeLists.txt |
Registers the three new test targets and renames the squeeze test |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| "The deleter and context arguments are mutually exclusive."); | ||
|
|
||
| PD_CHECK(!storage_offset_.has_value() || storage_offset_.value() == 0, | ||
| "storage_offset` should be zero."); |
There was a problem hiding this comment.
The error message string on line 81 has a mismatched backtick: "storage_offset should be zero.". The opening backtick before storage_offset is missing. It should read: `` "storage_offset` should be zero." ``
| std::size_t computeStorageSize() const noexcept; | ||
|
|
||
| DataPtr makeDataPtrFromDeleter() noexcept; | ||
|
|
||
| DataPtr makeDataPtrFromContext() noexcept; | ||
|
|
||
| IntArrayRef makeTempSizes() const noexcept; | ||
|
|
||
| void* data_; | ||
| IntArrayRef sizes_; | ||
| OptionalIntArrayRef strides_; | ||
| std::optional<int64_t> storage_offset_; | ||
| std::function<void(void*)> deleter_; | ||
| std::unique_ptr<void, ContextDeleter> ctx_{nullptr, detail::noopDelete}; | ||
| std::optional<Device> device_; | ||
| TensorOptions opts_; | ||
| bool resizeable_{}; |
There was a problem hiding this comment.
The four private methods computeStorageSize(), makeDataPtrFromDeleter(), makeDataPtrFromContext(), and makeTempSizes() are declared in TensorMaker but have no definitions anywhere in the codebase, and are never called inside make_tensor(). Similarly, the resizeable_ member is set by resizeable_storage() but never used in make_tensor(). These stubs inflate the class definition and would cause linker errors if anyone attempts to invoke them. They should either be implemented to reflect the intended behavior or removed to avoid confusion.
PR Category
Execute Infrastructure
PR Types
Bug fixes
Description
对于 from_blob 接口:修复了 tensor 创建的 place,原先采用如果没有传入 device,就使用默认的 device,现在使用 UNDEFINED 类型,创建的位置由 paddle::from_blob 通过传入的指针推断
对于 empty 接口:新增对 pin_memory 参数的支持
对于 abs 接口:在对非连续 tensor 进行 abs 操作时,明确告知用户可能导致歧义,方便用户进行逻辑错误排查
对于 cuda 接口:验证其可用性
是否引起精度变化
否