Liking cljdoc? Tell your friends :D

Some Tips for Improving MXNet Performance

Even after fixing the training or deployment environment and parallelization scheme, a number of configuration settings and data-handling choices can impact the MXNet performance. In this document, we address some tips for improving MXNet performance.

Performance is mainly affected by the following 4 factors:

  1. Implementation of operators (Convolution, Pooling, ..)
  2. Input data loading and augmentation
  3. Workloads (computation graph) optimization and scheduling
  4. Communication for multi-devices training

Intel CPU

For using Intel Xeon CPUs for training and inference, we suggest enabling USE_MKLDNN = 1 in config.mk.

We also find that setting the following environment variables can help:

VariableDescription
OMP_NUM_THREADSSuggested value: vCPUs / 2 in which vCPUs is the number of virtual CPUs. For more information, please see the guide for setting the number of threads using an OpenMP environment variable
KMP_AFFINITYSuggested value: granularity=fine,compact,1,0. For more information, please see the guide for Thread Affinity Interface (Linux* and Windows*).
MXNET_SUBGRAPH_BACKENDSet to MKLDNN to enable the subgraph feature for better performance. For more information please see Build/Install MXNet with MKL-DNN

Note that MXNet treats all CPUs on a single machine as a single device. So whether you specify cpu(0) or cpu(), MXNet will use all CPU cores on the machine.

Scoring results

The following table shows performance of MXNet-1.2.0.rc1, namely number of images that can be predicted per second. We used example/image-classification/benchmark_score.py to measure the performance on different AWS EC2 machines.

AWS EC2 C5.18xlarge:

BatchAlexnetVGG 16Inception-BNInception-v3Resnet 50Resnet 152
1390.5381.57124.1362.2676.2232.92
2596.45100.84206.5893.36119.5546.80
4710.77119.04275.55127.86148.6259.36
8921.40120.38380.82157.11167.9570.78
161018.43115.30411.67168.71178.5475.13
321290.31107.19483.34179.38193.4785.86

AWS EC2 C5.9xlarge:

BatchAlexnetVGG 16Inception-BNInception-v3Resnet 50Resnet 152
1257.7750.61130.9966.9575.3832.33
2410.6063.02195.1487.84102.6741.57
4462.5962.64263.15109.87127.1550.69
8573.7963.95309.99121.36140.8459.01
16709.4767.79350.19128.26147.4164.15
32831.4669.58354.91129.92149.1864.25

AWS EC2 C5.4xlarge:

BatchAlexnetVGG 16Inception-BNInception-v3Resnet 50Resnet 152
1214.1529.32114.9747.9661.0123.92
2310.0434.81150.0960.8971.1627.92
4330.6934.56186.6374.1586.8634.37
8378.8835.46204.8977.0591.1036.93
16424.0036.49211.5578.3991.2337.34
32481.9537.23213.7178.2391.6837.26

AWS EC2 C5.2xlarge:

BatchAlexnetVGG 16Inception-BNInception-v3Resnet 50Resnet 152
1131.0115.6778.7531.1237.3014.75
2182.2918.0198.5939.1345.9817.84
4189.3118.25110.2641.3549.2119.32
8211.7518.57115.4642.5349.9819.81
16236.0619.11117.1842.5950.2019.92
32261.1319.46116.2042.7249.9519.80

AWS EC2 C5.xlarge:

BatchAlexnetVGG 16Inception-BNInception-v3Resnet 50Resnet 152
136.643.9327.0610.0912.985.06
249.214.4929.6710.8012.945.14
450.124.5030.3110.8313.175.19
854.714.5830.2210.8913.195.20
1660.234.7030.2010.9113.235.19
3266.374.7630.1010.9013.225.15

Other CPU

If using CPUs (not just Intel CPUs -- ARMs also), NNPACK can improve the running performance with 2x~7x, please check nnpack.md for details.

Nvidia GPU

cuDNN typically accelerates MXNet performance on NVIDIA GPUs significantly, especially for convolution layers. We suggest always checking to make sure that a recent cuDNN version is used.

Setting the environment export MXNET_CUDNN_AUTOTUNE_DEFAULT=1 sometimes also helps.

We show results when using various GPUs including K80 (EC2 p2.2xlarge), M60 (EC2 g3.4xlarge), and V100 (EC2 p3.2xlarge).

Scoring results

Based on example/image-classification/benchmark_score.py and MXNet-1.2.0.rc1, with cuDNN 7.0.5

  • K80 (single GPU)
BatchAlexnetVGG 16Inception-BNInception-v3Resnet 50Resnet 152
1243.9343.5968.6235.5267.4123.65
2338.1649.14113.4156.2993.3533.88
4478.9253.44159.6174.43119.1845.23
8683.5270.50190.4986.23131.3250.54
161004.66109.01254.20105.70155.4062.55
321238.55114.98285.49116.79159.4264.99
641346.72123.56308.73122.21167.5870.21
1281416.91OOM320.98123.11171.5571.85
2561462.97OOM329.16127.53153.0157.23
  • M60
BatchAlexnetVGG 16Inception-BNInception-v3Resnet 50Resnet 152
1243.4959.95101.9748.3095.4639.29
2491.0469.14170.3580.27142.6160.17
4711.5478.94257.89123.09182.3676.51
81077.73109.34343.42152.82208.7487.27
161447.21144.93390.25166.32220.7392.41
321797.66151.86416.69176.56230.1997.03
641779.38150.18427.51183.47239.12101.59
1281787.36OOM439.04185.29243.31103.39
2561899.10OOM450.22183.42242.36100.98
  • V100
BatchAlexnetVGG 16Inception-BNInception-v3Resnet 50Resnet 152
1659.51205.16157.3787.71162.1561.38
21248.21265.40297.34159.24293.74116.30
42122.41333.97520.91279.84479.14195.17
83894.30420.26898.09455.03699.39294.19
165815.58654.161430.97672.54947.45398.79
327906.09708.431847.26814.591076.81451.82
649486.26701.592134.89899.011168.37480.44
12810177.84703.302318.32904.331233.15511.79
25610990.46473.622425.28960.201155.07449.35

Below is the performance result on V100 using float 16.

BatchVGG 16Inception-BNInception-v3Resnet 50Resnet 152
1276.29155.53150.99270.8996.79
2476.91296.45282.02493.99176.88
4711.92525.05492.45851.15321.52
81047.11900.26807.941282.36517.66
161299.881441.411192.211722.97724.57
321486.631854.301512.082085.51887.34
641219.652138.611687.352341.671002.90
1281169.812317.391818.262355.041046.98
256764.162425.161653.741991.88976.73

Training results

Based on example/image-classification/train_imagenet.py and MXNet-1.2.0.rc1, with CUDNN 7.0.5. The benchmark script is available at here, where the batch size for Alexnet is increased by 16x.

  • K80 (single GPU)

    BatchAlexnet(*16)Inception-v3Resnet 50
    1300.3010.4815.61
    2406.0816.0023.88
    4461.0122.1032.26
    8484.0026.8039.42
    16490.4531.6246.69
    32414.7233.7849.48
  • M60

    BatchAlexnet(*16)Inception-v3Resnet 50
    1380.9614.0620.55
    2530.5321.9032.65
    4600.1731.9645.57
    8633.6040.5854.92
    16639.3746.8864.44
    32576.5450.0568.34
  • V100

    BatchAlexnet(*16)Inception-v3Resnet 50
    11629.5221.8334.54
    22359.7340.1165.01
    42687.8972.79113.49
    82919.02118.43174.81
    162994.32173.15251.22
    322585.61214.48298.51
    641984.21247.43343.19
    128OOM253.68363.69

Multiple Devices

If more than one GPU or machine are used, MXNet uses kvstore to communicate data. It's critical to use the proper type of kvstore to get the best performance. Refer to multi_device.md for more details.

Besides, we can use tools/bandwidth to find the communication cost per batch. Ideally, the communication cost should be less than the time to compute a batch. To reduce the communication cost, we can consider:

  • Exploring different --kv-store options.
  • Increasing the batch size to improve the computation to communication ratio.

Input Data

To make sure you're handling input data in a reasonable way consider the following:

  • Data format: If you are using the rec format, then everything should be fine.
  • Decoding: By default, MXNet uses 4 CPU threads for decoding images. This is often sufficient to decode more than 1K images per second. If you are using a low-end CPU or your GPUs are very powerful, you can increase the number of threads.
  • Storage location. Any local or distributed file system (HDFS, Amazon S3) should be fine. If multiple devices read the data from the shared network file system (NFS) at the same time, problems might occur.
  • Use a large batch size. We often choose the largest one that fits into GPU memory. A value that's too large can slow down convergence. For example, the safe batch size for CIFAR 10 is approximately 200, while for ImageNet 1K, the batch size can exceed 1K.

Profiler

As of v0.9.1 (with the NNVM merge), MXNet has a built-in profiler that gives detailed information about execution time at the symbol level. This feature complements general profiling tools like nvprof and gprof by summarizing at the operator level, instead of a function, kernel, or instruction level.

In order to be able to use the profiler, you must compile MXNet with the USE_PROFILER=1 flag in config.mk.

The profiler can then be turned on with an environment variable for an entire program run, or programmatically for just part of a run. See example/profiler for complete examples of how to use the profiler in code, but briefly, the Python code looks like:

    mx.profiler.set_config(profile_all=True, filename='profile_output.json')
    mx.profiler.set_state('run')

    # Code to be profiled goes here...

    mx.profiler.set_state('stop')

The mode parameter can be set to

  • symbolic to only include symbolic operations
  • all to include all operations

After the program finishes, navigate to your browser's tracing (Example - chrome://tracing in a Chrome browser) and load the profile_output.json file output by the profiler to inspect the results.

MLP Profile

Note that the output file can grow extremely large, so this approach is not recommended for general use.

Can you improve this documentation? These fine people already did:
Da Zheng, thinksanky, Sheng Zha, Xinyu Chen, Ken Fehling & Ashok Emani
Edit on GitHub

cljdoc is a website building & hosting documentation for Clojure/Script libraries

× close