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
Changes from 8 commits
97ac270
26f4662
a7f3410
d4310af
7e5f102
71864fd
a99a5ba
95aa0b3
f706dea
d898df7
b770f41
f92e8cd
394ce5e
c577f46
3ad6fba
56fc5da
1f18c59
c606825
5dd4507
f59fa26
dea61c7
60c7359
ad8ceda
178dd27
1ace46f
7bb41d7
790cb0d
c44ac74
d35e923
b04ab6c
fa93d7d
a6d2cd0
67d14b4
268ac34
39c9750
06787c0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -187,6 +187,7 @@ DECLARE_ACTIVATION_GRAD_KERNEL_DEPX(Log1p); | |
DECLARE_ACTIVATION_GRAD_KERNEL_DEPOUT(Relu); | ||
DECLARE_ACTIVATION_GRAD_KERNEL_DEPOUT(Tanh); | ||
DECLARE_ACTIVATION_GRAD_KERNEL_DEPOUT(Sigmoid); | ||
DECLARE_ACTIVATION_GRAD_KERNEL_DEPOUT(Sqrt); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 之前 dense tensor 的 SqrtGrad kernel 没有在头文件中声明 |
||
|
||
DECLARE_ACTIVATION_GRAD_KERNEL_NODEP(Round); | ||
DECLARE_ACTIVATION_GRAD_KERNEL_NODEP(Floor); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,54 +13,53 @@ See the License for the specific language governing permissions and | |
limitations under the License. */ | ||
|
||
#include "paddle/phi/kernels/sparse/activation_kernel.h" | ||
#include "paddle/phi/kernels/copy_kernel.h" | ||
#include "paddle/phi/kernels/empty_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/kernels/sparse/utils.h" | ||
|
||
namespace phi { | ||
namespace sparse { | ||
DEFINE_AND_REGISTER_SPARSE_UNARY_KERNEL(sqrt, SqrtKernel) | ||
|
||
template <typename T, typename Context> | ||
void SparseReluKernel(const Context& dev_ctx, | ||
const SparseCooTensor& x, | ||
SparseCooTensor* out) { | ||
DenseTensor non_zero_indices = | ||
phi::EmptyLike<T, Context>(dev_ctx, x.non_zero_indices()); | ||
DenseTensor non_zero_elements = | ||
phi::EmptyLike<T, Context>(dev_ctx, x.non_zero_elements()); | ||
phi::Copy(dev_ctx, | ||
x.non_zero_indices(), | ||
dev_ctx.GetPlace(), | ||
false, | ||
&non_zero_indices); | ||
phi::ReluKernel<T, Context>( | ||
dev_ctx, x.non_zero_elements(), &non_zero_elements); | ||
out->SetMember(non_zero_indices, non_zero_elements, x.dims(), true); | ||
} | ||
|
||
} // namespace sparse | ||
} // namespace phi | ||
// 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 `sqrt` above | ||
// instead of `relu` following | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 为了避开 #41356 (comment) 的限制,relu 这个 kernel 用 PD_REGISTER_KERNEL 手动注册而没有用 DEFINE_AND_REGISTER_SPARSE_UNARY_KERNEL 宏。并写了详细的注释 |
||
DEFINE_SPARSE_UNARY_KERNEL(ReluKernel) | ||
|
||
PD_REGISTER_KERNEL(sparse_relu, | ||
PD_REGISTER_KERNEL(sparse_coo_relu, | ||
CPU, | ||
ALL_LAYOUT, | ||
phi::sparse::SparseReluKernel, | ||
phi::sparse::SparseCooReluKernel, | ||
float, | ||
double) { | ||
kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_COO); | ||
} | ||
PD_REGISTER_KERNEL(sparse_csr_relu, | ||
CPU, | ||
ALL_LAYOUT, | ||
phi::sparse::SparseCsrReluKernel, | ||
float, | ||
double) { | ||
kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_CSR); | ||
} | ||
|
||
#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP) | ||
PD_REGISTER_KERNEL(sparse_relu, | ||
PD_REGISTER_KERNEL(sparse_coo_relu, | ||
GPU, | ||
ALL_LAYOUT, | ||
phi::sparse::SparseReluKernel, | ||
phi::sparse::SparseCooReluKernel, | ||
float, | ||
double, | ||
phi::dtype::float16) { | ||
kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_COO); | ||
} | ||
|
||
PD_REGISTER_KERNEL(sparse_csr_relu, | ||
GPU, | ||
ALL_LAYOUT, | ||
phi::sparse::SparseCsrReluKernel, | ||
float, | ||
double, | ||
phi::dtype::float16) { | ||
kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_CSR); | ||
} | ||
#endif |
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.
这里 paddle 原本的代码是错误的,不知道是不是没有测试过。SparseCsrTensor 的构造函数内会检查 dims 的长度,只允许 2d 和 3d,这里 1d 的 dim 会导致 check 失败
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.
当前文件数超了最大限制了,要不把api_gen_utils.cc和sparse_csr_tensor.cc这两个修复的文件单独提一个PR?
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.
好的~