oneDNN icon indicating copy to clipboard operation
oneDNN copied to clipboard

LSTM stack implementation

Open pranathibl opened this issue 6 years ago • 11 comments

Hi,

I have tried to implement stack of three LSTM layers. whose configurations are as follows;

LSTM layer 1: input size(t, n, c) = (1, 1, 3), output size(t, n, c) = (1, 1, 100) LSTM layer 2: input size(t, n, c) = (1, 1, 100), output size(t, n, c) = (1, 1, 100) LSTM layer 3: input size(t, n, c) = (1, 1, 100), output size(t, n, c) = (1, 1, 100) No. of hidden units for all the layers are 100, all the layers are unidirectional layers.

in the above case, i am unable to fuse all the 3 layers, instead able to fuse only layers 2,3. when i try to fuse all the LSTM layers, it throws an error saying "could not create an LSTM forward descriptor". Is this expected?

can you also explain what are the requirements to fuse a stack of Bi directional LSTM layers?

Thanks, Pranathi.

pranathibl avatar Feb 14 '20 10:02 pranathibl

Hi @pranathibl. Can you please share the code?

rsdubtso avatar Feb 14 '20 15:02 rsdubtso

Hi @pranathibl , you cannot fuse the first layer here because the input size is different than the output size. This limitation is documented in the developper guide (https://intel.github.io/mkl-dnn/dev_guide_rnn.html). The reason behind this limitation is that the weights_layer tensor contains the weights applied to the input of each layer, and the weights for each layer share the same "input size" dimension in this tensor.

Regarding the bidirectional layers, the only restriction are that

  • the left2right and right2left should share the same dimensions
  • if using bidirectional concat, the output size in dst_layer should be equal to 2x the output size in dst_iter, weights_layer and weights_iter.

mgouicem avatar Feb 14 '20 23:02 mgouicem

Hi,

regarding Bidirectional LSTM stack, I have tried to fuse following Bi-LSTM layers

LSTM layer 1: input size(t, n, c) = (1, 1, 50), output size(t, n, c) = (1, 1, 50) LSTM layer 2: input size(t, n, c) = (1, 1, 50), output size(t, n, c) = (1, 1, 50) LSTM layer 3: input size(t, n, c) = (1, 1, 50), output size(t, n, c) = (1, 1, 50) No. of hidden units for all the layers are 25, all the layers are bidirectional layers.

with src_layer dims (t, n, c) = (1, 1, 50) weights_layer dim(l,d,i,g,o) = (3, 2, 50, 4, 25) weights_iter dims(l, d, i, g, o) = (3, 2, 25, 4, 25) dst_iter dims (l, d, n, c) = (3, 2, 1, 25) dst_layer dims (t, n, c) = (1, 1, 50)

I am seeing "could not create an LSTM forward descriptor" errors. Can you explain why i am seeing this error for above dimensions with fusion? (FYI, I am able to use Bi-LSTM properly for above layers without fusion)

Could you also explain how Bi-LSTM fusion works?

Thanks, Pranathi.

pranathibl avatar Feb 17 '20 06:02 pranathibl

Hi @pranathibl , from the dimension you specified, I assume you are using bidirectional_concat. It seems you are expecting the concatenation to happen between each layer. However, DNNL does not support that as of now. In DNNL, the concatenation happens after the left2right and right2left execution of both stack happened, as depicted below. In light blue, are the input and output of the primitive, in dark blue the concatenation, and in grey each RNN layer (with their output dimension). image

mgouicem avatar Feb 18 '20 17:02 mgouicem

Hi @mgouicem ,

I am sharing the standalone setup to reproduce the issue i am facing. bi_lstm_layerFusion.zip

Can you tell me where am i going wrong?

pranathibl avatar Feb 19 '20 05:02 pranathibl

Hi @pranathibl , There are 2 issues there.

  1. You are using unidirectional_left2right instead of bidirectional_concat at lstm.cpp:213
  2. my understanding from your weights sizes is that you are trying to do the concatenation between each layers (but let me know if it is not the case). In DNNL we do not support that at the moment, you will have to run each layer separately. (Sorry it was not very clear from the previous picture).

From my understanding, here is what you are trying to do (with I = 2 * H) image In that picture, the dimension of the data goes from I to H after multiplication by ipWgts in your code, and going from H to I happens in the concat layer.

In DNNL, here is what we support (no concat between fused layers, H = 2*I): image Note that here, ipWgts do not change the dimensions of the data. Only the concat at the end does change the dimension of the data from I to H. Let me know if this is still not clear.

mgouicem avatar Feb 19 '20 19:02 mgouicem

Hi @mgouicem

Thanks for the detailed explanation. I have the following doubts:

  1. The two methods that you have explained above seem different. I assume that outputs would not be same for both the cases. Is this expected?

  2. Is there any specific use case that follows second method? As, when fusing Two Bi-LSTM layers, we expect it to be done in first method.

Thanks, Pranathi

pranathibl avatar Feb 25 '20 11:02 pranathibl

Hi @pranathibl ,

  1. yes, these two methods are different: the weight shapes are different and they will return different results.
  2. When we designed our API, most model we were looking at used only 1 bidirectional layer (in which case both methods are equivalent), and one of the models used the second method (if I remember, the Tensorflow NMT tutorial implements method 2) so we implemented that second method. We left the first method as an option to implement later (but we have not done so yet). However, I agree that this first method seems to be widely used.

mgouicem avatar Feb 25 '20 18:02 mgouicem

When can we expect the support for first method?

pranathibl avatar Feb 26 '20 12:02 pranathibl

Hi @pranathibl There is no timeline as we did not have much asks for it yet. Also there is an easy workaround: you can use one primitive per layer.

Also, do you have examples of models that use multiple bidirectional layers with the first method?

mgouicem avatar Feb 27 '20 18:02 mgouicem

I am closing the issue due to the lack of activity. If you have any other follow-up questions or asks, feel free to reopen this issue or open a new one. Thank you.

mgouicem avatar Mar 06 '20 17:03 mgouicem