Skip to content

Commit 964d631

Browse files
committed
feat: add unittest of memory usage
1 parent 59c900e commit 964d631

File tree

1 file changed

+80
-0
lines changed

1 file changed

+80
-0
lines changed
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from __future__ import print_function
16+
import paddle
17+
import paddle.fluid as fluid
18+
import contextlib
19+
import unittest
20+
21+
22+
def train_simulator(use_cuda, test_batch_size=10):
23+
if test_batch_size <= 0:
24+
raise ValueError("batch_size should be a positive integeral value, "
25+
"but got batch_size={}".format(test_batch_size))
26+
27+
x = fluid.layers.data(name='x', shape=[13], dtype='float32')
28+
y_predict = fluid.layers.fc(input=x, size=1, act=None)
29+
y = fluid.layers.data(name='y', shape=[1], dtype='float32')
30+
31+
cost = fluid.layers.square_error_cost(input=y_predict, label=y)
32+
avg_cost = fluid.layers.mean(cost)
33+
34+
sgd_optimizer = fluid.optimizer.SGD(learning_rate=0.001)
35+
sgd_optimizer.minimize(avg_cost)
36+
37+
train_reader = paddle.batch(
38+
paddle.reader.shuffle(
39+
paddle.dataset.uci_housing.train(), buf_size=500),
40+
batch_size=test_batch_size)
41+
42+
place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace()
43+
exe = fluid.Executor(place)
44+
45+
lower_usage, upper_usage, unit = fluid.contrib.memory_usage(
46+
fluid.default_main_program(), batch_size=test_batch_size)
47+
48+
print("memory usage is about %.3f - %.3f %s" %
49+
(lower_usage, upper_usage, unit))
50+
51+
52+
class TestMemoryUsage(unittest.TestCase):
53+
def test_cpu(self):
54+
with self.program_scope_guard():
55+
train_simulator(use_cuda=False)
56+
57+
def test_cpu_with_unit_KB(self):
58+
with self.program_scope_guard():
59+
train_simulator(use_cuda=False, test_batch_size=1000)
60+
61+
def test_cpu_with_unit_MB(self):
62+
with self.program_scope_guard():
63+
train_simulator(use_cuda=False, test_batch_size=100000)
64+
65+
def test_cuda(self):
66+
with self.program_scope_guard():
67+
train_simulator(use_cuda=True)
68+
69+
@contextlib.contextmanager
70+
def program_scope_guard(self):
71+
prog = fluid.Program()
72+
startup_prog = fluid.Program()
73+
scope = fluid.core.Scope()
74+
with fluid.scope_guard(scope):
75+
with fluid.program_guard(prog, startup_prog):
76+
yield
77+
78+
79+
if __name__ == '__main__':
80+
unittest.main()

0 commit comments

Comments
 (0)