pytorch/torch/cuda
Christian Hudon 511f89eaa9 Add nvtx.range() context manager (#42925)
Summary:
Small quality-of-life improvement to NVTX Python bindings, that we're using internally and that would be useful to other folks using NVTX annotations via PyTorch. (And my first potential PyTorch contribution.)

Instead of needing to be careful with try/finally to make sure all your range_push'es are range_pop'ed:

```
nvtx.range_push("Some event")
try:
    # Code here...
finally:
    nvtx.range_pop()
```

you can simply do:

```
with nvtx.range("Some event"):
    # Code here...
```

or even use it as a decorator:

```
class MyModel(nn.Module):

    # Other methods here...

    nvtx.range("MyModel.forward()")
    def forward(self, *input):
        # Forward pass code here...
```

A couple small open questions:

1. I also added the ability to call `msg.format()` inside `range()`, with the intention that, if there is nothing listening to NVTX events, we should skip the string formatting, to lower the overhead in that case. If you like that idea, I could add the actual "skip string formatting if nobody is listening to events" parts. We can also just leave it as is. Or I can remove that if you folks don't like it. (In the first two cases, should we add that to `range_push()` and `mark()` too?) Just let me know which one it is, and I'll update the pull request.

2. I don't think there are many places for bugs to hide in that function, but I can certainly add a quick test, if you folks want.

Pull Request resolved: https://github.com/pytorch/pytorch/pull/42925

Reviewed By: gchanan

Differential Revision: D24476977

Pulled By: ezyang

fbshipit-source-id: 874882818d958e167e624052e42d52fae3c4abf1
2020-10-22 19:46:16 -07:00
..
amp Use MTA for amp grad unscaling, enforce op math type in MTA functors, and allow op lambdas (#44778) 2020-10-01 07:51:16 -07:00
__init__.py [minor] Fix undefined variable (#45246) 2020-09-24 20:17:13 -07:00
_utils.py Decouple DataParallel/DistributedDataParallel from CUDA (#38454) 2020-07-07 12:48:16 -07:00
comm.py Add torch.cuda.comm to typechecking CI (#45350) 2020-09-25 12:13:43 -07:00
error.py remediation of S205607 2020-07-17 17:19:47 -07:00
memory.py adds list_gpu_processes function (#44616) 2020-09-14 09:54:32 -07:00
nccl.py Enable torch.cuda.nccl typechecking (#45344) 2020-09-25 17:02:47 -07:00
nvtx.py Add nvtx.range() context manager (#42925) 2020-10-22 19:46:16 -07:00
profiler.py Replace map(lambda constructs (#46462) 2020-10-22 09:50:22 -07:00
random.py doc string fix for torch.cuda.set_rng_state_all (#40544) 2020-06-25 08:37:14 -07:00
sparse.py
streams.py Update docs about CUDA stream priority (#41364) 2020-08-05 20:03:18 -07:00