From 56acb0cf02e43cddb85610d39a56c15677b66c46 Mon Sep 17 00:00:00 2001 From: guru4elephant <35550832+guru4elephant@users.noreply.github.com> Date: Fri, 24 May 2019 16:10:10 +0800 Subject: [PATCH] add more specific example for timeline (#868) --- .../development/profiling/timeline_cn.md | 45 ++++++++++++++---- .../development/profiling/timeline_en.md | 46 +++++++++++++++---- 2 files changed, 71 insertions(+), 20 deletions(-) diff --git a/doc/fluid/advanced_usage/development/profiling/timeline_cn.md b/doc/fluid/advanced_usage/development/profiling/timeline_cn.md index 0ee02ae34..5f7840006 100644 --- a/doc/fluid/advanced_usage/development/profiling/timeline_cn.md +++ b/doc/fluid/advanced_usage/development/profiling/timeline_cn.md @@ -8,16 +8,41 @@ 请不要在timeline记录信息时运行太多次迭代,因为timeline中的记录数量和迭代次数是成正比的。 ```python - for pass_id in range(pass_num): - for batch_id, data in enumerate(train_reader()): - if pass_id == 0 and batch_id == 5: - profiler.start_profiler("All") - elif pass_id == 0 and batch_id == 10: - profiler.stop_profiler("total", "/tmp/profile") - exe.run(fluid.default_main_program(), - feed=feeder.feed(data), - fetch_list=[]) - ... + import numpy as np + import paddle + import paddle.fluid as fluid + from paddle.fluid import profiler + + place = fluid.CPUPlace() + + def reader(): + for i in range(100): + yield [np.random.random([4]).astype('float32'), np.random.random([3]).astype('float32')], + + main_program = fluid.Program() + startup_program = fluid.Program() + + with fluid.program_guard(main_program, startup_program): + data_1 = fluid.layers.data(name='data_1', shape=[1, 2, 2]) + data_2 = fluid.layers.data(name='data_2', shape=[1, 1, 3]) + out = fluid.layers.fc(input=[data_1, data_2], size=2) + # ... + + feeder = fluid.DataFeeder([data_1, data_2], place) + exe = fluid.Executor(place) + exe.run(startup_program) + pass_num = 10 + + for pass_id in range(pass_num): + for batch_id, data in enumerate(reader()): + if pass_id == 0 and batch_id == 5: + profiler.start_profiler("All") + elif pass_id == 0 and batch_id == 10: + profiler.stop_profiler("total", "/tmp/profile") + outs = exe.run(program=main_program, + feed=feeder.feed(data), + fetch_list=[out]) + ``` 1. 运行`python paddle/tools/timeline.py`来处理`/tmp/profile`,这个程序默认会生成一个`/tmp/timeline`文件,你也可以用命令行参数来修改这个路径,请参考[timeline.py](https://github.com/PaddlePaddle/Paddle/blob/develop/tools/timeline.py)。 diff --git a/doc/fluid/advanced_usage/development/profiling/timeline_en.md b/doc/fluid/advanced_usage/development/profiling/timeline_en.md index 6f4c06d77..7d7f0fbe7 100644 --- a/doc/fluid/advanced_usage/development/profiling/timeline_en.md +++ b/doc/fluid/advanced_usage/development/profiling/timeline_en.md @@ -5,16 +5,42 @@ 1. Add `profiler.start_profiler(...)` and `profiler.stop_profiler(...)` to the main training loop. After run, the code will generate a profile record file `/tmp/profile`. **Warning**: Please do not run too many batches when use profiler to record timeline information, for the profile record will grow with the batch number. ```python - for pass_id in range(pass_num): - for batch_id, data in enumerate(train_reader()): - if pass_id == 0 and batch_id == 5: - profiler.start_profiler("All") - elif pass_id == 0 and batch_id == 10: - profiler.stop_profiler("total", "/tmp/profile") - exe.run(fluid.default_main_program(), - feed=feeder.feed(data), - fetch_list=[]) - ... + + import numpy as np + import paddle + import paddle.fluid as fluid + from paddle.fluid import profiler + + place = fluid.CPUPlace() + + def reader(): + for i in range(100): + yield [np.random.random([4]).astype('float32'), np.random.random([3]).astype('float32')], + + main_program = fluid.Program() + startup_program = fluid.Program() + + with fluid.program_guard(main_program, startup_program): + data_1 = fluid.layers.data(name='data_1', shape=[1, 2, 2]) + data_2 = fluid.layers.data(name='data_2', shape=[1, 1, 3]) + out = fluid.layers.fc(input=[data_1, data_2], size=2) + # ... + + feeder = fluid.DataFeeder([data_1, data_2], place) + exe = fluid.Executor(place) + exe.run(startup_program) + pass_num = 10 + + for pass_id in range(pass_num): + for batch_id, data in enumerate(reader()): + if pass_id == 0 and batch_id == 5: + profiler.start_profiler("All") + elif pass_id == 0 and batch_id == 10: + profiler.stop_profiler("total", "/tmp/profile") + outs = exe.run(program=main_program, + feed=feeder.feed(data), + fetch_list=[out]) + ``` 2. Run `python paddle/tools/timeline.py` to process `/tmp/profile`, it will generate another -- GitLab