oneDNN icon indicating copy to clipboard operation
oneDNN copied to clipboard

Convolution: gemm:jit vs jit_avx2

Open duli2012 opened this issue 8 years ago • 9 comments

Hi,

I ran a few cnn models with MKLDNN. Based on profiling info, some of them use gemm:jit conv while the others use jit_avx2. It appears jit_avx2 is faster. How dose mkldnn decide which convolution implementation to use? What should I do if I want to use jit_avx2 all the time? BTW, I'm use mklddn 0.15.

Thanks.

Du

duli2012 avatar Oct 23 '18 22:10 duli2012

Hi Du,

MKL-DNN first tries to use the jitted direct convolution (jit_avx2 in your case), but if that is not available (due to incompatible shape of convolution, for example) it will "fall back" to the GEMM-based kernel (gemm:jit in your case).

Without the exact convolution shapes, I cannot say why some are going to gemm:jit and some are going to jit_avx2. But if any of your convolutions use number of (input or output) channels not a multiple of 8 (the SIMD width for f32 values on AVX2), then I suggest you try v0.16 as it includes "Improved performance of fp32 convolution with number of input channels not being a multiple of SIMD width on Intel(R) Xeon processors with Intel(R) AVX2 instruction set support."

If you need more help, please share mkldnn_verbose output.

Regards, Keola

kwiersch avatar Oct 23 '18 23:10 kwiersch

Thanks, Keola,

I upgraded to 0.16, but it didn't help.

