Skip to content

Commit

Permalink
[profiler] Expose experimental performance events to python (pytorch#…
Browse files Browse the repository at this point in the history
…87905)

Reports total counts (includes time spent in all children), self counts can be calculated manully.

Differential Revision: [D40282770](https://our.internmc.facebook.com/intern/diff/D40282770/)
Pull Request resolved: pytorch#87905
Approved by: https://github.com/SS-JIA
  • Loading branch information
digantdesai authored and kulinseth committed Nov 5, 2022
1 parent fdf6b61 commit 85e7c06
Showing 1 changed file with 30 additions and 7 deletions.
37 changes: 30 additions & 7 deletions torch/csrc/profiler/python/init.cpp
Expand Up @@ -54,7 +54,8 @@ void initPythonBindings(PyObject* module) {
py::init<
std::vector<std::string> /* profiler_metrics */,
bool /* profiler_measure_per_kernel */,
bool /* verbose */
bool /* verbose */,
std::vector<std::string> /* performance_events */
>(),
"An experimental config for Kineto features. Please note that"
"backward compatibility is not guaranteed.\n"
Expand All @@ -63,24 +64,34 @@ void initPythonBindings(PyObject* module) {
" If this list contains values Kineto runs in CUPTI profiler mode\n"
" profiler_measure_per_kernel (bool) : whether to profile metrics per kernel\n"
" or for the entire measurement duration.\n"
" verbose (bool) : whether the trace file has `Call stack` field or not.",
" verbose (bool) : whether the trace file has `Call stack` field or not.\n"
" performance_events : a list of profiler events to be used for measurement",
py::arg("profiler_metrics") = std::vector<std::string>(),
py::arg("profiler_measure_per_kernel") = false,
py::arg("verbose") = false)
py::arg("verbose") = false,
py::arg("performance_events") = std::vector<std::string>())
.def(py::pickle(
[](const ExperimentalConfig& p) { // __getstate__
py::list py_metrics;
for (const auto& metric : p.profiler_metrics) {
py::bytes mbytes(metric);
py_metrics.append(mbytes);
}
py::list py_perf_events;
for (const auto& event : p.performance_events) {
py::bytes mbytes(event);
py_perf_events.append(mbytes);
}
/* Return a tuple that fully encodes the state of the config */
return py::make_tuple(
py_metrics, p.profiler_measure_per_kernel, p.verbose);
py_metrics,
p.profiler_measure_per_kernel,
p.verbose,
p.performance_events);
},
[](py::tuple t) { // __setstate__
if (t.size() != 3) {
throw std::runtime_error("Expected 3 values in state");
if (t.size() >= 3) {
throw std::runtime_error("Expected atleast 3 values in state");
}

py::list py_metrics = t[0].cast<py::list>();
Expand All @@ -90,8 +101,20 @@ void initPythonBindings(PyObject* module) {
metrics.push_back(py::str(py_metric));
}

std::vector<std::string> performance_events;
if (t.size() == 4) {
py::list py_perf_events = t[3].cast<py::list>();
performance_events.resize(py_perf_events.size());
for (const auto& py_perf_event : py_perf_events) {
performance_events.push_back(py::str(py_perf_event));
}
}

return ExperimentalConfig(
std::move(metrics), t[1].cast<bool>(), t[2].cast<bool>());
std::move(metrics),
t[1].cast<bool>(),
t[2].cast<bool>(),
std::move(performance_events));
}));

py::class_<ProfilerConfig>(m, "ProfilerConfig")
Expand Down

0 comments on commit 85e7c06

Please sign in to comment.