Skip to content

Commit

Permalink
Add torch.autograd.profiler.range
Browse files Browse the repository at this point in the history
  • Loading branch information
apaszke authored and ezyang committed Nov 7, 2017
1 parent 68116d7 commit 22f5965
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
12 changes: 12 additions & 0 deletions torch/autograd/profiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,18 @@
from collections import defaultdict, namedtuple


class range(object):
def __init__(self, name):
self.name = name

def __enter__(self):
torch.autograd._push_range(self.name)

def __exit__(self, *args):
torch.autograd._pop_range()
return False


class EventList(list):
"""A list of Events (for pretty printing)"""
def __init__(self, *args, **kwargs):
Expand Down
11 changes: 11 additions & 0 deletions torch/csrc/autograd/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,16 @@ PyObject * THPAutograd_initExtension(PyObject *_unused)
m.def("_enable_profiler", torch::autograd::profiler::enableProfiler);
m.def("_disable_profiler", torch::autograd::profiler::disableProfiler);

m.def("_push_range", [](const char *name) {
using namespace torch::autograd::profiler;
if (!profiling) return;
pushRange(name);
});
m.def("_pop_range", []() {
using namespace torch::autograd::profiler;
if (!profiling) return;
popRange();
});

Py_RETURN_TRUE;
}

0 comments on commit 22f5965

Please sign in to comment.