-
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
Optimize performance of log_softmax #38992
Merged
Merged
Changes from 13 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
2125c83
Optimize performance of log_softmax
ZzSean c894654
delete unity build
ZzSean ec486b6
Merge branch 'develop' into opt_logsoftmax
ZzSean d70d0f0
Merge branch 'develop' into opt_logsoftmax
ZzSean 75155b2
Merge branch 'develop' into opt_logsoftmax
ZzSean 71c2bfc
Merge branch 'develop' into opt_logsoftmax
ZzSean 94e1cc8
Merge branch 'develop' into opt_logsoftmax
ZzSean 256500d
Merge branch 'develop' into opt_logsoftmax
ZzSean e80dd81
Merge branch 'develop' into opt_logsoftmax
ZzSean 5d7bce2
modify to phi
ZzSean bd91c76
fix
ZzSean bc56505
fixfixfixfix
ZzSean a1c358e
fix
ZzSean 4329528
fix
ZzSean 1b24a8e
fix
ZzSean f615517
fix
ZzSean 684ec04
Merge branch 'develop' into opt_logsoftmax
ZzSean 38bebf2
simplify
ZzSean 9b88e2f
fix
ZzSean 35cabf1
fix enforce
ZzSean 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 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 |
---|---|---|
|
@@ -18,10 +18,13 @@ | |
#include "paddle/phi/common/amp_type_traits.h" | ||
#include "paddle/phi/kernels/funcs/elementwise_functor.h" | ||
#include "paddle/phi/kernels/funcs/functors.h" | ||
#include "paddle/phi/kernels/gpudnn/softmax_gpudnn.h" | ||
|
||
namespace paddle { | ||
namespace operators { | ||
|
||
using Tensor = framework::Tensor; | ||
|
||
#define LAUNCH_WARP_FORWAR_COMPUTE(near_greater_power_of_two) \ | ||
case near_greater_power_of_two: \ | ||
ComputeLogSoftmaxForwardInWarp< \ | ||
|
@@ -468,6 +471,36 @@ class LogSoftmaxGradKernel<platform::CUDADeviceContext, T> | |
} | ||
}; | ||
|
||
template <typename T> | ||
class LogSoftmaxCUDNNKernel : public framework::OpKernel<T> { | ||
public: | ||
void Compute(const framework::ExecutionContext &ctx) const override { | ||
auto *x = ctx.Input<Tensor>("X"); | ||
auto *out = ctx.Output<Tensor>("Out"); | ||
out->mutable_data<T>(ctx.GetPlace()); | ||
|
||
int input_axis = ctx.Attr<int>("axis"); | ||
auto &dev_ctx = ctx.template device_context<platform::CUDADeviceContext>(); | ||
phi::SoftmaxForwardCUDAKernelDriver<T, true>(dev_ctx, *x, input_axis, out); | ||
} | ||
}; | ||
|
||
template <typename T> | ||
class LogSoftmaxGradCUDNNKernel : public framework::OpKernel<T> { | ||
public: | ||
void Compute(const framework::ExecutionContext &ctx) const override { | ||
auto *out = ctx.Input<Tensor>("Out"); | ||
auto *dout = ctx.Input<Tensor>(framework::GradVarName("Out")); | ||
auto *dx = ctx.Output<Tensor>(framework::GradVarName("X")); | ||
dx->mutable_data<T>(ctx.GetPlace()); | ||
|
||
int input_axis = ctx.Attr<int>("axis"); | ||
auto &dev_ctx = ctx.template device_context<platform::CUDADeviceContext>(); | ||
phi::SoftmaxBackwardCUDAKernelDriver<T, true>(dev_ctx, *out, *dout, | ||
input_axis, dx); | ||
} | ||
}; | ||
|
||
} // namespace operators | ||
} // namespace paddle | ||
|
||
|
@@ -483,3 +516,35 @@ REGISTER_OP_CUDA_KERNEL( | |
ops::LogSoftmaxGradKernel<plat::CUDADeviceContext, double>, | ||
ops::LogSoftmaxGradKernel<plat::CUDADeviceContext, plat::float16>, | ||
ops::LogSoftmaxGradKernel<plat::CUDADeviceContext, plat::bfloat16>); | ||
#ifdef PADDLE_WITH_HIP | ||
REGISTER_OP_KERNEL(log_softmax, CUDNN, plat::CUDAPlace, | ||
ops::LogSoftmaxCUDNNKernel<float>, | ||
ops::LogSoftmaxCUDNNKernel<plat::float16>, | ||
ops::LogSoftmaxCUDNNKernel<plat::bfloat16>); | ||
REGISTER_OP_KERNEL(log_softmax_grad, CUDNN, plat::CUDAPlace, | ||
ops::LogSoftmaxGradCUDNNKernel<float>, | ||
ops::LogSoftmaxGradCUDNNKernel<plat::float16>, | ||
ops::LogSoftmaxGradCUDNNKernel<plat::bfloat16>); | ||
#else | ||
#if CUDNN_VERSION_MIN(8, 1, 0) | ||
REGISTER_OP_KERNEL(log_softmax, CUDNN, plat::CUDAPlace, | ||
ops::LogSoftmaxCUDNNKernel<float>, | ||
ops::LogSoftmaxCUDNNKernel<double>, | ||
ops::LogSoftmaxCUDNNKernel<plat::float16>, | ||
ops::LogSoftmaxCUDNNKernel<plat::bfloat16>); | ||
REGISTER_OP_KERNEL(log_softmax_grad, CUDNN, plat::CUDAPlace, | ||
ops::LogSoftmaxGradCUDNNKernel<float>, | ||
ops::LogSoftmaxGradCUDNNKernel<double>, | ||
ops::LogSoftmaxGradCUDNNKernel<plat::float16>, | ||
ops::LogSoftmaxGradCUDNNKernel<plat::bfloat16>); | ||
#else | ||
REGISTER_OP_KERNEL(log_softmax, CUDNN, plat::CUDAPlace, | ||
ops::LogSoftmaxCUDNNKernel<float>, | ||
ops::LogSoftmaxCUDNNKernel<double>, | ||
ops::LogSoftmaxCUDNNKernel<plat::float16>); | ||
REGISTER_OP_KERNEL(log_softmax_grad, CUDNN, plat::CUDAPlace, | ||
ops::LogSoftmaxGradCUDNNKernel<float>, | ||
ops::LogSoftmaxGradCUDNNKernel<double>, | ||
ops::LogSoftmaxGradCUDNNKernel<plat::float16>); | ||
#endif | ||
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. 这三部分注册可以通过可变参数宏优化。 |
||
#endif |
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 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 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
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.
不建议新增属性,直接改原CUDA Kernel吧。
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.
done,thx