@@ -756,6 +756,105 @@ def test_floor_divide(self):
756756 np .testing .assert_array_equal (out3_1 .numpy (), out3_2 .numpy ())
757757 np .testing .assert_array_equal (out3_2 .numpy (), np .asarray (1 ))
758758
759+ def test_reshape_list (self ):
760+ x = paddle .rand ([])
761+ x .stop_gradient = False
762+
763+ out = paddle .reshape (x , [])
764+ out .backward ()
765+ self .assertEqual (x .grad .shape , [])
766+ self .assertEqual (out .shape , [])
767+ self .assertEqual (out .grad .shape , [])
768+
769+ out = paddle .reshape (x , [1 ])
770+ out .backward ()
771+ self .assertEqual (x .grad .shape , [])
772+ self .assertEqual (out .shape , [1 ])
773+ self .assertEqual (out .grad .shape , [1 ])
774+
775+ out = paddle .reshape (x , [- 1 ])
776+ out .backward ()
777+ self .assertEqual (x .grad .shape , [])
778+ self .assertEqual (out .shape , [1 ])
779+ self .assertEqual (out .grad .shape , [1 ])
780+
781+ out = paddle .reshape (x , [- 1 , 1 ])
782+ out .backward ()
783+ self .assertEqual (x .grad .shape , [])
784+ self .assertEqual (out .shape , [1 , 1 ])
785+ self .assertEqual (out .grad .shape , [1 , 1 ])
786+
787+ def test_reshape_tensor (self ):
788+ x = paddle .rand ([1 , 1 ])
789+ x .stop_gradient = False
790+
791+ out = paddle .reshape (x , [])
792+ out .backward ()
793+ self .assertEqual (x .grad .shape , [1 , 1 ])
794+ self .assertEqual (out .shape , [])
795+ self .assertEqual (out .grad .shape , [])
796+
797+ new_shape = paddle .full ([1 ], 1 , "int32" )
798+ out = paddle .reshape (x , new_shape )
799+ out .backward ()
800+ self .assertEqual (x .grad .shape , [1 , 1 ])
801+ self .assertEqual (out .shape , [1 ])
802+ self .assertEqual (out .grad .shape , [1 ])
803+
804+ new_shape = paddle .full ([1 ], - 1 , "int32" )
805+ out = paddle .reshape (x , new_shape )
806+ out .backward ()
807+ self .assertEqual (x .grad .shape , [1 , 1 ])
808+ self .assertEqual (out .shape , [1 ])
809+ self .assertEqual (out .grad .shape , [1 ])
810+
811+ new_shape = [paddle .full ([], - 1 , "int32" ), paddle .full ([], 1 , "int32" )]
812+ out = paddle .reshape (x , new_shape )
813+ out .backward ()
814+ self .assertEqual (x .grad .shape , [1 , 1 ])
815+ self .assertEqual (out .shape , [1 , 1 ])
816+ self .assertEqual (out .grad .shape , [1 , 1 ])
817+
818+ def test_reshape__list (self ):
819+ x = paddle .rand ([])
820+ out = paddle .reshape_ (x , [])
821+ self .assertEqual (out .shape , [])
822+
823+ out = paddle .reshape_ (x , [1 ])
824+ self .assertEqual (out .shape , [1 ])
825+
826+ out = paddle .reshape_ (x , [- 1 ])
827+ self .assertEqual (out .shape , [1 ])
828+
829+ out = paddle .reshape_ (x , [- 1 , 1 ])
830+ self .assertEqual (out .shape , [1 , 1 ])
831+
832+ def test_reshape__tensor (self ):
833+ x = paddle .rand ([1 , 1 ])
834+ out = paddle .reshape_ (x , [])
835+ self .assertEqual (out .shape , [])
836+
837+ new_shape = paddle .full ([1 ], 1 , "int32" )
838+ out = paddle .reshape_ (x , new_shape )
839+ self .assertEqual (out .shape , [1 ])
840+
841+ new_shape = paddle .full ([1 ], - 1 , "int32" )
842+ out = paddle .reshape_ (x , new_shape )
843+ self .assertEqual (out .shape , [1 ])
844+
845+ new_shape = [paddle .full ([], - 1 , "int32" ), paddle .full ([], 1 , "int32" )]
846+ out = paddle .reshape_ (x , new_shape )
847+ self .assertEqual (out .shape , [1 , 1 ])
848+
849+ def test_reverse (self ):
850+ x = paddle .rand ([])
851+ x .stop_gradient = False
852+ out = paddle .reverse (x , axis = [])
853+ out .backward ()
854+ self .assertEqual (x .shape , [])
855+ self .assertEqual (out .shape , [])
856+ self .assertEqual (out .grad .shape , [])
857+
759858
760859class TestSundryAPIStatic (unittest .TestCase ):
761860 def setUp (self ):
@@ -1011,6 +1110,78 @@ def test_floor_divide(self):
10111110 np .testing .assert_array_equal (out3_1 , out3_2 )
10121111 np .testing .assert_array_equal (out3_2 , np .asarray (1 ))
10131112
1113+ @prog_scope ()
1114+ def test_reshape_list (self ):
1115+ x1 = paddle .rand ([])
1116+ x2 = paddle .rand ([])
1117+ x3 = paddle .rand ([])
1118+ x4 = paddle .rand ([])
1119+ x1 .stop_gradient = False
1120+ x2 .stop_gradient = False
1121+ x3 .stop_gradient = False
1122+ x4 .stop_gradient = False
1123+
1124+ out1 = paddle .reshape (x1 , [])
1125+ paddle .static .append_backward (out1 )
1126+
1127+ out2 = paddle .reshape (x2 , [1 ])
1128+ paddle .static .append_backward (out2 )
1129+
1130+ out3 = paddle .reshape (x3 , [- 1 ])
1131+ paddle .static .append_backward (out3 )
1132+
1133+ out4 = paddle .reshape (x4 , [- 1 , 1 ])
1134+ paddle .static .append_backward (out4 )
1135+
1136+ program = paddle .static .default_main_program ()
1137+ res1 , res2 , res3 , res4 = self .exe .run (
1138+ program , fetch_list = [out1 , out2 , out3 , out4 ]
1139+ )
1140+ self .assertEqual (res1 .shape , ())
1141+ self .assertEqual (res2 .shape , (1 ,))
1142+ self .assertEqual (res3 .shape , (1 ,))
1143+ self .assertEqual (res4 .shape , (1 , 1 ))
1144+
1145+ @prog_scope ()
1146+ def test_reshape_tensor (self ):
1147+ x1 = paddle .rand ([])
1148+ x2 = paddle .rand ([])
1149+ x3 = paddle .rand ([])
1150+ x1 .stop_gradient = False
1151+ x2 .stop_gradient = False
1152+ x3 .stop_gradient = False
1153+
1154+ new_shape = paddle .full ([1 ], 1 , "int32" )
1155+ out1 = paddle .reshape (x1 , new_shape )
1156+ paddle .static .append_backward (out1 )
1157+
1158+ new_shape = paddle .full ([1 ], - 1 , "int32" )
1159+ out2 = paddle .reshape (x2 , new_shape )
1160+ paddle .static .append_backward (out2 )
1161+
1162+ new_shape = [paddle .full ([], - 1 , "int32" ), paddle .full ([], 1 , "int32" )]
1163+ out3 = paddle .reshape (x3 , new_shape )
1164+ paddle .static .append_backward (out3 )
1165+
1166+ program = paddle .static .default_main_program ()
1167+ res1 , res2 , res3 = self .exe .run (program , fetch_list = [out1 , out2 , out3 ])
1168+ self .assertEqual (res1 .shape , (1 ,))
1169+ self .assertEqual (res2 .shape , (1 ,))
1170+ self .assertEqual (res3 .shape , (1 , 1 ))
1171+
1172+ @prog_scope ()
1173+ def test_reverse (self ):
1174+ x = paddle .rand ([])
1175+ x .stop_gradient = False
1176+
1177+ out = paddle .reverse (x , axis = [])
1178+ paddle .static .append_backward (out )
1179+
1180+ program = paddle .static .default_main_program ()
1181+ res1 , res2 = self .exe .run (program , fetch_list = [x , out ])
1182+ self .assertEqual (res1 .shape , ())
1183+ self .assertEqual (res2 .shape , ())
1184+
10141185
10151186# Use to test API whose zero-dim input tensors don't have grad and not need to test backward in OpTest.
10161187class TestNoBackwardAPI (unittest .TestCase ):
0 commit comments