Commit 880ab925 authored by Marek Kolodziej's avatar Marek Kolodziej Committed by mcarilli
Browse files

Adding PyProf to Apex (#404)


Co-authored-by: default avatarAditya Agrawal <aditya.iitb@gmail.com>
Co-authored-by: default avatarMarek Kolodziej <mkolod@gmail.com>
parent 4a8c4ac0
......@@ -94,6 +94,11 @@ A Python-only build omits:
- Fused kernels that improve the performance of `apex.parallel.DistributedDataParallel` and `apex.amp`.
`DistributedDataParallel`, `amp`, and `SyncBatchNorm` will still be usable, but they may be slower.
To enable PyProf support, you need to install the packages required by PyProf. To do so, add the "--pyprof" option at installation time:
```
$ pip install -v --no-cache-dir --global-option="--pyprof" --global-option="--cpp_ext" --global-option="--cuda_ext" ./
```
### Windows support
Windows support is experimental, and Linux is recommended. `pip install -v --no-cache-dir --global-option="--cpp_ext" --global-option="--cuda_ext" .` may work if you were able to build Pytorch from source
on your system. `pip install -v --no-cache-dir .` (without CUDA/C++ extensions) is more likely to work. If you installed Pytorch in a Conda environment, make sure to install Apex in that same environment.
# May help avoid undefined symbol errors https://pytorch.org/cppdocs/notes/faq.html#undefined-symbol-errors-from-pytorch-aten
import torch
import warnings
from . import parallel
from . import amp
......@@ -14,3 +15,4 @@ from . import fp16_utils
# load time) the error message is timely and visible.
from . import optimizers
from . import normalization
from . import pyprof
1. How do I intercept the Adam optimizer in APEX ?
```python
from apex import pyprof
import fused_adam_cuda
pyprof.nvtx.wrap(fused_adam_cuda, 'adam')
```
2. If you are using JIT and/or AMP, the correct initialization sequence is
1. Let any JIT to finish.
2. Initlialize pyprof `pyprof.nvtx.init()`.
3. Initialize AMP.
3. How do I profile with `torch.distributed.launch` ?
```python
nvprof -f -o net%p.sql \
--profile-from-start off \
--profile-child-processes \
python -m torch.distributed.launch net.py
```
## PyProf - PyTorch Profiling tool
### What does this tool do?
Analyzing the performance of deep neural networks is hard. Getting kernels out of [NvProf]([https://developer.nvidia.com/nvidia-visual-profiler](https://developer.nvidia.com/nvidia-visual-profiler)) or [NSight Compute]([https://developer.nvidia.com/nsight-compute](https://developer.nvidia.com/nsight-compute)) provides some generic kernel name and its execution time, but not detailed information regarding the following:
- Which layer launched it: e.g. the association of `ComputeOffsetsKernel` with a concrete PyTorch layer or API is not obvious.
- What the tensor dimensions and precision were: without knowing the tensor dimensions and precision, it's impossible to reason about whether the actual (silicon) kernel time is close to maximum performance of such a kernel on the GPU. Knowing the tensor dimensions and precision, we can figure out the FLOPs and bandwidth required by a layer, and then determine how close to maximum performance the kernel is for that operation.
- Forward-backward correlation: currently it's very hard to determine what the forward pass step was that resulted in the particular weight and data gradients (wgrad, dgrad), which makes it difficult to determine the tensor dimensions required by these backprop steps to assess their performance.
- Did the kernel use [Tensor Cores]([https://www.youtube.com/watch?v=yyR0ZoCeBO8](https://www.youtube.com/watch?v=yyR0ZoCeBO8))?
- Which line in the user's code resulted in launching this particular kernel (program trace)?
PyProf addresses all of the issues above by:
1. Instrumenting PyTorch operations to capture the tensor dimensions and precision using [NVTX](https://devblogs.nvidia.com/cuda-pro-tip-generate-custom-application-profile-timelines-nvtx). This information is recorded at profile capture time, e.g. using [NvProf](https://developer.nvidia.com/nvidia-visual-profiler).
2. Querying the record produced by the profiler to correlate the kernel name and duration with PyTorch API/layer name, tensor dimensions, tensor precision, as well as calculating FLOPs and bandwidth for common operations. In addition, extra information from the profile is added for use by CUDA professionals, such as CUDA launch parameters (block/grid dimensions).
Regarding FLOP and bandwidth implementations, these are usually quite straightforward. For example, for matrices A<sub>MxK</sub> and B<sub>KxN</sub>, the FLOP count for a matrix multiplication is 2 * M * N * K, and bandwidth is M * K + N * K + M * N. Note that these numbers are based on the algorithm, not the actual performance of the specific kernel. For more details, see NVIDIA's [Deep Learning Performance Guide](https://docs.nvidia.com/deeplearning/sdk/dl-performance-guide/index.html).
Armed with such information, the user can determine various issues to help them tune the network. For instance, according to the [Tensor Core Performance Guide]([https://docs.nvidia.com/deeplearning/sdk/dl-performance-guide/index.html](https://docs.nvidia.com/deeplearning/sdk/dl-performance-guide/index.html)), the M, N and K dimensions that result in Tensor Core usage need to be divisible by 8. In fact, PyProf comes with a flag that lets the user obtain information regarding whether Tensor Cores were used by the kernel. Other useful information might include knowing that a particular kernel did not exploit much thread parallelism, as determined by the grid/block dimensions. Since many PyTorch kernels are open-source (or even custom written by the user, as in [CUDA Extensions]([https://pytorch.org/tutorials/advanced/cpp_extension.html](https://pytorch.org/tutorials/advanced/cpp_extension.html))), this provides the user with information that helps root cause performance issues and prioritize optimization work.
### How to get started?
1. Add the following lines to your PyTorch network:
```python
import torch.cuda.profiler as profiler
from apex import pyprof
pyprof.nvtx.init()
```
Run the training/inference loop with the [PyTorch's NVTX context manager](https://pytorch.org/docs/stable/_modules/torch/autograd/profiler.html#emit_nvtx)
`with torch.autograd.profiler.emit_nvtx()`. Optionally, you can
use `profiler.start()` and `profiler.stop()` to pick an iteration
(say after warm-up) for which you would like to capture data.
Here's an example:
```python
iters = 500
iter_to_capture = 100
# Define network, loss function, optimizer etc.
# PyTorch NVTX context manager
with torch.autograd.profiler.emit_nvtx():
for iter in range(iters):
if iter == iter_to_capture:
profiler.start()
output = net(images)
loss = criterion(output, labels)
loss.backward()
optimizer.step()
if iter == iter_to_capture:
profiler.stop()
```
2. Run NVprof to generate a SQL (NVVP) file. This file can be opened with NVVP, as usual.
```sh
# If you used profiler.start() and profiler.stop() in net.py
nvprof -f -o net.sql --profile-from-start off -- python net.py
# Profile everything
nvprof -f -o net.sql -- python net.py
```
**Note:** if you're experiencing issues with hardware counters and you get a message such as `**_ERR_NVGPUCTRPERM The user running <tool_name/application_name> does not have permission to access NVIDIA GPU Performance Counters on the target device_**`, please follow the steps described in [Hardware Counters](#hardware-counters).
3. Run parser on the SQL file. The output is an ASCII file. Each line
is a python dictionary which contains information about the kernel name,
duration, parameters etc. This file can be used as input to other custom
scripts as well.
```sh
python -m apex.pyprof.parse net.sql > net.dict
```
4. Run the profiler. The input is the python dictionary created above. The tool can produce a CSV output, a columnated output (similar to `column -t` for terminal readability) and a space separated output (for post processing by AWK for instance). The tool produces 20 columns of information for every GPU kernel but you can select a subset of columns using the `-c` flag. Note that a few columns might have the value "na" implying either its a work in progress or the tool was unable to extract that information. Assuming the directory is `prof`, here are a few examples of how to use `prof.py`.
```sh
# Print usage and help. Lists all available output columns.
python -m apex.pyprof.prof -h
# Columnated output of width 150 with some default columns.
python -m apex.pyprof.prof -w 150 net.dict
# CSV output.
python -m apex.pyprof.prof --csv net.dict
# Space seperated output.
python -m apex.pyprof.prof net.dict
# Columnated output of width 130 with columns index,direction,kernel name,parameters,silicon time.
python -m apex.pyprof.prof -w 130 -c idx,dir,kernel,params,sil net.dict
# CSV output with columns index,direction,kernel name,parameters,silicon time.
python -m apex.pyprof.prof --csv -c idx,dir,kernel,params,sil net.dict
# Space separated output with columns index,direction,kernel name,parameters,silicon time.
python -m apex.pyprof.prof -c idx,dir,kernel,params,sil net.dict
# Input redirection.
python -m apex.pyprof.prof < net.dict
```
5. Profile-guided optimization
If kernels that do matrix multiplication/GEMM or convolution use half precision (fp16) data but do not use Tensor Cores (the TC column in the profile analysis output doesn't show a "1"), one can follow some basic steps to increase the likelihood that a Tensor Core-compatible kernel will be chosen. For example, for GEMMs, M, N and K should be divisible by 8, and for convolutions, the number of input and output channels shuold be divisible by 8. For more information, see detailed Tensor Core guides such as:
- Blog Post: [Tips for Optimizing GPU Performance Using Tensor Cores](https://devblogs.nvidia.com/optimizing-gpu-performance-tensor-cores/)
- GTC Talk: [Tensor Core Deep Learning Performance Guide](https://developer.download.nvidia.com/video/gputechconf/gtc/2019/presentation/s9926-tensor-core-performance-the-ultimate-guide.pdf)
For both Tensor Core and non-Tensor Core Deep Learning performance optimization tips, see NVIDIA's [Deep Learning Performance Guide](https://docs.nvidia.com/deeplearning/sdk/dl-performance-guide/index.html).
### TODOs
1. The support for conv transpose is currently missing.
2. PyProf currently works only with NvProf, but Nsight Compute support will be added in the future.
### Example
1. Run `nvprof` on the LeNet model in `examples/lenet.py`. This will output a SQL file called `net.sql`.
```sh
nvprof -f -o net.sql --profile-from-start off -- python examples/lenet.py
```
**Note**: DO NOT add --analysis-metrics since that will change which table nvprof writes the kernels to (`CUPTI_ACTIVITY_KIND_KERNEL` instead of the usual `CUPTI_ACTIVITY_KIND_CONCURRENT_KERNEL`). Support for running with metrics may be added in the future.
If you don't care about a full correlation analysis and you'd just like to view the timeline with detailed NVTX annotations, you can do so, e.g. in the NVIDIA Visual Profiler (NVVP). For example, you can call `nvvp net.sql` to view the annotated timeline.
2. Run the `parse.py` script on `net.sql` to extract kernel and runtime information and
save it as `net.dict`.
```sh
python -m apex.pyprof.parse net.sql > net.dict
```
This will produce a text file, which can be parsed by any external tool, but it can also be directly read one line at a time by Python by calling `eval` on the line being read.
**Note: you do not need to process this output manually.** Here the output is just shown as an example of modularity - you can process the raw data yourself, or let the next step enrich the information further and dump a CSV.
The output of this step will look as follows. Note that the dictionary has a lot more keys than the ones shown in the example.
```
>>> with open('torchvision.resnet50.adam.64.dict') as f:
... for line in f:
... d = eval(line)
... print(d['kShortName'], d['op'], d['kDuration'], d['block'], d['grid'], d['device'], d['stream'], d['trace'])
...
nchwToNhwc3To4Kernel ['conv2d'] 376324 (256, 1, 1) (1568, 1, 64) 0 7 ['imagenet.py:137', 'imagenet.py:129', '/opt/conda/lib/python3.6/site-packages/torchvision/models/resnet.py:195']
generic4Channel_kernel ['conv2d'] 10720 (512, 1, 1) (19, 1, 1) 0 7 ['imagenet.py:137', 'imagenet.py:129', '/opt/conda/lib/python3.6/site-packages/torchvision/models/resnet.py:195']
first_layer_fwd_kernel ['conv2d'] 411204 (128, 1, 1) (2, 7, 64) 0 7 ['imagenet.py:137', 'imagenet.py:129', '/opt/conda/lib/python3.6/site-packages/torchvision/models/resnet.py:195']
nhwcToNchwKernel ['conv2d'] 342371 (256, 1, 1) (392, 2, 64) 0 7 ['imagenet.py:137', 'imagenet.py:129', '/opt/conda/lib/python3.6/site-packages/torchvision/models/resnet.py:195']
elementwise_kernel ['__iadd__'] 2816 (128, 1, 1) (1, 1, 1) 0 7 ['imagenet.py:137', 'imagenet.py:129', '/opt/conda/lib/python3.6/site-packages/torchvision/models/resnet.py:196']
batch_norm_collect_statistics_kernel ['batch_norm', 'batch_norm'] 929513 (512, 1, 1) (64, 1, 1) 0 7 ['imagenet.py:137', 'imagenet.py:129', '/opt/conda/lib/python3.6/site-packages/torchvision/models/resnet.py:196']
```
3. Run the `prof.py` script on `net.dict` to summarize the results into a CSV file, or to display the pretty-printed results on the screen. This step processes the raw output from step 2 to generate a nice output, but it also adds a lot of extra useful information inferred from the previous step, such as:
- FLOPs
- bandwidth (bytes in and out of GPU DRAM)
- tensor core usage
```sh
python -m apex.pyprof.prof --csv net.dict > results.csv
```
You can choose which columns you'd like to display. Here's a list from calling `python -m apex.pyprof.prof -h`:
```
idx: Index
seq: PyTorch Sequence Id
altseq: PyTorch Alternate Sequence Id
tid: Thread Id
layer: User annotated NVTX string (can be nested)
trace: Function Call Trace
dir: Direction
sub: Sub Sequence Id
mod: Module
op: Operattion
kernel: Kernel Name
params: Parameters
sil: Silicon Time (in ns)
tc: Tensor Core Usage
device: GPU Device Id
stream: Stream Id
grid: Grid Dimensions
block: Block Dimensions
flops: Floating point ops (FMA = 2 FLOPs)
bytes: Number of bytes in and out of DRAM
```
Let's have a look at the pretty-printed output:
```
python -m apex.pyprof.prof -w 100 -c kernel,op,sil,tc,flops,bytes,device,stream,block,grid torchvision.resnet50.adam.64.dict
Kernel Op Sil(ns) TC FLOPs Bytes Dev Str Block Grid
elementwise_kernel relu 381028 - 51380224 205520896 0 7 512,1,1 100352,1,1
volta_fp16_s884cudn conv2d 160002 1 1644167168 51388416 0 7 256,1,1 784,1,1
elementwise_kernel relu 96545 - 12845056 51380224 0 7 512,1,1 25088,1,1
volta_fp16_s884cudn conv2d 346083 1 6576668672 128483328 0 7 256,1,1 784,2,1
```
Not using the pretty-print width (`-w`) option and adding `--csv` results in a CSV output instead:
```
python -m apex.pyprof.prof --csv -c kernel,mod,op,dir,sil,tc,flops,bytes,device,stream,block,grid torchvision.resnet50.adam.64.dict
"Kernel","Module","Op","Direction","Sil(ns)","TC","FLOPs","Bytes","Device","Stream","Block","Grid"
"nchwToNhwc3To4Kernel","torch.nn.functional","conv2d","fprop","376324","-","0","0","0","7","256,1,1","1568,1,64"
"generic4Channel_kernel","torch.nn.functional","conv2d","fprop","10720","-","0","0","0","7","512,1,1","19,1,1"
"first_layer_fwd_kernel","torch.nn.functional","conv2d","fprop","411204","-","0","0","0","7","128,1,1","2,7,64"
"nhwcToNchwKernel","torch.nn.functional","conv2d","fprop","342371","-","0","0","0","7","256,1,1","392,2,64"
"elementwise_kernel","Tensor","__iadd__","fprop","2816","-","1.0","8","0","7","128,1,1","1,1,1"
"batch_norm_collect_statistics_kernel","torch.nn.functional","batch_norm","fprop","929513","-","411041792","411041792","0","7","512,1,1","64,1,1"
"batch_norm_transform_input_kernel","torch.nn.functional","batch_norm","fprop","377539","-","411041792","411041792","0","7","512,1,1","64,64,1"
"elementwise_kernel","torch.nn.functional","relu","fprop","381028","-","51380224","205520896","0","7","512,1,1","100352,1,1"
"MaxPoolForward","torch.nn.functional","max_pool2d","fprop","406531","-","0","0","0","7","256,1,1","50176,1,1"
"cudnn::gemm::computeOffsetsKernel","torch.nn.functional","conv2d","fprop","2464","-","0","0","0","7","128,1,1","25,1,1"
```
### Hardware Counters
Profiling GPU workloads may require access to [hardware performance counters]([https://en.wikipedia.org/wiki/Hardware_performance_counter](https://en.wikipedia.org/wiki/Hardware_performance_counter)). Due to a [fix](https://nvidia.custhelp.com/app/answers/detail/a_id/4738) in recent NVIDIA drivers addressing [CVE‑2018‑6260](https://nvd.nist.gov/vuln/detail/CVE-2018-6260), the hardware counters are disabled by default, and require elevated privileges to be enabled again. If you're using a recent driver, you may see the following message when trying to run nvprof:
```**_ERR_NVGPUCTRPERM The user running <tool_name/application_name> does not have permission to access NVIDIA GPU Performance Counters on the target device._**```
For details, see [here](https://developer.nvidia.com/nvidia-development-tools-solutions-ERR_NVGPUCTRPERM-permission-issue-performance-counters).
_Permanent solution_
Follow the steps [here]([https://developer.nvidia.com/nvidia-development-tools-solutions-ERR_NVGPUCTRPERM-permission-issue-performance-counters](https://developer.nvidia.com/nvidia-development-tools-solutions-ERR_NVGPUCTRPERM-permission-issue-performance-counters)). The current steps for Linux are:
```
sudo systemctl isolate multi-user
sudo modprobe -r nvidia_uvm nvidia_drm nvidia_modeset nvidia-vgpu-vfio nvidia
sudo modprobe nvidia NVreg_RestrictProfilingToAdminUsers=0
sudo systemctl isolate graphical
```
The above steps should result in a permanent change.
_Temporary solution_
When running on bare metal, you can run nvprof with `sudo`.
If you're running in a Docker image, you can temporarily elevate your privileges with one of the following (oldest to newest syntax):
<pre>
nvidia-docker run <b>--privileged</b>
docker run --runtime nvidia <b>--privileged</b>
docker run --gpus all <b>--privileged<b>
</pre>
import warnings
from . import nvtx
__pycache__
*.sql
*.dict
*.csv
This directory has examples of how to use `pyprof` with APEX extensions e.g. `fused_adam_cuda` and `fused_layer_norm_cuda`.
import torch
import fused_adam_cuda
from apex.optimizers import FusedAdam, FP16_Optimizer
from apex import pyprof
pyprof.nvtx.init()
pyprof.nvtx.wrap(fused_adam_cuda, 'adam')
model = torch.nn.Linear(10, 20).cuda().half()
criterion = torch.nn.CrossEntropyLoss().cuda()
optimizer = FusedAdam(model.parameters())
optimizer = FP16_Optimizer(optimizer)
x = torch.ones(32, 10).cuda().half()
target = torch.empty(32, dtype=torch.long).random_(20).cuda()
y = model(x)
loss = criterion(y, target)
optimizer.zero_grad()
loss.backward()
optimizer.step()
import torch
import fused_layer_norm_cuda
from apex.normalization import FusedLayerNorm
from apex import pyprof
pyprof.nvtx.init()
pyprof.nvtx.wrap(fused_layer_norm_cuda, 'forward')
pyprof.nvtx.wrap(fused_layer_norm_cuda, 'backward')
pyprof.nvtx.wrap(fused_layer_norm_cuda, 'forward_affine')
pyprof.nvtx.wrap(fused_layer_norm_cuda, 'backward_affine')
input = torch.randn(20, 5, 10, 10).cuda()
# With Learnable Parameters
m = FusedLayerNorm(input.size()[1:]).cuda()
output = m(input)
# Without Learnable Parameters
m = FusedLayerNorm(input.size()[1:], elementwise_affine=False).cuda()
output = m(input)
# Normalize over last two dimensions
m = FusedLayerNorm([10, 10]).cuda()
output = m(input)
# Normalize over last dimension of size 10
m = FusedLayerNorm(10).cuda()
output = m(input)
#!/bin/bash
set -e
SCRIPT=`realpath $0`
SCRIPTPATH=`dirname $SCRIPT`
PYPROF="$SCRIPTPATH/../.."
parse="python $PYPROF/parse/parse.py"
prof="python $PYPROF/prof/prof.py"
for f in *.py
do
base=`basename $f .py`
sql=$base.sql
dict=$base.dict
#NVprof
echo "nvprof -fo $sql python $f"
nvprof -fo $sql python $f
#Parse
echo $parse $sql
$parse $sql > $dict
#Prof
echo $prof $dict
$prof -w 130 $dict
\rm $sql $dict
done
This directory has examples which show how to intercept (monkey patch) custom functions and modules with `pyprof`. No changes are required in `pyprof/parse`, however, users can add support for bytes and flops calculation for custom functions and modules in `pyprof/prof` by extending the `OperatorLayerBase` class.
#!/usr/bin/env python3
import torch
import torch.cuda.profiler as profiler
from apex import pyprof
#Initialize pyprof
pyprof.nvtx.init()
class Foo(torch.autograd.Function):
@staticmethod
def forward(ctx, in1, in2):
out = in1 + in2 #This could be a custom C/C++ function.
return out
@staticmethod
def backward(ctx, grad):
in1_grad = grad #This could be a custom C/C++ function.
in2_grad = grad #This could be a custom C/C++ function.
return in1_grad, in2_grad
#Hook the forward and backward functions to pyprof
pyprof.nvtx.wrap(Foo, 'forward')
pyprof.nvtx.wrap(Foo, 'backward')
foo = Foo.apply
x = torch.ones(4,4).cuda()
y = torch.ones(4,4).cuda()
with torch.autograd.profiler.emit_nvtx():
profiler.start()
z = foo(x,y)
profiler.stop()
#!/usr/bin/env python3
import torch
import torch.cuda.profiler as profiler
from apex import pyprof
pyprof.nvtx.init()
class Foo(torch.nn.Module):
def __init__(self, size):
super(Foo, self).__init__()
self.n = torch.nn.Parameter(torch.ones(size))
self.m = torch.nn.Parameter(torch.ones(size))
def forward(self, input):
return self.n*input + self.m
#Hook the forward function to pyprof
pyprof.nvtx.wrap(Foo, 'forward')
foo = Foo(4)
foo.cuda()
x = torch.ones(4).cuda()
with torch.autograd.profiler.emit_nvtx():
profiler.start()
z = foo(x)
profiler.stop()
#!/bin/bash
set -e
SCRIPT=`realpath $0`
SCRIPTPATH=`dirname $SCRIPT`
PYPROF="$SCRIPTPATH/../.."
parse="python $PYPROF/parse/parse.py"
prof="python $PYPROF/prof/prof.py"
for f in *.py
do
base=`basename $f .py`
sql=$base.sql
dict=$base.dict
#NVprof
echo "nvprof -fo $sql python $f"
nvprof -fo $sql python $f
#Parse
echo $parse $sql
$parse $sql > $dict
#Prof
echo $prof $dict
$prof -w 130 $dict
\rm $sql $dict
done
#!/usr/bin/env python3
"""
Example to run pyprof with imagenet models.
"""
import sys
import torch
import torch.nn as nn
import torchvision.models as models
import torch.cuda.profiler as profiler
import argparse
from apex import pyprof
from apex.optimizers import FusedAdam, FP16_Optimizer
import fused_adam_cuda
def parseArgs():
parser = argparse.ArgumentParser(prog=sys.argv[0], description="Run popular imagenet models.")
parser.add_argument("-m",
type=str,
default="resnet50",
choices=["alexnet", "densenet121", "densenet161", "densenet169", "densenet201", "googlenet", "mnasnet0_5", "mnasnet0_75", "mnasnet1_0", "mnasnet1_3", "mobilenet_v2", "resnet18", "resnet34", "resnet50", "resnet101", "resnet152", "resnext50_32x4d", "resnext101_32x8d", "wide_resnet50_2", "wide_resnet101_2", "shufflenet_v2_x0_5", "shufflenet_v2_x1_0", "shufflenet_v2_x1_5", "shufflenet_v2_x2_0", "squeezenet1_0", "squeezenet1_1", "vgg11", "vgg11_bn", "vgg13", "vgg13_bn", "vgg16", "vgg16_bn", "vgg19", "vgg19_bn", "inception_v3"],
help="Model.")
parser.add_argument("-b",
type=int,
default=32,
help="Batch size.")
parser.add_argument("-o",
type=str,
default="adam",
choices=["adam", "sgd"],
help="Optimizer.")
args = parser.parse_args()
return args
d = {
"alexnet": {'H': 224, 'W': 224, 'opts': {}},
"densenet121": {'H': 224, 'W': 224, 'opts': {}},
"densenet161": {'H': 224, 'W': 224, 'opts': {}},
"densenet169": {'H': 224, 'W': 224, 'opts': {}},
"densenet201": {'H': 224, 'W': 224, 'opts': {}},
"googlenet": {'H': 224, 'W': 224, 'opts': {'aux_logits': False}},
"mnasnet0_5": {'H': 224, 'W': 224, 'opts': {}},
"mnasnet0_75": {'H': 224, 'W': 224, 'opts': {}},
"mnasnet1_0": {'H': 224, 'W': 224, 'opts': {}},
"mnasnet1_3": {'H': 224, 'W': 224, 'opts': {}},
"mobilenet_v2": {'H': 224, 'W': 224, 'opts': {}},
"resnet18": {'H': 224, 'W': 224, 'opts': {}},
"resnet34": {'H': 224, 'W': 224, 'opts': {}},
"resnet50": {'H': 224, 'W': 224, 'opts': {}},
"resnet101": {'H': 224, 'W': 224, 'opts': {}},
"resnet152": {'H': 224, 'W': 224, 'opts': {}},
"resnext50_32x4d": {'H': 224, 'W': 224, 'opts': {}},
"resnext101_32x8d": {'H': 224, 'W': 224, 'opts': {}},
"wide_resnet50_2": {'H': 224, 'W': 224, 'opts': {}},
"wide_resnet101_2": {'H': 224, 'W': 224, 'opts': {}},
"shufflenet_v2_x0_5": {'H': 224, 'W': 224, 'opts': {}},
"shufflenet_v2_x1_0": {'H': 224, 'W': 224, 'opts': {}},
"shufflenet_v2_x1_5": {'H': 224, 'W': 224, 'opts': {}},
"shufflenet_v2_x2_0": {'H': 224, 'W': 224, 'opts': {}},
"squeezenet1_0": {'H': 224, 'W': 224, 'opts': {}},
"squeezenet1_1": {'H': 224, 'W': 224, 'opts': {}},
"vgg11": {'H': 224, 'W': 224, 'opts': {}},
"vgg11_bn": {'H': 224, 'W': 224, 'opts': {}},
"vgg13": {'H': 224, 'W': 224, 'opts': {}},
"vgg13_bn": {'H': 224, 'W': 224, 'opts': {}},
"vgg16": {'H': 224, 'W': 224, 'opts': {}},
"vgg16_bn": {'H': 224, 'W': 224, 'opts': {}},
"vgg19": {'H': 224, 'W': 224, 'opts': {}},
"vgg19_bn": {'H': 224, 'W': 224, 'opts': {}},
"inception_v3": {'H': 299, 'W': 299, 'opts': {'aux_logits': False}},
}
def main():
args = parseArgs()
pyprof.nvtx.init()
pyprof.nvtx.wrap(fused_adam_cuda, 'adam')
N = args.b
C = 3
H = d[args.m]['H']
W = d[args.m]['W']
opts = d[args.m]['opts']
classes = 1000
net = getattr(models, args.m)
net = net(**opts).cuda().half()
net.train()
x = torch.rand(N, C, H, W).cuda().half()
target = torch.empty(N, dtype=torch.long).random_(classes).cuda()
criterion = nn.CrossEntropyLoss().cuda()
if (args.o == "sgd"):
optimizer = torch.optim.SGD(net.parameters(), lr = 0.01, momentum=0.9)
elif (args.o == "adam"):
optimizer = FusedAdam(net.parameters())
optimizer = FP16_Optimizer(optimizer)
else:
assert False
#Warm up without profiler
for i in range(2):
output = net(x)
loss = criterion(output, target)
optimizer.zero_grad()
loss.backward()
optimizer.step()
with torch.autograd.profiler.emit_nvtx():
profiler.start()
output = net(x)
loss = criterion(output, target)
optimizer.zero_grad()
loss.backward()
optimizer.step()
profiler.stop()
if __name__ == "__main__":
main()
#!/bin/bash
set -e
SCRIPT=`realpath $0`
SCRIPTPATH=`dirname $SCRIPT`
PYPROF="$SCRIPTPATH/../.."
parse="python -m apex.pyprof.parse"
prof="python -m apex.pyprof.prof"
for net in "resnet50"
do
for optim in adam sgd
do
for batch in 32 64
do
base="torchvision".$net.$optim.$batch
sql=$base.sql
dict=$base.dict
#NVprof
echo "nvprof -fo $sql --profile-from-start off python imagenet.py -m ${net} -o $optim -b $batch"
nvprof -fo $sql --profile-from-start off python imagenet.py -m ${net} -o $optim -b $batch
#Parse
echo $parse $sql
$parse $sql > $dict
#Prof
echo $prof $dict
$prof -w 130 $dict
# \rm $sql $dict
done
done
done
*As of this writing, these examples do not work
because of changes being proposed in PyTorch.*
There are two ways to use PyTorch JIT
- Scripting
- Tracing
In addition, we can JIT a
- Stand alone function
- Class / class method
This directory has an example for each of the 4 cases.
Intercepting (monkey patching) JITted code has a few extra steps,
which are explained through comments.
#!/usr/bin/env python3
import torch
import torch.cuda.profiler as profiler
from apex import pyprof
#The following creates an object "foo" of type ScriptModule
#The new object has a function called "forward"
@torch.jit.script
def foo(x, y):
return torch.sigmoid(x) + y
#Initialize pyprof after the JIT step
pyprof.nvtx.init()
#Assign a name to the object "foo"
foo.__name__ = "foo"
#Hook up the forward function to pyprof
pyprof.nvtx.wrap(foo, 'forward')
x = torch.zeros(4,4).cuda()
y = torch.ones(4,4).cuda()
with torch.autograd.profiler.emit_nvtx():
profiler.start()
z = foo(x, y)
profiler.stop()
print(z)
#!/usr/bin/env python3
import torch
import torch.cuda.profiler as profiler
from apex import pyprof
class Foo(torch.jit.ScriptModule):
def __init__(self, size):
super(Foo, self).__init__()
self.n = torch.nn.Parameter(torch.ones(size))
self.m = torch.nn.Parameter(torch.ones(size))
@torch.jit.script_method
def forward(self, input):
return self.n*input + self.m
#Initialize pyprof after the JIT step
pyprof.nvtx.init()
#Hook up the forward function to pyprof
pyprof.nvtx.wrap(Foo, 'forward')
foo = Foo(4)
foo.cuda()
x = torch.ones(4).cuda()
with torch.autograd.profiler.emit_nvtx():
profiler.start()
z = foo(x)
profiler.stop()
print(z)
#!/usr/bin/env python3
import torch
import torch.cuda.profiler as profiler
from apex import pyprof
def foo(x, y):
return torch.sigmoid(x) + y
x = torch.zeros(4,4).cuda()
y = torch.ones(4,4).cuda()
#JIT the function using tracing
#This returns an object of type ScriptModule with a forward method.
traced_foo = torch.jit.trace(foo, (x,y))
#Initialize pyprof after the JIT step
pyprof.nvtx.init()
#Assign a name to the object "traced_foo"
traced_foo.__dict__['__name__'] = "foo"
#Hook up the forward function to pyprof
pyprof.nvtx.wrap(traced_foo, 'forward')
with torch.autograd.profiler.emit_nvtx():
profiler.start()
z = traced_foo(x, y)
profiler.stop()
print(z)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment