[Cpp API Compatibility] add Sparse related APIs#77581
[Cpp API Compatibility] add Sparse related APIs#77581youge325 wants to merge 3 commits intoPaddlePaddle:developfrom
Conversation
|
你的PR提交成功,感谢你对开源项目的贡献! |
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## develop #77581 +/- ##
==========================================
Coverage ? 91.30%
==========================================
Files ? 8
Lines ? 92
Branches ? 0
==========================================
Hits ? 84
Misses ? 8
Partials ? 0 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
|
/re-run all-failed |
|
IXUCA rerun仍然失败的,辛苦rebase到develop后重新触发CI |
|
/re-run all-failed |
| Layout layout() const { | ||
| // Check tensor type first for sparse tensors | ||
| if (tensor_.is_sparse_csr_tensor()) { | ||
| return c10::kSparseCsr; |
There was a problem hiding this comment.
Line 45 in c7917fd
kSparseCsr应该加类型映射关系, 转换成paddle中对应的类型
There was a problem hiding this comment.
改了Paddle底层的稀疏张量创建方法,看看流水线能不能过
原来是根据tensor是SparseCooTensor还是DenseTensor来判断,现在根据tensor的layout属性来判断
There was a problem hiding this comment.
Pull request overview
This pull request adds sparse tensor support to the C++ API compatibility layer, specifically implementing is_sparse and is_sparse_csr APIs. The changes enable creating and checking sparse tensors (COO and CSR formats) through ATen-compatible interfaces.
Changes:
- Added
is_sparse()andis_sparse_csr()methods to TensorBase for checking sparse tensor types - Implemented sparse tensor creation functions (
sparse_coo_tensor,sparse_csr_tensor) - Updated
zeros,zeros_like,empty, andempty_likefunctions to support sparse layouts - Fixed layout field in SparseCooTensor and SparseCsrTensor to use correct DataLayout values
- Added comprehensive test coverage for sparse tensor operations
Reviewed changes
Copilot reviewed 13 out of 13 changed files in this pull request and generated 5 comments.
Show a summary per file
| File | Description |
|---|---|
| test/cpp/compat/c10_layout_test.cc | New test file with comprehensive coverage of sparse tensor creation and layout checking |
| test/cpp/compat/CMakeLists.txt | Registers the new c10_layout_test |
| paddle/phi/core/sparse_csr_tensor.cc | Fixed layout field to use SPARSE_CSR instead of NCHW |
| paddle/phi/core/sparse_coo_tensor.cc | Fixed layout field to use SPARSE_COO instead of NCHW |
| paddle/phi/api/include/compat/ATen/core/TensorBase.h | Added is_sparse() and is_sparse_csr() methods |
| paddle/phi/api/include/compat/ATen/Utils.h | Added helper function to convert dense tensors to sparse based on layout |
| paddle/phi/api/include/compat/ATen/ops/zeros_like.h | Updated to support sparse layouts with proper input conversion |
| paddle/phi/api/include/compat/ATen/ops/zeros.h | Updated to support sparse layouts |
| paddle/phi/api/include/compat/ATen/ops/empty_like.h | Updated to support sparse layouts |
| paddle/phi/api/include/compat/ATen/ops/empty.h | Updated to support sparse layouts |
| paddle/phi/api/include/compat/ATen/ops/sparse_csr_tensor.h | New implementation of sparse CSR tensor constructor |
| paddle/phi/api/include/compat/ATen/ops/sparse_coo_tensor.h | New implementation of sparse COO tensor constructor |
| paddle/phi/api/include/compat/ATen/Functions.h | Added includes for new sparse tensor headers |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| @@ -0,0 +1,69 @@ | |||
| // Copyright (c) 2025 PaddlePaddle Authors. All Rights Reserved. | |||
There was a problem hiding this comment.
The copyright year is inconsistent with other new files in this PR. The test file and sparse_csr_tensor.h both use 2026, but this file uses 2025. Since this is a new file being added in 2026, it should use 2026 for consistency.
| // Copyright (c) 2025 PaddlePaddle Authors. All Rights Reserved. | |
| // Copyright (c) 2026 PaddlePaddle Authors. All Rights Reserved. |
| auto dense = paddle::experimental::empty_like( | ||
| self._PD_GetInner(), | ||
| compat::_PD_AtenScalarTypeToPhiDataType(dtype), | ||
| place._PD_GetInner()); |
There was a problem hiding this comment.
When the input tensor is sparse (COO or CSR), this implementation directly passes it to paddle::experimental::empty_like without converting to dense first. This is inconsistent with the zeros_like implementation (lines 46-49 in zeros_like.h) which converts sparse inputs to dense before processing. If paddle::experimental::empty_like doesn't support sparse tensors, this will fail. Consider adding the same sparse-to-dense conversion logic used in zeros_like.
| auto dense = paddle::experimental::empty_like( | ||
| self._PD_GetInner(), | ||
| compat::_PD_AtenScalarTypeToPhiDataType(dtype.value_or(self.dtype())), | ||
| device.value_or(self.device())._PD_GetInner()); |
There was a problem hiding this comment.
When the input tensor is sparse (COO or CSR), this implementation directly passes it to paddle::experimental::empty_like without converting to dense first. This is inconsistent with the zeros_like implementation (lines 76-79 in zeros_like.h) which converts sparse inputs to dense before processing. If paddle::experimental::empty_like doesn't support sparse tensors, this will fail. Consider adding the same sparse-to-dense conversion logic used in zeros_like.
| PD_CHECK(!layout.has_value() || layout.value() == c10::kSparseCsr, | ||
| "`layout` must be SparseCsr for sparse_csr_tensor."); | ||
| PD_CHECK(!(pin_memory.has_value() && pin_memory.value() != false), | ||
| "`pin_memory` other than False is not supported now."); |
There was a problem hiding this comment.
The dtype and device parameters are accepted but completely ignored in the implementation. The function simply calls the basic sparse_csr_tensor overload which doesn't use these parameters. This could lead to unexpected behavior if a user specifies a dtype or device that differs from the input tensors. Either implement proper dtype/device conversion, or document that these parameters are currently ignored for validation purposes only.
| "`pin_memory` other than False is not supported now."); | |
| "`pin_memory` other than False is not supported now."); | |
| PD_CHECK(!dtype.has_value(), | |
| "`dtype` parameter is currently not supported for sparse_csr_tensor " | |
| "and must be unspecified."); | |
| PD_CHECK(!device.has_value(), | |
| "`device` parameter is currently not supported for sparse_csr_tensor " | |
| "and must be unspecified."); |
| // Note: dtype and device are used for validation/casting if needed | ||
| // Currently, we use the values tensor's dtype and device | ||
| return paddle::experimental::sparse::sparse_coo_tensor( |
There was a problem hiding this comment.
The dtype and device parameters are accepted but completely ignored in the implementation. The function simply calls paddle::experimental::sparse::sparse_coo_tensor which doesn't use these parameters. This could lead to unexpected behavior if a user specifies a dtype or device that differs from the input tensors. Either implement proper dtype/device conversion, or document that these parameters are currently ignored for validation purposes only.
| // Note: dtype and device are used for validation/casting if needed | |
| // Currently, we use the values tensor's dtype and device | |
| return paddle::experimental::sparse::sparse_coo_tensor( | |
| // Note: dtype and device are currently used only for validation. | |
| // The resulting tensor will use the dtype and device of the `values` tensor. | |
| if (dtype.has_value()) { | |
| PD_CHECK(dtype.value() == values.scalar_type(), | |
| "Requested dtype (", | |
| c10::toString(dtype.value()), | |
| ") does not match values tensor dtype (", | |
| c10::toString(values.scalar_type()), | |
| ") in sparse_coo_tensor."); | |
| } | |
| if (device.has_value()) { | |
| PD_CHECK(device.value() == values.device(), | |
| "Requested device (", | |
| device.value().str(), | |
| ") does not match values tensor device (", | |
| values.device().str(), | |
| ") in sparse_coo_tensor."); | |
| } | |
| return paddle::experimental::sparse::sparse::sparse_coo_tensor( |
PR Category
Environment Adaptation
PR Types
Bug fixes
Description
新增
is_sparseis_sparse_csr接口,拆分自 #77416是否引起精度变化
否