keras-team / keras

Deep Learning for humans
http://keras.io/
Apache License 2.0
61.6k stars 19.41k forks source link

[Feature Request] Add cuDNN-accelerated LSTM and GRU to PyTorch #19177

Open foxik opened 7 months ago

foxik commented 7 months ago

Hi,

are there any plans to add cuDNN-accelerated versions of LSTM and GRU to the PyTorch backend? Without cuDNN acceleration, the LSTM and GRU are considerably (several times) slower, even when running on GPU; however, we still use RNNs heavily (for example, adding them after Transformer encoder still helps in some cases).

The torch.nn.LSTM/torch.nn.GRU offer cuDNN acceleration, and wrapping them to a keras.layers.Layer works, but the resulting model is not backend-agnostic (so the resulting model cannot be used cross-frameworks).

Thanks for consideration :pray: and cheers!

PS: Relatedly, torch.nn.LSTM/GRU offers bidirectional computation by a single call (by passing bidirectional=True) -- I am not sure how much faster it is compared to two asynchronous unidirectional computations, but if it is faster, keras.layers.Bidirectional would probably have to be updated to handle keras.layers.LSTM and keras.layers.GRU specifically to support it.

haifeng-jin commented 7 months ago

@foxik Thanks for the issue! Would you like to contribute this by modifying the following file? https://github.com/keras-team/keras/blob/master/keras/backend/torch/rnn.py#L377C1-L382C30

foxik commented 7 months ago

@haifeng-jin I am not sure I can do it correctly. I assume that

In any case, for the time being I unfortunately do not have time to work on this.

park commented 1 month ago

This feature would be great indeed. Hopefully someone high capable will attend to this sometime soon.