@ -192,11 +192,16 @@ class TestTensorShapeBasic(unittest.TestCase):
self . input = numpy . ones ( 5 ) . astype ( " int32 " )
self . place = fluid . CUDAPlace ( 0 ) if fluid . is_compiled_with_cuda (
) else fluid . CPUPlace ( )
self . _set_input_spec ( )
self . _set_expected_op_num ( )
self . init_test_func ( )
def init_test_func ( self ) :
self . dygraph_func = dyfunc_tensor_shape_1
def _set_input_spec ( self ) :
self . input_spec = [ paddle . static . InputSpec ( shape = [ 5 ] , dtype = " int32 " ) ]
def _run ( self , to_static ) :
with fluid . dygraph . guard ( ) :
if to_static :
@ -219,6 +224,30 @@ class TestTensorShapeBasic(unittest.TestCase):
msg = ' dygraph res is {} \n static_res is {} ' . format ( dygraph_res ,
static_res ) )
def _set_expected_op_num ( self ) :
self . expected_op_num = 2
self . expected_shape_op_num = 0
self . expected_slice_op_num = 0
def _compute_op_num ( self , program ) :
self . op_num = sum ( [ len ( block . ops ) for block in program . blocks ] )
self . shape_op_num = 0
self . slice_op_num = 0
for block in program . blocks :
self . shape_op_num + = len (
[ op for op in block . ops if op . type == " shape " ] )
self . slice_op_num + = len (
[ op for op in block . ops if op . type == " slice " ] )
def test_op_num ( self ) :
static_layer = paddle . jit . to_static ( self . dygraph_func , self . input_spec )
program = static_layer . main_program
self . _compute_op_num ( program )
self . assertEqual ( self . op_num , self . expected_op_num )
self . assertEqual ( self . shape_op_num , self . expected_shape_op_num )
self . assertEqual ( self . slice_op_num , self . expected_slice_op_num )
class TestTensorShapeBasic2 ( TestTensorShapeBasic ) :
def init_test_func ( self ) :
@ -243,12 +272,14 @@ class TestTensorShapeBasic5(TestTensorShapeBasic):
class TestTupleShape1 ( TestTensorShapeBasic ) :
def init_test_func ( self ) :
self . input = numpy . ones ( ( 5 , 7 ) ) . astype ( " int32 " )
self . input_spec = [ paddle . static . InputSpec ( shape = [ 5 , 7 ] , dtype = " int32 " ) ]
self . dygraph_func = dyfunc_tuple_shape_1
class TestTupleShape2 ( TestTensorShapeBasic ) :
def init_test_func ( self ) :
self . input = numpy . ones ( ( 5 , 7 ) ) . astype ( " int32 " )
self . input_spec = [ paddle . static . InputSpec ( shape = [ 5 , 7 ] , dtype = " int32 " ) ]
self . dygraph_func = dyfunc_tuple_shape_2
@ -257,30 +288,45 @@ class TestTensorShapeInIf1(TestTensorShapeBasic):
def init_test_func ( self ) :
self . dygraph_func = dyfunc_with_if_1
def _set_expected_op_num ( self ) :
self . expected_op_num = 26
self . expected_shape_op_num = 2
self . expected_slice_op_num = 2
class TestTensorShapeInIf2 ( TestTensorShapeBasic ) :
def init_test_func ( self ) :
self . dygraph_func = dyfunc_with_if_2
def _set_expected_op_num ( self ) :
self . expected_op_num = 14
self . expected_shape_op_num = 2
self . expected_slice_op_num = 1
# 3. Tests with control flow for loop
class TestTensorShapeInFor1 ( TestTensorShapeBasic ) :
def init_test_func ( self ) :
self . dygraph_func = dyfunc_with_for_1
def _set_expected_op_num ( self ) :
self . expected_op_num = 22
self . expected_shape_op_num = 3
self . expected_slice_op_num = 3
class TestTensorShapeInFor2 ( TestTensorShapeBasic ) :
class TestTensorShapeInFor2 ( TestTensorShape InFor1 ) :
def init_test_func ( self ) :
self . dygraph_func = dyfunc_with_for_2
# 4. Tests with control flow while loop
class TestTensorShapeInWhile1 ( TestTensorShape Basic ) :
class TestTensorShapeInWhile1 ( TestTensorShape InFor1 ) :
def init_test_func ( self ) :
self . dygraph_func = dyfunc_with_while_1
class TestTensorShapeInWhile2 ( TestTensorShape Basic ) :
class TestTensorShapeInWhile2 ( TestTensorShape InFor1 ) :
def init_test_func ( self ) :
self . dygraph_func = dyfunc_with_while_2
@ -289,11 +335,113 @@ class TestTensorShapeInWhile3(TestTensorShapeBasic):
def init_test_func ( self ) :
self . dygraph_func = dyfunc_with_while_3
def _set_expected_op_num ( self ) :
self . expected_op_num = 25
self . expected_shape_op_num = 6
self . expected_slice_op_num = 3
class TestTensorShapeInWhile4 ( TestTensorShapeBasic ) :
def init_test_func ( self ) :
self . dygraph_func = dyfunc_with_while_4
def _set_expected_op_num ( self ) :
self . expected_op_num = 5
self . expected_shape_op_num = 0
self . expected_slice_op_num = 0
# 5. Test op num for negetive dim
class TestOpNumBasicWithTensorShape ( unittest . TestCase ) :
def setUp ( self ) :
self . _set_input_spec ( )
self . _set_test_func ( )
self . _set_expected_op_num ( )
def _set_input_spec ( self ) :
self . input_spec = [
paddle . static . InputSpec (
shape = [ - 1 , 5 ] , dtype = " int32 " )
]
def _set_test_func ( self ) :
self . dygraph_func = dyfunc_tensor_shape_1
def _set_expected_op_num ( self ) :
self . expected_op_num = 3
self . expected_shape_op_num = 1
self . expected_slice_op_num = 0
def _compute_op_num ( self , program ) :
self . op_num = sum ( [ len ( block . ops ) for block in program . blocks ] )
self . shape_op_num = 0
self . slice_op_num = 0
for block in program . blocks :
self . shape_op_num + = len (
[ op for op in block . ops if op . type == " shape " ] )
self . slice_op_num + = len (
[ op for op in block . ops if op . type == " slice " ] )
def test_op_num ( self ) :
static_layer = paddle . jit . to_static ( self . dygraph_func , self . input_spec )
program = static_layer . main_program
self . _compute_op_num ( program )
self . assertEqual ( self . op_num , self . expected_op_num )
self . assertEqual ( self . shape_op_num , self . expected_shape_op_num )
self . assertEqual ( self . slice_op_num , self . expected_slice_op_num )
class TestOpNumBasicWithTensorShape4 ( TestOpNumBasicWithTensorShape ) :
def _set_test_func ( self ) :
self . dygraph_func = dyfunc_tensor_shape_4
def _set_expected_op_num ( self ) :
self . expected_op_num = 6
self . expected_shape_op_num = 1
self . expected_slice_op_num = 1
class TestOpNumWithTensorShapeTuple1 ( TestOpNumBasicWithTensorShape ) :
def _set_test_func ( self ) :
self . dygraph_func = dyfunc_tuple_shape_1
def _set_expected_op_num ( self ) :
self . expected_op_num = 5
self . expected_shape_op_num = 1
self . expected_slice_op_num = 1
class TestOpNumWithTensorShapeInIf1 ( TestOpNumBasicWithTensorShape ) :
def _set_test_func ( self ) :
self . dygraph_func = dyfunc_with_if_1
def _set_expected_op_num ( self ) :
self . expected_op_num = 28
self . expected_shape_op_num = 4
self . expected_slice_op_num = 2
class TestOpNumWithTensorShapeInFor1 ( TestOpNumBasicWithTensorShape ) :
def _set_test_func ( self ) :
self . dygraph_func = dyfunc_with_for_1
def _set_expected_op_num ( self ) :
self . expected_op_num = 22
self . expected_shape_op_num = 3
self . expected_slice_op_num = 3
class TestOpNumWithTensorShapeInWhile1 ( TestOpNumBasicWithTensorShape ) :
def _set_test_func ( self ) :
self . dygraph_func = dyfunc_with_while_1
def _set_expected_op_num ( self ) :
self . expected_op_num = 22
self . expected_shape_op_num = 3
self . expected_slice_op_num = 3
if __name__ == ' __main__ ' :
unittest . main ( )