oneDNN icon indicating copy to clipboard operation
oneDNN copied to clipboard

Tensorflow w/ MKL is slow for mobilenet inference

Open YijinLiu opened this issue 8 years ago • 11 comments

I am trying to use MKL to accelerate tensorflow inference on some mobilenet models. Unfortunately, it turns out MKL actually slows it down quite a bit. The following results are based on tensorflow-1.8.0 running on i7-5557U. I also tried it on other CPUs, got similar results.

Using benchmark_model from tensorflow, It could clearly see that _MklConv2DWithBias and _MklFusedBatchNorm are slower than the normal versions. The weird part is that if I only use mkl (2018.2.199) instead of mkl+mkldnn, _MklFusedBatchNorm is way more fast.

  1. ssd mobilenet v2 benchmark results

OMP_NUM_THREADS=1 bazel run --config=mkl --config=opt --config=monolithic //tensorflow/tools/benchmark:benchmark_model -- --graph=ssd_mobilenet_v2_coco_2018_03_29_frozen.pb --show_flops --input_layer=image_tensor --input_layer_type=uint8 --input_layer_shape=1,1920,1080,3 --output_layer=num_detections,detection_classes,detection_scores,detection_boxes --num_threads=1

NOTE: w/o OMP_NUM_THREADS=1, it's 100% more slower ...

w/MKL FLOPs estimate: 3.75B FLOPs/second: 18.98B

         [Node type]  [count]  [avg ms]    [avg %]    [cdf %]  [mem KB][times called]
   _MklConv2DWithBias       12   143.936    42.710%    42.710% 56555.531       12
           _MklConv2D       43    48.872    14.502%    57.212% 43206.305       43
DepthwiseConv2dNative       21    27.330     8.110%    65.321% 17180.992       21
              _MklMul       43    12.751     3.784%    69.105% 32070.080       43
              _MklAdd       53    12.137     3.601%    72.706% 33662.238       53
                 Cast      183    11.584     3.437%    76.143% 24883.217      183
  _MklInputConversion       96    10.990     3.261%    79.404% 32261.969       96
                Const     1602    10.360     3.074%    82.479%     0.000     1602
                  Mul      127     7.063     2.096%    84.574%     0.004      127
                  Add      119     6.541     1.941%    86.515%    15.344      119
                Relu6       47     3.715     1.102%    87.618%     0.000       47
              Minimum      451     3.680     1.092%    88.709%     0.000      451
               Gather      546     3.607     1.070%    89.780%     0.000      546
                Slice       93     3.543     1.051%    90.831%  1380.240       93
 TensorArrayScatterV3        5     3.261     0.968%    91.799% 25604.012        5
          _MklReshape      107     3.183     0.944%    92.743%   810.972      107
                Where      180     2.560     0.760%    93.503%     1.440      180
         _MklConcatV2       98     2.260     0.671%    94.173%   782.604       98
              Greater      183     2.183     0.648%    94.821%   172.533      183

w/o MKL FLOPs estimate: 3.75B FLOPs/second: 25.61B

          [Node type]  [count]  [avg ms]    [avg %]    [cdf %]  [mem KB][times called]
               Conv2D       55   108.692    47.515%    47.515% 32798.539       55
