From 4abcca49d75049d37be4d66c48a04b1e277e2a59 Mon Sep 17 00:00:00 2001 From: zkh2016 Date: Mon, 5 Sep 2022 03:03:26 +0000 Subject: [PATCH 1/2] rename values_coo --- paddle/phi/api/yaml/sparse_api.yaml | 4 ++-- paddle/phi/kernels/sparse/cpu/sparse_utils_kernel.cc | 8 ++++---- paddle/phi/kernels/sparse/gpu/sparse_utils_kernel.cu | 8 ++++---- paddle/phi/kernels/sparse/sparse_utils_kernel.h | 4 ++-- 4 files changed, 12 insertions(+), 12 deletions(-) diff --git a/paddle/phi/api/yaml/sparse_api.yaml b/paddle/phi/api/yaml/sparse_api.yaml index e11306f21f24ec..201b8df35d6bd3 100644 --- a/paddle/phi/api/yaml/sparse_api.yaml +++ b/paddle/phi/api/yaml/sparse_api.yaml @@ -288,8 +288,8 @@ args : (Tensor x) output : Tensor(out) kernel : - func : coo_values{sparse_coo -> dense}, - csr_values{sparse_csr -> dense} + func : values_coo{sparse_coo -> dense}, + values_csr{sparse_csr -> dense} layout : x backward : values_grad diff --git a/paddle/phi/kernels/sparse/cpu/sparse_utils_kernel.cc b/paddle/phi/kernels/sparse/cpu/sparse_utils_kernel.cc index 85227b7cf64fcb..2e93dd35d50d97 100644 --- a/paddle/phi/kernels/sparse/cpu/sparse_utils_kernel.cc +++ b/paddle/phi/kernels/sparse/cpu/sparse_utils_kernel.cc @@ -391,10 +391,10 @@ PD_REGISTER_KERNEL(sparse_csr_to_dense, int, int64_t) {} -PD_REGISTER_KERNEL(coo_values, +PD_REGISTER_KERNEL(values_coo, CPU, ALL_LAYOUT, - phi::sparse::CooValuesKernel, + phi::sparse::ValuesCooKernel, float, double, phi::dtype::float16, @@ -406,10 +406,10 @@ PD_REGISTER_KERNEL(coo_values, kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_COO); } -PD_REGISTER_KERNEL(csr_values, +PD_REGISTER_KERNEL(values_csr, CPU, ALL_LAYOUT, - phi::sparse::CsrValuesKernel, + phi::sparse::ValuesCsrKernel, float, double, phi::dtype::float16, diff --git a/paddle/phi/kernels/sparse/gpu/sparse_utils_kernel.cu b/paddle/phi/kernels/sparse/gpu/sparse_utils_kernel.cu index dbd2f305936a2a..59c7dcabc987db 100644 --- a/paddle/phi/kernels/sparse/gpu/sparse_utils_kernel.cu +++ b/paddle/phi/kernels/sparse/gpu/sparse_utils_kernel.cu @@ -594,10 +594,10 @@ PD_REGISTER_KERNEL(sparse_csr_to_dense, int, int64_t) {} -PD_REGISTER_KERNEL(coo_values, +PD_REGISTER_KERNEL(values_coo, GPU, ALL_LAYOUT, - phi::sparse::CooValuesKernel, + phi::sparse::ValuesCooKernel, float, double, phi::dtype::float16, @@ -609,10 +609,10 @@ PD_REGISTER_KERNEL(coo_values, kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_COO); } -PD_REGISTER_KERNEL(csr_values, +PD_REGISTER_KERNEL(values_csr, GPU, ALL_LAYOUT, - phi::sparse::CsrValuesKernel, + phi::sparse::ValuesCsrKernel, float, double, phi::dtype::float16, diff --git a/paddle/phi/kernels/sparse/sparse_utils_kernel.h b/paddle/phi/kernels/sparse/sparse_utils_kernel.h index 12d55596a935d6..162b53095f902d 100644 --- a/paddle/phi/kernels/sparse/sparse_utils_kernel.h +++ b/paddle/phi/kernels/sparse/sparse_utils_kernel.h @@ -134,14 +134,14 @@ DenseTensor SparseCsrToDense(const Context& dev_ctx, const SparseCsrTensor& x) { } template -void CooValuesKernel(const Context& dev_ctx, +void ValuesCooKernel(const Context& dev_ctx, const SparseCooTensor& x, DenseTensor* out) { *out = x.non_zero_elements(); } template -void CsrValuesKernel(const Context& dev_ctx, +void ValuesCsrKernel(const Context& dev_ctx, const SparseCsrTensor& x, DenseTensor* out) { *out = x.non_zero_elements(); From 3a4e69b139ded6a9b67f18f5a816260a94c8c4a0 Mon Sep 17 00:00:00 2001 From: zkh2016 Date: Wed, 14 Sep 2022 03:27:03 +0000 Subject: [PATCH 2/2] remove unused code --- paddle/phi/api/lib/CMakeLists.txt | 10 +- paddle/phi/api/lib/sparse_api_custom_impl.cc | 202 ------------------ paddle/phi/api/lib/sparse_api_custom_impl.h | 32 --- .../yaml/generator/intermediate_api_gen.py | 1 - .../phi/api/yaml/generator/sparse_api_gen.py | 1 - .../api/yaml/generator/sparse_bw_api_gen.py | 1 - .../phi/tests/core/test_sparse_coo_tensor.cc | 1 - .../phi/tests/core/test_sparse_csr_tensor.cc | 1 - 8 files changed, 2 insertions(+), 247 deletions(-) delete mode 100644 paddle/phi/api/lib/sparse_api_custom_impl.cc delete mode 100644 paddle/phi/api/lib/sparse_api_custom_impl.h diff --git a/paddle/phi/api/lib/CMakeLists.txt b/paddle/phi/api/lib/CMakeLists.txt index 957d43b46234b1..fcd50b04919e70 100644 --- a/paddle/phi/api/lib/CMakeLists.txt +++ b/paddle/phi/api/lib/CMakeLists.txt @@ -370,11 +370,6 @@ cc_library( SRCS api_custom_impl.cc DEPS phi_tensor_raw phi kernel_dispatch api_gen_utils backward_infermeta phi_data_transform) -cc_library( - sparse_api_custom_impl - SRCS sparse_api_custom_impl.cc - DEPS phi_tensor_raw phi kernel_dispatch api_gen_utils phi_data_transform - tensor_copy) cc_library( phi_function_api @@ -396,12 +391,11 @@ cc_library( cc_library( sparse_api SRCS ${sparse_api_source_file} - DEPS phi_tensor_raw phi kernel_dispatch api_gen_utils sparse_api_custom_impl) + DEPS phi_tensor_raw phi kernel_dispatch api_gen_utils) cc_library( sparse_bw_api SRCS ${sparse_bw_api_source_file} - DEPS phi_tensor_raw phi kernel_dispatch api_gen_utils sparse_api - sparse_api_custom_impl) + DEPS phi_tensor_raw phi kernel_dispatch api_gen_utils sparse_api) cc_library( phi_dygraph_api SRCS ${dygraph_api_source_file} diff --git a/paddle/phi/api/lib/sparse_api_custom_impl.cc b/paddle/phi/api/lib/sparse_api_custom_impl.cc deleted file mode 100644 index 6aaf21a5e7f49f..00000000000000 --- a/paddle/phi/api/lib/sparse_api_custom_impl.cc +++ /dev/null @@ -1,202 +0,0 @@ -/* 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 "paddle/phi/api/lib/sparse_api_custom_impl.h" - -#include - -#include "glog/logging.h" -#include "paddle/phi/api/lib/kernel_dispatch.h" -#include "paddle/phi/core/kernel_registry.h" - -namespace paddle { -namespace experimental { -namespace sparse { - -Tensor to_sparse_coo_impl(const Tensor& x, const int64_t sparse_dim) { - if (x.layout() == phi::DataLayout::SPARSE_COO) { - return x; - } - - // 1. Get kernel signature and kernel - std::string kernel_name = "dense_to_coo"; - if (x.layout() == phi::DataLayout::SPARSE_CSR) { - kernel_name = "csr_to_coo"; - } - - auto kernel_key_set = ParseKernelKeyByInputArgs(x); - auto kernel_key = kernel_key_set.GetHighestPriorityKernelKey(); - - auto kernel_result = phi::KernelFactory::Instance().SelectKernelOrThrowError( - kernel_name, kernel_key); - const auto& kernel = kernel_result.kernel; - - VLOG(6) << "add API kernel key: " << kernel_key; - VLOG(6) << "to API kernel: " << kernel; - - // 2. Get Device Context - auto* dev_ctx = GetDeviceContextByBackend(kernel_key.backend()); - auto kernel_context = phi::KernelContext(dev_ctx); - - // 3. Auto data transform - if (x.layout() == phi::DataLayout::SPARSE_CSR) { - auto input = std::dynamic_pointer_cast(x.impl()); - kernel_context.EmplaceBackInput(input.get()); - } else { - auto input = std::dynamic_pointer_cast(x.impl()); - kernel_context.EmplaceBackInput(input.get()); - kernel_context.EmplaceBackAttr(sparse_dim); - } - - // 4. InferMeta - auto indices_meta = - phi::DenseTensorMeta(phi::DataType::INT64, {1}, phi::DataLayout::NCHW); - auto elements_meta = phi::DenseTensorMeta(x.dtype(), {1}, x.layout()); - - // 5. Prepare outputs - // create empty SparseCooTensor - phi::DenseTensor non_zero_indices(std::make_shared(), - std::move(indices_meta)); - phi::DenseTensor non_zero_elements(std::make_shared(), - std::move(elements_meta)); - auto coo = std::make_shared( - non_zero_indices, non_zero_elements, x.dims()); - - kernel_context.EmplaceBackOutput(coo.get()); - Tensor out; - out.set_impl(coo); - - // 6. Call kernel - kernel(&kernel_context); - - return out; -} - -Tensor to_sparse_csr_impl(const Tensor& x) { - if (x.layout() == phi::DataLayout::SPARSE_CSR) { - return x; - } - // 1. Get kernel signature and kernel - std::string kernel_name = "dense_to_csr"; - if (x.layout() == phi::DataLayout::SPARSE_COO) { - kernel_name = "coo_to_csr"; - } - - auto kernel_key_set = ParseKernelKeyByInputArgs(x); - auto kernel_key = kernel_key_set.GetHighestPriorityKernelKey(); - - auto kernel_result = phi::KernelFactory::Instance().SelectKernelOrThrowError( - kernel_name, kernel_key); - const auto& kernel = kernel_result.kernel; - - VLOG(6) << "add API kernel key: " << kernel_key; - VLOG(6) << "to API kernel: " << kernel; - - // 2. Get Device Context - auto* dev_ctx = GetDeviceContextByBackend(kernel_key.backend()); - auto kernel_context = phi::KernelContext(dev_ctx); - - // 3. Auto data transform - if (x.layout() == phi::DataLayout::SPARSE_COO) { - auto input = std::dynamic_pointer_cast(x.impl()); - kernel_context.EmplaceBackInput(input.get()); - } else { - auto input = std::dynamic_pointer_cast(x.impl()); - kernel_context.EmplaceBackInput(input.get()); - } - - // 4. InferMeta - auto crows_meta = - phi::DenseTensorMeta(phi::DataType::INT64, {1}, phi::DataLayout::NCHW); - auto cols_meta = - phi::DenseTensorMeta(phi::DataType::INT64, {1}, phi::DataLayout::NCHW); - auto elements_meta = phi::DenseTensorMeta(x.dtype(), {1}, x.layout()); - - // 5. Prepare outputs - // create empty SparseCooTensor - phi::DenseTensor non_zero_crows(std::make_shared(), - std::move(crows_meta)); - phi::DenseTensor non_zero_cols(std::make_shared(), - std::move(cols_meta)); - phi::DenseTensor non_zero_elements(std::make_shared(), - std::move(elements_meta)); - auto csr = std::make_shared( - non_zero_crows, non_zero_cols, non_zero_elements, x.dims()); - - kernel_context.EmplaceBackOutput(csr.get()); - Tensor out; - out.set_impl(csr); - - // 6. Call kernel - kernel(&kernel_context); - - return out; -} - -Tensor to_dense_impl(const Tensor& x) { - if (x.layout() != phi::DataLayout::SPARSE_CSR && - x.layout() != phi::DataLayout::SPARSE_COO) { - return x; - } - - // 1. Get kernel signature and kernel - std::string kernel_name = "coo_to_dense"; - if (x.layout() == phi::DataLayout::SPARSE_CSR) { - kernel_name = "csr_to_dense"; - } - - auto kernel_key_set = ParseKernelKeyByInputArgs(x); - auto kernel_key = kernel_key_set.GetHighestPriorityKernelKey(); - - auto kernel_result = phi::KernelFactory::Instance().SelectKernelOrThrowError( - kernel_name, kernel_key); - const auto& kernel = kernel_result.kernel; - - VLOG(6) << "add API kernel key: " << kernel_key; - VLOG(6) << "to API kernel: " << kernel; - - // 2. Get Device Context - auto* dev_ctx = GetDeviceContextByBackend(kernel_key.backend()); - auto kernel_context = phi::KernelContext(dev_ctx); - - // 3. Auto data transform - if (x.layout() == phi::DataLayout::SPARSE_COO) { - auto input = std::dynamic_pointer_cast(x.impl()); - kernel_context.EmplaceBackInput(input.get()); - } else { - auto input = std::dynamic_pointer_cast(x.impl()); - kernel_context.EmplaceBackInput(input.get()); - } - - // 4. InferMeta - auto dense_meta = phi::DenseTensorMeta(x.dtype(), x.dims(), x.layout()); - - // 5. Prepare outputs - // create empty SparseCooTensor - auto dense_out = std::make_shared( - std::make_shared(), std::move(dense_meta)); - - kernel_context.EmplaceBackOutput(dense_out.get()); - Tensor out; - out.set_impl(dense_out); - - // 6. Call kernel - kernel(&kernel_context); - - return out; -} - -} // namespace sparse -} // namespace experimental -} // namespace paddle diff --git a/paddle/phi/api/lib/sparse_api_custom_impl.h b/paddle/phi/api/lib/sparse_api_custom_impl.h deleted file mode 100644 index 6053d281f0ff16..00000000000000 --- a/paddle/phi/api/lib/sparse_api_custom_impl.h +++ /dev/null @@ -1,32 +0,0 @@ -/* 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/api/include/tensor.h" -#include "paddle/phi/common/backend.h" - -namespace paddle { -namespace experimental { -namespace sparse { - -Tensor to_dense_impl(const Tensor& x); - -Tensor to_sparse_coo_impl(const Tensor& x, const int64_t sparse_dim); - -Tensor to_sparse_csr_impl(const Tensor& x); - -} // namespace sparse -} // namespace experimental -} // namespace paddle diff --git a/paddle/phi/api/yaml/generator/intermediate_api_gen.py b/paddle/phi/api/yaml/generator/intermediate_api_gen.py index 8bec3e8c158eaf..e5359d442253eb 100644 --- a/paddle/phi/api/yaml/generator/intermediate_api_gen.py +++ b/paddle/phi/api/yaml/generator/intermediate_api_gen.py @@ -43,7 +43,6 @@ def source_include(header_file_path): #include "paddle/phi/api/lib/api_gen_utils.h" #include "paddle/phi/api/lib/data_transform.h" #include "paddle/phi/api/lib/kernel_dispatch.h" -#include "paddle/phi/api/lib/sparse_api_custom_impl.h" #include "paddle/phi/core/kernel_registry.h" #include "paddle/phi/infermeta/binary.h" #include "paddle/phi/infermeta/multiary.h" diff --git a/paddle/phi/api/yaml/generator/sparse_api_gen.py b/paddle/phi/api/yaml/generator/sparse_api_gen.py index eb36bea8e89577..f76de9bd798ea1 100644 --- a/paddle/phi/api/yaml/generator/sparse_api_gen.py +++ b/paddle/phi/api/yaml/generator/sparse_api_gen.py @@ -229,7 +229,6 @@ def source_include(header_file_path): #include "paddle/phi/api/lib/api_gen_utils.h" #include "paddle/phi/api/lib/data_transform.h" #include "paddle/phi/api/lib/kernel_dispatch.h" -#include "paddle/phi/api/lib/sparse_api_custom_impl.h" #include "paddle/phi/core/kernel_registry.h" """ diff --git a/paddle/phi/api/yaml/generator/sparse_bw_api_gen.py b/paddle/phi/api/yaml/generator/sparse_bw_api_gen.py index 6845f91c604d43..10ef016adb0b9f 100644 --- a/paddle/phi/api/yaml/generator/sparse_bw_api_gen.py +++ b/paddle/phi/api/yaml/generator/sparse_bw_api_gen.py @@ -111,7 +111,6 @@ def source_include(header_file_path): #include "paddle/phi/api/include/sparse_api.h" #include "paddle/phi/api/lib/api_gen_utils.h" #include "paddle/phi/api/lib/kernel_dispatch.h" -#include "paddle/phi/api/lib/sparse_api_custom_impl.h" #include "paddle/phi/core/kernel_registry.h" """ diff --git a/paddle/phi/tests/core/test_sparse_coo_tensor.cc b/paddle/phi/tests/core/test_sparse_coo_tensor.cc index e9ee1dde6b2a56..81e58843f5475c 100644 --- a/paddle/phi/tests/core/test_sparse_coo_tensor.cc +++ b/paddle/phi/tests/core/test_sparse_coo_tensor.cc @@ -52,7 +52,6 @@ TEST(sparse_coo_tensor, construct) { CHECK_EQ(sparse.numel(), 9); CHECK(sparse.dims() == dense_dims); CHECK(sparse.dtype() == DataType::FLOAT32); - CHECK(sparse.layout() == DataLayout::SPARSE_COO); CHECK(sparse.place() == phi::CPUPlace()); } diff --git a/paddle/phi/tests/core/test_sparse_csr_tensor.cc b/paddle/phi/tests/core/test_sparse_csr_tensor.cc index 7fad7bac399cd4..42f87fc5aae127 100644 --- a/paddle/phi/tests/core/test_sparse_csr_tensor.cc +++ b/paddle/phi/tests/core/test_sparse_csr_tensor.cc @@ -62,7 +62,6 @@ TEST(sparse_csr_tensor, construct) { CHECK_EQ(sparse.numel(), 9); CHECK(sparse.dims() == dense_dims); CHECK(sparse.dtype() == DataType::FLOAT32); - CHECK(sparse.layout() == DataLayout::SPARSE_CSR); CHECK(sparse.place() == paddle::platform::CPUPlace()); CHECK(sparse.initialized() == true); }