-
Notifications
You must be signed in to change notification settings - Fork 5.7k
Add Sparse op sparse_relu #40959
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
Merged
Merged
Add Sparse op sparse_relu #40959
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
5f70bfc
sparse relu
eb28517
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
564b2df
sparse relu
2dc4101
remove comment
76572e7
test sparse relu grad
a5d8fed
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
aefb721
adapt new eager api naming rules
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 hidden or 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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
|
||
set(SPARSE_KERNEL_DEPS dense_tensor sparse_coo_tensor sparse_csr_tensor kernel_context kernel_factory arg_map_context convert_utils lod_utils math_function custom_kernel) | ||
register_kernels(DEPS ${SPARSE_KERNEL_DEPS} SUB_DIR "sparse_kernel") | ||
set(SPARSE_KERNEL_DEPS dense_tensor sparse_coo_tensor sparse_csr_tensor kernel_context kernel_factory arg_map_context convert_utils lod_utils math_function custom_kernel copy_kernel) | ||
register_kernels(DEPS ${SPARSE_KERNEL_DEPS} SUB_DIR "sparse") |
70 changes: 70 additions & 0 deletions
70
paddle/phi/kernels/sparse/sparse_activation_grad_kernel.cc
This file contains hidden or 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,70 @@ | ||
/* 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/sparse_activation_grad_kernel.h" | ||
#include "paddle/phi/kernels/activation_grad_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" | ||
|
||
namespace phi { | ||
namespace sparse { | ||
|
||
template <typename T, typename Context> | ||
void SparseReluGradKernel(const Context& dev_ctx, | ||
const SparseCooTensor& x, | ||
const SparseCooTensor& out_grad, | ||
SparseCooTensor* x_grad) { | ||
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::ReluGradKernel<T, Context>(dev_ctx, | ||
x.non_zero_elements(), | ||
out_grad.non_zero_elements(), | ||
&non_zero_elements); | ||
x_grad->SetMember(non_zero_indices, non_zero_elements, x.dims(), true); | ||
} | ||
|
||
} // namespace sparse | ||
} // namespace phi | ||
|
||
PD_REGISTER_KERNEL(sparse_relu_grad, | ||
CPU, | ||
ALL_LAYOUT, | ||
phi::sparse::SparseReluGradKernel, | ||
float, | ||
double) { | ||
kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_COO); | ||
} | ||
|
||
#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP) | ||
PD_REGISTER_KERNEL(sparse_relu_grad, | ||
GPU, | ||
ALL_LAYOUT, | ||
phi::sparse::SparseReluGradKernel, | ||
float, | ||
double, | ||
phi::dtype::float16) { | ||
kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_COO); | ||
} | ||
#endif |
This file contains hidden or 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,29 @@ | ||
/* 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/core/sparse_coo_tensor.h" | ||
|
||
namespace phi { | ||
namespace sparse { | ||
|
||
template <typename T, typename Context> | ||
void SparseReluGradKernel(const Context& dev_ctx, | ||
const SparseCooTensor& x, | ||
const SparseCooTensor& out_grad, | ||
SparseCooTensor* x_grad); | ||
|
||
} // namespace sparse | ||
} // namespace phi |
This file contains hidden or 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,66 @@ | ||
/* 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/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" | ||
|
||
namespace phi { | ||
namespace sparse { | ||
|
||
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 | ||
|
||
PD_REGISTER_KERNEL(sparse_relu, | ||
CPU, | ||
ALL_LAYOUT, | ||
phi::sparse::SparseReluKernel, | ||
float, | ||
double) { | ||
kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_COO); | ||
} | ||
|
||
#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP) | ||
PD_REGISTER_KERNEL(sparse_relu, | ||
GPU, | ||
ALL_LAYOUT, | ||
phi::sparse::SparseReluKernel, | ||
float, | ||
double, | ||
phi::dtype::float16) { | ||
kernel->InputAt(0).SetDataLayout(phi::DataLayout::SPARSE_COO); | ||
} | ||
#endif |
This file contains hidden or 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,39 @@ | ||
/* 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/core/dense_tensor.h" | ||
#include "paddle/phi/core/sparse_coo_tensor.h" | ||
#include "paddle/phi/kernels/activation_kernel.h" | ||
#include "paddle/phi/kernels/empty_kernel.h" | ||
|
||
namespace phi { | ||
namespace sparse { | ||
|
||
template <typename T, typename Context> | ||
void SparseReluKernel(const Context& dev_ctx, | ||
const SparseCooTensor& x, | ||
SparseCooTensor* out); | ||
|
||
template <typename T, typename Context> | ||
SparseCooTensor SparseRelu(const Context& dev_ctx, const SparseCooTensor& x) { | ||
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. 在 C++ 层实现的是吗 ? 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. 是的,这里不能直接在前端通过SparseCooTensor的values熟悉来调用dense的relu的原因主要是:1. SparseCooTensor是在后端进行封装的,2. 动态图的autograd属性在API Tensor中,不在后端的DenseTensor里。 |
||
DenseTensor indices, values; | ||
SparseCooTensor coo(indices, values, x.dims()); | ||
SparseReluKernel<T, Context>(dev_ctx, x, &coo); | ||
return coo; | ||
} | ||
|
||
} // namespace sparse | ||
} // namespace phi |
This file contains hidden or 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
83 changes: 83 additions & 0 deletions
83
paddle/phi/tests/kernels/test_sparse_activation_dev_api.cc
This file contains hidden or 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,83 @@ | ||
/* 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 <gtest/gtest.h> | ||
#include <memory> | ||
|
||
#include "paddle/phi/backends/gpu/gpu_context.h" | ||
#include "paddle/phi/common/place.h" | ||
|
||
#include "paddle/fluid/memory/allocation/allocator_facade.h" | ||
#include "paddle/phi/api/lib/utils/allocator.h" | ||
#include "paddle/phi/core/kernel_registry.h" | ||
#include "paddle/phi/kernels/activation_grad_kernel.h" | ||
#include "paddle/phi/kernels/activation_kernel.h" | ||
#include "paddle/phi/kernels/empty_kernel.h" | ||
#include "paddle/phi/kernels/sparse/sparse_activation_grad_kernel.h" | ||
#include "paddle/phi/kernels/sparse/sparse_activation_kernel.h" | ||
#include "paddle/phi/kernels/sparse/sparse_utils_kernel.h" | ||
|
||
namespace phi { | ||
namespace tests { | ||
|
||
TEST(DEV_API, sparse_relu) { | ||
std::vector<float> data = {0, -1, 0, 2, 0, 0, -3, 0, 4, 5, 0, 0}; | ||
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()); | ||
dev_ctx_cpu.Init(); | ||
|
||
DenseTensor dense_x = | ||
phi::Empty(dev_ctx_cpu, | ||
DenseTensorMeta(DataType::FLOAT32, {3, 4}, DataLayout::NCHW)); | ||
memcpy(dense_x.data<float>(), data.data(), data.size() * sizeof(float)); | ||
auto sparse_coo = sparse::DenseToSparseCoo<float>(dev_ctx_cpu, dense_x, 2); | ||
|
||
auto sparse_out = sparse::SparseRelu<float>(dev_ctx_cpu, sparse_coo); | ||
DenseTensor dense_out = | ||
phi::EmptyLike<float>(dev_ctx_cpu, sparse_out.non_zero_elements()); | ||
ReluKernel<float>(dev_ctx_cpu, sparse_coo.non_zero_elements(), &dense_out); | ||
|
||
int cmp = memcmp(dense_out.data<float>(), | ||
sparse_out.non_zero_elements().data<float>(), | ||
dense_out.numel() * sizeof(float)); | ||
ASSERT_EQ(cmp, 0); | ||
// backward | ||
DenseTensor dense_grad_x = phi::EmptyLike<float>(dev_ctx_cpu, dense_out); | ||
ReluGradKernel<float>( | ||
dev_ctx_cpu, sparse_coo.non_zero_elements(), dense_out, &dense_grad_x); | ||
SparseCooTensor sparse_grad_x( | ||
phi::EmptyLike<int>(dev_ctx_cpu, sparse_coo.non_zero_indices()), | ||
phi::EmptyLike<int>(dev_ctx_cpu, sparse_coo.non_zero_elements()), | ||
{3, 4}); | ||
|
||
SparseCooTensor sparse_out_grad( | ||
sparse_coo.non_zero_indices(), dense_out, {3, 4}); | ||
sparse::SparseReluGradKernel<float>( | ||
dev_ctx_cpu, sparse_coo, sparse_out_grad, &sparse_grad_x); | ||
|
||
cmp = memcmp(dense_grad_x.data<float>(), | ||
sparse_grad_x.non_zero_elements().data<float>(), | ||
dense_grad_x.numel() * sizeof(float)); | ||
ASSERT_EQ(cmp, 0); | ||
} | ||
|
||
} // namespace tests | ||
} // namespace phi |
40 changes: 40 additions & 0 deletions
40
python/paddle/fluid/tests/unittests/test_sparse_activation_op.py
This file contains hidden or 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,40 @@ | ||
# 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. | ||
|
||
from __future__ import print_function | ||
import unittest | ||
import numpy as np | ||
import paddle | ||
from paddle import _C_ops | ||
from paddle.fluid.framework import _test_eager_guard | ||
|
||
|
||
class TestSparseActivation(unittest.TestCase): | ||
def test_sparse_relu(self): | ||
with _test_eager_guard(): | ||
x = [[0, -1, 0, 2], [0, 0, -3, 0], [4, 5, 0, 0]] | ||
dense_x = paddle.to_tensor(x, dtype='float32') | ||
dense_shape = [3, 4] | ||
stop_gradient = True | ||
sparse_dim = 2 | ||
sparse_coo_x = dense_x.to_sparse_coo(sparse_dim) | ||
#TODO(zhangkaihuo): change to test the corresponding API: paddle.sparse.relu(sparse_coo_x) | ||
sparse_act_out = _C_ops.final_state_sparse_relu(sparse_coo_x) | ||
correct_result = [0, 2, 0, 4, 5] | ||
actual_result = sparse_act_out.non_zero_elements().numpy() | ||
assert np.array_equal(correct_result, actual_result) | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |
This file contains hidden or 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 contains hidden or 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 contains hidden or 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
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.
这个文件命名是不是有点冗余,sparse目录下,还需要sparse_的前缀吗
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.
好的,我下一个PR里进行优化。