Skip to content

Conversation

@zhangbilang
Copy link
Collaborator

No description provided.

Copy link

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

Choose a reason for hiding this comment

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

Summary of Changes

Hello @SmudgedWings, 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 several bugs within the fastervlm module to improve its compatibility and correctness when used with llavahf (Llava Hugging Face) models. The changes focus on ensuring proper handling of model inputs, including image attentions, attention masks, and position IDs, and correcting the registration of a critical forward hook to ensure the model processes inputs as expected.

Highlights

  • Input Handling for LLaVA-HF: Introduced a specific check to ensure image_attentions are correctly populated from image_attentions_list for llavahf models, particularly when processing with a batch size of 1. This ensures the visual attention data is properly available for subsequent operations within the update_attentions_hook.
  • Accurate Attention and Position ID Propagation: Modified the prepare_inputs_for_llm_hook to correctly slice and reuse the original attention_mask and position_ids based on the keep_indexs. Previously, these were generically re-generated, which could lead to incorrect contextual information for the model. This change ensures that the precise attention and positional information is maintained after token reduction.
  • Correct Hook Registration Path: Adjusted the module path for registering the prepare_inputs_for_llm_hook by adding an extra .model attribute. This structural fix ensures the hook is correctly applied to the intended part of the llavahf model's forward pass, resolving an issue with hook placement.
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 in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.

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 issue 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 is currently in preview and 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 to provide feedback.

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.

Copy link

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

Choose a reason for hiding this comment

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

Code Review

This PR fixes a bug for FasterVLM with LlavaHf models. The changes adjust how attention masks and position IDs are handled, and where a forward hook is registered. The review identifies a potential error with attention handling and a maintainability issue with deeply nested attribute access.

Comment on lines +59 to +60
if 'image_attentions' not in pruning_paras:
pruning_paras['image_attentions'] = pruning_paras['image_attentions_list'][0]

Choose a reason for hiding this comment

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

high

This logic introduces a potential IndexError. Accessing pruning_paras['image_attentions_list'][0] will raise an exception if the list is empty, which can happen with text-only inputs where no image attentions are generated. A more robust implementation should handle this issue.

if 'image_attentions' not in pruning_paras and pruning_paras.get('image_attentions_list'):
    pruning_paras['image_attentions'] = pruning_paras['image_attentions_list'][-1]

dtype=kwargs['attention_mask'].dtype, device=device
)
new_position_ids = torch.arange(new_inputs_embeds.shape[1], device=device).unsqueeze(0)
new_attention_mask = kwargs['attention_mask'][:, keep_indexs]

Choose a reason for hiding this comment

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

medium

Consider using the original attention_mask and position_ids tensors with the keep_indexs to maintain the correct attention patterns and positional information after pruning. This ensures that the model attends to the correct tokens and positions after the visual tokens have been reduced.

new_attention_mask = kwargs['attention_mask'][:, keep_indexs]
new_position_ids = kwargs['position_ids'][:, keep_indexs]

functools.partial(
prepare_inputs_for_llm_hook, pruning_paras=self.pruning_paras
),
self.model.model.model.register_forward_pre_hook(

Choose a reason for hiding this comment

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

medium

Consider simplifying the nested attribute access self.model.model.model to improve code readability and maintainability. Accessing nested attributes can be fragile and tightly couples the code to the specific internal structure of the LlavaHf and Hugging Face Llama models.

@zhangbilang zhangbilang merged commit 10c7ac3 into ModelTC:main Jul 2, 2025
2 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.

2 participants