Skip to content

[Cpp API Compatibility] Align resize api#78554

Merged
SigureMo merged 3 commits intoPaddlePaddle:developfrom
youge325:align-resize-api
Apr 2, 2026
Merged

[Cpp API Compatibility] Align resize api#78554
SigureMo merged 3 commits intoPaddlePaddle:developfrom
youge325:align-resize-api

Conversation

@youge325
Copy link
Copy Markdown
Contributor

@youge325 youge325 commented Apr 1, 2026

PR Category

Execute Infrastructure

PR Types

Bug fixes

Description

拆分自 #78484

对齐 resize_ API,用于 DeepGEMM 等场景中的内存对齐。

变更详情

变更内容 (ATen/ops/resize.h)

支持多种调用方式:

// 基础 resize
tensor.resize_(IntArrayRef new_size);

// 带内存格式的 resize
tensor.resize_(IntArrayRef new_size, std::optional<MemoryFormat> memory_format);

关键修复:

  • 修复 resize_IntArrayRef 参数的处理
  • 正确处理 memory_format 参数
  • 对齐异常抛出语义

回归测试补充 (test/cpp/compat/ATen_resize_test.cc)

  • ResizeBasic: 基础 resize 操作
  • ResizeWithMemoryFormat: 带内存格式的 resize
  • ResizeException: 异常路径测试

相关文档

是否引起精度变化

Copilot AI review requested due to automatic review settings April 1, 2026 11:18
@paddle-bot
Copy link
Copy Markdown

paddle-bot bot commented Apr 1, 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 Apr 1, 2026
Copy link
Copy Markdown
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

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 use reshape when numel is 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));
Copy link

Copilot AI Apr 1, 2026

Choose a reason for hiding this comment

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

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.

Suggested change
static_cast<int>(*memory_format));
*memory_format);

Copilot uses AI. Check for mistakes.
Comment on lines +112 to +116
TEST(TensorResizeTest, ResizeShrinkDifferentNumel) {
at::Tensor t = at::arange(24, at::kFloat).reshape({2, 3, 4});

t.resize_({4, 5});

Copy link

Copilot AI Apr 1, 2026

Choose a reason for hiding this comment

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

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.

Copilot uses AI. Check for mistakes.
Comment on lines +37 to +41
std::vector<int64_t> dims(size.begin(), size.end());
int64_t new_numel = 1;
for (auto dim : dims) {
new_numel *= dim;
}
Copy link

Copilot AI Apr 1, 2026

Choose a reason for hiding this comment

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

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().

Copilot uses AI. Check for mistakes.
@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented Apr 1, 2026

Codecov Report

❌ Patch coverage is 95.55556% with 2 lines in your changes missing coverage. Please review.
⚠️ Please upload report for BASE (develop@638c7c3). Learn more about missing BASE report.

Files with missing lines Patch % Lines
paddle/phi/api/include/compat/ATen/ops/resize.h 95.55% 2 Missing ⚠️
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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@youge325
Copy link
Copy Markdown
Contributor Author

youge325 commented Apr 1, 2026

/re-run all-failed

1 similar comment
@youge325
Copy link
Copy Markdown
Contributor Author

youge325 commented Apr 2, 2026

/re-run all-failed

@SigureMo SigureMo closed this Apr 2, 2026
@SigureMo SigureMo reopened this Apr 2, 2026
Copy link
Copy Markdown
Member

@SigureMo SigureMo left a comment

Choose a reason for hiding this comment

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

LGTMeow 🐾

@SigureMo SigureMo merged commit 2893705 into PaddlePaddle:develop Apr 2, 2026
128 of 132 checks passed
@youge325 youge325 deleted the align-resize-api branch April 2, 2026 13:47
liuhao2638 pushed a commit to liuhao2638/Paddle that referenced this pull request Apr 7, 2026
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.

4 participants