-
Notifications
You must be signed in to change notification settings - Fork 5.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
【Hackathon No.60】refactor unary sparse ops and add sparse sqrt, tanh, sin #41356
Merged
zkh2016
merged 36 commits into
PaddlePaddle:develop
from
tiancaishaonvjituizi:sparse_relu
May 12, 2022
Merged
Changes from all commits
Commits
Show all changes
36 commits
Select commit
Hold shift + click to select a range
97ac270
refactor unary sparse ops and add relu
tiancaishaonvjituizi 26f4662
add test
tiancaishaonvjituizi a7f3410
fix the bug in generated api code, tests are passed now
tiancaishaonvjituizi d4310af
Merge branch 'develop' into sparse_relu
tiancaishaonvjituizi 7e5f102
update relu for new sparse api
tiancaishaonvjituizi 71864fd
update test, implement api, fix sqrt grad
tiancaishaonvjituizi a99a5ba
manually register relu and relu_grad kernel to bypass the restriction
tiancaishaonvjituizi 95aa0b3
polish sqrt docs
tiancaishaonvjituizi f706dea
reformat
tiancaishaonvjituizi d898df7
polish docs
tiancaishaonvjituizi b770f41
remove csr backward api
tiancaishaonvjituizi f92e8cd
fix test compile error
tiancaishaonvjituizi 394ce5e
use allclose instead of array_equal
tiancaishaonvjituizi c577f46
move sqrt to math_kernel.cc, implement sin and tanh
tiancaishaonvjituizi 3ad6fba
polish header file
tiancaishaonvjituizi 56fc5da
reformat
tiancaishaonvjituizi 1f18c59
refine
tiancaishaonvjituizi c606825
fix typo
tiancaishaonvjituizi 5dd4507
fix typo
tiancaishaonvjituizi f59fa26
add test about error, reformat
tiancaishaonvjituizi dea61c7
fix test error
tiancaishaonvjituizi 60c7359
fix format
tiancaishaonvjituizi ad8ceda
fix false positive warning in gcc>=9
tiancaishaonvjituizi 178dd27
use more aggressive way
tiancaishaonvjituizi 1ace46f
Merge remote-tracking branch 'origin/develop' into sparse_relu
tiancaishaonvjituizi 7bb41d7
add api in paddle.sparse namespace
tiancaishaonvjituizi 790cb0d
Merge remote-tracking branch 'tiancaishaonv/variant_fix_gcc9_fp_warni…
tiancaishaonvjituizi c44ac74
address reviews
tiancaishaonvjituizi d35e923
Merge remote-tracking branch 'origin/develop' into sparse_relu
tiancaishaonvjituizi b04ab6c
fix ci error
tiancaishaonvjituizi fa93d7d
rename to unary_kernel, update name
tiancaishaonvjituizi a6d2cd0
Merge remote-tracking branch 'origin/develop' into sparse_relu
tiancaishaonvjituizi 67d14b4
remove unused files
tiancaishaonvjituizi 268ac34
rename python files
tiancaishaonvjituizi 39c9750
fix import path
tiancaishaonvjituizi 06787c0
reformat
tiancaishaonvjituizi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,183 @@ | ||
// 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/kernels/sparse/unary_grad_kernel.h" | ||
|
||
#include "paddle/phi/backends/cpu/cpu_context.h" | ||
#include "paddle/phi/backends/gpu/gpu_context.h" | ||
#include "paddle/phi/core/kernel_registry.h" | ||
#include "paddle/phi/core/sparse_coo_tensor.h" | ||
#include "paddle/phi/core/sparse_csr_tensor.h" | ||
#include "paddle/phi/kernels/activation_grad_kernel.h" | ||
#include "paddle/phi/kernels/copy_kernel.h" | ||
#include "paddle/phi/kernels/empty_kernel.h" | ||
|
||
#define DEFINE_SPARSE_UNARY_GRAD_KERNEL(DenseKernelFunc) \ | ||
namespace phi { \ | ||
namespace sparse { \ | ||
\ | ||
template <typename T, typename Context> \ | ||
void SparseCoo##DenseKernelFunc(const Context& dev_ctx, \ | ||
const SparseCooTensor& x_or_out, \ | ||
const SparseCooTensor& out_grad, \ | ||
SparseCooTensor* x_grad) { \ | ||
DenseTensor non_zero_indices = \ | ||
phi::EmptyLike<T, Context>(dev_ctx, x_or_out.non_zero_indices()); \ | ||
DenseTensor non_zero_elements = \ | ||
phi::EmptyLike<T, Context>(dev_ctx, x_or_out.non_zero_elements()); \ | ||
phi::Copy(dev_ctx, \ | ||
x_or_out.non_zero_indices(), \ | ||
dev_ctx.GetPlace(), \ | ||
false, \ | ||
&non_zero_indices); \ | ||
phi::DenseKernelFunc<T, Context>(dev_ctx, \ | ||
x_or_out.non_zero_elements(), \ | ||
out_grad.non_zero_elements(), \ | ||
&non_zero_elements); \ | ||
x_grad->SetMember( \ | ||
non_zero_indices, non_zero_elements, x_or_out.dims(), true); \ | ||
} \ | ||
\ | ||
template <typename T, typename Context> \ | ||
void SparseCsr##DenseKernelFunc(const Context& dev_ctx, \ | ||
const SparseCsrTensor& x_or_out, \ | ||
const SparseCsrTensor& out_grad, \ | ||
SparseCsrTensor* out) { \ | ||
DenseTensor non_zero_crows = \ | ||
phi::EmptyLike<T, Context>(dev_ctx, x_or_out.non_zero_crows()); \ | ||
DenseTensor non_zero_cols = \ | ||
phi::EmptyLike<T, Context>(dev_ctx, x_or_out.non_zero_cols()); \ | ||
DenseTensor non_zero_elements = \ | ||
phi::EmptyLike<T, Context>(dev_ctx, x_or_out.non_zero_elements()); \ | ||
phi::Copy(dev_ctx, \ | ||
x_or_out.non_zero_crows(), \ | ||
dev_ctx.GetPlace(), \ | ||
false, \ | ||
&non_zero_crows); \ | ||
phi::Copy(dev_ctx, \ | ||
x_or_out.non_zero_cols(), \ | ||
dev_ctx.GetPlace(), \ | ||
false, \ | ||
&non_zero_cols); \ | ||
phi::DenseKernelFunc<T, Context>(dev_ctx, \ | ||
x_or_out.non_zero_elements(), \ | ||
out_grad.non_zero_elements(), \ | ||
&non_zero_elements); \ | ||
out->SetMember( \ | ||
non_zero_crows, non_zero_cols, non_zero_elements, x_or_out.dims()); \ | ||
} \ | ||
} \ | ||
} | ||
|
||
#define REGISTER_CPU_SPARSE_UNARY_KERNEL(kernel_name, DenseKernelFunc) \ | ||
PD_REGISTER_KERNEL(sparse_coo_##kernel_name, \ | ||
CPU, \ | ||
ALL_LAYOUT, \ | ||
phi::sparse::SparseCoo##DenseKernelFunc, \ | ||
float, \ | ||
double) { \ | ||
kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_COO); \ | ||
} \ | ||
PD_REGISTER_KERNEL(sparse_csr_##kernel_name, \ | ||
CPU, \ | ||
ALL_LAYOUT, \ | ||
phi::sparse::SparseCsr##DenseKernelFunc, \ | ||
float, \ | ||
double) { \ | ||
kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_CSR); \ | ||
} | ||
|
||
#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP) | ||
#define REGISTER_GPU_SPARSE_UNARY_KERNEL(kernel_name, DenseKernelFunc) \ | ||
PD_REGISTER_KERNEL(sparse_coo_##kernel_name, \ | ||
GPU, \ | ||
ALL_LAYOUT, \ | ||
phi::sparse::SparseCoo##DenseKernelFunc, \ | ||
float, \ | ||
double, \ | ||
phi::dtype::float16) { \ | ||
kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_COO); \ | ||
} \ | ||
\ | ||
PD_REGISTER_KERNEL(sparse_csr_##kernel_name, \ | ||
GPU, \ | ||
ALL_LAYOUT, \ | ||
phi::sparse::SparseCsr##DenseKernelFunc, \ | ||
float, \ | ||
double, \ | ||
phi::dtype::float16) { \ | ||
kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_CSR); \ | ||
} | ||
#else | ||
// This macro definition is empty when GPU is disabled | ||
#define REGISTER_GPU_SPARSE_UNARY_KERNEL(sparse_kernel_name, DenseKernelFunc) | ||
#endif | ||
|
||
#define REGISTER_SPARSE_UNARY_KERNEL(kernel_name, DenseKernelFunc) \ | ||
REGISTER_CPU_SPARSE_UNARY_KERNEL(kernel_name, DenseKernelFunc) \ | ||
REGISTER_GPU_SPARSE_UNARY_KERNEL(kernel_name, DenseKernelFunc) | ||
|
||
#define DEFINE_AND_REGISTER_SPARSE_UNARY_GRAD_KERNEL(kernel_name, \ | ||
DenseKernelFunc) \ | ||
DEFINE_SPARSE_UNARY_GRAD_KERNEL(DenseKernelFunc) \ | ||
REGISTER_SPARSE_UNARY_KERNEL(kernel_name, DenseKernelFunc) | ||
|
||
// NOTE: the following code is to bypass the restriction of Paddle | ||
// kernel registration mechanism. Do NOT refactor them unless you | ||
// know what you are doing. | ||
// If you want to implement any new kernel, please follow `sin_grad`, | ||
// `tanh_grad` etc, do NOT follow the following `relu_grad`. | ||
DEFINE_SPARSE_UNARY_GRAD_KERNEL(ReluGradKernel) | ||
|
||
PD_REGISTER_KERNEL(sparse_coo_relu_grad, | ||
CPU, | ||
ALL_LAYOUT, | ||
phi::sparse::SparseCooReluGradKernel, | ||
float, | ||
double) { | ||
kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_COO); | ||
} | ||
PD_REGISTER_KERNEL(sparse_csr_relu_grad, | ||
CPU, | ||
ALL_LAYOUT, | ||
phi::sparse::SparseCsrReluGradKernel, | ||
float, | ||
double) { | ||
kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_CSR); | ||
} | ||
#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP) | ||
PD_REGISTER_KERNEL(sparse_coo_relu_grad, | ||
GPU, | ||
ALL_LAYOUT, | ||
phi::sparse::SparseCooReluGradKernel, | ||
float, | ||
double, | ||
phi::dtype::float16) { | ||
kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_COO); | ||
} | ||
|
||
PD_REGISTER_KERNEL(sparse_csr_relu_grad, | ||
GPU, | ||
ALL_LAYOUT, | ||
phi::sparse::SparseCsrReluGradKernel, | ||
float, | ||
double, | ||
phi::dtype::float16) { | ||
kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_CSR); | ||
} | ||
#endif | ||
|
||
DEFINE_AND_REGISTER_SPARSE_UNARY_GRAD_KERNEL(sin_grad, SinGradKernel) | ||
DEFINE_AND_REGISTER_SPARSE_UNARY_GRAD_KERNEL(sqrt_grad, SqrtGradKernel) | ||
DEFINE_AND_REGISTER_SPARSE_UNARY_GRAD_KERNEL(tanh_grad, TanhGradKernel) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
之前 dense tensor 的 SqrtGrad kernel 没有在头文件中声明