Skip to content
This repository was archived by the owner on Nov 17, 2023. It is now read-only.

Commit 8aa9980

Browse files
committed
Enable subgraph backend mkldnn by default.
1 parent 6191dd7 commit 8aa9980

File tree

24 files changed

+4352
-215
lines changed

24 files changed

+4352
-215
lines changed

cpp-package/example/inference/README.md

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ The following performance numbers are collected via using C++ inference API on A
4141
```
4242
export KMP_AFFINITY=granularity=fine,noduplicates,compact,1,0
4343
export OMP_NUM_THREADS=$(vCPUs/2)
44-
export MXNET_SUBGRAPH_BACKEND=MKLDNN
4544
export MXNET_ENGINE_TYPE=NaiveEngine
4645
```
4746
Also users are recommended to use ```numactl``` or ```taskset``` to bind a running process to the specified cores.
@@ -87,8 +86,6 @@ Follow the below steps to do inference with more models.
8786

8887
The below command lines show how to run inference with FP32/INT8 resnet50_v1 model. Because the C++ inference script provides the almost same command line as this [Python script](https://github.com/apache/incubator-mxnet/blob/master/example/quantization/imagenet_inference.py) and then users can easily go from Python to C++.
8988
```
90-
# set MKLDNN as subgraph backend
91-
export MXNET_SUBGRAPH_BACKEND=MKLDNN
9289
9390
# FP32 inference
9491
./imagenet_inference --symbol_file "./model/resnet50_v1-symbol.json" --params_file "./model/resnet50_v1-0000.params" --dataset "./data/val_256_q90.rec" --rgb_mean "123.68 116.779 103.939" --rgb_std "58.393 57.12 57.375" --batch_size 64 --num_skipped_batches 50 --num_inference_batches 500

docs/faq/env_var.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,9 +297,10 @@ If ctypes is used, it must be `mxnet._ctypes.ndarray.NDArrayBase`.
297297
- This variable controls how many CuDNN dropout state resources to create for each GPU context for use in operator.
298298

299299
* MXNET_SUBGRAPH_BACKEND
300-
- Values: String ```(default="")```
300+
- Values: String ```(default="MKLDNN")``` if MKLDNN is avaliable, otherwise ```(default="")```
301301
- This variable controls the subgraph partitioning in MXNet.
302302
- This variable is used to perform MKL-DNN FP32 operator fusion and quantization. Please refer to the [MKL-DNN operator list](../tutorials/mkldnn/operator_list.md) for how this variable is used and the list of fusion passes.
303+
- Set ```MXNET_SUBGRAPH_BACKEND=NONE``` to disable subgraph backend.
303304

304305
* MXNET_SAFE_ACCUMULATION
305306
- Values: Values: 0(false) or 1(true) ```(default=0)```

docs/tutorials/c++/subgraphAPI.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,15 @@ There're 2 built-in attributes that used by MXNet executor.
111111

112112
`inference_only` : bool, apply this property only for inference. Property will be skiped when need_grad=True. Default `false` if this attribute isn't defined.
113113

114-
After defining the subgraph property, we need to register it in .cc file.
114+
After defining the subgraph property, we need to register it under a backend in .cc file.
115+
116+
Firstly, we need to register the backend
117+
118+
```C++
119+
MXNET_REGISTER_SUBGRAPH_BACKEND(SgTest);
120+
```
121+
122+
Then register the property under it.
115123
116124
```C++
117125
MXNET_REGISTER_SUBGRAPH_PROPERTY(SgTest, SgProperty);
@@ -124,6 +132,7 @@ It's possible to register multiple properties for same backend. In practice, we
124132
#include "SgProperty2.h" // Define SgProperty2 class
125133
#include "SgProperty3.h" // Define SgProperty3 class
126134

135+
MXNET_REGISTER_SUBGRAPH_BACKEND(SgTest);
127136
MXNET_REGISTER_SUBGRAPH_PROPERTY(SgTest, SgProperty); // Execution order 1.
128137
MXNET_REGISTER_SUBGRAPH_PROPERTY(SgTest, SgProperty2); // Execution order 2.
129138
MXNET_REGISTER_SUBGRAPH_PROPERTY(SgTest, SgProperty3); // Execution order 3.

docs/tutorials/mkldnn/MKLDNN_README.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ LIBRARY_PATH=$(brew --prefix llvm)/lib/ make -j $(sysctl -n hw.ncpu) CC=$(brew -
103103
<h2 id="3">Windows</h2>
104104

105105
On Windows, you can use [Micrsoft Visual Studio 2015](https://www.visualstudio.com/vs/older-downloads/) and [Microsoft Visual Studio 2017](https://www.visualstudio.com/downloads/) to compile MXNet with Intel MKL-DNN.
106-
[Micrsoft Visual Studio 2015](https://www.visualstudio.com/vs/older-downloads/) is recommended.
106+
[Micrsoft Visual Studio 2015](https://www.visualstudio.com/vs/older-downloads/) is recommended.
107107

108108
**Visual Studio 2015**
109109

@@ -113,8 +113,8 @@ To build and install MXNet yourself, you need the following dependencies. Instal
113113
2. Download and Install [CMake 3](https://cmake.org/files/v3.14/cmake-3.14.0-win64-x64.msi) if it is not already installed.
114114
3. Download [OpenCV 3](https://sourceforge.net/projects/opencvlibrary/files/3.4.5/opencv-3.4.5-vc14_vc15.exe/download), and unzip the OpenCV package, set the environment variable ```OpenCV_DIR``` to point to the ```OpenCV build directory``` (e.g.,```OpenCV_DIR = C:\opencv\build ```). Also, add the OpenCV bin directory (```C:\opencv\build\x64\vc14\bin``` for example) to the ``PATH`` variable.
115115
4. If you have Intel Math Kernel Library (Intel MKL) installed, set ```MKL_ROOT``` to point to ```MKL``` directory that contains the ```include``` and ```lib```. If you want to use MKL blas, you should set ```-DUSE_BLAS=mkl``` when cmake. Typically, you can find the directory in ```C:\Program Files (x86)\IntelSWTools\compilers_and_libraries\windows\mkl```.
116-
5. If you don't have the Intel Math Kernel Library (MKL) installed, download and install [OpenBLAS](http://sourceforge.net/projects/openblas/files/v0.2.14/), or build the latest version of OpenBLAS from source. Note that you should also download ```mingw64.dll.zip``` along with openBLAS and add them to PATH.
117-
6. Set the environment variable ```OpenBLAS_HOME``` to point to the ```OpenBLAS``` directory that contains the ```include``` and ```lib``` directories. Typically, you can find the directory in ```C:\Downloads\OpenBLAS\```.
116+
5. If you don't have the Intel Math Kernel Library (MKL) installed, download and install [OpenBLAS](http://sourceforge.net/projects/openblas/files/v0.2.14/), or build the latest version of OpenBLAS from source. Note that you should also download ```mingw64.dll.zip``` along with openBLAS and add them to PATH.
117+
6. Set the environment variable ```OpenBLAS_HOME``` to point to the ```OpenBLAS``` directory that contains the ```include``` and ```lib``` directories. Typically, you can find the directory in ```C:\Downloads\OpenBLAS\```.
118118

119119
After you have installed all of the required dependencies, build the MXNet source code:
120120

@@ -123,17 +123,17 @@ After you have installed all of the required dependencies, build the MXNet sourc
123123
git clone --recursive https://github.com/apache/incubator-mxnet.git
124124
cd C:\incubator-mxent
125125
```
126-
2. Enable Intel MKL-DNN by -DUSE_MKLDNN=1. Use [CMake 3](https://cmake.org/) to create a Visual Studio solution in ```./build```. Make sure to specify the architecture in the
126+
2. Enable Intel MKL-DNN by -DUSE_MKLDNN=1. Use [CMake 3](https://cmake.org/) to create a Visual Studio solution in ```./build```. Make sure to specify the architecture in the
127127
command:
128128
```
129129
>mkdir build
130130
>cd build
131131
>cmake -G "Visual Studio 14 Win64" .. -DUSE_CUDA=0 -DUSE_CUDNN=0 -DUSE_NVRTC=0 -DUSE_OPENCV=1 -DUSE_OPENMP=1 -DUSE_PROFILER=1 -DUSE_BLAS=open -DUSE_LAPACK=1 -DUSE_DIST_KVSTORE=0 -DCUDA_ARCH_NAME=All -DUSE_MKLDNN=1 -DCMAKE_BUILD_TYPE=Release
132132
```
133-
3. Enable Intel MKL-DNN and Intel MKL as BLAS library by the command:
133+
3. Enable Intel MKL-DNN and Intel MKL as BLAS library by the command:
134134
```
135135
>"C:\Program Files (x86)\IntelSWTools\compilers_and_libraries\windows\mkl\bin\mklvars.bat" intel64
136-
>cmake -G "Visual Studio 14 Win64" .. -DUSE_CUDA=0 -DUSE_CUDNN=0 -DUSE_NVRTC=0 -DUSE_OPENCV=1 -DUSE_OPENMP=1 -DUSE_PROFILER=1 -DUSE_BLAS=mkl -DUSE_LAPACK=1 -DUSE_DIST_KVSTORE=0 -DCUDA_ARCH_NAME=All -DUSE_MKLDNN=1 -DCMAKE_BUILD_TYPE=Release -DMKL_ROOT="C:\Program Files (x86)\IntelSWTools\compilers_and_libraries\windows\mkl"
136+
>cmake -G "Visual Studio 14 Win64" .. -DUSE_CUDA=0 -DUSE_CUDNN=0 -DUSE_NVRTC=0 -DUSE_OPENCV=1 -DUSE_OPENMP=1 -DUSE_PROFILER=1 -DUSE_BLAS=mkl -DUSE_LAPACK=1 -DUSE_DIST_KVSTORE=0 -DCUDA_ARCH_NAME=All -DUSE_MKLDNN=1 -DCMAKE_BUILD_TYPE=Release -DMKL_ROOT="C:\Program Files (x86)\IntelSWTools\compilers_and_libraries\windows\mkl"
137137
```
138138
4. After the CMake successfully completed, in Visual Studio, open the solution file ```.sln``` and compile it, or compile the the MXNet source code by using following command:
139139
```r
@@ -154,7 +154,7 @@ User can follow the same steps of Visual Studio 2015 to build MXNET with MKL-DNN
154154

155155
<h2 id="4">Verify MXNet with python</h2>
156156

157-
Preinstall python and some dependent modules:
157+
Preinstall python and some dependent modules:
158158
```
159159
pip install numpy graphviz
160160
set PYTHONPATH=[workdir]\incubator-mxnet\python
@@ -261,7 +261,7 @@ MKL_VERBOSE SGEMM(T,N,12,10,8,0x7f7f927b1378,0x1bc2140,8,0x1ba8040,8,0x7f7f927b1
261261

262262
<h2 id="6">Enable graph optimization</h2>
263263

264-
Graph optimization by subgraph feature are available in master branch. You can build from source and then use below command to enable this *experimental* feature for better performance:
264+
Graph optimization with subgraph is available and enabled by default in master branch. For MXNet release v1.5, you can manually enable it by:
265265

266266
```
267267
export MXNET_SUBGRAPH_BACKEND=MKLDNN
@@ -271,7 +271,7 @@ This limitations of this experimental feature are:
271271

272272
- Use this feature only for inference. When training, be sure to turn the feature off by unsetting the `MXNET_SUBGRAPH_BACKEND` environment variable.
273273

274-
- This feature will only run on the CPU, even if you're using a GPU-enabled build of MXNet.
274+
- This feature will only run on the CPU, even if you're using a GPU-enabled build of MXNet.
275275

276276

277277
<h2 id="7">Quantization and Inference with INT8</h2>

example/quantization/README.md

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,7 @@ python imagenet_gen_qsym_mkldnn.py --model=resnet50_v1 --num-calib-batches=5 --c
5050
The model would be automatically replaced in fusion and quantization format. It is then saved as the quantized symbol and parameter files in the `./model` directory. The following command is to launch inference.
5151

5252
```
53-
# USE MKLDNN AS SUBGRAPH BACKEND
54-
export MXNET_SUBGRAPH_BACKEND=MKLDNN
55-
56-
# Launch FP32 Inference
53+
# Launch FP32 Inference
5754
python imagenet_inference.py --symbol-file=./model/resnet50_v1-symbol.json --param-file=./model/resnet50_v1-0000.params --rgb-mean=123.68,116.779,103.939 --rgb-std=58.393,57.12,57.375 --num-skipped-batches=50 --batch-size=64 --num-inference-batches=500 --dataset=./data/val_256_q90.rec --ctx=cpu
5855
5956
# Launch INT8 Inference
@@ -74,8 +71,6 @@ python imagenet_gen_qsym_mkldnn.py --model=squeezenet1.0 --num-calib-batches=5 -
7471
The model would be automatically replaced in fusion and quantization format. It is then saved as the quantized symbol and parameter files in the `./model` directory. The following command is to launch inference.
7572

7673
```
77-
# USE MKLDNN AS SUBGRAPH BACKEND
78-
export MXNET_SUBGRAPH_BACKEND=MKLDNN
7974
8075
# Launch FP32 Inference
8176
python imagenet_inference.py --symbol-file=./model/squeezenet1.0-symbol.json --param-file=./model/squeezenet1.0-0000.params --rgb-mean=123.68,116.779,103.939 --rgb-std=58.393,57.12,57.375 --num-skipped-batches=50 --batch-size=64 --num-inference-batches=500 --dataset=./data/val_256_q90.rec --ctx=cpu
@@ -98,8 +93,6 @@ python imagenet_gen_qsym_mkldnn.py --model=mobilenet1.0 --num-calib-batches=5 --
9893
The model would be automatically replaced in fusion and quantization format. It is then saved as the quantized symbol and parameter files in the `./model` directory. The following command is to launch inference.
9994

10095
```
101-
# USE MKLDNN AS SUBGRAPH BACKEND
102-
export MXNET_SUBGRAPH_BACKEND=MKLDNN
10396
10497
# Launch FP32 Inference
10598
python imagenet_inference.py --symbol-file=./model/mobilenet1.0-symbol.json --param-file=./model/mobilenet1.0-0000.params --rgb-mean=123.68,116.779,103.939 --rgb-std=58.393,57.12,57.375 --num-skipped-batches=50 --batch-size=64 --num-inference-batches=500 --dataset=./data/val_256_q90.rec --ctx=cpu
@@ -122,8 +115,6 @@ python imagenet_gen_qsym_mkldnn.py --model=mobilenetv2_1.0 --num-calib-batches=5
122115
The model would be automatically replaced in fusion and quantization format. It is then saved as the quantized symbol and parameter files in the `./model` directory. The following command is to launch inference.
123116

124117
```
125-
# USE MKLDNN AS SUBGRAPH BACKEND
126-
export MXNET_SUBGRAPH_BACKEND=MKLDNN
127118
128119
# Launch FP32 Inference
129120
python imagenet_inference.py --symbol-file=./model/mobilenetv2_1.0-symbol.json --param-file=./model/mobilenetv2_1.0-0000.params --rgb-mean=123.68,116.779,103.939 --rgb-std=58.393,57.12,57.375 --num-skipped-batches=50 --batch-size=64 --num-inference-batches=500 --dataset=./data/val_256_q90.rec --ctx=cpu
@@ -146,8 +137,6 @@ python imagenet_gen_qsym_mkldnn.py --model=inceptionv3 --image-shape=3,299,299 -
146137
The model would be automatically replaced in fusion and quantization format. It is then saved as the quantized symbol and parameter files in the `./model` directory. The following command is to launch inference.
147138

148139
```
149-
# USE MKLDNN AS SUBGRAPH BACKEND
150-
export MXNET_SUBGRAPH_BACKEND=MKLDNN
151140
152141
# Launch FP32 Inference
153142
python imagenet_inference.py --symbol-file=./model/inceptionv3-symbol.json --param-file=./model/inceptionv3-0000.params --image-shape=3,299,299 --rgb-mean=123.68,116.779,103.939 --rgb-std=58.393,57.12,57.375 --num-skipped-batches=50 --batch-size=64 --num-inference-batches=500 --dataset=./data/val_256_q90.rec --ctx=cpu
@@ -171,10 +160,8 @@ python imagenet_gen_qsym_mkldnn.py --model=imagenet1k-resnet-152 --num-calib-bat
171160
The model would be automatically replaced in fusion and quantization format. It is then saved as the quantized symbol and parameter files in the `./model` directory. The following command is to launch inference.
172161

173162
```
174-
# USE MKLDNN AS SUBGRAPH BACKEND
175-
export MXNET_SUBGRAPH_BACKEND=MKLDNN
176163
177-
# Launch FP32 Inference
164+
# Launch FP32 Inference
178165
python imagenet_inference.py --symbol-file=./model/imagenet1k-resnet-152-symbol.json --param-file=./model/imagenet1k-resnet-152-0000.params --num-skipped-batches=50 --batch-size=64 --num-inference-batches=500 --dataset=./data/val_256_q90.rec --ctx=cpu
179166
180167
# Launch INT8 Inference
@@ -196,10 +183,8 @@ python imagenet_gen_qsym_mkldnn.py --model=imagenet1k-inception-bn --num-calib-b
196183
The model would be automatically replaced in fusion and quantization format. It is then saved as the quantized symbol and parameter files in the `./model` directory. The following command is to launch inference.
197184

198185
```
199-
# USE MKLDNN AS SUBGRAPH BACKEND
200-
export MXNET_SUBGRAPH_BACKEND=MKLDNN
201186
202-
# Launch FP32 Inference
187+
# Launch FP32 Inference
203188
python imagenet_inference.py --symbol-file=./model/imagenet1k-inception-bn-symbol.json --param-file=./model/imagenet1k-inception-bn-0000.params --rgb-mean=123.68,116.779,103.939 --num-skipped-batches=50 --batch-size=64 --num-inference-batches=500 --dataset=./data/val_256_q90.rec --ctx=cpu
204189
205190
# Launch INT8 Inference
@@ -240,10 +225,8 @@ Some tips on quantization configs:
240225
2. Then, you should run the following command and verify that your fp32 symbolic model runs inference as expected.
241226

242227
```
243-
# USE MKLDNN AS SUBGRAPH BACKEND
244-
export MXNET_SUBGRAPH_BACKEND=MKLDNN
245228
246-
# Launch FP32 Inference
229+
# Launch FP32 Inference
247230
python imagenet_inference.py --symbol-file=./model/custom-symbol.json --param-file=./model/custom-0000.params --rgb-mean=* --rgb-std=* --num-skipped-batches=* --batch-size=* --num-inference-batches=*--dataset=./data/* --ctx=cpu
248231
```
249232

@@ -260,7 +243,7 @@ python imagenet_gen_qsym_mkldnn.py --model=custom --num-calib-batches=5 --calib-
260243
6. Finally, you can run INT8 inference:
261244

262245
```
263-
# Launch INT8 Inference
246+
# Launch INT8 Inference
264247
python imagenet_inference.py --symbol-file=./model/*.json --param-file=./model/*.params --rgb-mean=* --rgb-std=* --num-skipped-batches=* --batch-size=* --num-inference-batches=*--dataset=./data/* --ctx=cpu
265248
266249
# Launch dummy data Inference
@@ -289,6 +272,6 @@ the console to run model quantization for a specific configuration.
289272
- `launch_inference.sh` This is a shell script that calculate the accuracies of all the quantized models generated
290273
by invoking `launch_quantize.sh`.
291274

292-
**NOTE**:
275+
**NOTE**:
293276
- This example has only been tested on Linux systems.
294277
- Performance is expected to decrease with GPU, however the memory footprint of a quantized model is smaller. The purpose of the quantization implementation is to minimize accuracy loss when converting FP32 models to INT8. MXNet community is working on improving the performance.

example/ssd/README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,8 +234,6 @@ python quantization.py
234234
After quantization, INT8 models will be saved in `model/` dictionary. Use the following command to launch inference.
235235

236236
```
237-
# USE MKLDNN AS SUBGRAPH BACKEND
238-
export MXNET_SUBGRAPH_BACKEND=MKLDNN
239237
240238
# Launch FP32 Inference on VOC dataset
241239
python evaluate.py --cpu --num-batch 10 --batch-size 224 --deploy --prefix=./model/ssd_

src/c_api/c_api_symbolic.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1035,15 +1035,15 @@ int MXSetCalibTableToQuantizedSymbol(SymbolHandle qsym_handle,
10351035
API_END_HANDLE_ERROR(delete s);
10361036
}
10371037

1038-
int MXGenBackendSubgraph(SymbolHandle sym_handle, const char *backend,
1038+
int MXGenBackendSubgraph(SymbolHandle sym_handle, const char *backend_name,
10391039
SymbolHandle *ret_sym_handle) {
10401040
nnvm::Symbol *s = new nnvm::Symbol();
10411041
API_BEGIN();
10421042
nnvm::Symbol *sym = static_cast<nnvm::Symbol *>(sym_handle);
10431043
*s = sym->Copy();
1044-
std::vector<mxnet::op::SubgraphPropertyPtr> properties =
1045-
mxnet::op::SubgraphPropertyRegistry::Get()->CreateSubgraphProperty(backend);
1046-
for (auto property : properties) {
1044+
auto backend = mxnet::op::SubgraphBackendRegistry::Get()->GetSubgraphBackend(backend_name);
1045+
const auto& subgraph_prop_list = backend->GetSubgraphProperties();
1046+
for (auto property : subgraph_prop_list) {
10471047
nnvm::Graph g = Symbol2Graph(*s);
10481048
property->SetAttr("graph", g);
10491049
g.attrs["subgraph_property"] = std::make_shared<nnvm::any>(std::move(property));

src/c_api/c_api_test.cc

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,11 @@ int MXBuildSubgraphByOpNames(SymbolHandle sym_handle,
4141
nnvm::Symbol* sym = static_cast<nnvm::Symbol*>(sym_handle);
4242
*s = sym->Copy();
4343
if (!op_name_set.empty()) {
44-
std::vector<mxnet::op::SubgraphPropertyPtr> properties =
45-
mxnet::op::SubgraphPropertyRegistry::Get()->CreateSubgraphProperty(prop_name);
46-
for (auto property : properties) {
44+
auto& backend =
45+
mxnet::op::SubgraphBackendRegistry::Get()->GetSubgraphBackend(prop_name);
46+
LOG(INFO) << "Subgraph backend " << backend->GetName() << " is activated.";
47+
const auto& subgraph_prop_list = backend->GetSubgraphProperties();
48+
for (auto property : subgraph_prop_list) {
4749
nnvm::Graph g;
4850
g.outputs = s->outputs;
4951
property->SetAttr("graph", g);

0 commit comments

Comments
 (0)