-
Notifications
You must be signed in to change notification settings - Fork 66
fix fastv bugs #400
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix fastv bugs #400
Conversation
There was a problem hiding this 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 @chengtao-lv, 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 in the fastv pruning implementation. It includes fixes for multimodal input handling, attention mechanisms, sequence pruning, and hook management. Additionally, it simplifies the model loading process for Llava models.
Highlights
- Multimodal Input Handling: Modified
hook_prepare_inputs_labels_for_multimodalto acceptmodalitiesand setimage_sizesto None by default, providing more flexibility in handling different types of multimodal inputs. - Attention Mechanism: The
update_output_attentions_hooknow explicitly setsoutput_attentionsto False after storing attention scores, preventing unintended side effects. - Sequence Pruning: The
fastv_pruning_hookhas been updated to correctly handle attention masks, cache positions, position IDs, and position embeddings after pruning, ensuring consistency in sequence length. - Hook Management: Removed the
read_parameter_hookand the loop that registered it for blocks after the pruning location, simplifying the hook management process. - Llava Model Loading: Refactored the
build_modelfunction inllava.pyto streamline the model loading process, usingdevice_map='cpu'andattn_implementation='sdpa'.
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
-
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. ↩
There was a problem hiding this comment.
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 aims to fix bugs in the FastV token reduction implementation. Key changes include refactoring how model parameters like attention masks and position embeddings are updated during pruning, switching to a more efficient in-place modification strategy. It also simplifies the LLaVA model loading by removing several monkey-patches, likely due to updates in the underlying library. However, a critical performance issue has been introduced in fastv.py where a pre-hook incorrectly causes a module's forward pass to be executed twice.
| def update_output_attentions_hook(module, args, kwargs, pruning_paras): | ||
| kwargs['output_attentions'] = True | ||
| pruning_paras['attn_scores'] = module.__class__.forward(module, *args, **kwargs)[1] | ||
| kwargs['output_attentions'] = False | ||
| return args, kwargs |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The update_output_attentions_hook is registered as a pre_hook, but it manually executes the module's forward pass via module.__class__.forward. This will cause the forward pass for this layer to be executed twice: once inside this hook, and a second time by the PyTorch framework after the hook returns. This is a major performance bottleneck.
The correct way to get attention scores is to use a forward_hook which runs after the forward method and receives its output. The deleted store_attention_hook was the right pattern.
If the goal was to get attention scores without affecting the output signature of the layer (which might break subsequent layers), the forward_hook should be modified to strip the attention scores from the output tuple before returning.
I strongly recommend reverting to the pre-hook/forward-hook pattern to fix this critical performance issue.
|
|
||
| self.tokenizer, self.vlm_model, image_processor, context_len = load_pretrained_model( | ||
| self.model_path, | ||
| None, | ||
| get_model_name_from_path(self.model_path), | ||
| load_8bit=False, | ||
| load_4bit=False, | ||
| device='cpu', | ||
| torch_dtype=self.torch_dtype, | ||
| config=self.llava_config, | ||
| ) | ||
|
|
||
| # llava forward not support "cache_position" | ||
| ori_forward = self.vlm_model.forward | ||
|
|
||
| def safe_forward(*args, **kwargs): | ||
| kwargs.pop('cache_position', None) | ||
| return ori_forward(*args, **kwargs) | ||
| self.vlm_model.forward = safe_forward | ||
|
|
||
| # llava generate use "inputs" instead of "input_ids" | ||
| ori_generate = self.vlm_model.generate | ||
|
|
||
| def safe_generate(*args, **kwargs): | ||
| if 'input_ids' in kwargs: | ||
| kwargs['inputs'] = kwargs.pop('input_ids') | ||
| return ori_generate(*args, **kwargs) | ||
| self.vlm_model.generate = safe_generate | ||
|
|
||
| # "attention_mask" is passed via kwargs rather than as an explicit keyword argument. | ||
| ori_prepare_inputs_for_generation = self.vlm_model.prepare_inputs_for_generation | ||
|
|
||
| def safe_prepare_inputs_for_generation( | ||
| self, input_ids, past_key_values=None, | ||
| inputs_embeds=None, attention_mask=None, **kwargs): | ||
| if attention_mask is not None: | ||
| kwargs['attention_mask'] = attention_mask | ||
| return ori_prepare_inputs_for_generation( | ||
| input_ids, past_key_values, inputs_embeds, **kwargs) | ||
| self.vlm_model.prepare_inputs_for_generation = types.MethodType( | ||
| safe_prepare_inputs_for_generation, self.vlm_model | ||
| device_map='cpu', | ||
| attn_implementation='sdpa' | ||
| ) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The removal of monkey-patching for cache_position, input_ids, and attention_mask suggests significant updates in the LLaVA library. Verify that the new attn_implementation='sdpa' and device_map='cpu' configurations are thoroughly tested across different hardware setups to ensure consistent performance and compatibility.
device_map='cpu',
attn_implementation='sdpa'
No description provided.