|
16 | 16 |
|
17 | 17 | import unittest |
18 | 18 | import numpy as np |
19 | | -from scipy.special import expit, erf |
| 19 | +from scipy.special import expit |
20 | 20 | 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 |
22 | 22 | from paddle.fluid.tests.unittests.test_activation_op import TestActivation, TestRelu, TestTanh, TestSqrt, TestAbs, TestLeakyRelu, TestSwish, TestHardSwish, TestRelu6, TestSigmoid |
23 | 23 | from paddle.fluid.tests.unittests.test_gelu_op import gelu |
24 | 24 | from mkldnn_op_test import check_if_mkldnn_primitives_exist_in_bwd |
@@ -79,88 +79,46 @@ def setUp(self): |
79 | 79 | self.attrs = {"use_mkldnn": True, "approximate": True} |
80 | 80 |
|
81 | 81 |
|
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): |
100 | 85 | def setUp(self): |
| 86 | + self.op_type = "gelu" |
101 | 87 | self.dtype = np.uint16 |
102 | | - self.init_data() |
103 | | - self.config() |
104 | | - self.out = self.op_forward(self.x) |
105 | 88 |
|
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)) |
109 | 91 |
|
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} |
112 | 95 |
|
113 | 96 | def test_check_output(self): |
114 | 97 | self.check_output_with_place(core.CPUPlace()) |
115 | 98 |
|
116 | 99 | 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 |
141 | 101 |
|
142 | 102 |
|
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): |
145 | 107 | self.op_type = "gelu" |
| 108 | + self.dtype = np.uint16 |
146 | 109 |
|
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)) |
156 | 112 |
|
157 | | - def set_attrs(self): |
| 113 | + self.inputs = {'X': convert_float_to_uint16(x)} |
| 114 | + self.outputs = {'Out': out} |
158 | 115 | self.attrs = {"use_mkldnn": True, "approximate": True} |
159 | 116 |
|
| 117 | + def test_check_output(self): |
| 118 | + self.check_output_with_place(core.CPUPlace()) |
160 | 119 |
|
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 |
164 | 122 |
|
165 | 123 |
|
166 | 124 | class TestMKLDNNTanhDim2(TestTanh): |
|
0 commit comments