/* Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ #pragma once #include "paddle/phi/common/int_array.h" #include "paddle/phi/core/dense_tensor.h" #include "paddle/phi/core/sparse_coo_tensor.h" #include "paddle/phi/core/sparse_csr_tensor.h" #include "paddle/phi/kernels/empty_kernel.h" namespace phi { namespace sparse { template void DenseToCooKernel(const Context& dev_ctx, const DenseTensor& x, const int64_t sparse_dim, SparseCooTensor* out); template SparseCooTensor DenseToCoo(const Context& dev_ctx, const DenseTensor& x, const int64_t sparse_dim) { DenseTensor indices; DenseTensor values; SparseCooTensor coo(indices, values, x.dims()); DenseToCooKernel(dev_ctx, x, sparse_dim, &coo); return coo; } template void CsrToCooKernel(const Context& dev_ctx, const SparseCsrTensor& x, SparseCooTensor* out); template SparseCooTensor CsrToCoo(const Context& dev_ctx, const SparseCsrTensor& x) { DenseTensor indices; DenseTensor values; SparseCooTensor coo(indices, values, x.dims()); CsrToCooKernel(dev_ctx, x, &coo); return coo; } template void CooToCsrKernel(const Context& dev_ctx, const SparseCooTensor& x, SparseCsrTensor* out); template SparseCsrTensor CooToCsr(const Context& dev_ctx, const SparseCooTensor& x) { DenseTensor crows; DenseTensor cols; DenseTensor non_zero_elements; SparseCsrTensor csr(crows, cols, non_zero_elements, x.dims()); CooToCsrKernel(dev_ctx, x, &csr); return csr; } template void DenseToCsrKernel(const Context& dev_ctx, const DenseTensor& x, SparseCsrTensor* out) { const auto& x_dims = x.dims(); bool valid = x_dims.size() == 2 || x_dims.size() == 3; PADDLE_ENFORCE_EQ(valid, true, phi::errors::InvalidArgument( "SparseCsrTensor only support 2-D or 3-D Tensor.")); const int64_t sparse_dim = x_dims.size() == 2 ? 2 : 3; DenseTensor indices; DenseTensor values; SparseCooTensor coo(indices, values, x.dims()); DenseToCooKernel(dev_ctx, x, sparse_dim, &coo); CooToCsrKernel(dev_ctx, coo, out); } template SparseCsrTensor DenseToCsr(const Context& dev_ctx, const DenseTensor& x) { DenseTensor crows; DenseTensor cols; DenseTensor non_zero_elements; SparseCsrTensor csr(crows, cols, non_zero_elements, x.dims()); DenseToCsrKernel(dev_ctx, x, &csr); return csr; } template void CooToDenseKernel(const Context& dev_ctx, const SparseCooTensor& x, DenseTensor* out); template DenseTensor CooToDense(const Context& dev_ctx, const SparseCooTensor& x) { DenseTensorMeta meta(x.dtype(), x.dims(), x.non_zero_elements().layout()); DenseTensor dense = phi::Empty(dev_ctx, std::move(meta)); CooToDenseKernel(dev_ctx, x, &dense); return dense; } template void CsrToDenseKernel(const Context& dev_ctx, const SparseCsrTensor& x, DenseTensor* out) { DenseTensor indices; DenseTensor values; SparseCooTensor coo(indices, values, x.dims()); CsrToCooKernel(dev_ctx, x, &coo); CooToDenseKernel(dev_ctx, coo, out); } template DenseTensor CsrToDense(const Context& dev_ctx, const SparseCsrTensor& x) { DenseTensorMeta meta(x.dtype(), x.dims(), x.non_zero_elements().layout()); DenseTensor dense = phi::Empty(dev_ctx, std::move(meta)); CsrToDenseKernel(dev_ctx, x, &dense); return dense; } template void ValuesCooKernel(const Context& dev_ctx, const SparseCooTensor& x, DenseTensor* out) { *out = x.non_zero_elements(); } template void ValuesCsrKernel(const Context& dev_ctx, const SparseCsrTensor& x, DenseTensor* out) { *out = x.non_zero_elements(); } template void SparseCooTensorKernel(const Context& dev_ctx, const DenseTensor& values, const DenseTensor& indices, const IntArray& dense_shape, SparseCooTensor* out) { *out = SparseCooTensor(indices, values, phi::make_ddim(dense_shape.GetData())); } } // namespace sparse } // namespace phi