@ -52,6 +52,16 @@ def test_list_in_for_loop(x, iter_num):
return a
def test_list_in_for_loop_with_concat ( x , iter_num ) :
# Note: for_loop can't be transformed before PR22867 merged.
x = fluid . dygraph . to_variable ( x )
a = [ ]
for i in range ( iter_num ) :
a . append ( x )
out = fluid . layers . concat ( a , axis = 0 )
return out
def test_list_in_while_loop ( x , iter_num ) :
x = fluid . dygraph . to_variable ( x )
iter_num = fluid . layers . fill_constant (
@ -67,12 +77,28 @@ def test_list_in_while_loop(x, iter_num):
return a
def test_list_in_while_loop_with_stack ( x , iter_num ) :
x = fluid . dygraph . to_variable ( x )
iter_num = fluid . layers . fill_constant (
shape = [ 1 ] , value = iter_num , dtype = " int32 " )
a = [ ]
i = 0
while i < iter_num . numpy ( ) [ 0 ] :
a . append ( x )
i + = 1
out = fluid . layers . stack ( a , axis = 1 )
return out
class TestListWithoutControlFlow ( unittest . TestCase ) :
def setUp ( self ) :
self . input = np . random . random ( ( 3 ) ) . astype ( ' int32 ' )
self . dygraph_func = test_list_without_control_flow
self . place = fluid . CUDAPlace ( 0 ) if fluid . is_compiled_with_cuda (
) else fluid . CPUPlace ( )
self . init_dygraph_func ( )
def init_dygraph_func ( self ) :
self . dygraph_func = test_list_without_control_flow
def run_dygraph_mode ( self ) :
with fluid . dygraph . guard ( ) :
@ -100,11 +126,8 @@ class TestListWithoutControlFlow(unittest.TestCase):
class TestListInIf ( TestListWithoutControlFlow ) :
def setUp ( self ) :
self . input = np . random . random ( ( 3 ) ) . astype ( ' int32 ' )
def init_dygraph_func ( self ) :
self . dygraph_func = test_list_in_if
self . place = fluid . CUDAPlace ( 0 ) if fluid . is_compiled_with_cuda (
) else fluid . CPUPlace ( )
def run_static_mode ( self ) :
main_program = fluid . Program ( )
@ -120,13 +143,16 @@ class TestListInIf(TestListWithoutControlFlow):
return numpy_res [ 0 ]
class TestListInWhileLoop ( unittest. TestCase ) :
class TestListInWhileLoop ( TestListWithoutControlFlow ) :
def setUp ( self ) :
self . iter_num = 3
self . input = np . random . random ( ( 3 ) ) . astype ( ' int32 ' )
self . dygraph_func = test_list_in_while_loop
self . place = fluid . CUDAPlace ( 0 ) if fluid . is_compiled_with_cuda (
) else fluid . CPUPlace ( )
self . init_dygraph_func ( )
def init_dygraph_func ( self ) :
self . dygraph_func = test_list_in_while_loop
def run_dygraph_mode ( self ) :
with fluid . dygraph . guard ( ) :
@ -146,56 +172,40 @@ class TestListInWhileLoop(unittest.TestCase):
tensor_array ,
i = fluid . layers . fill_constant (
shape = [ 1 ] , value = i , dtype = ' int64 ' ) ) )
exe = fluid . Executor ( self . place )
numpy_res = exe . run ( main_program , fetch_list = static_outs )
return numpy_res
def test_transformed_static_result ( self ) :
static_res = self . run_static_mode ( )
dygraph_res = self . run_dygraph_mode ( )
self . assertTrue (
np . array_equal ( dygraph_res , static_res ) ,
msg = ' dygraph res is {} \n static_res is {} ' . format ( dygraph_res ,
static_res ) )
class TestListInForLoop ( unittest . TestCase ) :
def setUp ( self ) :
self . iter_num = 3
self . input = np . random . random ( ( 3 ) ) . astype ( ' int32 ' )
self . dygraph_func = test_list_in_for_loop
self . place = fluid . CUDAPlace ( 0 ) if fluid . is_compiled_with_cuda (
) else fluid . CPUPlace ( )
class TestListInWhileLoopWithStack ( TestListInWhileLoop ) :
def init_dygraph_func ( self ) :
self . dygraph_func = test_list_in_while_loop_with_stack
def run_dygraph_mode ( self ) :
with fluid . dygraph . guard ( ) :
var_res = self . dygraph_func ( self . input , self . iter_num )
numpy_res = [ ele . numpy ( ) for ele in var_res ]
numpy_res = var_res . numpy ( )
return numpy_res
def run_static_mode ( self ) :
main_program = fluid . Program ( )
with fluid . program_guard ( main_program ) :
tensor_array = dygraph_to_static_graph ( self . dygraph_func ) (
self . input , self . iter_num )
static_outs = [ ]
for i in range ( self . iter_num ) :
static_outs . append (
fluid . layers . array_read (
tensor_array ,
i = fluid . layers . fill_constant (
shape = [ 1 ] , value = i , dtype = ' int64 ' ) ) )
out_var = dygraph_to_static_graph ( self . dygraph_func ) ( self . input ,
self . iter_num )
exe = fluid . Executor ( self . place )
numpy_res = exe . run ( main_program , fetch_list = static_outs )
return numpy_res
numpy_res = exe . run ( main_program , fetch_list = out_var )
return numpy_res [ 0 ]
def test_transformed_static_result ( self ) :
static_res = self . run_static_mode ( )
dygraph_res = self . run_dygraph_mode ( )
self . assertTrue (
np . array_equal ( dygraph_res , static_res ) ,
msg = ' dygraph res is {} \n static_res is {} ' . format ( dygraph_res ,
static_res ) )
class TestListInForLoop ( TestListInWhileLoop ) :
def init_dygraph_func ( self ) :
self . dygraph_func = test_list_in_for_loop
class TestListInForLoopWithConcat ( TestListInWhileLoopWithStack ) :
def init_dygraph_func ( self ) :
self . dygraph_func = test_list_in_for_loop_with_concat
if __name__ == ' __main__ ' :