@@ -30,7 +30,7 @@ def forward(ctx, x1, x2, func1, func2=paddle.square):
30
30
y1 = func1 (x1 )
31
31
y2 = func1 (x2 )
32
32
ctx .save_for_backward (y1 , y2 )
33
- return y1 , y2
33
+ return y1 , 1 , y2 , None
34
34
35
35
@staticmethod
36
36
def backward (ctx , dy1 , dy2 ):
@@ -44,7 +44,7 @@ def backward(ctx, dy1, dy2):
44
44
input1 .stop_gradient = False
45
45
input2 .stop_gradient = False
46
46
z = tanh .apply (input1 , input1 , paddle .tanh , paddle .square )
47
- z = z [0 ] + z [1 ]
47
+ z = z [0 ] + z [2 ]
48
48
z .mean ().backward ()
49
49
50
50
z2 = paddle .tanh (input2 ) + paddle .tanh (input2 )
@@ -61,7 +61,7 @@ def forward(ctx, x1, x2, func1, func2=paddle.square):
61
61
y1 = func1 (x1 )
62
62
y2 = func1 (x2 )
63
63
ctx .save_for_backward (y1 , y2 )
64
- return y1 , y2
64
+ return 1 , None , y1 , y2 , ''
65
65
66
66
@staticmethod
67
67
def backward (ctx , dy1 , dy2 ):
@@ -79,7 +79,7 @@ def backward(ctx, dy1, dy2):
79
79
input3 .stop_gradient = True
80
80
input4 .stop_gradient = True
81
81
z = tanh .apply (input1 , input3 , paddle .tanh , paddle .square )
82
- z = z [0 ] + z [1 ]
82
+ z = z [2 ] + z [3 ]
83
83
z .mean ().backward ()
84
84
85
85
z2 = paddle .tanh (input2 ) + paddle .tanh (input4 )
@@ -115,6 +115,27 @@ def backward(ctx, dy1):
115
115
self .assertTrue (
116
116
np .max (np .abs ((input1 .grad .numpy () - input2 .grad .numpy ()))) < 1e-10 )
117
117
118
+ def test_pylayer_num_output_match (self ):
119
+ class tanh (PyLayer ):
120
+ @staticmethod
121
+ def forward (
122
+ ctx ,
123
+ x1 ,
124
+ x2 , ):
125
+ return x1 + x2
126
+
127
+ @staticmethod
128
+ def backward (ctx , dy1 ):
129
+ return dy1 + 1
130
+
131
+ input1 = paddle .randn ([2 , 3 ]).astype ("float64" )
132
+ input2 = input1 .detach ().clone ()
133
+ input1 .stop_gradient = False
134
+ input2 .stop_gradient = False
135
+ z = tanh .apply (input1 , input2 )
136
+ with self .assertRaises (ValueError ):
137
+ z .mean ().backward ()
138
+
118
139
def test_pylayer_dtype (self ):
119
140
class tanh (PyLayer ):
120
141
@staticmethod
@@ -150,21 +171,21 @@ def backward(ctx, *args):
150
171
return args
151
172
152
173
input1 = paddle .randn ([2 , 3 ]).astype ("float64" )
153
- with self .assertRaises (NotImplementedError ):
174
+ with self .assertRaises (ValueError ):
154
175
z = Layer_None1 .apply (input1 )
155
176
156
177
class Layer_None2 (PyLayer ):
157
178
@staticmethod
158
179
def forward (ctx , * args ):
159
- return [None , None ]
180
+ return [None , args [ 0 ] ]
160
181
161
182
@staticmethod
162
183
def backward (ctx , * args ):
163
184
return args
164
185
165
186
input1 = paddle .randn ([2 , 3 ]).astype ("float64" )
166
- with self . assertRaises ( NotImplementedError ):
167
- z = Layer_None2 .apply (input1 )
187
+ # return None
188
+ z = Layer_None2 .apply (input1 )
168
189
169
190
class Layer_one1 (PyLayer ):
170
191
@staticmethod
@@ -176,21 +197,22 @@ def backward(ctx, *args):
176
197
return args
177
198
178
199
input1 = paddle .randn ([2 , 3 ]).astype ("float64" )
179
- with self .assertRaises (NotImplementedError ):
200
+ # At least one output of `PyLayer.backward` is a `Tensor`
201
+ with self .assertRaises (ValueError ):
180
202
z = Layer_one1 .apply (input1 )
181
203
182
204
class Layer_one2 (PyLayer ):
183
205
@staticmethod
184
206
def forward (ctx , * args ):
185
- return [1 , 2 ]
207
+ return [1 , 2 , args [ 0 ] ]
186
208
187
209
@staticmethod
188
210
def backward (ctx , * args ):
189
211
return args
190
212
191
213
input1 = paddle .randn ([2 , 3 ]).astype ("float64" )
192
- with self . assertRaises ( NotImplementedError ):
193
- z = Layer_one2 .apply (input1 )
214
+ # return int
215
+ z = Layer_one2 .apply (input1 )
194
216
195
217
class Layer_no_fw (PyLayer ):
196
218
@staticmethod
@@ -234,8 +256,7 @@ def backward(ctx, dy1):
234
256
z = Layer_bk_none1 .apply (input2 )
235
257
236
258
with self .assertRaises (ValueError ):
237
- with paddle .fluid .dygraph .guard ():
238
- z .sum ().backward ()
259
+ z .sum ().backward ()
239
260
240
261
class Layer_bk_none2 (PyLayer ):
241
262
@staticmethod
@@ -249,9 +270,9 @@ def backward(ctx, dy1):
249
270
input1 = paddle .randn ([2 , 3 ]).astype ("float64" )
250
271
input1 .stop_gradient = False
251
272
z = Layer_bk_none2 .apply (input1 , input1 )
273
+
252
274
with self .assertRaises (ValueError ):
253
- with paddle .fluid .dygraph .guard ():
254
- z .mean ().backward ()
275
+ z .mean ().backward ()
255
276
256
277
class Layer_bk_one1 (PyLayer ):
257
278
@staticmethod
@@ -265,9 +286,9 @@ def backward(ctx, dy):
265
286
input1 = paddle .randn ([2 , 3 ]).astype ("float64" )
266
287
input1 .stop_gradient = False
267
288
z = Layer_bk_one1 .apply (input1 )
289
+
268
290
with self .assertRaises (ValueError ):
269
- with paddle .fluid .dygraph .guard ():
270
- z .mean ().backward ()
291
+ z .mean ().backward ()
271
292
272
293
class Layer_bk_one2 (PyLayer ):
273
294
@staticmethod
@@ -280,11 +301,11 @@ def backward(ctx, *args):
280
301
281
302
input1 = paddle .randn ([2 , 3 ]).astype ("float64" )
282
303
input1 .stop_gradient = False
304
+
283
305
y = Layer_bk_one2 .apply (input1 , input1 )
284
306
z = y [0 ] + y [1 ]
285
307
with self .assertRaises (ValueError ):
286
- with paddle .fluid .dygraph .guard ():
287
- z .mean ().backward ()
308
+ z .mean ().backward ()
288
309
289
310
class Layer_no_bk (PyLayer ):
290
311
@staticmethod
@@ -295,10 +316,9 @@ def forward(ctx, x):
295
316
input1 .stop_gradient = False
296
317
z = Layer_no_bk .apply (input1 )
297
318
298
- with self .assertRaises (NotImplementedError ):
299
- with paddle .fluid .dygraph .guard ():
300
- z = z [0 ] + z [1 ]
301
- z .mean ().backward ()
319
+ with self .assertRaises (OSError ):
320
+ z = z [0 ] + z [1 ]
321
+ z .mean ().backward ()
302
322
303
323
class Layer_bk_match (PyLayer ):
304
324
@staticmethod
@@ -313,9 +333,8 @@ def backward(ctx, dy1, dy2):
313
333
input1 .stop_gradient = False
314
334
z = Layer_bk_match .apply (input1 )
315
335
with self .assertRaises (ValueError ):
316
- with paddle .fluid .dygraph .guard ():
317
- z = z [0 ] + z [1 ]
318
- z .mean ().backward ()
336
+ z = z [0 ] + z [1 ]
337
+ z .mean ().backward ()
319
338
320
339
def test_pylayer_bk_return_none (self ):
321
340
class Layer_bk_none1 (PyLayer ):
@@ -334,8 +353,7 @@ def backward(ctx, dy):
334
353
z = Layer_bk_none1 .apply (input1 , input2 )
335
354
336
355
with self .assertRaises (ValueError ):
337
- with paddle .fluid .dygraph .guard ():
338
- z .mean ().backward ()
356
+ z .mean ().backward ()
339
357
340
358
class Layer_bk_none2 (PyLayer ):
341
359
@staticmethod
@@ -353,8 +371,7 @@ def backward(ctx, *args):
353
371
z = Layer_bk_none2 .apply (input1 , input2 )
354
372
z = z [0 ] + z [1 ]
355
373
with self .assertRaises (ValueError ):
356
- with paddle .fluid .dygraph .guard ():
357
- z .mean ().backward ()
374
+ z .mean ().backward ()
358
375
359
376
def test_pylayer_inplace (self ):
360
377
class cus_tanh (PyLayer ):
0 commit comments