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
Show file tree
Hide file tree
Changes from all 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions paddle/fluid/operators/elementwise/elementwise_functor.h
Original file line number Diff line number Diff line change
Expand Up @@ -275,5 +275,32 @@ struct MulGradXYFunctor<Complex<InT>, Complex<OutT>> {
}
};

// Ternary compare
template <typename T>
struct MaxGradXFunctor {
inline HOSTDEVICE T operator()(const T& x, const T& y, const T& dout) const {
return dout * static_cast<T>(x > y);
}
};
template <typename T>
struct MaxGradYFunctor {
inline HOSTDEVICE T operator()(const T& x, const T& y, const T& dout) const {
return dout * static_cast<T>(x <= y);
}
};

template <typename InT, typename OutT>
struct MaxGradXYFunctor {
inline HOSTDEVICE paddle::framework::Array<OutT, 2> operator()(
const InT& x, const InT& y, const InT& dout) {
paddle::framework::Array<OutT, 2> outs;
// dx = dout * (x > y)
outs[0] = static_cast<OutT>(dout * static_cast<InT>(x > y));
// dy = dout * (x <= y)
outs[1] = static_cast<OutT>(dout * static_cast<InT>(x <= y));
return outs;
AshburnLee marked this conversation as resolved.
Show resolved Hide resolved
}
};

} // namespace operators
} // namespace paddle
30 changes: 28 additions & 2 deletions paddle/fluid/operators/elementwise/elementwise_max_op.cu
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,41 @@ class ElementwiseMaxKernel<platform::CUDADeviceContext, T>
void Compute(const framework::ExecutionContext& ctx) const override {
std::vector<const framework::Tensor*> ins;
std::vector<framework::Tensor*> outs;
const auto& cuda_ctx =
const auto& dev_ctx =
ctx.template device_context<platform::CUDADeviceContext>();

int axis = PackTensorsIntoVector<T>(ctx, &ins, &outs);
LaunchElementwiseCudaKernel<ElementwiseType::kBinary, T, T>(
cuda_ctx, ins, &outs, axis, MaxFunctor<T>());
dev_ctx, ins, &outs, axis, MaxFunctor<T>());
}
};

template <typename DeviceContext, typename T>
typename std::enable_if<
std::is_same<DeviceContext, platform::CUDADeviceContext>::value>::type
ElementwiseMaxGrad(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& dev_ctx =
ctx.template device_context<platform::CUDADeviceContext>();
const auto place = ctx.GetPlace();
if (dx != nullptr && dy != nullptr) {
std::vector<const framework::Tensor*> ins = {x, y, dout};
GetGradXAndYOut<ElementwiseType::kTernary, T>(
dev_ctx, place, axis, ins, dout, dx, dy, MaxGradXYFunctor<T, T>());
} else if (dx != nullptr && dy == nullptr) {
std::vector<const framework::Tensor*> ins = {x, y, dout};
GetGradXOrYOut<ElementwiseType::kTernary, T>(
dev_ctx, place, axis, ins, dout, dx, MaxGradXFunctor<T>());
} else if (dx == nullptr && dy != nullptr) {
std::vector<const framework::Tensor*> ins = {x, y, dout};
GetGradXOrYOut<ElementwiseType::kTernary, T>(
dev_ctx, place, axis, ins, dout, dy, MaxGradYFunctor<T>());
}
}

} // namespace operators
} // namespace paddle

Expand Down
29 changes: 25 additions & 4 deletions paddle/fluid/operators/elementwise/elementwise_max_op.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,28 @@ struct MaxGradDy {
}
};

template <typename DeviceContext, typename T>
typename std::enable_if<
std::is_same<DeviceContext, platform::CPUDeviceContext>::value>::type
ElementwiseMaxGrad(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");
ElemwiseGradCompute<DeviceContext, T, MaxGradDx<T>, MaxGradDy<T>>(
ctx, *x, *y, *out, *dout, axis, dx, dy, MaxGradDx<T>(), MaxGradDy<T>());
}

#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)
template <typename DeviceContext, typename T>
typename std::enable_if<
std::is_same<DeviceContext, platform::CUDADeviceContext>::value>::type
ElementwiseMaxGrad(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);
#endif

template <typename DeviceContext, typename T>
class ElementwiseMaxGradKernel : public ElemwiseGradKernel<T> {
public:
Expand All @@ -74,12 +96,11 @@ class ElementwiseMaxGradKernel : public ElemwiseGradKernel<T> {
auto* x = ctx.Input<Tensor>("X");
auto* y = ctx.Input<Tensor>("Y");
auto* dout = ctx.Input<Tensor>(framework::GradVarName("Out"));
auto* out = dout; // out is not necessary
auto* dx = ctx.Output<Tensor>(framework::GradVarName("X"));
auto* dy = ctx.Output<Tensor>(framework::GradVarName("Y"));
auto* out = dout; // Fake out, not used
int axis = ctx.Attr<int>("axis");
ElemwiseGradCompute<DeviceContext, T, MaxGradDx<T>, MaxGradDy<T>>(
ctx, *x, *y, *out, *dout, axis, dx, dy, MaxGradDx<T>(), MaxGradDy<T>());

ElementwiseMaxGrad<DeviceContext, T>(ctx, x, y, out, dout, dx, dy);
}
};

Expand Down