@@ -228,6 +228,7 @@ def test_input_same_dtype():
228
228
229
229
class TestConcatAPI (unittest .TestCase ):
230
230
def test_fluid_api (self ):
231
+ paddle .enable_static ()
231
232
x_1 = fluid .data (shape = [None , 1 , 4 , 5 ], dtype = 'int32' , name = 'x_1' )
232
233
fluid .layers .concat ([x_1 , x_1 ], 0 )
233
234
@@ -253,6 +254,7 @@ def test_fluid_api(self):
253
254
assert np .array_equal (res_3 , np .concatenate ((input_2 , input_3 ), axis = 1 ))
254
255
255
256
def test_api (self ):
257
+ paddle .enable_static ()
256
258
x_1 = paddle .fluid .data (
257
259
shape = [None , 1 , 4 , 5 ], dtype = 'int32' , name = 'x_1' )
258
260
paddle .concat ([x_1 , x_1 ], 0 )
@@ -338,21 +340,44 @@ def setUp(self):
338
340
self .x = np .random .random (self .input_shape ).astype ("float32" )
339
341
self .place = fluid .CUDAPlace (0 ) \
340
342
if fluid .is_compiled_with_cuda () else fluid .CPUPlace ()
341
- self .set_program ()
342
343
343
- def set_program (self ):
344
- self .program = fluid .Program ()
345
- with fluid .program_guard (self .program ):
346
- input = fluid .layers .assign (self .x )
347
- tensor_array = fluid .layers .create_array (dtype = 'float32' )
348
- zero = fluid .layers .fill_constant (shape = [1 ], value = 0 , dtype = "int64" )
344
+ def set_program (self , use_fluid_api ):
345
+ paddle .enable_static ()
346
+ if use_fluid_api :
347
+ self .program = fluid .Program ()
348
+ with fluid .program_guard (self .program ):
349
+ input = fluid .layers .assign (self .x )
350
+ tensor_array = fluid .layers .create_array (dtype = 'float32' )
351
+ zero = fluid .layers .fill_constant (
352
+ shape = [1 ], value = 0 , dtype = "int64" )
353
+
354
+ for i in range (self .iter_num ):
355
+ fluid .layers .array_write (input , zero + i , tensor_array )
356
+
357
+ self .out_var = fluid .layers .concat (tensor_array , axis = self .axis )
358
+ else :
359
+ self .program = paddle .static .Program ()
360
+ with paddle .static .program_guard (self .program ):
361
+ input = paddle .assign (self .x )
362
+ tensor_array = fluid .layers .create_array (
363
+ dtype = 'float32'
364
+ ) # Api create_array is not supported in paddle 2.0 yet.
365
+ zero = paddle .zeros (shape = [1 ], dtype = "int64" )
349
366
350
- for i in range (self .iter_num ):
351
- fluid .layers .array_write (input , zero + i , tensor_array )
367
+ for i in range (self .iter_num ):
368
+ # Api array_write is not supported in paddle 2.0 yet.
369
+ fluid .layers .array_write (input , zero + i , tensor_array )
370
+
371
+ self .out_var = paddle .concat (tensor_array , axis = self .axis )
372
+
373
+ def test_fluid_api (self ):
374
+ self ._run_static_mode (use_fluid_api = True )
352
375
353
- self .out_var = fluid .layers .concat (tensor_array , axis = self .axis )
376
+ def test_paddle_api (self ):
377
+ self ._run_static_mode (use_fluid_api = False )
354
378
355
- def test_case (self ):
379
+ def _run_static_mode (self , use_fluid_api ):
380
+ self .set_program (use_fluid_api )
356
381
self .assertTrue (self .out_var .shape [self .axis ] == - 1 )
357
382
exe = fluid .Executor (self .place )
358
383
res = exe .run (self .program , fetch_list = self .out_var )
0 commit comments