Skip to content

Commit ea31b4d

Browse files
committed
Add unit test.
1 parent 483b840 commit ea31b4d

File tree

3 files changed

+57
-5
lines changed

3 files changed

+57
-5
lines changed

paddle/fluid/framework/parallel_executor.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ class ParallelExecutorPrivate {
4343
#endif
4444
};
4545

46+
std::vector<Scope *> &ParallelExecutor::GetLocalScopes() {
47+
return member_->local_scopes_;
48+
}
49+
4650
ParallelExecutor::ParallelExecutor(
4751
size_t num_threads, bool use_event,
4852
const std::vector<platform::Place> &places,

python/paddle/fluid/parallel_executor.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,12 @@
2323
class ParallelExecutor(object):
2424
def __init__(self,
2525
use_cuda,
26-
main_program,
27-
startup_program,
2826
loss_name=None,
27+
main_program=None,
28+
startup_program=None,
2929
num_threads=None,
3030
allow_op_delay=False,
31+
run_startup=True,
3132
share_vars_from=None):
3233
self._places = []
3334
self._act_places = []
@@ -55,13 +56,16 @@ def __init__(self,
5556

5657
main = main_program
5758
startup = startup_program
59+
main = main if main else framework.default_main_program()
60+
startup = startup if startup else framework.default_startup_program()
5861
scope = executor.global_scope()
5962

60-
if startup:
63+
if run_startup:
6164
exe = executor.Executor(self._act_places[0])
6265
exe.run(startup)
6366

64-
local_scopes = share_vars_from.local_scopes() if share_vars_from else []
67+
local_scopes = share_vars_from.executor.local_scopes(
68+
) if share_vars_from else []
6569

6670
self.executor = core.ParallelExecutor(
6771
num_threads,

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

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ def check_network_convergence(self,
207207
if memory_opt:
208208
fluid.memory_optimize(main)
209209

210-
exe = fluid.ParallelExecutor(loss_name=loss.name, use_cuda=True)
210+
exe = fluid.ParallelExecutor(True, loss_name=loss.name)
211211
if batch_size is not None:
212212
batch_size *= fluid.core.get_cuda_device_count()
213213
begin = time.time()
@@ -453,3 +453,47 @@ def setUpClass(cls):
453453
@unittest.skip("transformer is buggy in multi gpu")
454454
def test_main(self):
455455
self.check_network_convergence(transformer)
456+
457+
458+
class ParallelExecutorTestingDuringTraining(unittest.TestCase):
459+
def test_parallel_testing(self):
460+
main = fluid.Program()
461+
startup = fluid.Program()
462+
with fluid.program_guard(main, startup):
463+
loss = simple_fc_net(True)
464+
test_program = main.clone(for_test=True)
465+
466+
opt = fluid.optimizer.SGD(learning_rate=0.0001)
467+
opt.minimize(loss)
468+
469+
batch_size = 32
470+
image = numpy.random.normal(size=(batch_size,
471+
784)).astype('float32')
472+
label = numpy.random.randint(0, 10, (batch_size, 1), dtype="int64")
473+
place = fluid.CUDAPlace(0)
474+
im_t = fluid.LoDTensor()
475+
im_t.set(image, place)
476+
lbl_t = fluid.LoDTensor()
477+
lbl_t.set(label, place)
478+
feed_dict = {'image': im_t, 'label': lbl_t}
479+
480+
train_exe = fluid.ParallelExecutor(
481+
loss_name=loss.name,
482+
use_cuda=True,
483+
main_program=main,
484+
startup_program=startup)
485+
486+
test_exe = fluid.ParallelExecutor(
487+
use_cuda=True,
488+
main_program=test_program,
489+
startup_program=startup,
490+
run_startup=False,
491+
share_vars_from=train_exe)
492+
493+
for i in xrange(5):
494+
test_loss, = test_exe.run([loss.name], feed_dict=feed_dict)
495+
test_loss = numpy.array(test_loss)
496+
497+
train_loss, = train_exe.run([loss.name], feed_dict=feed_dict)
498+
train_loss = numpy.array(train_loss)
499+
self.assertTrue(numpy.allclose(train_loss, test_loss))

0 commit comments

Comments
 (0)