Skip to content

Commit 99661bd

Browse files
committed
polish the code
1 parent f6fc880 commit 99661bd

File tree

2 files changed

+278
-194
lines changed

2 files changed

+278
-194
lines changed

python/paddle/profiler/profiler.py

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -268,10 +268,10 @@ class Profiler:
268268
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.
269269
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,
270270
which means profiling range [start_batch, end_batch).
271-
on_trace_ready (callable): callable object, takes the Profiler object as parameter, which provides a way for users to do post-processing.
272-
This callable object will be called when ``scheduler`` returns ``ProfilerState.RECORD_AND_RETURN``.
273-
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
274-
be timed and profiled.
271+
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.
272+
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/).
273+
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
274+
be timed and profiled. Default: False.
275275
276276
Examples:
277277
1. profiling range [2, 5).
@@ -322,6 +322,7 @@ class Profiler:
322322
4. Use profiler to get throughput and cost of the model
323323
324324
.. code-block:: python
325+
:name: code-example-timer1
325326
326327
import paddle
327328
import paddle.profiler as profiler
@@ -368,8 +369,19 @@ def forward(self, image, label=None):
368369
if i % 10 == 0:
369370
step_info = p.step_info(unit='images')
370371
print("Iter {}: {}".format(i, step_info))
372+
# The average statistics for 10 steps between the last and this call will be
373+
# printed when the "step_info" is called at 10 iteration intervals.
374+
# The values you get may be different from the following.
375+
# Iter 0: reader_cost: 0.51946 s batch_cost: 0.66077 s ips: 6.054 images/s
376+
# Iter 10: reader_cost: 0.00014 s batch_cost: 0.00441 s ips: 907.009 images/s
371377
p.stop()
372-
378+
# The performance summary will be automatically printed when the "stop" is called.
379+
# Reader Ratio: 2.658%
380+
# Time Unit: s, IPS Unit: images/s
381+
# | | avg | max | min |
382+
# | reader_cost | 0.00011 | 0.00013 | 0.00007 |
383+
# | batch_cost | 0.00405 | 0.00434 | 0.00326 |
384+
# | ips | 1086.42904 | 1227.30604 | 959.92796 |
373385
"""
374386

375387
def __init__(
@@ -455,6 +467,7 @@ def start(self):
455467
#train()
456468
prof.step()
457469
prof.stop()
470+
458471
'''
459472
# Timing only without profiling
460473
benchmark().begin()
@@ -519,7 +532,7 @@ def step(self, num_samples: Optional[int]=None):
519532
Signals the profiler that the next profiling step has started.
520533
Get the new ProfilerState and trigger corresponding action.
521534
522-
Parameters:
535+
Args:
523536
num_samples (int|None, optional): Specifies the batch size of every step of the model
524537
that is used to compute throughput when timer_only is True. Default: None.
525538
@@ -569,7 +582,7 @@ def step_info(self, unit=None):
569582
or others depends on the `unit`. When `num_samples` of `step()` is None, it is
570583
measured in `steps/s`.
571584
572-
Parameters:
585+
Args:
573586
unit (string, optional): The unit of input data is only used When `num_samples`
574587
of `step()` is specified as a number. For example, when it is `images`, the unit
575588
of throughput is `images/s`. Default: None, the unit of throughput is `samples/s`.
@@ -578,6 +591,7 @@ def step_info(self, unit=None):
578591
string: A string representing the statistic.
579592
Examples:
580593
.. code-block:: python
594+
:name: code-example-timer2
581595
582596
import paddle.profiler as profiler
583597
prof = profiler.Profiler(timer_only=True)
@@ -586,8 +600,15 @@ def step_info(self, unit=None):
586600
#train()
587601
prof.step()
588602
if iter % 10 == 0:
589-
print(prof.step_info())
603+
print("Iter {}: {}".format(iter, prof.step_info()))
604+
# The example does not call the DataLoader, so there is no "reader_cost".
605+
# Iter 0: batch_cost: 0.00001 s ips: 86216.623 steps/s
606+
# Iter 10: batch_cost: 0.00001 s ips: 103645.034 steps/s
590607
prof.stop()
608+
# Time Unit: s, IPS Unit: steps/s
609+
# | | avg | max | min |
610+
# | batch_cost | 0.00000 | 0.00002 | 0.00000 |
611+
# | ips | 267846.19437 | 712030.38727 | 45134.16662 |
591612
"""
592613
if unit is None:
593614
unit = 'samples'

0 commit comments

Comments
 (0)