-
Notifications
You must be signed in to change notification settings - Fork 5.9k
Refine profiler and expose to Python. #7576
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
Conversation
A simple usage is as follows: image = fluid.layers.data(name='x', shape=[784], dtype='float32')
# ...
avg_cost = fluid.layers.mean(x=cost)
optimizer = fluid.optimizer.Momentum(learning_rate=0.001, momentum=0.9)
opts = optimizer.minimize(avg_cost)
accuracy = fluid.evaluator.Accuracy(input=predict, label=label)
place = fluid.CPUPlace() # or fluid.CUDAPlace(0)
exe = fluid.Executor(place)
exe.run(fluid.default_startup_program())
accuracy.reset(exe)
with profiler.profiler('CPU', 'total') as prof:
for iter in range(10):
if iter == 2:
profiler.reset_profiler()
x = np.random.random((32, 784)).astype("float32")
y = np.random.randint(0, 10, (32, 1)).astype("int64")
outs = exe.run(fluid.default_main_program(),
feed={'x': x,
'y': y},
fetch_list=[avg_cost] + accuracy.metrics)
acc = np.array(outs[1])
pass_acc = accuracy.eval(exe) |
kuke
left a comment
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.
Great. Thx.
python/paddle/v2/fluid/profiler.py
Outdated
| and GPU program. | ||
| Args: | ||
| state (string) : The profiler state, It should be 'CPU' or 'GPU'. |
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.
Feel that it is necessary to add more explanation about param state. Users may be confused why a state is needed after the place is given.
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.
Add more explanation here.
python/paddle/v2/fluid/profiler.py
Outdated
| Args: | ||
| state (string) : The profiler state, It should be 'CPU' or 'GPU'. | ||
| sorted_key (string) : If None, the profiler results will be printed |
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.
If None, the profiler results will be printed without sorting. -> If None, the profiling results will be printed in the order of first end time of events.
profiler results -> profiling results, the same below.
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/v2/fluid/profiler.py
Outdated
| without sorting. Otherwise, the profiler results will be sorted | ||
| by the this flag. This flag should be one of 'calls', 'total', | ||
| 'max', 'min' or 'ave'. | ||
| The `calls` means sorting by the calling counter. |
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.
the calling counter -> number of calls
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/v2/fluid/profiler.py
Outdated
| @contextmanager | ||
| def profiler(state, sorted_key=None): | ||
| """The profiler interface. | ||
| Different from cuda_profiler, this fuction can be used to profile both CPU |
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.
function -> profiler
Maybe CPU and GPU program -> CPU and GPU operator kernels is better.
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.
Modified the comments.
paddle/framework/executor.cc
Outdated
|
|
||
| platform::DeviceContextPool& pool = platform::DeviceContextPool::Instance(); | ||
| auto dev_ctx = const_cast<platform::DeviceContext*>(pool.Get(place_)); | ||
| platform::RecordEvent record_event(op->Type(), dev_ctx); |
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.
RecordEvent should not be always called, only when ProfilerState is not kDisabled.
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.
@chengduoZH Thanks for your review. Whether to record timeline is judged in the constructor of RecordEvent: https://github.com/PaddlePaddle/Paddle/blob/develop/paddle/platform/profiler.cc#L130
| ParseEvents(all_events, sorted_key); | ||
| ResetProfiler(); | ||
| } | ||
|
|
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.
I think there should have a ProfilerState GetProfilerState(), and this function is called in executor.cc.
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.
As the above reply. The less the operation, the better.
qingqing01
left a comment
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.
@kuke @chengduoZH Thanks!
| ParseEvents(all_events, sorted_key); | ||
| ResetProfiler(); | ||
| } | ||
|
|
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.
As the above reply. The less the operation, the better.
python/paddle/v2/fluid/profiler.py
Outdated
| @contextmanager | ||
| def profiler(state, sorted_key=None): | ||
| """The profiler interface. | ||
| Different from cuda_profiler, this fuction can be used to profile both CPU |
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.
Modified the comments.
python/paddle/v2/fluid/profiler.py
Outdated
| and GPU program. | ||
| Args: | ||
| state (string) : The profiler state, It should be 'CPU' or 'GPU'. |
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.
Add more explanation here.
python/paddle/v2/fluid/profiler.py
Outdated
| Args: | ||
| state (string) : The profiler state, It should be 'CPU' or 'GPU'. | ||
| sorted_key (string) : If None, the profiler results will be printed |
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/v2/fluid/profiler.py
Outdated
| without sorting. Otherwise, the profiler results will be sorted | ||
| by the this flag. This flag should be one of 'calls', 'total', | ||
| 'max', 'min' or 'ave'. | ||
| The `calls` means sorting by the calling counter. |
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.profiler('CPU') | ||
|
|
||
| def not_test_cuda_profiler(self): | ||
| self.profiler('GPU') |
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.
profiler does not be executed in the stage of unit tests, right?
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.
Fixed. Thanks!
kuke
left a comment
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.
Maybe need a bit improvement in comments. Thx
python/paddle/v2/fluid/profiler.py
Outdated
| to add more records. | ||
| Args: | ||
| state (string) : The profiling state, It should be 'CPU' or 'GPU'. |
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.
-> The profiling state, which should be CPU or GPU, telling the profiler to use CPU timer or GPU timer for profiling.
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/v2/fluid/profiler.py
Outdated
| Args: | ||
| state (string) : The profiling state, It should be 'CPU' or 'GPU'. | ||
| Although users may define CPUPlace or CUDAPlace when using Fluid, |
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.
Although users may define CPUPlace or CUDAPlace when using Fluid, the profiler doesn't get the state based on this Place. Since the implementation is an independent part from the Fluid.
->
Although users may have already specified the execution place(CPUPlace/CUDAPlace) in the begining, for flexibility the profiler doesn't inherit this place.
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.
|
LGTM |
chengduoZH
left a comment
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
chengduoZH
left a comment
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
Fix #7577