Skip to content

Commit ab86fb1

Browse files
committed
complete parallel accuracy test
1 parent 415460b commit ab86fb1

File tree

2 files changed

+39
-13
lines changed

2 files changed

+39
-13
lines changed

python/paddle/fluid/parallel_executor.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,8 @@ def run(self, fetch_list, feed={}, feed_dict={}):
130130
or numpy array.
131131
:return: fetched value list.
132132
"""
133-
feed = feed_dict
133+
if feed == {}:
134+
feed = feed_dict
134135
if not isinstance(feed, dict):
135136
raise TypeError("feed should be a dict")
136137

python/paddle/fluid/tests/unittests/test_parallel_executor.py

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -200,17 +200,29 @@ class TestParallelExecutorBase(unittest.TestCase):
200200
def check_network_convergence(self,
201201
method,
202202
memory_opt=True,
203-
iter=10,
203+
iter=50,
204204
batch_size=None,
205205
allow_op_delay=False,
206206
feed_dict={},
207207
seed=None,
208208
use_parallel_executor=True):
209+
def run_executor(exe, feed, fetch_list, program=None):
210+
if isinstance(exe, fluid.ParallelExecutor):
211+
res = exe.run(fetch_list=fetch_list, feed=feed)
212+
elif isinstance(exe, fluid.Executor):
213+
if program is None:
214+
program = fluid.default_main_program()
215+
res = exe.run(program=program, feed=feed, fetch_list=fetch_list)
216+
else:
217+
raise ValueError('Unkown type exe')
218+
return res
219+
209220
main = fluid.Program()
210221
startup = fluid.Program()
211222
with fluid.program_guard(main, startup):
212223
if seed is not None:
213224
startup.random_seed = seed
225+
main.random_seed = seed
214226
loss = method(use_feed=len(feed_dict) > 0)
215227
adam = fluid.optimizer.Adam()
216228
adam.minimize(loss)
@@ -229,13 +241,15 @@ def check_network_convergence(self,
229241
if batch_size is not None:
230242
batch_size *= fluid.core.get_cuda_device_count()
231243
begin = time.time()
232-
first_loss, = exe.run([loss.name], feed=feed_dict)
244+
first_loss, = run_executor(
245+
exe=exe, feed=feed_dict, fetch_list=[loss.name])
233246
first_loss = numpy.array(first_loss)
234247

235248
for i in xrange(iter):
236-
exe.run([], feed=feed_dict)
249+
run_executor(exe=exe, feed=feed_dict, fetch_list=[])
237250

238-
last_loss, = exe.run([loss.name], feed=feed_dict)
251+
last_loss, = run_executor(
252+
exe=exe, feed=feed_dict, fetch_list=[loss.name])
239253
end = time.time()
240254

241255
if batch_size is not None:
@@ -277,14 +291,25 @@ def test_simple_fc(self):
277291
"label": label})
278292

279293
def test_simple_fc_parallel_accuracy(self):
280-
#single_first_loss, single_last_loss = self.check_network_convergence(
281-
# simple_fc_net, seed=0, use_parallel_executor=False)
282-
#parallel_first_loss, parallel_last_loss = self.check_network_convergence(
283-
# simple_fc_net, seed=0, use_parallel_executor=True)
284-
print('single_first_loss=', single_first_loss)
285-
print('single_last_loss=', single_last_loss)
286-
print('parallel_first_loss=', parallel_first_loss)
287-
print('parallel_last_loss=', parallel_last_loss)
294+
img = numpy.zeros(shape=[32, 784], dtype='float32')
295+
label = numpy.ones(shape=[32, 1], dtype='int64')
296+
single_first_loss, single_last_loss = self.check_network_convergence(
297+
method=simple_fc_net,
298+
seed=1000,
299+
feed_dict={"image": img,
300+
"label": label},
301+
use_parallel_executor=False)
302+
parallel_first_loss, parallel_last_loss = self.check_network_convergence(
303+
method=simple_fc_net,
304+
seed=1000,
305+
feed_dict={"image": img,
306+
"label": label},
307+
use_parallel_executor=True)
308+
309+
for p_f in parallel_first_loss:
310+
self.assertAlmostEquals(p_f, single_first_loss[0], delta=1e-6)
311+
for p_l in parallel_last_loss:
312+
self.assertAlmostEquals(p_l, single_last_loss[0], delta=1e-6)
288313

289314
def test_batchnorm_fc(self):
290315
self.check_network_convergence(fc_with_batchnorm)

0 commit comments

Comments
 (0)