Skip to content
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

Enable profile for cpu userbenchmark #1759

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

WeizhuoZhang-intel
Copy link
Contributor

@WeizhuoZhang-intel WeizhuoZhang-intel commented Jul 10, 2023

Enable a new option --profile in cpu userbenchmark for profiling content based on torch.profiler.

Works for #1293

@facebook-github-bot
Copy link
Contributor

Hi @WeizhuoZhang-intel!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at [email protected]. Thanks!

@WeizhuoZhang-intel WeizhuoZhang-intel changed the title userbernchmark/cpu new features userbernchmark/cpu enable profling content Jul 10, 2023
@chuanqi129 chuanqi129 changed the title userbernchmark/cpu enable profling content Enable profile for cpu userbenchmark Jul 10, 2023
@chuanqi129 chuanqi129 requested a review from xuzhao9 July 10, 2023 09:06
print("[INFO] Collecting Profiling logs...")
with profiler.profile(activities=[profiler.ProfilerActivity.CPU]) as prof, profiler.record_function("model_inference"):
result: TorchBenchModelMetrics = get_model_test_metrics(model, metrics=metrics)
print(prof.key_averages().table(sort_by="self_cpu_time_total", row_limit=-1))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we also support to write the profiling results to a json file?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we also support to write the profiling results to a json file?

It's not easy to transfer this table to Json file. As Json file is not good at showing table-content in format.
The torch.profile provide a function for chrome_trace json file, which can only open in Chrome Browser or VScode.
And prof.key_averages().table() is suggested print out directly, as the line breaker \n will be printed at the end each line.
It looks like the command line output is the most suggested way.

>>> prof.key_averages().table()
'-------------------  ------------  ------------  ------------  ------------  ------------  ------------  \n               Name    Self CPU %      Self CPU   CPU total %     CPU total  CPU time avg    # of Calls  \n-------------------  ------------  ------------  ------------  ------------  ------------  ------------  \n    model_inference       100.00%     124.000us       100.00%     124.000us     124.000us             1  \n-------------------  ------------  ------------  ------------  ------------  ------------  ------------  \nSelf CPU time total: 124.000us\n'
>>> prof.key_averages()

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, seems it profile for all iterations, is it our expected behavior? refer profile in run.py, https://github.com/pytorch/benchmark/blob/main/run.py#L187

Copy link
Contributor

@xuzhao9 xuzhao9 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@@ -59,7 +60,13 @@ def run_config(config: TorchBenchModelConfig, metrics: List[str], dryrun: bool=F
# load the model instance within current process
model = load_model(config)
# get the model test metrics
result: TorchBenchModelMetrics = get_model_test_metrics(model, metrics=metrics)
if profile:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better to move the import torch.profiler as profiler under this line

result: TorchBenchModelMetrics = get_model_test_metrics(model, metrics=metrics)
if profile:
print("[INFO] Collecting Profiling logs...")
with profiler.profile(activities=[profiler.ProfilerActivity.CPU]) as prof, profiler.record_function("model_inference"):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not suggest to use "model_inference" in here, it also support train test

print("[INFO] Collecting Profiling logs...")
with profiler.profile(activities=[profiler.ProfilerActivity.CPU]) as prof, profiler.record_function("model_inference"):
result: TorchBenchModelMetrics = get_model_test_metrics(model, metrics=metrics)
print(prof.key_averages().table(sort_by="self_cpu_time_total", row_limit=-1))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, seems it profile for all iterations, is it our expected behavior? refer profile in run.py, https://github.com/pytorch/benchmark/blob/main/run.py#L187

@chuanqi129 chuanqi129 self-requested a review July 11, 2023 08:43
@xuzhao9
Copy link
Contributor

xuzhao9 commented Jul 20, 2023

@WeizhuoZhang-intel Can you please rebase this PR on the latest main branch?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants