Skip to content

Commit 440b96d

Browse files
authored
support sign op backward refuse forward (#46002)
1 parent 491e4df commit 440b96d

3 files changed

Lines changed: 85 additions & 0 deletions

File tree

paddle/phi/api/yaml/legacy_backward.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2096,6 +2096,12 @@
20962096
optional : grad_grad_out_grad
20972097
inplace : (grad_grad_x -> fwd_grad_out_grad)
20982098

2099+
- backward_op : sign_grad
2100+
forward : sign (Tensor x) -> Tensor(out)
2101+
args : (Tensor out_grad)
2102+
output : Tensor(x_grad)
2103+
invoke : scale(out_grad, 0.0, 0.0, true)
2104+
20992105
- backward_op : silu_grad
21002106
forward : silu (Tensor x) -> Tensor(out)
21012107
args : (Tensor x, Tensor out_grad)

paddle/phi/api/yaml/legacy_ops.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2377,6 +2377,7 @@
23772377
func : UnchangedInferMeta
23782378
kernel :
23792379
func : sign
2380+
backward : sign_grad
23802381

23812382
- op : silu
23822383
args : (Tensor x)

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

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,11 @@
1919
from op_test import OpTest
2020
import paddle
2121
import paddle.fluid as fluid
22+
import paddle.fluid.core as core
2223
from paddle.fluid import Program, program_guard
24+
import gradient_checker
25+
from decorator_helper import prog_scope
26+
import paddle.fluid.layers as layers
2327

2428

2529
class TestSignOp(OpTest):
@@ -91,6 +95,80 @@ def test_static(self):
9195
paddle.sign(input4)
9296

9397

98+
class TestSignDoubleGradCheck(unittest.TestCase):
99+
100+
def sign_wrapper(self, x):
101+
return paddle.sign(x[0])
102+
103+
@prog_scope()
104+
def func(self, place):
105+
# the shape of input variable should be clearly specified, not inlcude -1.
106+
eps = 0.005
107+
dtype = np.float32
108+
109+
data = layers.data('data', [1, 4], False, dtype)
110+
data.persistable = True
111+
out = paddle.sign(data)
112+
data_arr = np.random.uniform(-1, 1, data.shape).astype(dtype)
113+
114+
gradient_checker.double_grad_check([data],
115+
out,
116+
x_init=[data_arr],
117+
place=place,
118+
eps=eps)
119+
fluid.set_flags({"FLAGS_retain_grad_for_all_tensor": True})
120+
gradient_checker.double_grad_check_for_dygraph(self.sign_wrapper,
121+
[data],
122+
out,
123+
x_init=[data_arr],
124+
place=place)
125+
126+
def test_grad(self):
127+
paddle.enable_static()
128+
places = [fluid.CPUPlace()]
129+
if core.is_compiled_with_cuda():
130+
places.append(fluid.CUDAPlace(0))
131+
for p in places:
132+
self.func(p)
133+
134+
135+
class TestSignTripleGradCheck(unittest.TestCase):
136+
137+
def sign_wrapper(self, x):
138+
return paddle.sign(x[0])
139+
140+
@prog_scope()
141+
def func(self, place):
142+
# the shape of input variable should be clearly specified, not inlcude -1.
143+
eps = 0.005
144+
dtype = np.float32
145+
146+
data = layers.data('data', [1, 4], False, dtype)
147+
data.persistable = True
148+
out = paddle.sign(data)
149+
data_arr = np.random.uniform(-1, 1, data.shape).astype(dtype)
150+
151+
gradient_checker.triple_grad_check([data],
152+
out,
153+
x_init=[data_arr],
154+
place=place,
155+
eps=eps)
156+
fluid.set_flags({"FLAGS_retain_grad_for_all_tensor": True})
157+
gradient_checker.triple_grad_check_for_dygraph(self.sign_wrapper,
158+
[data],
159+
out,
160+
x_init=[data_arr],
161+
place=place)
162+
163+
def test_grad(self):
164+
paddle.enable_static()
165+
places = [fluid.CPUPlace()]
166+
if core.is_compiled_with_cuda():
167+
places.append(fluid.CUDAPlace(0))
168+
for p in places:
169+
self.func(p)
170+
171+
94172
if __name__ == "__main__":
95173
paddle.enable_static()
96174
unittest.main()

0 commit comments

Comments
 (0)