-
Notifications
You must be signed in to change notification settings - Fork 29.2k
Access profiler from cpp #16580
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
Access profiler from cpp #16580
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,7 @@ | |
| #include <torch/csrc/autograd/function.h> | ||
|
|
||
| #include <sstream> | ||
| #include <fstream> | ||
|
|
||
| namespace torch { namespace autograd { namespace profiler { | ||
|
|
||
|
|
@@ -192,4 +193,79 @@ double Event::cuda_elapsed_us(const Event & e) { | |
|
|
||
| CUDAStubs::~CUDAStubs() = default; | ||
|
|
||
|
|
||
| static jit::CodeTemplate event_template(R"( | ||
| { | ||
| "name": "${name}", | ||
| "ph": "X", | ||
| "ts": ${ts}, | ||
| "dur": ${dur}, | ||
| "tid": ${tid}, | ||
| "pid": "CPU Functions", | ||
| "args": {} | ||
| })"); | ||
|
|
||
|
|
||
| RecordProfile::RecordProfile(std::ostream& out) | ||
| : out_(out) { | ||
| init(); | ||
| } | ||
|
|
||
| RecordProfile::RecordProfile(const std::string& filename) | ||
| : file_(new std::ofstream(filename)), out_(*file_) { | ||
| init(); | ||
| } | ||
|
|
||
| void RecordProfile::init() { | ||
| enableProfiler(ProfilerState::CPU); | ||
| } | ||
|
|
||
| RecordProfile::~RecordProfile() { | ||
| thread_event_lists event_lists = disableProfiler(); | ||
| std::vector<Event*> events; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is kind of unnecessary. We could simply have this double loop inside
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Plus you don't even reserve memory, meaning that we will keep reallocating the storage many many times |
||
| for(auto& l : event_lists) { | ||
| for(auto& e : l) { | ||
| events.push_back(&e); | ||
| } | ||
| } | ||
| processEvents(events); | ||
| if (file_){ | ||
| file_->close(); | ||
| } | ||
| } | ||
|
|
||
| void RecordProfile::processEvents(const std::vector<Event*>& events) { | ||
| AT_CHECK(out_, "could not open file"); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: check in constructor (when the file is actually opened)? |
||
| Event* start = nullptr; | ||
| for (Event* e : events) { | ||
| if(0 == strcmp(e->name(), "__start_profile")) { | ||
| start = e; | ||
| break; | ||
| } | ||
| } | ||
| AT_CHECK(start, "could not find start?"); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| std::vector<Event*> stack; | ||
| out_ << "[\n"; | ||
| bool first = true; | ||
| for(Event* e : events) { | ||
| if(e->kind() == "push") { | ||
| stack.push_back(e); | ||
| } else if(e->kind() == "pop") { | ||
| if(!first) { | ||
| out_ << ",\n"; | ||
| } | ||
| first = false; | ||
| Event* e_start = stack.back(); | ||
| stack.pop_back(); | ||
| jit::TemplateEnv env; | ||
| env.s("name", e_start->name()); | ||
| env.d("ts", start->cpu_elapsed_us(*e_start)); | ||
| env.d("dur", e_start->cpu_elapsed_us(*e)); | ||
| env.d("tid", e_start->thread_id()); | ||
| out_ << event_template.format(env); | ||
| } | ||
| } | ||
| out_ << "]\n"; | ||
| } | ||
|
|
||
| }}} | ||
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 guess it would be nice to take this as a constructor argument too