DepthwiseConv2dNative       21    28.022    12.250%    59.765% 17180.992       21
                  Mul      170    18.866     8.247%    68.012%     0.008      170
                  Add      172    18.460     8.070%    76.082%    15.344      172
                 Cast      183    12.116     5.297%    81.378% 24883.217      183
               Gather      546     4.815     2.105%    83.483%     0.000      546
 TensorArrayScatterV3        5     3.971     1.736%    85.219% 25604.012        5
                Relu6       47     3.619     1.582%    86.801%     0.000       47
                Slice       93     2.915     1.274%    88.075%  1380.240       93
              Minimum      451     2.877     1.258%    89.333%     0.000      451
                Const      476     2.802     1.225%    90.558%     0.000      476
              Maximum      360     2.761     1.207%    91.765%     0.000      360
              Reshape      287     2.318     1.013%    92.778%     0.000      287
                Where      180     1.793     0.784%    93.562%     1.440      180
                  Sub      190     1.749     0.765%    94.327%     0.008      190
                Split      180     1.749     0.765%    95.091%     0.000      180
              Greater      183     1.220     0.533%    95.625%   172.533      183
             ConcatV2       99     1.057     0.462%    96.087%   730.868       99
                Shape      112     1.001     0.438%    96.524%     0.952      112
              Squeeze       92     0.978     0.428%    96.952%     0.000       92
       ResizeBilinear        1     0.885     0.387%    97.339%  1080.000        1
         StridedSlice      113     0.878     0.384%    97.722%     0.432      113
  NonMaxSuppressionV2       90     0.820     0.358%    98.081%     0.000       90
            Transpose        3     0.706     0.309%    98.390%    92.016        3
                Enter       26     0.434     0.190%    98.579%     0.000       26
                 Pack       19     0.433     0.189%    98.769%    30.908       19
            ZerosLike       92     0.362     0.158%    98.927%     0.004       92
              BiasAdd       12     0.326     0.143%    99.069%     0.000       12
        NextIteration        8     0.308     0.135%    99.204%     0.000        8
  1. mobilenet v2 benchmark results w/ MKL FLOPs estimate: 1.16B FLOPs/second: 6.68B

           [Node type]  [count]  [avg ms]    [avg %]    [cdf %]  [mem KB][times called]
    _MklFusedBatchNorm       17   101.507    57.383%    57.383% 13348.368       17
    _MklConv2DWithBias       36    41.942    23.710%    81.094% 49078.117       36
    DepthwiseConv2dNative       17    20.096    11.361%    92.454% 13504.128       17
              _MklToTf       37     5.933     3.354%    95.808% 21880.740       37
                 Relu6       35     3.419     1.933%    97.741%     0.000       35
           _MklAvgPool        1     2.429     1.373%    99.114%     7.696        1
                 Const      333     0.693     0.392%    99.506%     0.000      333
               _MklAdd       10     0.367     0.207%    99.713%  1210.496       10
                  NoOp        1     0.317     0.179%    99.893%     0.000        1
     _MklInputConversion       10     0.101     0.057%    99.950%     0.000       10
           _MklSoftmax        1     0.058     0.033%    99.982%     4.532        1
               _Retval        1     0.007     0.004%    99.986%     0.000        1
           _MklReshape        1     0.007     0.004%    99.990%     0.528        1
               Squeeze        1     0.007     0.004%    99.994%     0.000        1
                  _Arg        1     0.006     0.003%    99.998%     0.000        1
                 Shape        1     0.004     0.002%   100.000%     0.008        1
    

w/o MKL FLOPs estimate: 1.16B FLOPs/second: 8.57B

          [Node type]  [count]  [avg ms]    [avg %]    [cdf %]  [mem KB][times called]
       FusedBatchNorm       17    88.240    63.409%    63.409%   160.512       17
               Conv2D       36    24.969    17.943%    81.351% 25154.725       36
    DepthwiseConv2dNative       17    19.866    14.276%    95.627% 13504.128       17
              BiasAdd       36     2.873     2.065%    97.691%     0.000       36
                Relu6       35     2.642     1.899%    99.590%     0.000       35
                Const      157     0.256     0.184%    99.774%     0.000      157
                  Add       10     0.166     0.119%    99.893%     0.000       10
                 NoOp        1     0.074     0.053%    99.946%     0.000        1
              Softmax        1     0.031     0.022%    99.968%     0.000        1
              AvgPool        1     0.023     0.017%    99.985%     7.168        1
              _Retval        1     0.006     0.004%    99.989%     0.000        1
              Squeeze        1     0.006     0.004%    99.994%     0.000        1
                Shape        1     0.004     0.003%    99.996%     0.008        1
              Reshape        1     0.003     0.002%    99.999%     0.000        1
                 _Arg        1     0.002     0.001%   100.000%     0.000        1

YijinLiu avatar May 09 '18 22:05 YijinLiu

Hi @YijinLiu, thank you for the report. MobileNet topology includes a number of depthwise separable convolutions are currently implemented in Tensorflow via Eigen (DepthwiseConv2dNative) instead of MKL-DNN. Because of this fact batch normalization following depthwise convolution gets the data in plain format (NHWC or NCHW), instead of blocked format as blocked format as expected. With MKL-DNN v0.13 this results in a performance hit that you observe. As soon as Tensorflow will starts using MKL-DNN for depthwise convolution you'll see significant performance improvement in comparison to the native implementation.

We also optimized a number of primitives including batch normalization for NHWC and NCHW data formats in Intel MKL-DNN v0.14. If you build Tensorflow with this version the performance issue should go away as well.

vpirogov avatar May 09 '18 23:05 vpirogov

Hi @YijinLiu! While this result is expected for batch normalization as @vpirogov already mentioned, it's really strange that regular (non-depthwise) convolutions are so slow. Could you please rerun the first topology with export MKLDNN_VERBOSE=1 and check which MKL-DNN implementation works for convolution?

tprimak avatar May 10 '18 00:05 tprimak

@vpirogov Thanks. I tried mkldnn v0.14, _MklFusedBatchNorm indeed gets much faster. However, it's still much slower than mkl. Any clue what's going on?

w/ mkldnn v0.14 FLOPs estimate: 1.16B FLOPs/second: 11.86B

             [Node type]      [count]     [avg ms]      [avg %]     [cdf %]   [mem KB]  [times called]
      _MklFusedBatchNorm           17       44.421      44.027%     44.027%  13348.368         17  
      _MklConv2DWithBias           36       34.020      33.718%     77.745%  49078.117         36  
   DepthwiseConv2dNative           17       10.907      10.810%     88.555%  15059.328         17  
                _MklToTf           37        5.109       5.064%     93.619%  21880.740         37  
                   Relu6           35        3.046       3.019%     96.638%      0.000         35  
             _MklAvgPool            1        2.189       2.170%     98.808%      7.696          1   
                   Const          333        0.573       0.568%     99.376%      0.000        333 
                 _MklAdd           10        0.311       0.308%     99.684%   1210.496         10  
                    NoOp            1        0.197       0.195%     99.879%      0.000          1   
     _MklInputConversion           10        0.069       0.068%     99.947%      0.000         10  
             _MklSoftmax            1        0.031       0.031%     99.978%      4.532          1   
                 _Retval            1        0.006       0.006%     99.984%      0.000          1   
             _MklReshape            1        0.005       0.005%     99.989%      0.528          1   
                 Squeeze            1        0.005       0.005%     99.994%      0.000          1   
                    _Arg            1        0.003       0.003%     99.997%      0.000          1   
                   Shape            1        0.003       0.003%    100.000%      0.008          1  

w/ mkl 2018.2.199 FLOPs estimate: 1.16B FLOPs/second: 13.92B

             [Node type]      [count]     [avg ms]      [avg %]     [cdf %]   [mem KB]  [times called]
      _MklConv2DWithBias           36       47.385      56.937%     56.937%  63826.758         36          
      _MklFusedBatchNorm           17       12.916      15.520%     72.457%  13614.604         17          
   DepthwiseConv2dNative           17       10.804      12.982%     85.439%  15059.328         17          
                _MklToTf           49        7.988       9.598%     95.037%  36704.613         49          
                   Relu6           35        2.828       3.398%     98.436%      0.000         35          
                   Const          343        0.569       0.684%     99.119%      0.000        343         
                    NoOp            1        0.236       0.284%     99.403%      0.000          1           
                     Add            6        0.147       0.177%     99.579%      0.000          6           
             _MklAvgPool            1        0.141       0.169%     99.749%     24.300          1           
     _MklInputConversion            4        0.094       0.113%     99.862%    381.216          4           
                 _MklAdd            4        0.064       0.077%     99.939%    370.048          4           
                 Softmax            1        0.028       0.034%     99.972%      0.000          1           
                 _Retval            1        0.006       0.007%     99.980%      0.000          1           
             _MklReshape            1        0.006       0.007%     99.987%      2.696          1           
                    _Arg            1        0.004       0.005%     99.992%      0.000          1           
                 Squeeze            1        0.004       0.005%     99.996%      0.000          1           
                   Shape            1        0.003       0.004%    100.000%      0.008          1

YijinLiu avatar May 10 '18 08:05 YijinLiu

@tprimak I run these benchmarks on v0.12, v0.13 and v0.14. All have very slow _MklConv2DWithBias. The verbose log for v0.14 is here https://drive.google.com/open?id=14KUmBeIMsobxHRYoO7xGzGRIgvLTQ9XU

