Skip to content

Commit

Permalink
unified softplus kernel
Browse files Browse the repository at this point in the history
  • Loading branch information
jakpiase committed Oct 13, 2021
1 parent 0377970 commit be4e56c
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 11 deletions.
9 changes: 1 addition & 8 deletions paddle/fluid/operators/mkldnn/activation_mkldnn_op.cc
Original file line number Diff line number Diff line change
Expand Up @@ -173,14 +173,7 @@ struct GeluMKLDNNGradFunctor : public BaseActivationFunctor<T> {
template <typename T>
struct SoftplusMKLDNNFunctor : public BaseActivationFunctor<T> {
void operator()(const framework::ExecutionContext &ctx) const {
const float beta = ctx.Attr<float>("beta");
// if beta is equal to 1.0f then we can simply use oneDNN's soft_relu but if
// it has other value, we have to fuse binary + eltwise + binary
if (beta == 1.0f) {
eltwise_forward<T>(ctx, mkldnn::algorithm::eltwise_soft_relu);
} else {
custom_softplus_eltwise_forward<T>(ctx);
}
custom_softplus_eltwise_forward<T>(ctx);
}
};

Expand Down
9 changes: 6 additions & 3 deletions paddle/fluid/operators/mkldnn/softplus_mkldnn_op.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,11 @@ class SoftplusMKLDNNHandler
dnnl::post_ops post_ops;
post_ops.append_eltwise(1.0f, dnnl::algorithm::eltwise_soft_relu, 0.0f,
0.0f);
post_ops.append_binary(dnnl::algorithm::binary_div, beta_md);
if (beta != 1.0f) {
post_ops.append_eltwise(1.0f, dnnl::algorithm::eltwise_linear,
1.0f / beta, 0.0f);
}

dnnl::primitive_attr attrs;
attrs.set_post_ops(post_ops);

Expand Down Expand Up @@ -78,8 +82,7 @@ void custom_softplus_eltwise_forward(const framework::ExecutionContext& ctx) {
const std::unordered_map<int, dnnl::memory> args = {
{DNNL_ARG_SRC_0, *src_memory_p},
{DNNL_ARG_SRC_1, *beta_memory_p},
{DNNL_ARG_DST, *dst_memory_p},
{DNNL_ARG_ATTR_MULTIPLE_POST_OP(1) | DNNL_ARG_SRC_1, *beta_memory_p}};
{DNNL_ARG_DST, *dst_memory_p}};

binary_p->execute(astream, args);
astream.wait();
Expand Down

1 comment on commit be4e56c

@paddle-bot-old
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Congratulation! Your pull request passed all required CI. You could ask reviewer(s) to approve and merge. 🎉

Please sign in to comment.