33
33
34
34
35
35
class BuildIrMemOptBase (unittest .TestCase ):
36
+ def setup_reader (self ):
37
+ self .batch_size = 32
38
+ self .word_dict = paddle .dataset .imdb .word_dict ()
39
+ self .train_reader = paddle .batch (
40
+ paddle .dataset .imdb .train (self .word_dict ),
41
+ batch_size = self .batch_size )
42
+
36
43
def check_network_convergence (self ,
37
44
network ,
38
45
use_cuda = True ,
@@ -51,35 +58,34 @@ def check_network_convergence(self,
51
58
return
52
59
fluid .default_startup_program ().random_seed = 100
53
60
fluid .default_main_program ().random_seed = 100
54
- batch_size = 32
55
- batch_size *= fluid .core .get_cuda_device_count () if use_cuda else int (
56
- os .environ .get ('CPU_NUM' , multiprocessing .cpu_count ()))
57
-
58
- # build network
59
- word_dict = paddle .dataset .imdb .word_dict ()
60
- train_reader = paddle .batch (
61
- paddle .dataset .imdb .train (word_dict ), batch_size = batch_size )
62
61
63
62
data = fluid .layers .data (
64
63
name = "words" , shape = [1 ], dtype = "int64" , lod_level = 1 )
65
64
66
65
label = fluid .layers .data (name = "label" , shape = [1 ], dtype = "int64" )
67
66
68
- cost = network (data , label , len (word_dict ))
67
+ cost = network (data , label , len (self . word_dict ))
69
68
optimizer = fluid .optimizer .Adam (learning_rate = 0.001 )
70
69
optimizer .minimize (cost )
70
+ build_strategy = fluid .BuildStrategy ()
71
+ build_strategy .enable_inplace = False
72
+ build_strategy .memory_optimize = False
71
73
if memory_opt :
72
74
fluid .memory_optimize (fluid .default_main_program ())
75
+ else :
76
+ build_strategy .enable_inplace = use_ir_memory_optimize
77
+ build_strategy .memory_optimize = enable_inplace
73
78
74
79
# execution
75
80
place = fluid .CUDAPlace (0 ) if use_cuda else fluid .CPUPlace ()
76
81
feeder = fluid .DataFeeder (feed_list = [data , label ], place = place )
77
- reader = feeder .decorate_reader (train_reader , multi_devices = True )
82
+ reader = feeder .decorate_reader (self . train_reader , multi_devices = True )
78
83
exe = fluid .Executor (place )
79
84
exe .run (fluid .default_startup_program ())
80
85
81
86
train_cp = compiler .CompiledProgram (fluid .default_main_program ())
82
- train_cp = train_cp .with_data_parallel (loss_name = cost .name )
87
+ train_cp = train_cp .with_data_parallel (
88
+ loss_name = cost .name , build_strategy = build_strategy )
83
89
fetch_list = [cost .name ]
84
90
85
91
begin = time .time ()
@@ -100,7 +106,7 @@ def check_network_convergence(self,
100
106
end = time .time ()
101
107
102
108
print ("%.4f Instance per second" % (
103
- (batch_size * iter ) / (end - begin )))
109
+ (self . batch_size * iter ) / (end - begin )))
104
110
105
111
print (first_loss , last_loss )
106
112
avg_last_loss_val = np .array (last_loss ).mean ()
@@ -120,31 +126,21 @@ def test_network(self):
120
126
if self .network is None or not core .is_compiled_with_cuda ():
121
127
return
122
128
123
- baseline_first_loss , baseline_last_loss = None , None
124
- for use_cuda in [True ]:
125
- for use_python_mem_opt in [True , False ]:
126
- print (
127
- 'network: {}, use_cuda: {}, use_python_mem_opt: {}, use_ir_mem_opt : {}' .
128
- format (self .network .__name__ , use_cuda , use_python_mem_opt ,
129
- not use_python_mem_opt ))
130
- with fluid .program_guard (fluid .Program (), fluid .Program ()):
131
- with fluid .scope_guard (core .Scope ()):
132
- if use_cuda is True and use_python_mem_opt is True :
133
- baseline_first_loss , baseline_last_loss = self .check_network_convergence (
134
- self .network ,
135
- use_cuda = use_cuda ,
136
- memory_opt = use_python_mem_opt )
137
- else :
138
- cur_first_loss , cur_last_loss = self .check_network_convergence (
139
- self .network ,
140
- use_cuda = use_cuda ,
141
- memory_opt = use_python_mem_opt )
142
-
143
- self .assertAlmostEquals (
144
- np .mean (baseline_last_loss ),
145
- np .mean (cur_last_loss ),
146
- delta = 1e-2 )
147
- self .assertAlmostEquals (
148
- np .mean (baseline_first_loss ),
149
- np .mean (cur_first_loss ),
150
- delta = 1e-2 )
129
+ self .setup_reader ()
130
+
131
+ with fluid .program_guard (fluid .Program (), fluid .Program ()):
132
+ with fluid .scope_guard (core .Scope ()):
133
+ baseline_first_loss , baseline_last_loss = self .check_network_convergence (
134
+ self .network )
135
+
136
+ cur_first_loss , cur_last_loss = self .check_network_convergence (
137
+ self .network , memory_opt = False )
138
+
139
+ self .assertAlmostEquals (
140
+ np .mean (baseline_last_loss ),
141
+ np .mean (cur_last_loss ),
142
+ delta = 1e-6 )
143
+ self .assertAlmostEquals (
144
+ np .mean (baseline_first_loss ),
145
+ np .mean (cur_first_loss ),
146
+ delta = 1e-6 )
0 commit comments