Skip to content

[Cpp API Compatibility] add Sparse related APIs#77581

Open
youge325 wants to merge 3 commits intoPaddlePaddle:developfrom
youge325:cSparse
Open

[Cpp API Compatibility] add Sparse related APIs#77581
youge325 wants to merge 3 commits intoPaddlePaddle:developfrom
youge325:cSparse

Conversation

@youge325
Copy link
Contributor

@youge325 youge325 commented Jan 29, 2026

PR Category

Environment Adaptation

PR Types

Bug fixes

Description

新增 is_sparse is_sparse_csr 接口,拆分自 #77416

是否引起精度变化

@paddle-bot
Copy link

paddle-bot bot commented Jan 29, 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 29, 2026
@codecov-commenter
Copy link

codecov-commenter commented Jan 29, 2026

Codecov Report

❌ Patch coverage is 91.30435% with 8 lines in your changes missing coverage. Please review.
⚠️ Please upload report for BASE (develop@f2de748). Learn more about missing BASE report.

Files with missing lines Patch % Lines
...addle/phi/api/include/compat/ATen/ops/zeros_like.h 80.95% 4 Missing ⚠️
paddle/phi/api/include/compat/ATen/Utils.h 81.81% 2 Missing ⚠️
paddle/phi/api/include/compat/ATen/ops/empty.h 87.50% 1 Missing ⚠️
paddle/phi/api/include/compat/ATen/ops/zeros.h 87.50% 1 Missing ⚠️
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.
📢 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

/re-run all-failed

@YqGe585
Copy link
Member

YqGe585 commented Jan 30, 2026

IXUCA rerun仍然失败的,辛苦rebase到develop后重新触发CI

@youge325
Copy link
Contributor Author

/re-run all-failed

Layout layout() const {
// Check tensor type first for sparse tensors
if (tensor_.is_sparse_csr_tensor()) {
return c10::kSparseCsr;
Copy link
Contributor

@yongqiangma yongqiangma Feb 2, 2026

Choose a reason for hiding this comment

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

SPARSE_COO,

kSparseCsr应该加类型映射关系, 转换成paddle中对应的类型

Copy link
Contributor Author

Choose a reason for hiding this comment

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

改了Paddle底层的稀疏张量创建方法,看看流水线能不能过

原来是根据tensor是SparseCooTensor还是DenseTensor来判断,现在根据tensor的layout属性来判断

Copilot AI review requested due to automatic review settings February 5, 2026 07:32
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 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() and is_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, and empty_like functions 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.
Copy link

Copilot AI Feb 5, 2026

Choose a reason for hiding this comment

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

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.

Suggested change
// Copyright (c) 2025 PaddlePaddle Authors. All Rights Reserved.
// Copyright (c) 2026 PaddlePaddle Authors. All Rights Reserved.

Copilot uses AI. Check for mistakes.
Comment on lines +37 to 40
auto dense = paddle::experimental::empty_like(
self._PD_GetInner(),
compat::_PD_AtenScalarTypeToPhiDataType(dtype),
place._PD_GetInner());
Copy link

Copilot AI Feb 5, 2026

Choose a reason for hiding this comment

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

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.

Copilot uses AI. Check for mistakes.
Comment on lines +56 to 59
auto dense = paddle::experimental::empty_like(
self._PD_GetInner(),
compat::_PD_AtenScalarTypeToPhiDataType(dtype.value_or(self.dtype())),
device.value_or(self.device())._PD_GetInner());
Copy link

Copilot AI Feb 5, 2026

Choose a reason for hiding this comment

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

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.

Copilot uses AI. Check for mistakes.
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.");
Copy link

Copilot AI Feb 5, 2026

Choose a reason for hiding this comment

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

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.

Suggested change
"`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.");

Copilot uses AI. Check for mistakes.
Comment on lines +49 to +51
// 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(
Copy link

Copilot AI Feb 5, 2026

Choose a reason for hiding this comment

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

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.

Suggested change
// 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(

Copilot uses AI. Check for mistakes.
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