oneDNN icon indicating copy to clipboard operation
oneDNN copied to clipboard

was relu fused in the convolution layers?

Open haidj opened this issue 9 years ago • 8 comments

Hi all,

I have a question with this convolution code and with_relu flag.

If this is an implementation for fusing the convolution layer and relu, is it redundant to add the relu layer in network configuration?

Thank you.

haidj avatar Oct 24 '17 10:10 haidj

Hi @HAIDJ,

MKL-DNN has two kind of primitives: convolution and convolution merged with relu.

So if a topology has a sequence of conv followed by relu you can

  • either create two separate primitives (conv and separately relu)
  • or create one but merged (conv+relu)

The latter does not require creating a separate relu primitive. It also should give better performance due to less memory passes and .

emfomenk avatar Oct 24 '17 15:10 emfomenk

Hi @emfomenk Thank you for your quick reply. But, I ask for your answers to additional questions.

For the same reason to improve performance, are there other primitives such as (bn + relu), (conv + bn) (inference) yet? (or do you have additional plans?) Theoretically, I think their fusion is also possible.

Also, for the purpose of minimizing the memory pass, the current concat implementation seems to have a large overhead.

Do you plan to improve the concat implementation in such a way that physical memory transfer does not actually occur in the future?

Thank you.

haidj avatar Oct 24 '17 15:10 haidj

Thanks for the great questions! 👍

For the same reason to improve performance, are there other primitives such as (bn + relu), (conv + bn) (inference) yet?

It depends on whether you are talking about training or inference.

  • For training it is not that straightforward to express fusion via API. Also you need to keep intermediate buffers to handle backward pass then. So fusion definitely makes sense, but so far it is not clear how to proceed.
  • For inference you typically can incorporate bn into convolution weights directly, so MKL-DNN doesn't need to do anything on its side. bn + relu typically following convolution, so you want just fuse them all-together, i.e.: conv + bn + relu.

As of plans. We are currently working on adding accumulation capabilities to some MKL-DNN primitives, so that one can perform the following chain: dst += conv().relu(). This can substantially improve performance of the topologies with sum node, e.g. ResNet.

Do you plan to improve the concat implementation in such a way that physical memory transfer does not actually occur in the future?

Good point! We are definitely thinking about concat-fusion, since this can significantly improve the performance of some important topologies, like ones that have inception module. Alas we don't have clear vision on how can we do that from API perspective -- it looks really complicated/messy/confusing, especially due to our blocked/any data format we have.

emfomenk avatar Oct 25 '17 15:10 emfomenk

Hi, @emfomenk

Thank you for your detailed answer. As you said, the fusion between backward layers seems not easy.

Nevertheless, it may be necessary to use streaming store instructions for concatenation layer that just copy src to dst.

Thank you.

haidj avatar Nov 08 '17 01:11 haidj

Good point, @HAIDJ.

Right now our concat primitive (as well as sum) is based on reorders which are mostly implemented in C/C++. At some point we may rewrite them using jit and will probably use nt-stores (e.g. whenever the data is bigger than LLC) as you suggested. Though I don't have any timeline on that...

emfomenk avatar Nov 08 '17 15:11 emfomenk

I truly appreciate your careful reply.

Thank you.

haidj avatar Nov 15 '17 06:11 haidj

Can you share psudo code for merge con+relu?

hag5888 avatar Dec 13 '22 19:12 hag5888

Hi @hag5888, you can find real code for merging convolution with relu in this example (just search for "append_eltwise"): https://oneapi-src.github.io/oneDNN/page_cnn_inference_int8_cpp.html#doxid-cnn-inference-int8-cpp.

If you need more help, please open a new issue -- that's the best way to ensure you get a response.

kwiersch avatar Dec 15 '22 05:12 kwiersch