Skip to content

Fix ESM attention for TFLite compatibility - #2466

Merged
sachinprasadhs merged 2 commits into
keras-team:masterfrom
pctablet505:fix-esm-attention-tflite
Dec 8, 2025
Merged

sachinprasadhs merged 2 commits into
keras-team:masterfrom
pctablet505:fix-esm-attention-tflite

Conversation

@pctablet505

Copy link
Copy Markdown
Contributor

This pull request improves compatibility with TensorFlow Lite (TFLite) by making shape computations dynamic in the rotary positional embedding logic of esm_attention.py. The changes ensure that the code works with tensors whose shapes may not be statically known at graph build time.

Dynamic shape compatibility:

  • Updated the computation of the sequence length in _compute_cos_sin_embedding to use ops.shape(x)[position] instead of x.shape[position], enabling support for dynamic shapes required by TFLite.
  • Modified the slicing in apply_rotary_pos_emb to use ops.shape(x)[1] for the sequence length, ensuring correct behavior when input shapes are not statically defined.## Description of the change

@gemini-code-assist

Copy link
Copy Markdown
Contributor

Summary of Changes

Hello @pctablet505, 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 enhances the ESM attention mechanism's compatibility with TensorFlow Lite by introducing dynamic shape computations within its rotary positional embedding logic. The changes ensure that the model can correctly process tensors whose dimensions are not fixed at graph compilation time, thereby improving its adaptability for deployment in TFLite environments.

Highlights

  • Dynamic Shape Compatibility in _compute_cos_sin_embedding: The method now uses "ops.shape(x)[position]" instead of "x.shape[position]" to determine sequence length, enabling support for dynamic tensor shapes crucial for TFLite.
  • Dynamic Shape Compatibility in apply_rotary_pos_emb: Slicing logic has been updated to use "ops.shape(x)[1]" for sequence length, ensuring correct behavior when input shapes are not statically defined, which is necessary for TFLite.
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 👍 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

  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. ↩

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request improves compatibility with TensorFlow Lite by making shape computations dynamic in esm_attention.py. The changes correctly use ops.shape() instead of .shape to support tensors with shapes not known at graph-building time. The modifications in _compute_cos_sin_embedding and apply_rotary_pos_emb are accurate and address the stated goal. While the changes in this PR are correct, I noticed a related area for improvement for full dynamic shape compatibility. In _compute_cos_sin_embedding on line 13, dim = x.shape[-1] is used. To make the model fully robust for TFLite conversion, this should be changed to dim = ops.shape(x)[-1]. Since this is outside the current diff, it could be addressed in a follow-up PR to complete the dynamic shape compatibility work for this component.

@pctablet505

Copy link
Copy Markdown
Contributor Author

@gemini-code-assist review

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request correctly updates esm_attention.py to use ops.shape for dynamic shape compatibility, which is essential for TFLite. The changes are well-implemented. I've added one comment regarding a potential backend compatibility issue with JAX that was observed in the context of the changes. Addressing this would ensure the code remains fully backend-agnostic as per the project's guidelines.

@sachinprasadhs sachinprasadhs added the kokoro:force-run Runs Tests on GPU label Dec 8, 2025
@kokoro-team kokoro-team removed the kokoro:force-run Runs Tests on GPU label Dec 8, 2025
@sachinprasadhs
sachinprasadhs merged commit cd82a95 into keras-team:master Dec 8, 2025
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants