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

[Prim][PIR] elu op forward decomp #62255

Merged
merged 4 commits into from
Mar 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"batch_norm",
"batch_norm_",
"dropout",
"elu",
"embedding",
"flatten",
"full_like",
Expand Down Expand Up @@ -58,6 +59,7 @@
decomp_interface_implementation_gen_op_list = [
"add_n",
"dropout",
"elu",
"embedding",
"flatten",
"full_like",
Expand Down
20 changes: 20 additions & 0 deletions paddle/fluid/primitive/composite/composite.h
Original file line number Diff line number Diff line change
Expand Up @@ -1043,6 +1043,26 @@ Tensor index_sample_decomp(const Tensor& x, const Tensor& index) {
}
}

template <typename T>
Tensor elu_decomp(const Tensor& x, const float alpha) {
auto org_dtype = x.dtype();
auto x_cast = x;

bool need_cast = is_half_dtype(org_dtype);
if (need_cast) {
x_cast = cast<T>(x, DataType::FLOAT32);
}

const Tensor zero = full<T>(x_cast.shape(), 0, x_cast.type());
auto tmp_res = alpha * (exp<T>(x_cast) - 1);
auto ans = where<T>(x_cast > zero, x_cast, tmp_res);
if (need_cast) {
return cast<T>(ans, org_dtype);
} else {
return ans;
}
}

} // namespace details

} // namespace primitive
Expand Down
17 changes: 14 additions & 3 deletions test/legacy_test/test_activation_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -3443,6 +3443,8 @@ def setUp(self):
self.init_dtype()
self.init_shape()
self.python_api = paddle.nn.functional.elu
self.prim_op_type = "comp"
self.public_python_api = paddle.nn.functional.elu

np.random.seed(1024)
x = np.random.uniform(-3, 3, self.shape).astype(self.dtype)
Expand All @@ -3463,7 +3465,16 @@ def test_check_grad(self):
if self.dtype == np.float16:
return
self.check_grad(
['X'], 'Out', check_pir=True, check_pir_onednn=self.check_pir_onednn
['X'],
'Out',
check_pir=True,
check_prim_pir=True,
check_pir_onednn=self.check_pir_onednn,
)

def test_check_output(self):
self.check_output(
check_prim_pir=True, check_pir_onednn=self.check_pir_onednn
)

def get_alpha(self):
Expand Down Expand Up @@ -5320,7 +5331,7 @@ def test_check_grad(self):
create_test_act_fp16_class(TestBRelu, check_pir=True)
create_test_act_fp16_class(TestRelu6)
create_test_act_fp16_class(TestSoftRelu, check_dygraph=False)
create_test_act_fp16_class(TestELU, check_pir=True)
create_test_act_fp16_class(TestELU, check_pir=True, check_prim_pir=True)
create_test_act_fp16_class(TestCELU, check_pir=True)
create_test_act_fp16_class(TestReciprocal, check_pir=True)
create_test_act_fp16_class(TestLog, check_prim=True, check_pir=True)
Expand Down Expand Up @@ -5492,7 +5503,7 @@ def test_check_grad(self):
create_test_act_bf16_class(TestBRelu, check_pir=True)
create_test_act_bf16_class(TestRelu6)
create_test_act_bf16_class(TestSoftRelu, check_dygraph=False)
create_test_act_bf16_class(TestELU, check_pir=True)
create_test_act_bf16_class(TestELU, check_pir=True, check_prim_pir=True)
create_test_act_bf16_class(TestCELU, check_pir=True)
create_test_act_bf16_class(TestReciprocal, check_pir=True)
create_test_act_bf16_class(TestLog, check_prim=True, check_pir=True)
Expand Down