feat(nnx): add preferred_element_type to recurrent cells
What does this PR do?
adds preferred_element_type parameter to the recurrent cell classes in flax/nnx/nn/recurrent.py : LSTMCell, OptimizedLSTMCell, SimpleCell, and GRUCell.
split from #5179 for easier review.
changes:
- adds
preferred_element_type: Dtype | None = Noneto__init__of all 4 cell types - forwards
preferred_element_typeto all internalLinearlayer instantiations - adds class-level docstring
Argssections forLSTMCellandSimpleCell - adds
preferred_element_typedoc entry toOptimizedLSTMCellandGRUCellexistingArgs
tests:
- adds
test_preferred_element_typeforLSTMCellandGRUCellinrecurrent_test.py
Checklist
- [ ] This PR fixes a minor issue (e.g.: typo or small bug) or improves the docs (you can dismiss the other checks if that's the case).
- [ ] This change is discussed in a Github issue/discussion (please add a link).
- [x] The documentation and docstrings adhere to the documentation guidelines.
- [x] This change includes necessary high-coverage tests. (No quality testing = no merge!)
Summary of Changes
Hello @aarushisingh04, I'm Gemini Code Assist[^1]! 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 the preferred_element_type parameter to several recurrent neural network cell implementations within Flax NNX, specifically LSTMCell, OptimizedLSTMCell, SimpleCell, and GRUCell. This enhancement provides users with explicit control over the data type of the output from dot product operations, which is crucial for fine-tuning precision and potentially optimizing performance in JAX-based computations.
Highlights
- Feature Addition: Added the
preferred_element_typeparameter to the__init__methods ofLSTMCell,OptimizedLSTMCell,SimpleCell, andGRUCell. - Parameter Propagation: Ensured the
preferred_element_typeis correctly forwarded to all internalLinearlayer instantiations within the affected recurrent cells. - Documentation Updates: Enhanced docstrings for
LSTMCellandSimpleCellwith comprehensiveArgssections, and added thepreferred_element_typedoc entry toOptimizedLSTMCellandGRUCell. - Testing: Introduced new unit tests (
test_preferred_element_type) forLSTMCellandGRUCellto validate the functionality of the new parameter.
🧠 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
- flax/nnx/nn/recurrent.py
- Added
preferred_element_type: Dtype | None = Noneto the__init__method signatures ofLSTMCell,OptimizedLSTMCell,SimpleCell, andGRUCell. - Updated the initialization of internal
Linearlayers within these cells to pass thepreferred_element_type. - Expanded the docstrings for
LSTMCellandSimpleCellto include a comprehensiveArgssection. - Appended documentation for
preferred_element_typeto the existingArgssections ofOptimizedLSTMCellandGRUCell.
- Added
- tests/nnx/nn/recurrent_test.py
- Added
test_preferred_element_typetoTestLSTMCellto verifypreferred_element_typepropagation and effect. - Created
TestGRUCellclass and addedtest_preferred_element_typeto it, confirming similar functionality forGRUCell.
- Added
Activity
- The pull request was intentionally split from a larger PR (#5179) to facilitate an easier and more focused review process.
- The documentation and docstrings have been updated to adhere to established guidelines.
- High-coverage tests have been included to ensure the reliability and correctness of the new feature.
Using Gemini Code Assist
The 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 /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.
| Feature | Command | Description |
|---|---|---|
| Code Review | /gemini review |
Performs a code review for the current pull request in its current state. |
| Pull Request Summary | /gemini summary |
Provides a summary of the current pull request in its current state. |
| Comment | @gemini-code-assist | Responds in comments when explicitly tagged, both in pull request comments and review comments. |
| Help | /gemini help |
Displays a list of available commands. |
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 .gemini/ folder in the base of the repository. Detailed instructions can be found here.
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 :thumbsup: and :thumbsdown: 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.
[^1]: Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.
@aarushisingh04 sorry it's taken so long to get your PRs merged. Would you be able to address the comment from the code assist bot before I approve this one?
@samanklesaria no issues! i have addressed the comment now.