Skip to content
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

optimize elementwise_max_grad using new interfaces #37906

Merged
merged 41 commits into from
Jan 12, 2022
Merged
Changes from 15 commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
8f532b0
Merge pull request #1 from PaddlePaddle/develop
AshburnLee Sep 8, 2020
5b5804d
Merge pull request #2 from PaddlePaddle/develop
AshburnLee Sep 17, 2020
cee2470
Merge pull request #3 from PaddlePaddle/develop
AshburnLee Sep 30, 2020
5be3a45
Merge pull request #4 from PaddlePaddle/develop
AshburnLee Oct 13, 2020
a1d92b7
Merge pull request #5 from PaddlePaddle/develop
AshburnLee Oct 20, 2020
e674a5d
Merge pull request #6 from PaddlePaddle/develop
AshburnLee Nov 15, 2020
855d00b
Merge pull request #7 from PaddlePaddle/develop
AshburnLee Nov 18, 2020
7cb2c97
Merge pull request #8 from PaddlePaddle/develop
AshburnLee Mar 31, 2021
db9fc91
Merge pull request #9 from PaddlePaddle/develop
AshburnLee Apr 7, 2021
c7b68c8
Merge branch 'develop' of https://github.com/PaddlePaddle/paddle into…
AshburnLee Apr 26, 2021
0fd630e
Merge branch 'PaddlePaddle:develop' into develop
AshburnLee Aug 16, 2021
4bbb33b
Merge branch 'PaddlePaddle:develop' into develop
AshburnLee Sep 28, 2021
30a1a89
Merge branch 'PaddlePaddle:develop' into develop
AshburnLee Nov 22, 2021
b4a51ab
init elem_max_grad op
AshburnLee Dec 7, 2021
835d492
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
AshburnLee Dec 7, 2021
391d533
optimize code and reply review comments
AshburnLee Dec 10, 2021
ab9af16
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
AshburnLee Dec 10, 2021
504043e
ternary functors
AshburnLee Dec 16, 2021
819e195
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
AshburnLee Dec 16, 2021
051e44d
apply new reduce func
AshburnLee Dec 17, 2021
92016a4
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
AshburnLee Dec 17, 2021
430a6f8
resolved confict
AshburnLee Dec 17, 2021
ca893de
move functor to .h
AshburnLee Dec 17, 2021
2badce0
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
AshburnLee Dec 17, 2021
b768ef7
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
AshburnLee Dec 30, 2021
916217b
multi-outputs init
AshburnLee Dec 30, 2021
0fcc1d1
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
AshburnLee Dec 30, 2021
2cadeb0
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
AshburnLee Jan 5, 2022
e7465f0
rearrange code
AshburnLee Jan 6, 2022
69f0cfd
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
AshburnLee Jan 6, 2022
0999b4f
modifed functors
AshburnLee Jan 6, 2022
c401bdb
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
AshburnLee Jan 6, 2022
815c9c0
optimizer code
AshburnLee Jan 7, 2022
3730bad
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
AshburnLee Jan 7, 2022
3faf253
pass nullptr
AshburnLee Jan 7, 2022
a8464e4
revert the last change as seg fault occurs
AshburnLee Jan 7, 2022
a910426
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
AshburnLee Jan 7, 2022
c618711
optimize code
AshburnLee Jan 7, 2022
817d00d
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
AshburnLee Jan 12, 2022
6ab80a3
remove inplace
AshburnLee Jan 12, 2022
4bfe7aa
remove comments
AshburnLee Jan 12, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
116 changes: 116 additions & 0 deletions paddle/fluid/operators/elementwise/elementwise_max_op.cu
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ See the License for the specific language governing permissions and
limitations under the License. */

#include "paddle/fluid/operators/elementwise/elementwise_max_op.h"
#include "paddle/fluid/operators/elementwise/elementwise_mul_op.h"
#include "paddle/fluid/operators/elementwise/elementwise_op_broadcast.cu.h"
#include "paddle/fluid/operators/reduce_ops/reduce_functor_op.h"
#include "paddle/fluid/operators/reduce_ops/reduce_op.cu.h"

namespace paddle {
namespace operators {
Expand All @@ -34,6 +37,119 @@ class ElementwiseMaxKernel<platform::CUDADeviceContext, T>
}
};

template <typename T>
struct GreaterThanTFunctor {
AshburnLee marked this conversation as resolved.
Show resolved Hide resolved
inline HOSTDEVICE T operator()(const T& a, const T& b) const {
return a > b ? static_cast<T>(1) : static_cast<T>(0);
}
};
template <typename T>
struct LessEqualThanTFunctor {
AshburnLee marked this conversation as resolved.
Show resolved Hide resolved
inline HOSTDEVICE T operator()(const T& a, const T& b) const {
return (a < b || a == b) ? static_cast<T>(1) : static_cast<T>(0);
}
};

