Skip to content

Conversation

@youge325
Copy link
Contributor

@youge325 youge325 commented Jan 4, 2026

PR Category

Environment Adaptation

PR Types

New features

Description

新增 layout 兼容接口

Copilot AI review requested due to automatic review settings January 4, 2026 07:45
@paddle-bot
Copy link

paddle-bot bot commented Jan 4, 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 Jan 4, 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 adds a layout() API to the TensorBase class for C++ API compatibility with PyTorch/ATen. The implementation maps Paddle's DataLayout enum values to corresponding c10::Layout values to maintain API consistency.

Key Changes:

  • Added layout() method to TensorBase class that converts Paddle's DataLayout to c10::Layout
  • Added necessary include for c10/core/Layout.h in both implementation and test files
  • Added unit test to verify the layout API works correctly for strided tensors

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.

File Description
paddle/phi/api/include/compat/ATen/core/TensorBase.h Implements the layout() method with a switch statement mapping various DataLayout types (STRIDED, NCHW, NHWC, etc.) to corresponding c10::Layout values (kStrided, kSparse, kSparseCsr, kMkldnn)
test/cpp/compat/compat_basic_test.cc Adds test case LayoutAPI that verifies default tensors return kStrided layout and tests the layout output stream operator

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +212 to +227
switch (tensor_.layout()) {
case common::DataLayout::STRIDED:
case common::DataLayout::NCHW:
case common::DataLayout::NHWC:
case common::DataLayout::NCDHW:
case common::DataLayout::NDHWC:
return c10::kStrided;
case common::DataLayout::SPARSE_COO:
return c10::kSparse;
case common::DataLayout::SPARSE_CSR:
return c10::kSparseCsr;
case common::DataLayout::ONEDNN:
return c10::kMkldnn;
default:
return c10::kStrided;
}
Copy link

Copilot AI Jan 4, 2026

Choose a reason for hiding this comment

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

The switch statement is missing a case for common::DataLayout::PSTRING_UNION. According to the paddle/common/layout.h file, PSTRING_UNION is a valid DataLayout enum value. Without an explicit case for it, it will fall through to the default case and return c10::kStrided, which may not be the correct mapping. Consider adding an explicit case for PSTRING_UNION or documenting why it should default to kStrided.

Copilot uses AI. Check for mistakes.
case common::DataLayout::SPARSE_CSR:
return c10::kSparseCsr;
case common::DataLayout::ONEDNN:
return c10::kMkldnn;
Copy link

Copilot AI Jan 4, 2026

Choose a reason for hiding this comment

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

The switch statement is missing cases for common::DataLayout::UNDEFINED and common::DataLayout::ANY. According to the paddle/common/layout.h file, these are valid DataLayout enum values (with ANY being an alias for UNDEFINED). While they fall through to the default case and return c10::kStrided, it would be clearer to explicitly handle these cases or document the intended behavior for undefined/uninitialized layouts.

Suggested change
return c10::kMkldnn;
return c10::kMkldnn;
case common::DataLayout::UNDEFINED:
case common::DataLayout::ANY:
// Treat undefined/any Paddle layouts as strided in ATen.
return c10::kStrided;

Copilot uses AI. Check for mistakes.
Comment on lines +345 to +356
TEST(TensorBaseTest, LayoutAPI) {
// Test layout() API for strided tensors
at::TensorBase tensor = at::ones({2, 3}, at::kFloat);

// Default tensor should have Strided layout
ASSERT_EQ(tensor.layout(), c10::kStrided);

// Test layout output stream operator
std::ostringstream oss;
oss << tensor.layout();
ASSERT_EQ(oss.str(), "Strided");
}
Copy link

Copilot AI Jan 4, 2026

Choose a reason for hiding this comment

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

The test only covers the strided layout case. Consider adding test cases for other layout types that are supported by the layout() method, such as SPARSE_COO (maps to c10::kSparse), SPARSE_CSR (maps to c10::kSparseCsr), and ONEDNN (maps to c10::kMkldnn). This would ensure the switch statement logic in the layout() method is properly validated for all supported layout types.

Copilot uses AI. Check for mistakes.
Comment on lines +211 to +228
Layout layout() const {
switch (tensor_.layout()) {
case common::DataLayout::STRIDED:
case common::DataLayout::NCHW:
case common::DataLayout::NHWC:
case common::DataLayout::NCDHW:
case common::DataLayout::NDHWC:
return c10::kStrided;
case common::DataLayout::SPARSE_COO:
return c10::kSparse;
case common::DataLayout::SPARSE_CSR:
return c10::kSparseCsr;
case common::DataLayout::ONEDNN:
return c10::kMkldnn;
default:
return c10::kStrided;
}
}
Copy link

Copilot AI Jan 4, 2026

Choose a reason for hiding this comment

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

The layout() method lacks documentation. Consider adding a comment explaining what the method returns and how Paddle's DataLayout values map to c10::Layout values. This would help developers understand the mapping logic, especially for edge cases like PSTRING_UNION that fall through to the default case.

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

codecov-commenter commented Jan 4, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
⚠️ Please upload report for BASE (develop@a1d519c). Learn more about missing BASE report.

Additional details and impacted files
@@             Coverage Diff             @@
##             develop    #77185   +/-   ##
===========================================
  Coverage           ?   100.00%           
===========================================
  Files              ?         1           
  Lines              ?         2           
  Branches           ?         0           
===========================================
  Hits               ?         2           
  Misses             ?         0           
  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
Contributor Author

youge325 commented Jan 4, 2026

/re-run all-failed

1 similar comment
@youge325
Copy link
Contributor Author

youge325 commented Jan 5, 2026

/re-run all-failed

@SigureMo SigureMo self-requested a review January 5, 2026 07:15
@BingooYang
Copy link
Contributor

/re-run all-failed

Copy link
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 🐾

@BingooYang
Copy link
Contributor

/re-run all-failed

@SigureMo SigureMo merged commit 27eef5c into PaddlePaddle:develop Jan 12, 2026
93 of 98 checks passed
youge325 added a commit to youge325/Paddle that referenced this pull request Jan 12, 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.

5 participants