/* 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. */ #include #include #include #include "paddle/fluid/memory/allocation/allocator_facade.h" #include "paddle/phi/api/lib/utils/allocator.h" #include "paddle/phi/backends/cpu/cpu_context.h" #include "paddle/phi/core/dense_tensor.h" #include "paddle/phi/core/kernel_registry.h" #include "paddle/phi/kernels/elementwise_add_grad_kernel.h" #include "paddle/phi/kernels/elementwise_add_kernel.h" #include "paddle/phi/kernels/elementwise_divide_grad_kernel.h" #include "paddle/phi/kernels/elementwise_divide_kernel.h" #include "paddle/phi/kernels/elementwise_multiply_grad_kernel.h" #include "paddle/phi/kernels/elementwise_multiply_kernel.h" #include "paddle/phi/kernels/elementwise_subtract_grad_kernel.h" #include "paddle/phi/kernels/elementwise_subtract_kernel.h" #include "paddle/phi/kernels/sparse/elementwise_grad_kernel.h" #include "paddle/phi/kernels/sparse/elementwise_kernel.h" #include "paddle/phi/kernels/sparse/sparse_utils_kernel.h" namespace phi { namespace tests { #define TEST_ELEMENTWISE_OP(name) \ TEST_ELEMENTWISE_OP_WITH_TYPE(name, Csr) \ \ TEST_ELEMENTWISE_OP_WITH_TYPE(name, Coo) #define TEST_ELEMENTWISE_OP_WITH_TYPE(name, type) \ template \ void TestElementWise##name##type(const Context& dev_ctx_cpu, \ const Sparse##type##Tensor& x, \ const Sparse##type##Tensor& y, \ const DDim& dense_dims) { \ auto out = sparse::ElementWise##name##type(dev_ctx_cpu, x, y); \ const DenseTensor denseX = \ sparse::Sparse##type##ToDense(dev_ctx_cpu, x); \ const DenseTensor denseY = \ sparse::Sparse##type##ToDense(dev_ctx_cpu, y); \ const DenseTensor denseOut = \ sparse::Sparse##type##ToDense(dev_ctx_cpu, out); \ auto expectResult = name(dev_ctx_cpu, denseX, denseY); \ for (int j = 0; j < denseOut.numel(); ++j) { \ auto actualResultRow = denseOut.template data()[j]; \ auto expectResultRow = expectResult.template data()[j]; \ if (std::is_same::value || std::is_same::value) { \ if (!std::isnan(expectResultRow)) { \ ASSERT_DOUBLE_EQ(expectResultRow, actualResultRow); \ } \ } else { \ ASSERT_EQ(expectResultRow, actualResultRow); \ } \ } \ } TEST_ELEMENTWISE_OP(Add) TEST_ELEMENTWISE_OP(Subtract) TEST_ELEMENTWISE_OP(Multiply) TEST_ELEMENTWISE_OP(Divide) TEST(DEV_API, sparse_elementwise_coo_kernel_double) { using T = double; using IntT = int64_t; for (int epoch = 0; epoch < 100; ++epoch) { DDim dense_dims = phi::make_ddim({2, 4, 4}); IntT sparse_dim = 2; // 32els std::vector x_dense_data = {0.0, 1.0, 0.0, 0.0, 2.0, 0.0, 3.0, 0.0, 0.0, 0.0, 0.0, 4.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 2.0, 0.0, 3.0, 0.0, 0.0, 0.0, 0.0, 4.0, 0.0, 0.0, 0.0, 0.0}; std::vector y_dense_data = {0.0, 0.0, 0.0, 4.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 2.0, 0.0, 3.0, 0.0, 0.0, 0.0, 0.0, 4.0, 0.0, 0.0, 0.0, 0.0}; const auto alloc = std::make_unique( paddle::platform::CPUPlace()); phi::DenseTensor dense_x( alloc.get(), phi::DenseTensorMeta(DataType::FLOAT32, dense_dims, DataLayout::NCHW)); auto* dense_x_data = dense_x.mutable_data(paddle::platform::CPUPlace()); memcpy(dense_x_data, x_dense_data.data(), x_dense_data.size() * sizeof(T)); phi::DenseTensor dense_y( alloc.get(), phi::DenseTensorMeta(DataType::FLOAT32, dense_dims, DataLayout::NCHW)); auto* dense_y_data = dense_y.mutable_data(paddle::platform::CPUPlace()); memcpy(dense_y_data, y_dense_data.data(), y_dense_data.size() * sizeof(T)); phi::CPUContext dev_ctx_cpu; dev_ctx_cpu.SetAllocator( paddle::memory::allocation::AllocatorFacade::Instance() .GetAllocator(paddle::platform::CPUPlace()) .get()); auto coo_x = sparse::DenseToSparseCoo(dev_ctx_cpu, dense_x, sparse_dim); auto coo_y = sparse::DenseToSparseCoo(dev_ctx_cpu, dense_y, sparse_dim); TestElementWiseAddCoo(dev_ctx_cpu, coo_x, coo_y, dense_dims); TestElementWiseSubtractCoo(dev_ctx_cpu, coo_x, coo_y, dense_dims); TestElementWiseMultiplyCoo(dev_ctx_cpu, coo_x, coo_y, dense_dims); TestElementWiseDivideCoo(dev_ctx_cpu, coo_x, coo_y, dense_dims); } } TEST(DEV_API, sparse_elementwise_csr_kernel_float) { using T = float; DDim dense_dims = phi::make_ddim({6, 4}); // 24els std::vector x_dense_data = {0.0, 0.0, 4.0, 2.0, 6.0, 3.0, 0.2, 0.1, 2.2, 1.1, 4.2, 2.1, 0.4, 0.2, 0.0, 0.0, 4.4, 2.2, 0.6, 0.3, 2.6, 1.3, 0.0, 0.0}; std::vector y_dense_data = {0.0, 1.0, 0.0, 2.0, 0.0, 3.0, 0.0, 3.5, 0.7, 0.0, 3.5, 0.7, 3.2, 0.1, 0.0, 3.2, 1.0, 0.0, 1.2, 0.5, 0.7, 3.3, 0.0, 9.0}; const auto alloc = std::make_unique( paddle::platform::CPUPlace()); phi::DenseTensor dense_x( alloc.get(), phi::DenseTensorMeta(DataType::FLOAT32, dense_dims, DataLayout::NCHW)); auto* dense_x_data = dense_x.mutable_data(paddle::platform::CPUPlace()); memcpy(dense_x_data, x_dense_data.data(), x_dense_data.size() * sizeof(T)); phi::DenseTensor dense_y( alloc.get(), phi::DenseTensorMeta(DataType::FLOAT32, dense_dims, DataLayout::NCHW)); auto* dense_y_data = dense_y.mutable_data(paddle::platform::CPUPlace()); memcpy(dense_y_data, y_dense_data.data(), y_dense_data.size() * sizeof(T)); phi::CPUContext dev_ctx_cpu; dev_ctx_cpu.SetAllocator( paddle::memory::allocation::AllocatorFacade::Instance() .GetAllocator(paddle::platform::CPUPlace()) .get()); auto csr_x = sparse::DenseToSparseCsr(dev_ctx_cpu, dense_x); auto csr_y = sparse::DenseToSparseCsr(dev_ctx_cpu, dense_y); TestElementWiseAddCsr(dev_ctx_cpu, csr_x, csr_y, dense_dims); TestElementWiseSubtractCsr(dev_ctx_cpu, csr_x, csr_y, dense_dims); TestElementWiseMultiplyCsr(dev_ctx_cpu, csr_x, csr_y, dense_dims); TestElementWiseDivideCsr(dev_ctx_cpu, csr_x, csr_y, dense_dims); } #define TEST_ELEMENTWISE_OP_GRAD(name) \ TEST_ELEMENTWISE_OP_GRAD_WITH_TYPE(name, Csr) \ \ TEST_ELEMENTWISE_OP_GRAD_WITH_TYPE(name, Coo) #define TEST_ELEMENTWISE_OP_GRAD_WITH_TYPE(name, type) \ template \ void TestElementWise##name##type##Grad(const Context& dev_ctx_cpu, \ const Sparse##type##Tensor& x, \ const Sparse##type##Tensor& y, \ const DDim& dense_dims) { \ auto out = sparse::ElementWise##name##type(dev_ctx_cpu, x, y); \ auto dresult = \ sparse::ElementWise##name##type##Grad(dev_ctx_cpu, x, y, out); \ \ DenseTensor expectdy = phi::Empty( \ dev_ctx_cpu, \ DenseTensorMeta(DataType::FLOAT32, dense_dims, DataLayout::NCHW)); \ DenseTensor expectdx = phi::Empty( \ dev_ctx_cpu, \ DenseTensorMeta(DataType::FLOAT32, dense_dims, DataLayout::NCHW)); \ \ phi::name##GradKernel( \ dev_ctx_cpu, \ sparse::Sparse##type##ToDense(dev_ctx_cpu, x), \ sparse::Sparse##type##ToDense(dev_ctx_cpu, y), \ sparse::Sparse##type##ToDense(dev_ctx_cpu, out), \ -1, \ &expectdx, \ &expectdy); \ const DenseTensor densedX = \ sparse::Sparse##type##ToDense(dev_ctx_cpu, dresult[0]); \ const DenseTensor densedY = \ sparse::Sparse##type##ToDense(dev_ctx_cpu, dresult[1]); \ const DenseTensor denseOut = \ sparse::Sparse##type##ToDense(dev_ctx_cpu, out); \ \ for (int j = 0; j < densedX.numel(); ++j) { \ auto actualResultRow = densedX.template data()[j]; \ auto expectResultRow = expectdx.template data()[j]; \ if (std::is_same::value || std::is_same::value) { \ if (!std::isnan(expectResultRow)) { \ ASSERT_DOUBLE_EQ(expectResultRow, actualResultRow); \ } \ } else { \ ASSERT_EQ(expectResultRow, actualResultRow); \ } \ } \ for (int j = 0; j < densedY.numel(); ++j) { \ auto actualResultRow = densedY.template data()[j]; \ auto expectResultRow = expectdy.template data()[j]; \ if (std::is_same::value || std::is_same::value) { \ if (!std::isnan(expectResultRow)) { \ ASSERT_DOUBLE_EQ(expectResultRow, actualResultRow); \ } \ } else { \ ASSERT_EQ(expectResultRow, actualResultRow); \ } \ } \ } TEST_ELEMENTWISE_OP_GRAD(Add) TEST_ELEMENTWISE_OP_GRAD(Subtract) TEST_ELEMENTWISE_OP_GRAD(Multiply) template void TestElementWiseDivideCsrGrad(const Context& dev_ctx_cpu, const SparseCsrTensor& x, const SparseCsrTensor& y, const DDim& dense_dims) { auto out = sparse::ElementWiseDivideCsr(dev_ctx_cpu, x, y); auto dresult = sparse::ElementWiseDivideCsrGrad(dev_ctx_cpu, x, y, out, out); DenseTensor expectdy = phi::Empty( dev_ctx_cpu, DenseTensorMeta(DataType::FLOAT32, dense_dims, DataLayout::NCHW)); DenseTensor expectdx = phi::Empty( dev_ctx_cpu, DenseTensorMeta(DataType::FLOAT32, dense_dims, DataLayout::NCHW)); phi::DivideGradKernel(dev_ctx_cpu, sparse::SparseCsrToDense(dev_ctx_cpu, x), sparse::SparseCsrToDense(dev_ctx_cpu, y), sparse::SparseCsrToDense(dev_ctx_cpu, out), sparse::SparseCsrToDense(dev_ctx_cpu, out), -1, &expectdx, &expectdy); const DenseTensor densedX = sparse::SparseCsrToDense(dev_ctx_cpu, dresult[0]); const DenseTensor densedY = sparse::SparseCsrToDense(dev_ctx_cpu, dresult[1]); const DenseTensor denseOut = sparse::SparseCsrToDense(dev_ctx_cpu, out); for (int j = 0; j < densedX.numel(); ++j) { auto actualResultRow = densedX.template data()[j]; auto expectResultRow = expectdx.template data()[j]; if (!std::isnan(expectResultRow)) { ASSERT_DOUBLE_EQ(expectResultRow, actualResultRow); } } for (int j = 0; j < densedY.numel(); ++j) { auto actualResultRow = densedY.template data()[j]; auto expectResultRow = expectdy.template data()[j]; if (!std::isnan(expectResultRow)) { ASSERT_DOUBLE_EQ(expectResultRow, actualResultRow); } } } template void TestElementWiseDivideCooGrad(const Context& dev_ctx_cpu, const SparseCooTensor& x, const SparseCooTensor& y, const DDim& dense_dims) { auto out = sparse::ElementWiseDivideCoo(dev_ctx_cpu, x, y); auto dresult = sparse::ElementWiseDivideCooGrad(dev_ctx_cpu, x, y, out, out); DenseTensor expectdy = phi::Empty( dev_ctx_cpu, DenseTensorMeta(DataType::FLOAT32, dense_dims, DataLayout::NCHW)); DenseTensor expectdx = phi::Empty( dev_ctx_cpu, DenseTensorMeta(DataType::FLOAT32, dense_dims, DataLayout::NCHW)); phi::DivideGradKernel(dev_ctx_cpu, sparse::SparseCooToDense(dev_ctx_cpu, x), sparse::SparseCooToDense(dev_ctx_cpu, y), sparse::SparseCooToDense(dev_ctx_cpu, out), sparse::SparseCooToDense(dev_ctx_cpu, out), -1, &expectdx, &expectdy); const DenseTensor densedX = sparse::SparseCooToDense(dev_ctx_cpu, dresult[0]); const DenseTensor densedY = sparse::SparseCooToDense(dev_ctx_cpu, dresult[1]); const DenseTensor denseOut = sparse::SparseCooToDense(dev_ctx_cpu, out); for (int j = 0; j < densedX.numel(); ++j) { auto actualResultRow = densedX.template data()[j]; auto expectResultRow = expectdx.template data()[j]; if (!std::isnan(expectResultRow)) { ASSERT_DOUBLE_EQ(expectResultRow, actualResultRow); } } for (int j = 0; j < densedY.numel(); ++j) { auto actualResultRow = densedY.template data()[j]; auto expectResultRow = expectdy.template data()[j]; if (!std::isnan(expectResultRow)) { ASSERT_DOUBLE_EQ(expectResultRow, actualResultRow); } } } TEST(DEV_API, sparse_elementwise_csr_grad_kernel_float) { using T = float; DDim dense_dims = phi::make_ddim({2, 3, 4}); std::vector x_dense_data = {0.0, 0.0, 4.0, 2.0, 6.0, 3.0, 0.2, 0.1, 2.2, 1.1, 4.2, 2.1, 0.4, 0.2, 0.0, 0.0, 4.4, 2.2, 0.6, 0.3, 2.6, 1.3, 0.0, 0.0}; std::vector y_dense_data = {0.0, 1.0, 0.0, 2.0, 0.0, 3.0, 0.0, 3.5, 0.7, 0.0, 3.5, 0.7, 3.2, 0.1, 0.0, 3.2, 1.0, 0.0, 1.2, 0.5, 0.7, 3.3, 0.0, 9.0}; const auto alloc = std::make_unique( paddle::platform::CPUPlace()); phi::DenseTensor dense_x( alloc.get(), phi::DenseTensorMeta(DataType::FLOAT32, dense_dims, DataLayout::NCHW)); auto* dense_x_data = dense_x.mutable_data(paddle::platform::CPUPlace()); memcpy(dense_x_data, x_dense_data.data(), x_dense_data.size() * sizeof(T)); phi::DenseTensor dense_y( alloc.get(), phi::DenseTensorMeta(DataType::FLOAT32, dense_dims, DataLayout::NCHW)); auto* dense_y_data = dense_y.mutable_data(paddle::platform::CPUPlace()); memcpy(dense_y_data, y_dense_data.data(), y_dense_data.size() * sizeof(T)); phi::CPUContext dev_ctx_cpu; dev_ctx_cpu.SetAllocator( paddle::memory::allocation::AllocatorFacade::Instance() .GetAllocator(paddle::platform::CPUPlace()) .get()); dev_ctx_cpu.SetHostAllocator( paddle::memory::allocation::AllocatorFacade::Instance() .GetAllocator(paddle::platform::CPUPlace()) .get()); auto csr_x = sparse::DenseToSparseCsr(dev_ctx_cpu, dense_x); auto csr_y = sparse::DenseToSparseCsr(dev_ctx_cpu, dense_y); auto dx = sparse::DenseToSparseCsr(dev_ctx_cpu, dense_y); auto dy = sparse::DenseToSparseCsr(dev_ctx_cpu, dense_x); TestElementWiseAddCsrGrad(dev_ctx_cpu, csr_x, csr_y, dense_dims); TestElementWiseSubtractCsrGrad(dev_ctx_cpu, csr_x, csr_y, dense_dims); TestElementWiseMultiplyCsrGrad(dev_ctx_cpu, csr_x, csr_y, dense_dims); TestElementWiseDivideCsrGrad(dev_ctx_cpu, csr_x, csr_y, dense_dims); } TEST(DEV_API, sparse_elementwise_coo_grad_kernel_double) { using T = double; int64_t sparse_dim = 2; DDim dense_dims = phi::make_ddim({3, 4}); std::vector x_dense_data = { 0.0, 1.0, 0.0, 2.0, 0.0, 3.0, 3.2, 0.0, 0.0, 3.2, 0.0, 0.0}; std::vector y_dense_data = { 0.0, 1.0, 0.0, 2.0, 0.0, 3.0, 0.0, 3.5, 0.7, 0.0, 3.5, 0.7}; const auto alloc = std::make_unique( paddle::platform::CPUPlace()); phi::DenseTensor dense_x( alloc.get(), phi::DenseTensorMeta(DataType::FLOAT32, dense_dims, DataLayout::NCHW)); auto* dense_x_data = dense_x.mutable_data(paddle::platform::CPUPlace()); memcpy(dense_x_data, x_dense_data.data(), x_dense_data.size() * sizeof(T)); phi::DenseTensor dense_y( alloc.get(), phi::DenseTensorMeta(DataType::FLOAT32, dense_dims, DataLayout::NCHW)); auto* dense_y_data = dense_y.mutable_data(paddle::platform::CPUPlace()); memcpy(dense_y_data, y_dense_data.data(), y_dense_data.size() * sizeof(T)); phi::CPUContext dev_ctx_cpu; dev_ctx_cpu.SetAllocator( paddle::memory::allocation::AllocatorFacade::Instance() .GetAllocator(paddle::platform::CPUPlace()) .get()); dev_ctx_cpu.SetHostAllocator( paddle::memory::allocation::AllocatorFacade::Instance() .GetAllocator(paddle::platform::CPUPlace()) .get()); auto csr_x = sparse::DenseToSparseCoo(dev_ctx_cpu, dense_x, sparse_dim); auto csr_y = sparse::DenseToSparseCoo(dev_ctx_cpu, dense_y, sparse_dim); auto dx = sparse::DenseToSparseCoo(dev_ctx_cpu, dense_y, sparse_dim); auto dy = sparse::DenseToSparseCoo(dev_ctx_cpu, dense_x, sparse_dim); TestElementWiseAddCooGrad(dev_ctx_cpu, csr_x, csr_y, dense_dims); TestElementWiseSubtractCooGrad(dev_ctx_cpu, csr_x, csr_y, dense_dims); TestElementWiseMultiplyCooGrad(dev_ctx_cpu, csr_x, csr_y, dense_dims); TestElementWiseDivideCooGrad(dev_ctx_cpu, csr_x, csr_y, dense_dims); } } // namespace tests } // namespace phi