@@ -0,0 +1,2742 @@
+#   Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.%0A#%0A# Licensed under the Apache License, Version 2.0 (the %22License%22);%0A# you may not use this file except in compliance with the License.%0A# You may obtain a copy of the License at%0A#%0A#     http://www.apache.org/licenses/LICENSE-2.0%0A#%0A# Unless required by applicable law or agreed to in writing, software%0A# distributed under the License is distributed on an %22AS IS%22 BASIS,%0A# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.%0A# See the License for the specific language governing permissions and%0A# limitations under the License.%0A%0Afrom __future__ import print_function%0Aimport paddle%0Aimport paddle.fluid as fluid%0Aimport contextlib%0Aimport unittest%0A%0A%0Adef train_simulator(use_cuda, test_batch_size=10):%0A    if test_batch_size %3C= 0:%0A        raise ValueError(%22batch_size should be a positive integeral value, %22%0A                         %22but got batch_size=%7B%7D%22.format(test_batch_size))%0A%0A    x = fluid.layers.data(name='x', shape=%5B13%5D, dtype='float32')%0A    y_predict = fluid.layers.fc(input=x, size=1, act=None)%0A    y = fluid.layers.data(name='y', shape=%5B1%5D, dtype='float32')%0A%0A    cost = fluid.layers.square_error_cost(input=y_predict, label=y)%0A    avg_cost = fluid.layers.mean(cost)%0A%0A    sgd_optimizer = fluid.optimizer.SGD(learning_rate=0.001)%0A    sgd_optimizer.minimize(avg_cost)%0A%0A    train_reader = paddle.batch(%0A        paddle.reader.shuffle(%0A            paddle.dataset.uci_housing.train(), buf_size=500),%0A        batch_size=test_batch_size)%0A%0A    place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace()%0A    exe = fluid.Executor(place)%0A%0A    lower_usage, upper_usage, unit = fluid.contrib.memory_usage(%0A        fluid.default_main_program(), batch_size=test_batch_size)%0A%0A    print(%22memory usage is about %25.3f - %25.3f %25s%22 %25%0A          (lower_usage, upper_usage, unit))%0A%0A%0Aclass TestMemoryUsage(unittest.TestCase):%0A    def test_cpu(self):%0A        with self.program_scope_guard():%0A            train_simulator(use_cuda=False)%0A%0A    def test_cpu_with_unit_KB(self):%0A        with self.program_scope_guard():%0A            train_simulator(use_cuda=False, test_batch_size=1000)%0A%0A    def test_cpu_with_unit_MB(self):%0A        with self.program_scope_guard():%0A            train_simulator(use_cuda=False, test_batch_size=100000)%0A%0A    def test_cuda(self):%0A        with self.program_scope_guard():%0A            train_simulator(use_cuda=True)%0A%0A    @contextlib.contextmanager%0A    def program_scope_guard(self):%0A        prog = fluid.Program()%0A        startup_prog = fluid.Program()%0A        scope = fluid.core.Scope()%0A        with fluid.scope_guard(scope):%0A            with fluid.program_guard(prog, startup_prog):%0A                yield%0A%0A%0Aif __name__ == '__main__':%0A    unittest.main()%0A

add unittest of memory usage