ptrendx opened a new pull request #19142:
URL: https://github.com/apache/incubator-mxnet/pull/19142
## Description ##
CUDA graphs is a feature of CUDA 10, which enables lowering CPU overhead by
bundling multiple kernel launches together.
The main limitation of CUDA graphs is that they do require the graph to be
static - no parameters to the kernels can change, otherwise the graph needs to
be recaptured. That is why this feature is currently only enabled for the
symbolic models and Gluon models hybridized with `hybridize(static_alloc=True,
static_shape=True)`.
The feature is not enabled by default and requires environment variable
`MXNET_ENABLE_CUDA_GRAPHS` to be set. In order to not capture the operations,
the execution of which may change during the course of the training job,
stateful operators and operators relying on resources other than workspace are
not included in the graph.
Since the feature lowers the CPU overhead of the execution, the impact is
most visible in the inference or scale-out workloads with small batch size. Let
us consider following script comparing fp16, batch size 1 inference of RN50v2
model from GluonCV with and without graphs:
```python
import mxnet as mx
import gluoncv as gcv
from gluoncv.model_zoo import get_model
import time
import os
net = get_model('ResNet50_V2')
net2 = get_model('ResNet50_V2')
net.initialize(ctx=mx.gpu())
net2.initialize(ctx=mx.gpu())
net.cast('float16')
net2.cast('float16')
net.hybridize(static_alloc=True, static_shape=True)
net2.hybridize(static_alloc=True, static_shape=True)
img = mx.random.uniform(shape=(1, 3, 224, 224), ctx=mx.gpu(),
dtype='float16')
os.environ["MXNET_ENABLE_CUDA_GRAPHS"] = "0"
for _ in range(10):
o = net(img)
mx.nd.waitall()
s = time.time()
for _ in range(1000):
o = net(img)
mx.nd.waitall()
e = time.time()
print("No graphs: ", e - s)
mx.nd.waitall()
os.environ["MXNET_ENABLE_CUDA_GRAPHS"] = "1"
for _ in range(10):
o = net2(img)
mx.nd.waitall()
s = time.time()
for _ in range(1000):
o = net2(img)
mx.nd.waitall()
e = time.time()
print("With graphs: ", e - s)
```
The result obtained on V100 16GB:
```bash
[20:33:46] ../src/operator/nn/./cudnn/./cudnn_algoreg-inl.h:97: Running
performance tests to find the best convolution algorithm, this can take a
while... (set the environment variable MXNET_CUDNN_AUTOTUNE_DEFAULT to 0 to
disable)
No graphs: 2.8153152465820312
With graphs: 2.3230245113372803
```
so over 17% increase in performance. The same script but with 128 batch size
gives much smaller improvement:
```bash
[20:38:34] ../src/operator/nn/./cudnn/./cudnn_algoreg-inl.h:97: Running
performance tests to find the best convolution algorithm, this can take a
while... (set the environment variable MXNET_CUDNN_AUTOTUNE_DEFAULT to 0 to
disable)
No graphs: 47.31952977180481
With graphs: 47.152849197387695
```
so 0.3%.
## Checklist ##
### Essentials ###
- [x] PR's title starts with a category (e.g. [BUGFIX], [MODEL], [TUTORIAL],
[FEATURE], [DOC], etc)
- [ ] Changes are complete (i.e. I finished coding on this PR)
- [ ] All changes have test coverage
- [ ] Code is well-documented
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]