[Cpp API Compatibility] Align resize api#78554
[Cpp API Compatibility] Align resize api#78554SigureMo merged 3 commits intoPaddlePaddle:developfrom
Conversation
|
你的PR提交成功,感谢你对开源项目的贡献! |
There was a problem hiding this comment.
Pull request overview
Updates the ATen compatibility layer’s Tensor::resize_ behavior to better match PyTorch semantics when resizing changes the total number of elements, and adds regression tests to cover shrink/grow scenarios.
Changes:
- Implement
Tensor::resize_to usereshapewhennumelis unchanged, otherwise fall back to a storage-changing path. - Add C++ compat tests for shrinking/growing resize cases to validate shape and prefix data preservation.
- Update test commentary to reflect the new compat behavior.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| test/cpp/compat/ATen_resize_test.cc | Adds tests for resize_ with different numel (shrink/grow) and updates test notes. |
| paddle/phi/api/include/compat/ATen/ops/resize.h | Adjusts compat Tensor::resize_ implementation to handle both same-numel reshapes and storage-changing resizes. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| if (memory_format.has_value()) { | ||
| TORCH_CHECK(*memory_format == at::MemoryFormat::Contiguous, | ||
| "resize_ only supports contiguous memory format, but got ", | ||
| static_cast<int>(*memory_format)); |
There was a problem hiding this comment.
The memory_format rejection message reports static_cast<int>(*memory_format), which is hard to interpret and inconsistent with other compat ops that report unsupported MemoryFormat values more directly. Prefer emitting a clearer value (e.g., the enum name) and/or reusing the same phrasing used in other ATen compat ops (like empty/empty_like) so downstream users get consistent diagnostics.
| static_cast<int>(*memory_format)); | |
| *memory_format); |
| TEST(TensorResizeTest, ResizeShrinkDifferentNumel) { | ||
| at::Tensor t = at::arange(24, at::kFloat).reshape({2, 3, 4}); | ||
|
|
||
| t.resize_({4, 5}); | ||
|
|
There was a problem hiding this comment.
New behavior was added for the memory_format argument (only contiguous is accepted), but the test suite here doesn't cover that path. Adding a small gtest that passes a non-contiguous memory format and asserts the expected failure would lock in the intended compatibility behavior.
| std::vector<int64_t> dims(size.begin(), size.end()); | ||
| int64_t new_numel = 1; | ||
| for (auto dim : dims) { | ||
| new_numel *= dim; | ||
| } |
There was a problem hiding this comment.
new_numel is computed by multiplying dims without any checks for negative dimensions or int64 overflow. If a caller passes an invalid/very large size, new_numel can wrap and the code may take the reshape path or pass sizes into set_ in a surprising way. Consider validating dim >= 0 and using overflow-safe multiplication (or an existing checked helper) before comparing against tensor_.numel().
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## develop #78554 +/- ##
==========================================
Coverage ? 95.55%
==========================================
Files ? 1
Lines ? 45
Branches ? 0
==========================================
Hits ? 43
Misses ? 2
Partials ? 0 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
|
/re-run all-failed |
1 similar comment
|
/re-run all-failed |
PR Category
Execute Infrastructure
PR Types
Bug fixes
Description
拆分自 #78484
对齐
resize_API,用于 DeepGEMM 等场景中的内存对齐。变更详情
变更内容 (
ATen/ops/resize.h)支持多种调用方式:
关键修复:
resize_对IntArrayRef参数的处理memory_format参数回归测试补充 (
test/cpp/compat/ATen_resize_test.cc)ResizeBasic: 基础 resize 操作ResizeWithMemoryFormat: 带内存格式的 resizeResizeException: 异常路径测试相关文档
是否引起精度变化
否