Here is the profile info: mkldnn_verbose,exec,reorder,jit:uni,undef,in:f32_nhwc out:f32_nchw,num:1,800x1x15x200,1.9912 mkldnn_verbose,exec,reorder,jit:uni,undef,in:f32_hwio out:f32_oihw,num:1,100x1x5x200,0.1027 mkldnn_verbose,exec,convolution,gemm:jit,forward_training,fsrc:nchw fwei:oihw fbia:undef fdst:nchw,alg:convolution_direct,mb800_g1ic1oc100_ih15oh11kh5sh1dh0ph0_iw200ow1kw200sw1dw0pw0,40.5582 mkldnn_verbose,exec,reorder,jit:uni,undef,in:f32_nchw out:f32_nhwc,num:1,800x100x11x1,0.9083 mkldnn_verbose,exec,pooling,nhwc_pooling:any,forward_inference,fdata:nhwc fws:undef,alg:pooling_max,mb800ic100_ih11oh1kh11sh1ph0_iw1ow1kw1sw1pw0,0.324 mkldnn_verbose,exec,reorder,jit:uni,undef,in:f32_nhwc out:f32_nchw,num:1,320x1x15x200,0.4652 mkldnn_verbose,exec,reorder,jit:uni,undef,in:f32_hwio out:f32_oihw,num:1,100x1x5x200,0.0568 mkldnn_verbose,exec,convolution,gemm:jit,forward_training,fsrc:nchw fwei:oihw fbia:undef fdst:nchw,alg:convolution_direct,mb320_g1ic1oc100_ih15oh11kh5sh1dh0ph0_iw200ow1kw200sw1dw0pw0,9.3916 mkldnn_verbose,exec,reorder,jit:uni,undef,in:f32_nchw out:f32_nhwc,num:1,320x100x11x1,0.142 mkldnn_verbose,exec,pooling,nhwc_pooling:any,forward_inference,fdata:nhwc fws:undef,alg:pooling_max,mb320ic100_ih11oh1kh11sh1ph0_iw1ow1kw1sw1pw0,0.0727 mkldnn_verbose,exec,reorder,jit:uni,undef,in:f32_nhwc out:f32_nchw,num:1,8x1x40x100,0.0136 mkldnn_verbose,exec,reorder,jit:uni,undef,in:f32_hwio out:f32_oihw,num:1,100x1x1x100,0.0104001 mkldnn_verbose,exec,convolution,gemm:jit,forward_training,fsrc:nchw fwei:oihw fbia:undef fdst:nchw,alg:convolution_direct,mb8_g1ic1oc100_ih40oh40kh1sh1dh0ph0_iw100ow1kw100sw1dw0pw0,0.1009 mkldnn_verbose,exec,reorder,jit:uni,undef,in:f32_nchw out:f32_nhwc,num:1,8x100x40x1,0.0146999 mkldnn_verbose,exec,pooling,nhwc_pooling:any,forward_inference,fdata:nhwc fws:undef,alg:pooling_max,mb8ic100_ih40oh1kh40sh1ph0_iw1ow1kw1sw1pw0,0.0138 mkldnn_verbose,exec,reorder,jit:uni,undef,in:f32_nhwc out:f32_nchw,num:1,8x1x40x100,0.0108 mkldnn_verbose,exec,reorder,jit:uni,undef,in:f32_hwio out:f32_oihw,num:1,100x1x40x1,0.00880003 mkldnn_verbose,exec,convolution,gemm:jit,forward_training,fsrc:nchw fwei:oihw fbia:undef fdst:nchw,alg:convolution_direct,mb8_g1ic1oc100_ih40oh1kh40sh1dh0ph0_iw100ow100kw1sw1dw0pw0,0.0765001 mkldnn_verbose,exec,reorder,jit:uni,undef,in:f32_nchw out:f32_nhwc,num:1,8x100x1x100,0.0247999 mkldnn_verbose,exec,pooling,nhwc_pooling:any,forward_inference,fdata:nhwc fws:undef,alg:pooling_max,mb8ic100_ih1oh1kh1sh1ph0_iw100ow1kw100sw1pw0,0.0306 mkldnn_verbose,exec,reorder,jit:uni,undef,in:f32_nhwc out:f32_nchw,num:1,800x1x15x200,1.262 mkldnn_verbose,exec,reorder,jit:uni,undef,in:f32_hwio out:f32_oihw,num:1,100x1x5x200,0.0709 mkldnn_verbose,exec,convolution,gemm:jit,forward_training,fsrc:nchw fwei:oihw fbia:undef fdst:nchw,alg:convolution_direct,mb800_g1ic1oc100_ih15oh11kh5sh1dh0ph0_iw200ow1kw200sw1dw0pw0,23.2282 mkldnn_verbose,exec,reorder,jit:uni,undef,in:f32_nchw out:f32_nhwc,num:1,800x100x11x1,0.4496 mkldnn_verbose,exec,pooling,nhwc_pooling:any,forward_inference,fdata:nhwc fws:undef,alg:pooling_max,mb800ic100_ih11oh1kh11sh1ph0_iw1ow1kw1sw1pw0,0.1982 mkldnn_verbose,exec,reorder,jit:uni,undef,in:f32_nhwc out:f32_nchw,num:1,320x1x15x200,0.4691 mkldnn_verbose,exec,reorder,jit:uni,undef,in:f32_hwio out:f32_oihw,num:1,100x1x5x200,0.0644 mkldnn_verbose,exec,convolution,gemm:jit,forward_training,fsrc:nchw fwei:oihw fbia:undef fdst:nchw,alg:convolution_direct,mb320_g1ic1oc100_ih15oh11kh5sh1dh0ph0_iw200ow1kw200sw1dw0pw0,9.7718 mkldnn_verbose,exec,reorder,jit:uni,undef,in:f32_nchw out:f32_nhwc,num:1,320x100x11x1,0.163 mkldnn_verbose,exec,pooling,nhwc_pooling:any,forward_inference,fdata:nhwc fws:undef,alg:pooling_max,mb320ic100_ih11oh1kh11sh1ph0_iw1ow1kw1sw1pw0,0.086 mkldnn_verbose,exec,reorder,jit:uni,undef,in:f32_nhwc out:f32_nchw,num:1,8x1x40x100,0.0157 mkldnn_verbose,exec,reorder,jit:uni,undef,in:f32_hwio out:f32_oihw,num:1,100x1x1x100,0.0152 mkldnn_verbose,exec,convolution,gemm:jit,forward_training,fsrc:nchw fwei:oihw fbia:undef fdst:nchw,alg:convolution_direct,mb8_g1ic1oc100_ih40oh40kh1sh1dh0ph0_iw100ow1kw100sw1dw0pw0,0.0846 mkldnn_verbose,exec,reorder,jit:uni,undef,in:f32_nchw out:f32_nhwc,num:1,8x100x40x1,0.0142 mkldnn_verbose,exec,pooling,nhwc_pooling:any,forward_inference,fdata:nhwc fws:undef,alg:pooling_max,mb8ic100_ih40oh1kh40sh1ph0_iw1ow1kw1sw1pw0,0.0124999 mkldnn_verbose,exec,reorder,jit:uni,undef,in:f32_nhwc out:f32_nchw,num:1,8x1x40x100,0.0123 mkldnn_verbose,exec,reorder,jit:uni,undef,in:f32_hwio out:f32_oihw,num:1,100x1x40x1,0.00900006 mkldnn_verbose,exec,convolution,gemm:jit,forward_training,fsrc:nchw fwei:oihw fbia:undef fdst:nchw,alg:convolution_direct,mb8_g1ic1oc100_ih40oh1kh40sh1dh0ph0_iw100ow100kw1sw1dw0pw0,0.101 mkldnn_verbose,exec,reorder,jit:uni,undef,in:f32_nchw out:f32_nhwc,num:1,8x100x1x100,0.0263 mkldnn_verbose,exec,pooling,nhwc_pooling:any,forward_inference,fdata:nhwc fws:undef,alg:pooling_max,mb8ic100_ih1oh1kh1sh1ph0_iw100ow1kw100sw1pw0,0.0243

