Fix Moonshine LiteRT export: Handle boolean masks in test runner#2555
Fix Moonshine LiteRT export: Handle boolean masks in test runner#2555MAUK9086 wants to merge 2 commits intokeras-team:masterfrom
Conversation
Summary of ChangesHello @MAUK9086, 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 addresses a critical bug in the LiteRT export testing framework where boolean input types were being incorrectly converted to integers, leading to test failures for models that strictly rely on boolean masks. By ensuring that boolean types are preserved during conversion for TFLite and by refining the test data preparation for models like Moonshine, this change enables accurate and robust testing of such models within the LiteRT environment. 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. 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 correctly fixes a bug in the LiteRT export test utility that was improperly casting boolean inputs to integers. The changes in test_case.py to preserve boolean dtypes are a good general fix, and the updates to the Moonshine test enable it to pass with the corrected infrastructure. I have one suggestion to improve the conciseness of the test data preparation logic.
| input_data = {} | ||
| for k, v in self.input_data.items(): | ||
| input_data[k] = ops.convert_to_numpy(v) | ||
|
|
||
| # 2. Force masks to boolean | ||
| if "encoder_padding_mask" in input_data: | ||
| input_data["encoder_padding_mask"] = np.array( | ||
| input_data["encoder_padding_mask"], dtype=bool | ||
| ) | ||
|
|
||
| if "decoder_padding_mask" in input_data: | ||
| input_data["decoder_padding_mask"] = np.array( | ||
| input_data["decoder_padding_mask"], dtype=bool | ||
| ) |
There was a problem hiding this comment.
This data preparation logic can be made more concise and less repetitive by using a dictionary comprehension for the initial conversion and a loop to handle the type casting for all relevant mask keys. This improves readability and maintainability.
| input_data = {} | |
| for k, v in self.input_data.items(): | |
| input_data[k] = ops.convert_to_numpy(v) | |
| # 2. Force masks to boolean | |
| if "encoder_padding_mask" in input_data: | |
| input_data["encoder_padding_mask"] = np.array( | |
| input_data["encoder_padding_mask"], dtype=bool | |
| ) | |
| if "decoder_padding_mask" in input_data: | |
| input_data["decoder_padding_mask"] = np.array( | |
| input_data["decoder_padding_mask"], dtype=bool | |
| ) | |
| input_data = {k: ops.convert_to_numpy(v) for k, v in self.input_data.items()} | |
| # 2. Force masks to boolean | |
| for mask_key in ("encoder_padding_mask", "decoder_padding_mask"): | |
| if mask_key in input_data: | |
| input_data[mask_key] = input_data[mask_key].astype(bool) |
d40b970 to
71ff520
Compare
Description of the change
This PR fixes a bug in the LiteRT export testing infrastructure that prevented models with boolean inputs (like Moonshine) from being tested correctly.
The Issue:
The
run_litert_export_testutility intest_case.pywas incorrectly casting all boolean inputs toint32before passing them to the TFLite interpreter. This caused a type mismatch error (ValueError: Cannot set tensor: Got value of type INT32 but expected type BOOL) for models that strictly require boolean masks.The Fix:
test_case.py: Modifiedconvert_for_tfliteto preservebooldata types for both NumPy arrays and TensorFlow tensors, instead of forcing a cast toint32.@pytest.mark.skipfromtest_litert_exportinmoonshine_audio_to_text_test.py.keras.ops.convert_to_numpyand explicitly ensure masks are cast tobool, resolving the "mixing tensors and non-tensors" validation error.Reference
Fixes #2493
Colab Notebook
N/A (Bug fix for existing test infrastructure)
Checklist