-
Notifications
You must be signed in to change notification settings - Fork 5.9k
[Cpp API Compatibility] add layout API #77185
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,6 +16,7 @@ | |
|
|
||
| #include <ATen/core/TensorAccessor.h> | ||
| #include <c10/core/Device.h> | ||
| #include <c10/core/Layout.h> | ||
| #include <c10/core/MemoryFormat.h> | ||
| #include <c10/core/Scalar.h> | ||
| #include <c10/core/ScalarType.h> | ||
|
|
@@ -207,6 +208,25 @@ class PADDLE_API TensorBase { | |
|
|
||
| bool defined() const { return tensor_.defined(); } | ||
|
|
||
| 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; | ||
| } | ||
|
Comment on lines
+212
to
+227
|
||
| } | ||
|
Comment on lines
+211
to
+228
|
||
|
|
||
| // Return a `TensorAccessor` for CPU `Tensor`s. You have to specify scalar | ||
| // type and | ||
| // dimension. | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,6 +17,7 @@ | |
| #include <ATen/cuda/EmptyTensor.h> | ||
| #include <ATen/native/cuda/Resize.h> | ||
| #include <ATen/ops/tensor.h> | ||
| #include <c10/core/Layout.h> | ||
| #include <c10/core/ScalarType.h> | ||
| #include <c10/core/SymInt.h> | ||
| #include <c10/core/TensorOptions.h> | ||
|
|
@@ -340,3 +341,16 @@ TEST(TestTensorOperators, SubScriptOperator) { | |
| ASSERT_EQ(tensor_2.data_ptr<float>()[i], static_cast<float>(i + offset)); | ||
| } | ||
| } | ||
|
|
||
| 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"); | ||
| } | ||
|
Comment on lines
+345
to
+356
|
||
There was a problem hiding this comment.
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::UNDEFINEDandcommon::DataLayout::ANY. According to thepaddle/common/layout.hfile, these are valid DataLayout enum values (with ANY being an alias for UNDEFINED). While they fall through to the default case and returnc10::kStrided, it would be clearer to explicitly handle these cases or document the intended behavior for undefined/uninitialized layouts.