Skip to content

[Cpp API Compatibility] Fix empty() and from_blob() and Verify cuda()#78255

Open
youge325 wants to merge 1 commit intoPaddlePaddle:developfrom
youge325:cAlign
Open

[Cpp API Compatibility] Fix empty() and from_blob() and Verify cuda()#78255
youge325 wants to merge 1 commit intoPaddlePaddle:developfrom
youge325:cAlign

Conversation

@youge325
Copy link
Contributor

@youge325 youge325 commented Mar 10, 2026

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 接口:验证其可用性

是否引起精度变化

Copilot AI review requested due to automatic review settings March 10, 2026 13:33
@paddle-bot
Copy link

paddle-bot bot commented Mar 10, 2026

你的PR提交成功,感谢你对开源项目的贡献!
请关注后续CI自动化测试结果,详情请参考Paddle-CI手册
Your PR has been submitted. Thanks for your contribution!
Please wait for the result of CI firstly. See Paddle CI Manual for details.

@paddle-bot paddle-bot bot added the contributor External developers label Mar 10, 2026
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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_blob fix: Replaces direct paddle::from_blob calls with a TensorMaker builder class; when no device is specified, uses an UNDEFINED place so paddle::from_blob can auto-detect device from the pointer.
  • empty fix: Adds pin_memory support by allocating on CPU and copying to GPUPinnedPlace/XPUPinnedPlace; removes the old error that rejected pin_memory=true.
  • New tests & test rename: Adds ATen_empty_test.cc, ATen_from_blob_test.cc, ATen_cuda_test.cc and renames compat_squeeze_test to ATen_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.");
Copy link

Copilot AI Mar 10, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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." ``

Copilot uses AI. Check for mistakes.
Comment on lines +138 to +154
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_{};
Copy link

Copilot AI Mar 10, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

contributor External developers

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants