Add cuDNN LSTM for JAX backend#22399
Conversation
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request introduces cuDNN LSTM support for the JAX backend, enhancing performance on GPUs. It includes weight conversion and masking support while maintaining a fallback mechanism for CPU execution. The changes aim to improve the speed of LSTM computations using the cuDNN library when available, with benchmarks showing significant performance gains. Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
|
Warning Gemini is experiencing higher than usual traffic and was unable to create the review. Please try again in a few hours by commenting |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #22399 +/- ##
==========================================
- Coverage 83.00% 82.97% -0.04%
==========================================
Files 596 596
Lines 66586 66737 +151
Branches 10368 10391 +23
==========================================
+ Hits 55272 55372 +100
- Misses 8677 8724 +47
- Partials 2637 2641 +4
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
|
/gemini review |
There was a problem hiding this comment.
Code Review
This pull request adds cuDNN-accelerated LSTM support for the JAX backend, which is a great performance improvement. The implementation correctly handles weight conversion, masking for right-padded sequences, and falling back to the generic implementation when cuDNN is not available or applicable.
I have a couple of suggestions to improve robustness and efficiency:
- In
cudnn_ok, I suggest adding a check forjax.nn.sigmoidfor consistency with thetanhcheck. - I've also identified a small redundant computation in the mask handling logic and suggest a refactoring to improve efficiency.
| from keras.src import activations | ||
| from keras.src import ops | ||
|
|
||
| return ( | ||
| activation in (activations.tanh, jnp.tanh, ops.tanh) | ||
| and recurrent_activation in (activations.sigmoid, ops.sigmoid) # noqa: E501 | ||
| and not unroll | ||
| and use_bias | ||
| and _is_gpu_available() | ||
| ) |
There was a problem hiding this comment.
For consistency with the tanh activation check and to make the cudnn_ok check more robust, you should also check for jax.nn.sigmoid. The tanh check includes jnp.tanh, which is the base JAX function. The equivalent for sigmoid is jax.nn.sigmoid. This will ensure that if a user passes the base JAX function directly, cudnn_ok will correctly identify it.
| from keras.src import activations | |
| from keras.src import ops | |
| return ( | |
| activation in (activations.tanh, jnp.tanh, ops.tanh) | |
| and recurrent_activation in (activations.sigmoid, ops.sigmoid) # noqa: E501 | |
| and not unroll | |
| and use_bias | |
| and _is_gpu_available() | |
| ) | |
| from keras.src import activations | |
| from keras.src import ops | |
| from jax import nn | |
| return ( | |
| activation in (activations.tanh, jnp.tanh, ops.tanh) | |
| and recurrent_activation | |
| in (activations.sigmoid, ops.sigmoid, nn.sigmoid) | |
| and not unroll | |
| and use_bias | |
| and _is_gpu_available() | |
| ) |
| seq_lengths = jnp.full((batch_size,), inputs.shape[1], dtype=jnp.int32) | ||
|
|
There was a problem hiding this comment.
There is a redundant computation here. jnp.sum(mask.astype(jnp.int32), axis=1) is calculated here to get seq_lengths, but it's also calculated inside _assert_valid_mask as count_of_true.
To improve efficiency, you can modify _assert_valid_mask to return count_of_true and use that value here.
- In
_assert_valid_mask, addreturn count_of_trueat the end. - Then, you can replace these two lines with just
seq_lengths = _assert_valid_mask(mask).
| seq_lengths = jnp.full((batch_size,), inputs.shape[1], dtype=jnp.int32) | |
| seq_lengths = _assert_valid_mask(mask) |
|
I added test for this! hope it helps |
Summary
The JAX backend's lstm() was a NotImplementedError stub, so it always fell back to the generic lax.scan loop. This wires it up to jax.experimental.rnn.lstm, which goes through cuDNN directly on GPU.
The weight conversion is straightforward, Keras and cuDNN use the same gate order [i, f, g, o], so the kernels just need to be transposed and flattened. Masking works by computing seq_lengths from the boolean mask, with the same right-padded constraint the torch backend has.
Falls back to lax.scan on CPU or when cuDNN isn't available, so nothing changes for non-GPU users.
Benchmarks from the JAX repo put the cuDNN path at around 5x faster than lax.scan for single-layer LSTMs.