Skip to content

Commit 05a733b

Browse files
committed
Fix unit test bug in test_profiler.py.
1 parent eaabf2a commit 05a733b

File tree

1 file changed

+38
-28
lines changed

1 file changed

+38
-28
lines changed

python/paddle/v2/fluid/tests/test_profiler.py

Lines changed: 38 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -41,39 +41,49 @@ def test_nvprof(self):
4141
exe.run(fluid.default_main_program(), feed={'data': input})
4242
os.remove(output_file)
4343

44-
def test_profiler(self):
45-
image = fluid.layers.data(name='x', shape=[784], dtype='float32')
46-
hidden1 = fluid.layers.fc(input=image, size=128, act='relu')
47-
hidden2 = fluid.layers.fc(input=hidden1, size=64, act='relu')
48-
predict = fluid.layers.fc(input=hidden2, size=10, act='softmax')
49-
label = fluid.layers.data(name='y', shape=[1], dtype='int64')
50-
cost = fluid.layers.cross_entropy(input=predict, label=label)
51-
avg_cost = fluid.layers.mean(x=cost)
44+
def profiler(self, state):
45+
if state == 'GPU' and core.is_compile_gpu():
46+
return
47+
startup_program = fluid.Program()
48+
main_program = fluid.Program()
49+
50+
with fluid.program_guard(main_program, startup_program):
51+
image = fluid.layers.data(name='x', shape=[784], dtype='float32')
52+
hidden1 = fluid.layers.fc(input=image, size=128, act='relu')
53+
hidden2 = fluid.layers.fc(input=hidden1, size=64, act='relu')
54+
predict = fluid.layers.fc(input=hidden2, size=10, act='softmax')
55+
label = fluid.layers.data(name='y', shape=[1], dtype='int64')
56+
cost = fluid.layers.cross_entropy(input=predict, label=label)
57+
avg_cost = fluid.layers.mean(x=cost)
58+
accuracy = fluid.evaluator.Accuracy(input=predict, label=label)
59+
5260
optimizer = fluid.optimizer.Momentum(learning_rate=0.001, momentum=0.9)
53-
opts = optimizer.minimize(avg_cost)
54-
accuracy = fluid.evaluator.Accuracy(input=predict, label=label)
61+
opts = optimizer.minimize(avg_cost, startup_program=startup_program)
62+
63+
place = fluid.CPUPlace() if state == 'CPU' else fluid.CUDAPlace(0)
64+
exe = fluid.Executor(place)
65+
exe.run(startup_program)
5566

56-
states = ['CPU', 'GPU'] if core.is_compile_gpu() else ['CPU']
57-
for state in states:
58-
place = fluid.CPUPlace() if state == 'CPU' else fluid.CUDAPlace(0)
59-
exe = fluid.Executor(place)
60-
exe.run(fluid.default_startup_program())
67+
accuracy.reset(exe)
68+
with profiler.profiler(state, 'total') as prof:
69+
for iter in range(10):
70+
if iter == 2:
71+
profiler.reset_profiler()
72+
x = np.random.random((32, 784)).astype("float32")
73+
y = np.random.randint(0, 10, (32, 1)).astype("int64")
6174

62-
accuracy.reset(exe)
75+
outs = exe.run(main_program,
76+
feed={'x': x,
77+
'y': y},
78+
fetch_list=[avg_cost] + accuracy.metrics)
79+
acc = np.array(outs[1])
80+
pass_acc = accuracy.eval(exe)
6381

64-
with profiler.profiler(state, 'total') as prof:
65-
for iter in range(10):
66-
if iter == 2:
67-
profiler.reset_profiler()
68-
x = np.random.random((32, 784)).astype("float32")
69-
y = np.random.randint(0, 10, (32, 1)).astype("int64")
82+
def not_test_cpu_profiler(self):
83+
self.profiler('CPU')
7084

71-
outs = exe.run(fluid.default_main_program(),
72-
feed={'x': x,
73-
'y': y},
74-
fetch_list=[avg_cost] + accuracy.metrics)
75-
acc = np.array(outs[1])
76-
pass_acc = accuracy.eval(exe)
85+
def not_test_cuda_profiler(self):
86+
self.profiler('GPU')
7787

7888

7989
if __name__ == '__main__':

0 commit comments

Comments
 (0)