oneDNN icon indicating copy to clipboard operation
oneDNN copied to clipboard

does mkldnn support depthwise convolution?

Open xiechao4 opened this issue 9 years ago • 16 comments

does it support depthwise convolution? If it supports, there is any example?

xiechao4 avatar Nov 28 '17 08:11 xiechao4

Hi @kriszhangyf,

Formally speaking MKL-DNN supports groups which covers the functionality of depth-wise convolution:

dst[n][c][oh][ow] <-- src[n, c, ih, iw] (*) wei[c][1][1][kh][kw]

Here wei is 5D tensor of weights with number of groups = c, oc = 1, ic = 1, and kh x kw spatial.

But for now we don't have an optimized (jitted) version of those. MKL-DNN will fallback to gemm-based convolution. Though, we have plans to add the jitted version as well (so far the forward pass only).

emfomenk avatar Nov 28 '17 15:11 emfomenk

Does it means that the out channel is still c? can it supports the depthwide conv as src[n, c, ih, iw] * wei[oc, 1, kh, hw] -> dst[n, oc, oh, ow]? which oc = g*c

xiechao4 avatar Nov 29 '17 02:11 xiechao4

Convolution has strict definition, so

src[n, ic, ih, iw] (*) wei[oc, 1, kh, hw]

is illegal, since ic in source doesn't match ic in weights.

emfomenk avatar Nov 29 '17 23:11 emfomenk

Hi @emfomenk,

Are there any updates on the optimized depthwise convolution?

Thanks

jimeiyang avatar Feb 23 '18 21:02 jimeiyang

Hi,

we recently tuned gemm-based implementation for depthwise convolution (see 967d7bf). Also we will promote avx2 specialized version for forward pass (maybe in few days).

If you want to check performance for mobilenet or xception convolutions, you may use benchdnn with corresponding input file (see 3768fb22).

emfomenk avatar Feb 25 '18 23:02 emfomenk

Just a quick update: optimized avx2 version is here 3edac962.

emfomenk avatar Feb 27 '18 06:02 emfomenk

That's awesome! Thanks! I'm still struggling with compiling intel-caffe on macOS. Hope I can test it soon.

jimeiyang avatar Feb 27 '18 19:02 jimeiyang

@emfomenk Hi! Thank you for your answer! Is there any instructions about how to integrate this in Intel caffe?

fei1107 avatar Apr 10 '18 13:04 fei1107

Hi @fei1107,

Depthwise convolution uses regular grouped API (basically ic = oc = g), so I think IntelCaffe already supports it (since IntelCaffe supports grouped convolutions).

emfomenk avatar Apr 10 '18 14:04 emfomenk

@emfomenk Thanks! Do you mean that when ic = oc = g, Intel caffe will automatically run on the optimized depthwise convolution code rather than general convolution?

fei1107 avatar Apr 10 '18 14:04 fei1107

I would think so. Whenever you feed IntelCaffe with the topology like the one below optimized depthwise convolution should be used.

...
layer {
  type: "Convolution"
  bottom: "conv1" # with num_output: 32
  ...
  convolution_param {
    num_output: 32
    group: 32
    ...
  }
}
...

emfomenk avatar Apr 10 '18 14:04 emfomenk

@emfomenk Many thanks! Does the depthwise conv support quantization for int8 on avx512 right now? And one last question, how can I fuse the convolution and relu layer in intel caffe, do I have to set some flag, or it can fuse automatically?Really appreciate for your answer!

fei1107 avatar Apr 10 '18 14:04 fei1107

Does the depthwise conv support quantization for int8 on avx512 right now?

Not sure I understand the question.

  • If you are asking whether MKL-DNN supports int8 depthwise convolution then the answer is yes, thought we don't have a kernel with direct implementation (so int8-gemm-based fallback will be used).

  • If you are asking whether you can quantize the input/weights (f32 -> int8) or dequantize output (int8 -> f32) then the answer is also yes. You can do that via reorders.

  • Also you can apply output_scaling factor for int8 depthwise convolution output.

Long story short, everything applicable for regular convolution is applicable for depthwise convolution as well (since in MKL-DNN depthwise convolution is treated as the regular one).

how can I fuse the convolution and relu layer in intel caffe, do I have to set some flag, or it can fuse automatically?

AFAIK, IntelCaffe will do some fusion on the fly. In particular it can merge convlutions followed by relu. It also can fuse convolutions followed by batch-normalization (on inference), and residual blocks (conv + relu + sum). For more details see: compile rules in net.cpp. You can also try running your topology and look at the output of IntelCaffe. If you run ResNet-50 you most likely will see something like:

...
I0305 23:44:59.466156  3880 remove_batch_norm.cpp:276] Dropped Layer: bn_conv1 # <-- batch normalization is fused
...
I0305 23:44:59.466895  3880 net.cpp:739] Dropped layer: res2a_branch2a_relu # <-- relu is fused
...

emfomenk avatar Apr 10 '18 14:04 emfomenk

@emfomenk That's so great! I will try that after my network's training is done. Once again , thanks for your reply!

fei1107 avatar Apr 10 '18 14:04 fei1107

Hi, What would happen if ic == g but oc == ic*n (is a multiple)?

sfraczek avatar Oct 26 '18 12:10 sfraczek

Hi @sfraczek, oc a multiple of ic = g is a valid convolution. In this case, within each group there are n output filters for each input filter. But note that unlike the case of depthwise convolutions (g = ic = oc), there is currently no jitted implementation, so convolutions such as mb1_g32ic32oc64_ih10oh10kh3ph1 will dispatch to gemm-based implementation.

kwiersch avatar Oct 26 '18 15:10 kwiersch