Skip to content

Commit 8a05925

Browse files
authored
Revert "Added sigmoid BF16 FWD/BWD kernels and gelu BF16 BWD kernel (PaddlePaddle#34216)"
This reverts commit 5d3c89c.
1 parent 609f822 commit 8a05925

3 files changed

Lines changed: 29 additions & 73 deletions

File tree

paddle/fluid/operators/mkldnn/activation_mkldnn_op.cc

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -251,22 +251,19 @@ namespace ops = paddle::operators;
251251
ops::MKLDNNActivationKernel<ops::functor<paddle::platform::bfloat16>>); \
252252
REGISTER_OP_KERNEL( \
253253
act_type##_grad, MKLDNN, ::paddle::platform::CPUPlace, \
254-
ops::MKLDNNActivationGradKernel<ops::grad_functor<float>>, \
255-
ops::MKLDNNActivationGradKernel< \
256-
ops::grad_functor<paddle::platform::bfloat16>>);
254+
ops::MKLDNNActivationGradKernel<ops::grad_functor<float>>);
257255

258256
#define FOR_EACH_MKLDNN_KERNEL_FUNCTOR(__macro) \
259257
__macro(relu, ReluMKLDNNFunctor, ReluMKLDNNGradFunctor); \
260258
__macro(relu6, Relu6MKLDNNFunctor, Relu6MKLDNNGradFunctor); \
261259
__macro(leaky_relu, ReluMKLDNNFunctor, ReluMKLDNNGradFunctor); \
262260
__macro(swish, SwishMKLDNNFunctor, SwishMKLDNNGradFunctor); \
263261
__macro(hardswish, HardSwishMKLDNNFunctor, HardSwishMKLDNNGradFunctor); \
262+
__macro(sigmoid, SigmoidMKLDNNFunctor, SigmoidMKLDNNGradFunctor); \
264263
__macro(tanh, TanhMKLDNNFunctor, TanhMKLDNNGradFunctor); \
265264
__macro(sqrt, SqrtMKLDNNFunctor, SqrtMKLDNNGradFunctor); \
266265
__macro(abs, AbsMKLDNNFunctor, AbsMKLDNNGradFunctor);
267266

268267
FOR_EACH_MKLDNN_KERNEL_FUNCTOR(REGISTER_ACTIVATION_MKLDNN_KERNEL);
269268
REGISTER_ACTIVATION_MKLDNN_BF16_KERNEL(gelu, GeluMKLDNNFunctor,
270269
GeluMKLDNNGradFunctor);
271-
REGISTER_ACTIVATION_MKLDNN_BF16_KERNEL(sigmoid, SigmoidMKLDNNFunctor,
272-
SigmoidMKLDNNGradFunctor);

python/paddle/fluid/tests/unittests/mkldnn/test_activation_mkldnn_op.py

Lines changed: 25 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616

1717
import unittest
1818
import numpy as np
19-
from scipy.special import expit, erf
19+
from scipy.special import expit
2020
import paddle.fluid.core as core
21-
from paddle.fluid.tests.unittests.op_test import OpTest, OpTestTool, convert_float_to_uint16
21+
from paddle.fluid.tests.unittests.op_test import OpTest, convert_float_to_uint16
2222
from paddle.fluid.tests.unittests.test_activation_op import TestActivation, TestRelu, TestTanh, TestSqrt, TestAbs, TestLeakyRelu, TestSwish, TestHardSwish, TestRelu6, TestSigmoid
2323
from paddle.fluid.tests.unittests.test_gelu_op import gelu
2424
from mkldnn_op_test import check_if_mkldnn_primitives_exist_in_bwd
@@ -79,88 +79,46 @@ def setUp(self):
7979
self.attrs = {"use_mkldnn": True, "approximate": True}
8080

8181

82-
#Use it as a base class for BF16 activation tests, just override necessary functions
83-
class TestMKLDNNSigmoidBF16Op(TestActivation):
84-
@OpTestTool.skip_if_not_cpu_bf16()
85-
def config(self):
86-
self.op_type = "sigmoid"
87-
88-
def op_forward(self, x):
89-
return 1 / (1 + np.exp(-x))
90-
91-
def op_grad(self, dout, x):
92-
return dout * self.op_forward(x) * (1 - self.op_forward(x))
93-
94-
def set_attrs(self):
95-
self.attrs = {"use_mkldnn": True}
96-
97-
def init_data(self):
98-
self.x = np.random.uniform(-1, 1, [2, 4, 3, 5]).astype(np.float32)
99-
82+
@unittest.skipIf(not core.supports_bfloat16(),
83+
"place does not support BF16 evaluation")
84+
class TestMKLDNNGeluBf16Dim2(TestActivation):
10085
def setUp(self):
86+
self.op_type = "gelu"
10187
self.dtype = np.uint16
102-
self.init_data()
103-
self.config()
104-
self.out = self.op_forward(self.x)
10588