template <typename DeviceContext, typename T>
void DefaultElementMaxGrad(const framework::ExecutionContext& ctx,
const framework::Tensor* x,
const framework::Tensor* y,
const framework::Tensor* out,
const framework::Tensor* dout, framework::Tensor* dx,
framework::Tensor* dy) {
int axis = ctx.Attr<int>("axis");
const auto& cuda_ctx =
AshburnLee marked this conversation as resolved.
Show resolved Hide resolved
ctx.template device_context<platform::CUDADeviceContext>();
// dx
if (dx != nullptr) {
if (dx->dims() == dout->dims()) {
// dx = dout * (x > y)
framework::Tensor x_y_compare;
AshburnLee marked this conversation as resolved.
Show resolved Hide resolved
x_y_compare.mutable_data<T>(dout->dims(), ctx.GetPlace());

std::vector<const framework::Tensor*> ins = {x, y};
std::vector<framework::Tensor*> outs = {&x_y_compare};
LaunchElementwiseCudaKernel<ElementwiseType::kBinary, T, T>(
cuda_ctx, ins, &outs, axis, GreaterThanTFunctor<T>());
default_elementwise_mul<DeviceContext, T>(ctx, dout, &x_y_compare, dx);
} else {
// For inplace strategy, dx will be stored in addr of dout, which makes
// the result of dy wrong.
if (dx->IsSharedBufferWith(*dout)) {
AshburnLee marked this conversation as resolved.
Show resolved Hide resolved
dx->clear();
dx->mutable_data<T>(x->dims(), ctx.GetPlace());
}
std::vector<int> reduce_dims = GetReduceDim(x->dims(), out->dims(), axis);
gpuStream_t stream = ctx.cuda_device_context().stream();

framework::Tensor dx_tmp;
AshburnLee marked this conversation as resolved.
Show resolved Hide resolved
AshburnLee marked this conversation as resolved.
Show resolved Hide resolved
dx_tmp.Resize(dout->dims());
framework::Tensor x_y_compare;
x_y_compare.mutable_data<T>(dout->dims(), ctx.GetPlace());

std::vector<const framework::Tensor*> ins = {x, y};
std::vector<framework::Tensor*> outs = {&x_y_compare};
LaunchElementwiseCudaKernel<ElementwiseType::kBinary, T, T>(
cuda_ctx, ins, &outs, axis, GreaterThanTFunctor<T>());
// dx(dx_tmp)=dout * y(x_y_compare)
default_elementwise_mul<DeviceContext, T>(ctx, dout, &x_y_compare,
&dx_tmp);
TensorReduceFunctorImpl<T, T, CustomSum>(dx_tmp, dx, reduce_dims, stream);
}
}
// dy
if (dy != nullptr) {
if (dy->dims() == dout->dims()) {
// dy = dout * (x <= y)
framework::Tensor x_y_compare;
x_y_compare.mutable_data<T>(dout->dims(), ctx.GetPlace());

std::vector<const framework::Tensor*> ins = {x, y};
std::vector<framework::Tensor*> outs = {&x_y_compare};
LaunchElementwiseCudaKernel<ElementwiseType::kBinary, T, T>(
cuda_ctx, ins, &outs, axis, LessEqualThanTFunctor<T>());

default_elementwise_mul<DeviceContext, T>(ctx, dout, &x_y_compare, dy);
} else {
std::vector<int> reduce_dims = GetReduceDim(y->dims(), out->dims(), axis);
gpuStream_t stream = ctx.cuda_device_context().stream();

framework::Tensor dy_tmp;
AshburnLee marked this conversation as resolved.
Show resolved Hide resolved
dy_tmp.Resize(dout->dims());
framework::Tensor x_y_compare;
x_y_compare.mutable_data<T>(dout->dims(), ctx.GetPlace());

std::vector<const framework::Tensor*> ins = {x, y};
std::vector<framework::Tensor*> outs = {&x_y_compare};
LaunchElementwiseCudaKernel<ElementwiseType::kBinary, T, T>(
cuda_ctx, ins, &outs, axis, LessEqualThanTFunctor<T>());

// dy(dy_tmp)=dout * x(x_y_compare)
default_elementwise_mul<DeviceContext, T>(ctx, dout, &x_y_compare,
&dy_tmp);
TensorReduceFunctorImpl<T, T, CustomSum>(dy_tmp, dy, reduce_dims, stream);
}
}
}

template <typename T>
class ElementwiseMaxGradKernel<platform::CUDADeviceContext, T>
: public framework::OpKernel<T> {
public:
void Compute(const framework::ExecutionContext& ctx) const override {
using Tensor = framework::Tensor;

auto* x = ctx.Input<Tensor>("X");
auto* y = ctx.Input<Tensor>("Y");
auto* dout = ctx.Input<Tensor>(framework::GradVarName("Out"));
auto* dx = ctx.Output<Tensor>(framework::GradVarName("X"));
auto* dy = ctx.Output<Tensor>(framework::GradVarName("Y"));
auto* out = dout; // Fake out, not used
AshburnLee marked this conversation as resolved.
Show resolved Hide resolved
DefaultElementMaxGrad<platform::CUDADeviceContext, T>(ctx, x, y, out, dout,
AshburnLee marked this conversation as resolved.
Show resolved Hide resolved
dx, dy);
}
};

} // namespace operators
} // namespace paddle

Expand Down