One interesting I want to point out is mkldnn's _MklConv2DWithBias is much faster than mkl-2018.2.199's...

YijinLiu avatar May 10 '18 08:05 YijinLiu

@vpirogov Forgot to ask. Do you know when Tensorflow will have MKL's version of DepthwiseConv2dNative? I am eager to try that..

YijinLiu avatar May 10 '18 08:05 YijinLiu

@YijinLiu, thanks for running this experiment. One of the things about batch normalization is that it's performance dependent on compiler ability to vectorize the code. If you are building the Tensorflow with older version of GCC (think 4.x) it might not be able to use Intel AVX2 instructions. What version of GCC did you use?

I don't have much visiblity into Tensorflow plans. You might want to submit an issue to Tensorflow repo and tag @agramesh1.

vpirogov avatar May 10 '18 17:05 vpirogov

I note there are too many conversions (reorders). So even though newer GCC might help, especially if the corresponding flags like -mavx2 are passed, but still seems like something (most likely depthwise convolutions) triggers conversions from/to Intel MKL-DNN internal blocked layout to/from TensorFlow ones.

emfomenk avatar May 10 '18 17:05 emfomenk

Indeed, I missed the fact that reorders are included into primitive time in TF performance report.

vpirogov avatar May 10 '18 17:05 vpirogov

I used gcc 5.4.0 with -march=native. "gcc -march=native -mtune=native -Q --help=target | grep avx2 shows" -mavx2 [enabled] -mavx256-split-unaligned-load [disabled] -mavx256-split-unaligned-store [disabled]

If this may help, I copied one of the compilation command here: /usr/bin/c++ -DMKLDNN_DLL -DMKLDNN_DLL_EXPORTS -DUSE_CBLAS -DUSE_MKL -D__STDC_CONSTANT_MACROS -D__STDC_LIMIT_MACROS -I/usr/local/intel/mkl/include -I/home/chao/projects/mkl-dnn/include -I/home/chao/projects/mkl-dnn/src -I/home/chao/projects/mkl-dnn/src/common -I/home/chao/projects/mkl-dnn/src/cpu/xbyak -std=c++11 -fvisibility-inlines-hidden -Wall -Werror -Wno-unknown-pragmas -fvisibility=internal -Wno-strict-overflow -march=native -Wformat -Wformat-security -fstack-protector-strong -O3 -DNDEBUG -D_FORTIFY_SOURCE=2 -std=gnu++11 -o CMakeFiles/mkldnn.dir/common/batch_normalization.cpp.o -c /home/chao/projects/mkl-dnn/src/common/batch_normalization.cpp

and "c++ -v" output Using built-in specs. COLLECT_GCC=c++ COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/5/lto-wrapper Target: x86_64-linux-gnu Configured with: ../src/configure -v --with-pkgversion='Ubuntu 5.4.0-6ubuntu1~16.04.9' --with-bugurl=file:///usr/share/doc/gcc-5/README.Bugs --enable-languages=c,ada,c++,java,go,d,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-5 --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-vtable-verify --enable-libmpx --enable-plugin --with-system-zlib --disable-browser-plugin --enable-java-awt=gtk --enable-gtk-cairo --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-5-amd64/jre --enable-java-home --with-jvm-root-dir=/usr/lib/jvm/java-1.5.0-gcj-5-amd64 --with-jvm-jar-dir=/usr/lib/jvm-exports/java-1.5.0-gcj-5-amd64 --with-arch-directory=amd64 --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --enable-objc-gc --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu Thread model: posix gcc version 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.9)

YijinLiu avatar May 10 '18 18:05 YijinLiu

I also uploaded a log file for ssd mobilenet: https://drive.google.com/file/d/12ClzFKiOryge6So-trXrvEyEk6ycOheY/view?usp=sharing

YijinLiu avatar May 14 '18 19:05 YijinLiu

@YijinLiu, thanks for the data. Based on the log the root of performance issue seem to be in the number of reorders that Tensorflow generates for this topology and the degradation should go away when MKL-DNN's depthwise convolution is integrated.

vpirogov avatar May 14 '18 22:05 vpirogov