106-
self.inputs = {'X': convert_float_to_uint16(self.x)}
107-
self.outputs = {'Out': self.out}
108-
self.set_attrs()
89+
x = np.random.uniform(-1, 1, [11, 17]).astype(np.float32)
90+
out = convert_float_to_uint16(gelu(x, False))
10991

110-
def calculate_grads(self):
111-
self.dx = self.op_grad(self.out, self.x)
92+
self.inputs = {'X': convert_float_to_uint16(x)}
93+
self.outputs = {'Out': out}
94+
self.attrs = {"use_mkldnn": True}
11295

11396
def test_check_output(self):
11497
self.check_output_with_place(core.CPUPlace())
11598

11699
def test_check_grad(self):
117-
self.calculate_grads()
118-
self.check_grad_with_place(
119-
core.CPUPlace(), ["X"],
120-
"Out",
121-
user_defined_grads=[self.dx],
122-
user_defined_grad_outputs=[convert_float_to_uint16(self.out)])
123-
124-
125-
class TestMKLDNNGeluErfBF16Op(TestMKLDNNSigmoidBF16Op):
126-
def config(self):
127-
self.op_type = "gelu"
128-
129-
def op_forward(self, x):
130-
return gelu(x, False)
131-
132-
def op_grad(self, dout, x):
133-
return (dout *
134-
(0.5 + 0.5 * erf(x / np.sqrt(2)) +
135-
(x / np.sqrt(2 * np.pi) * np.exp(-0.5 * np.power(x, 2)))))
136-
137-
138-
class TestMKLDNNGeluErfDim2BF16Op(TestMKLDNNGeluErfBF16Op):
139-
def init_data(self):
140-
self.x = np.random.uniform(-1, 1, [11, 17]).astype(np.float32)
100+
pass
141101

142102

143-
class TestMKLDNNGeluTanhBF16Op(TestMKLDNNSigmoidBF16Op):
144-
def config(self):
103+
@unittest.skipIf(not core.supports_bfloat16(),
104+
"place does not support BF16 evaluation")
105+
class TestMKLDNNGeluBf16Dim2Approx(TestActivation):
106+
def setUp(self):
145107
self.op_type = "gelu"
108+
self.dtype = np.uint16
146109

147-
def op_forward(self, x):
148-
return gelu(x, True)
149-
150-
def op_grad(self, dout, x):
151-
grad_part = np.tanh(
152-
np.sqrt(2 / np.pi) * (x + 0.044715 * np.power(x, 3)))
153-
return dout * 0.5 * (1 + grad_part) * (1 + np.sqrt(2 / np.pi) *
154-
(x + 0.134145 * np.power(x, 3)) *
155-
(1 - grad_part))
110+
x = np.random.uniform(-1, 1, [11, 17]).astype(np.float32)
111+
out = convert_float_to_uint16(gelu(x, True))
156112

157-
def set_attrs(self):
113+
self.inputs = {'X': convert_float_to_uint16(x)}
114+
self.outputs = {'Out': out}
158115
self.attrs = {"use_mkldnn": True, "approximate": True}
159116

117+
def test_check_output(self):
118+
self.check_output_with_place(core.CPUPlace())
160119

161-
class TestMKLDNNGeluTanhDim2BF16Op(TestMKLDNNGeluTanhBF16Op):
162-
def init_data(self):
163-
self.x = np.random.uniform(-1, 1, [11, 17]).astype(np.float32)
120+
def test_check_grad(self):
121+
pass
164122

165123

166124
class TestMKLDNNTanhDim2(TestTanh):

python/paddle/fluid/tests/unittests/test_activation_op.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
import numpy as np
1919
from scipy.special import expit, erf
2020

21-
from paddle.fluid.tests.unittests.op_test import OpTest, convert_float_to_uint16, skip_check_grad_ci
21+
from op_test import OpTest, convert_float_to_uint16
2222
import paddle
2323
import paddle.nn as nn
2424
import paddle.nn.functional as F
@@ -1619,6 +1619,7 @@ def setUp(self):
16191619
self.op_type = 'hard_swish'
16201620
self.init_dtype()
16211621

1622+
from op_test import skip_check_grad_ci
16221623
skip_check_grad_ci(reason="not implemented yet")
16231624

16241625
np.random.seed(1024)

0 commit comments

Comments
 (0)