-
Notifications
You must be signed in to change notification settings - Fork 5.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add timer tool to Profiler #40386
Add timer tool to Profiler #40386
Conversation
Thanks for your contribution! |
python/paddle/profiler/profiler.py
Outdated
@@ -371,11 +383,14 @@ def stop(self): | |||
if self.on_trace_ready: | |||
self.on_trace_ready(self) | |||
|
|||
def step(self): | |||
def step(self, num_samples=None): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
是否需要支持允许指定单位?或者作为step_info
的参数?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
目前修改为在step_info中,通过unit参数指定样本单位,比如unit='images',吞吐量则表示为images/s
Sorry to inform you that 27f7dc2's CIs have passed for more than 7 days. To prevent PR conflicts, you need to re-run all CIs manually. |
27f7dc2
to
e5367af
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
和@Xreki 讨论了下,这块step_indo,在API中已经说明了统计的是2次调用之间经过的迭代的均值,所以不再额外增加avg字段了。 |
1b01098
to
3c45e1c
Compare
275ba57
to
51088e9
Compare
@@ -256,6 +257,8 @@ def __next__(self): | |||
event_type=profiler.TracerEventType.Dataloader) | |||
trace_event.begin() | |||
try: | |||
benchmark().check_if_need_record(self) | |||
benchmark().before_reader() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
check_if_need_record
是否在before_reader
中自动调用比较好?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
因为check_if_need_record要接受一个reader参数,和hook的几个基本接口的参数列表不一样。这块为了保持接口的一致性,这个函数单独抽出来
python/paddle/profiler/profiler.py
Outdated
@@ -253,6 +254,8 @@ class Profiler: | |||
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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
写下默认值。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
p.step(num_samples=BATCH_SIZE) | ||
if i % 10 == 0: | ||
step_info = p.step_info(unit='images') | ||
print("Iter {}: {}".format(i, step_info)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
用注释的方式写下输出。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
simple_net = SimpleNet() | ||
opt = paddle.optimizer.SGD(learning_rate=1e-3, | ||
parameters=simple_net.parameters()) | ||
BATCH_SIZE = 4 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
python里面不怎么用这种命名风格吧?batch_size
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
因为这里是一个常量名,python代码风格一般是用大写的
python/paddle/profiler/timer.py
Outdated
return None | ||
|
||
|
||
class Hook: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
python类型最好都继承object?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
python/paddle/profiler/timer.py
Outdated
self.print_stats('batch_cost', summary['batch_summary']) | ||
self.print_stats('ips', summary['ips_summary']) | ||
|
||
def print_stats(self, item, message_dict): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
内部函数可以加_
前缀
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
self.reader = None | ||
self.need_record = True | ||
self.speed_mode = 'samples/s' | ||
self.speed_unit = 'samples/s' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
为啥要设置speed_mode
和speed_unit
两个?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
speed_mode是速度的表示模式,用来确定是采用吞吐量samples/s, 还是steps/s给出性能,它取决于num_samples的设置。
speed_unit是速度的单位,取决于用户在step_info调用时自定义的样本单位,这个值仅仅在speed_mode为samples/s时生效,用于产生images/s, words/s等不同的吞吐量单位
python/paddle/profiler/timer.py
Outdated
return | ||
reader_cost = timeit.default_timer() - self.start_reader | ||
benchmark.current_event.record_reader(reader_cost) | ||
if benchmark.current_event.total_iters >= benchmark.current_event.skip_iter: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个逻辑会不会放在Event
里面比较好?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
python/paddle/profiler/timer.py
Outdated
return | ||
batch_cost = timeit.default_timer() - self.start_time | ||
benchmark.current_event.record_batch(batch_cost, benchmark.num_samples) | ||
if benchmark.current_event.total_iters >= benchmark.current_event.skip_iter: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个逻辑会不会放在Event
里面比较好?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
python/paddle/profiler/profiler.py
Outdated
#train() | ||
prof.step() | ||
if iter % 10 == 0: | ||
print(prof.step_info()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
以注释的方式,写一下打印出来的内容
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
d254a09
to
1ce7507
Compare
python/paddle/profiler/profiler.py
Outdated
# 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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
每个iter的log放在L370之后,总的log放在L371之后,表明哪个函数产生哪样的log。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
python/paddle/profiler/profiler.py
Outdated
print("Iter {}: {}".format(iter, prof.step_info())) | ||
prof.stop() | ||
|
||
# The example does not call the DataLoader, so there is no "reader_cost". |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
同上。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
1ce7507
to
99661bd
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
|
||
Returns: | ||
string: A string representing the statistic. | ||
Examples: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add a black line before Examples
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
python/paddle/profiler/profiler.py
Outdated
|
||
import paddle | ||
import paddle.profiler as profiler | ||
import numpy as np |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please use Paddle's API create Tensor instead of numpy
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
TODO:Fix docs
f0f1cdd
to
3fb9373
Compare
3fb9373
to
5cc1bcd
Compare
5cc1bcd
to
81ac3a6
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM for API docs
PR types
Function optimizationPR changes
OthersDescribe
Add timer tool to Profiler实现说明
结果展示
doc