Shard input data for generate() for causal_lm model#2597
Shard input data for generate() for causal_lm model#2597dvadym wants to merge 1 commit intokeras-team:masterfrom
Conversation
Summary of ChangesHello @dvadym, 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 optimizes the inference process for causal language models, particularly when using the Jax backend. By implementing input data sharding, it resolves an inefficiency where input embeddings and subsequent computations were unnecessarily replicated across multiple accelerators. This change ensures that data is properly distributed, leading to more efficient memory usage and computation during inference. 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
|
There was a problem hiding this comment.
Code Review
This pull request introduces input sharding for the generate() method in CausalLM, which is an important fix for distributed inference, especially with the JAX backend. The implementation of the shard function is clean and correctly handles both distributed and non-distributed scenarios. However, as you've noted in the description, the change lacks unit tests. According to the repository's contribution guidelines, testing is a mandatory requirement. I've added a specific comment with a suggestion on how to add a unit test for this sharding logic by using mocking, which would validate the new code path without requiring a full distributed test environment. Please add tests to ensure the correctness and prevent future regressions.
| if self.preprocessor is not None: | ||
| inputs = [preprocess(x) for x in inputs] | ||
|
|
||
| inputs = [shard(x) for x in inputs] |
There was a problem hiding this comment.
This change introduces important logic for distributed execution, but it lacks corresponding unit tests. The contribution guidelines are very clear on the requirement for testing for all new logic.
Since testing distributed code can be complex, you can add a unit test that verifies the sharding logic is correctly invoked by using mocks. Here's a possible approach:
- In your test case, use
unittest.mock.patchto mockkeras.src.distribution.distribution_lib.distribution. - Make the mock return a mock
Distributionobject. - This mock distribution object should have mock methods for
get_data_layoutanddistribute_tensor. - Call the
generate()method on aCausalLMinstance. - Assert that
distribution_lib.distribution()was called, and thatget_data_layoutanddistribute_tensorwere called on your mock object for the input tensors.
This will ensure that your sharding code path is exercised and correctly integrated, even without a real distributed environment.
Here is a conceptual example of what the test could look like:
from unittest import mock
# Inside a test method for CausalLM
mock_dist_layout = mock.Mock()
mock_dist = mock.Mock()
mock_dist.get_data_layout.return_value = mock_dist_layout
mock_dist.distribute_tensor.return_value = "sharded_tensor"
with mock.patch(
"keras_hub.src.models.causal_lm.distribution_lib.distribution",
return_value=mock_dist,
) as mock_distribution_fn:
causal_lm = CausalLM.from_preset(...)
causal_lm.generate("test prompt")
mock_distribution_fn.assert_called()
mock_dist.get_data_layout.assert_called()
mock_dist.distribute_tensor.assert_called_with(
mock.ANY, mock_dist_layout
)Please add tests to validate this new functionality.
References
- The contribution guidelines state that testing is a non-negotiable part of every contribution (line 403). Every file containing logic must have a corresponding test file to ensure all core functionality is covered (line 406). (link)
sachinprasadhs
left a comment
There was a problem hiding this comment.
Thanks for the PR, for the unit test, you can follow something similar to what Keras has for mock setup https://github.com/keras-team/keras/blob/master/keras/src/distribution/distribution_lib_test.py
If you have tested in distributed settings, may be you can attach the screenshots of the findings for any model like Gemma3.
Can you also test with TensorFlow backend
Description of the change
This change introduces sharding of
inputsfor causal_lm.py model for interence.The current behavior with Jax backend, DataDistribution (I only tested with Jax backend and Gemma3): there is no sharding of
inputs, as a result when embeddings computed (code) `embeddings' replicated over multiple accelerators.And further all computations are replicated on all accelerators.
This change ensures that
inputsis sharded and all computations are sharded.I haven't have unit tests to cover this code. Please advice.
Checklist