Skip to content

Commit

Permalink
polish the code
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangting2020 committed Mar 28, 2022
1 parent f6fc880 commit 99661bd
Show file tree
Hide file tree
Showing 2 changed files with 278 additions and 194 deletions.
37 changes: 29 additions & 8 deletions python/paddle/profiler/profiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,10 +268,10 @@ class Profiler:
scheduler (Callable|tuple, optional): If it is a callable object, it takes a step number as parameter and return the corresponding :ref:`ProfilerState <api_paddle_profiler_ProfilerState>`. This callable object can be generated by :ref:`make_scheduler <api_paddle_profiler_make_scheduler>` function.
If not provided (None), the default scheduler will keep tracing until the profiler exits. If it is a tuple, it has two values start_batch and end_batch,
which means profiling range [start_batch, end_batch).
on_trace_ready (callable): callable object, takes the Profiler object as parameter, which provides a way for users to do post-processing.
This callable object will be called when ``scheduler`` returns ``ProfilerState.RECORD_AND_RETURN``.
timer_only (bool): If it is True, the cost of Dataloader and every step of the model will be count without profiling. Otherwise, the model will
be timed and profiled.
on_trace_ready (Callable, optional): Callable object, serves as callback function, and takes the Profiler object as parameter, which provides a way for users to do post-processing.
This callable object will be called when ``scheduler`` returns ``ProfilerState.RECORD_AND_RETURN``. The default value is :ref:`export_chrome_tracing <api_paddle_profiler_export_chrome_tracing>` (./profiler_log/).
timer_only (bool, optional): If it is True, the cost of Dataloader and every step of the model will be count without profiling. Otherwise, the model will
be timed and profiled. Default: False.
Examples:
1. profiling range [2, 5).
Expand Down Expand Up @@ -322,6 +322,7 @@ class Profiler:
4. Use profiler to get throughput and cost of the model
.. code-block:: python
:name: code-example-timer1
import paddle
import paddle.profiler as profiler
Expand Down Expand Up @@ -368,8 +369,19 @@ def forward(self, image, label=None):
if i % 10 == 0:
step_info = p.step_info(unit='images')
print("Iter {}: {}".format(i, step_info))
# The average statistics for 10 steps between the last and this call will be
# printed when the "step_info" is called at 10 iteration intervals.
# The values you get may be different from the following.
# Iter 0: reader_cost: 0.51946 s batch_cost: 0.66077 s ips: 6.054 images/s
# Iter 10: reader_cost: 0.00014 s batch_cost: 0.00441 s ips: 907.009 images/s
p.stop()
# The performance summary will be automatically printed when the "stop" is called.
# Reader Ratio: 2.658%
# Time Unit: s, IPS Unit: images/s
# | | avg | max | min |
# | reader_cost | 0.00011 | 0.00013 | 0.00007 |
# | batch_cost | 0.00405 | 0.00434 | 0.00326 |
# | ips | 1086.42904 | 1227.30604 | 959.92796 |
"""

def __init__(
Expand Down Expand Up @@ -455,6 +467,7 @@ def start(self):
#train()
prof.step()
prof.stop()
'''
# Timing only without profiling
benchmark().begin()
Expand Down Expand Up @@ -519,7 +532,7 @@ def step(self, num_samples: Optional[int]=None):
Signals the profiler that the next profiling step has started.
Get the new ProfilerState and trigger corresponding action.
Parameters:
Args:
num_samples (int|None, optional): Specifies the batch size of every step of the model
that is used to compute throughput when timer_only is True. Default: None.
Expand Down Expand Up @@ -569,7 +582,7 @@ def step_info(self, unit=None):
or others depends on the `unit`. When `num_samples` of `step()` is None, it is
measured in `steps/s`.
Parameters:
Args:
unit (string, optional): The unit of input data is only used When `num_samples`
of `step()` is specified as a number. For example, when it is `images`, the unit
of throughput is `images/s`. Default: None, the unit of throughput is `samples/s`.
Expand All @@ -578,6 +591,7 @@ def step_info(self, unit=None):
string: A string representing the statistic.
Examples:
.. code-block:: python
:name: code-example-timer2
import paddle.profiler as profiler
prof = profiler.Profiler(timer_only=True)
Expand All @@ -586,8 +600,15 @@ def step_info(self, unit=None):
#train()
prof.step()
if iter % 10 == 0:
print(prof.step_info())
print("Iter {}: {}".format(iter, prof.step_info()))
# The example does not call the DataLoader, so there is no "reader_cost".
# Iter 0: batch_cost: 0.00001 s ips: 86216.623 steps/s
# Iter 10: batch_cost: 0.00001 s ips: 103645.034 steps/s
prof.stop()
# Time Unit: s, IPS Unit: steps/s
# | | avg | max | min |
# | batch_cost | 0.00000 | 0.00002 | 0.00000 |
# | ips | 267846.19437 | 712030.38727 | 45134.16662 |
"""
if unit is None:
unit = 'samples'
Expand Down
Loading

0 comments on commit 99661bd

Please sign in to comment.