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 prelu alpha grad #7600

Merged
merged 9 commits into from
Feb 26, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
8 changes: 7 additions & 1 deletion oneflow/core/functional/impl/activation_functor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,13 @@ class PReluGradFunctor {
}
Maybe<TensorTuple> operator()(const std::shared_ptr<Tensor>& dy, const std::shared_ptr<Tensor>& x,
const std::shared_ptr<Tensor>& alpha) const {
return OpInterpUtil::Dispatch<one::TensorTuple>(*op_, {dy, x, alpha});
MutableAttrMap attrs;
if(alpha->requires_grad()){
JUST(attrs.SetAttr<bool>("alpha_requires_grad", true));
}else{
JUST(attrs.SetAttr<bool>("alpha_requires_grad", false));
}
return OpInterpUtil::Dispatch<one::TensorTuple>(*op_, {dy, x, alpha}, attrs);
}

private:
Expand Down
3 changes: 3 additions & 0 deletions oneflow/ir/include/OneFlow/OneFlowUserOps.td
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,9 @@ def OneFlow_PreluOp : OneFlow_BaseOp<"prelu", [NoSideEffect, DeclareOpInterfaceM
let output = (outs
OneFlow_Tensor:$y
);
let attrs = (ins
DefaultValuedAttr<BoolAttr, "false">:$alpha_requires_grad
);
let has_logical_tensor_desc_infer_fn = 1;
let has_physical_tensor_desc_infer_fn = 1;
let has_get_sbp_fn = 1;
Expand Down
5 changes: 4 additions & 1 deletion oneflow/user/kernels/prelu_kernel.cu
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,7 @@ class GpuPReluGradKernel final : public user_op::OpKernel {
user_op::Tensor* dx = ctx->Tensor4ArgNameAndIndex("dx", 0);
user_op::Tensor* alpha_diff = ctx->Tensor4ArgNameAndIndex("alpha_diff", 0);
user_op::Tensor* tmp_buffer = ctx->Tensor4ArgNameAndIndex("tmp_buffer", 0);
const bool alpha_requires_grad = ctx->Attr<bool>("alpha_requires_grad");
const int32_t elem_cnt = x->shape().elem_cnt();
T* broadcasted_alpha_diff = tmp_buffer->mut_dptr<T>();
T* reduce_sum_tmp_buf = reinterpret_cast<T*>(tmp_buffer->mut_dptr<char>()
Expand All @@ -400,10 +401,12 @@ class GpuPReluGradKernel final : public user_op::OpKernel {
alpha->dptr<T>(), dy->dptr<T>(), dx->mut_dptr<T>(),
broadcasted_alpha_diff);
Copy link
Contributor

Choose a reason for hiding this comment

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

broadcasted_alpha_diff 也不应该算,可以在cuda kernel中处理一下不写broadcasted_alpha_diff的情况

Copy link
Contributor Author

Choose a reason for hiding this comment

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

好的

Copy link
Contributor Author

Choose a reason for hiding this comment

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

已修改

}
NdarrayUtil<DeviceType::kCUDA, T>::ReduceSum(
if(alpha_requires_grad){
Copy link
Contributor

Choose a reason for hiding this comment

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

这个if应该放到386行,并且如果alpha不需要grad的话,底下不需要申请tmp_buffer

NdarrayUtil<DeviceType::kCUDA, T>::ReduceSum(
ctx->stream(), XpuVarNdarray<T>(left_extended_shape, alpha_diff->mut_dptr<T>()),
XpuVarNdarray<const T>(x->shape(), broadcasted_alpha_diff),
XpuVarNdarray<T>(x->shape(), reduce_sum_tmp_buf));
}
}
bool AlwaysComputeWhenAllOutputsEmpty() const override { return false; }
};
Expand Down
1 change: 1 addition & 0 deletions oneflow/user/ops/prelu_op.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ REGISTER_USER_OP_GRAD("prelu").SetGenBackwardOpConfFn([](const user_op::UserOpWr
.Input("x", op.input("x", 0))
.Input("dy", op.GetGradTensorWithOpOutput("y", 0))
.Input("alpha", op.input("alpha", 0))
.Attr("alpha_requires_grad", op.attr<bool>("alpha_requires_grad"))
.Output("dx")
.Output("alpha_diff")
.Build();
Expand Down