Best.

Du

duli2012 avatar Oct 24 '18 01:10 duli2012

BTW, it's on Windows.

duli2012 avatar Oct 24 '18 01:10 duli2012

Okay, I think I understand why the convolutions are going to gemm:jit instead of jit_avx2: the jitted direct convolution implementation requires fdst and fwei to be in blocked format (in this case nChw8c and Ohwi8o), but instead they are being passed in plain format (in this case nchw and oihw). Normally fsrc would also need to be blocked (nChw8c), but since there is a single input channel nchw is okay (this is a corner case that MKL-DNN supports due to 1st convolution in image processing models often having only 1 or 3 input channels).

Since it would be way too confusing to keep track of all these memory formats for each convolution, it is recommended to create memory descriptors for convolution data with no specified format (ie mkldnn_any), use these to create the convolution, then query the convolution to see if reorder is needed. You can follow the example simple_net.cpp to see how it's done in practice. Also, for more information on memory formats in MKL-DNN, please refer to this page in our documentation.

One other thing that stands out to me is that the filter size in your convolutions can be quite large, especially in the w-direction. So please let me know if you still see gemm:jit convolutions after switching to format any with reorders as needed. Thanks!

kwiersch avatar Oct 24 '18 06:10 kwiersch

Hi Keola,

I was actually following the pattern in the simple_net example: create a convolution primitive using mkldnn_any format and reorder was invoked as showed in the log: mkldnn_verbose,exec,reorder,jit:uni,undef,in:f32_nhwc out:f32_nchw,num:1,800x1x15x200,1.9912 // reorder src, since my input fsrc was NHWC mkldnn_verbose,exec,reorder,jit:uni,undef,in:f32_hwio out:f32_oihw,num:1,100x1x5x200,0.1027 // reorder wei, since my input fwei was hwio mkldnn_verbose,exec,convolution,gemm:jit,forward_training,fsrc:nchw fwei:oihw fbia:undef fdst:nchw,alg:convolution_direct,mb800_g1ic1oc100_ih15oh11kh5sh1dh0ph0_iw200ow1kw200sw1dw0pw0,40.5582 mkldnn_verbose,exec,reorder,jit:uni,undef,in:f32_nchw out:f32_nhwc,num:1,800x100x11x1,0.9083 // reorder dst, since my fdst is supposed to be NHWC

Is there anything wrong? Otherwise, is this due to the filter size?

Thanks.

Du

duli2012 avatar Oct 24 '18 16:10 duli2012

It should not be due to filter size, as when I run these convolutions in benchdnn they all go to jit:avx2 implementation. Can you send a reproducer (either stand-alone program or benchdnn command)?

kwiersch avatar Oct 24 '18 17:10 kwiersch

Hi @duli2012, can you please try updating to latest master? Since v0.16, we have some performance improvements that could be relevant (specifically, from 89350ffaaddd12f97480a651ee95b122daa88ed9 we added support in the JITted AVX2 convolution implementation for number output channels not a mulitple of SIMD width (such as oc=100, which is not a multiple of 8, the SIMD width for AVX2).

Edit: In my previous comment, where I said that these convolutions were going to jit:avx2 implementation when running in benchdnn, I was using the lastest updates to master branch. But after double-checking with v0.16, I see they go to gemm:jit, and that it isn't until commit 89350ffaaddd12f97480a651ee95b122daa88ed9 that they go to jit:avx2. Sorry for any confusion!

kwiersch avatar Oct 25 '18 16:10 kwiersch

Thank you, Keola. I'll try master to see if the issue is addressed.

Du

duli2012 avatar Oct 25 '18 17:10 duli2012

After updating to master, the jit:avx2 was enabled for conv.

Thanks.

Du

duli2012 avatar Nov 01 '18 19:11 duli2012