Skip to content

random #415

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

Merged
merged 1 commit into from
Jul 14, 2025
Merged

random #415

merged 1 commit into from
Jul 14, 2025

Conversation

chengtao-lv
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 @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 integrates a new token reduction method, RandomPrune, into the llmc library. This method focuses on randomly pruning image tokens to potentially optimize model performance or reduce computational overhead. The implementation ensures compatibility with different Llava model architectures by correctly identifying and adjusting relevant input tensors after pruning.

Highlights

  • New Token Reduction Strategy: Introduces RandomPrune, a new token reduction module that implements a random pruning strategy specifically for image tokens within a sequence.
  • Dynamic Image Token Pruning: The RandomPrune class calculates a number of image tokens to keep based on a specified rate, then randomly selects these tokens. It meticulously updates the hidden_states, position_ids, attention_mask, cache_position, and position_embeddings to reflect the pruned sequence, ensuring model integrity.
  • Llava Model Integration: Includes specific forward pre-hooks for LlavaHf and Llava models to correctly identify the starting index and length of image tokens, enabling accurate application of the random pruning logic.
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.

@helloyongyang helloyongyang merged commit ee525d4 into main Jul 14, 2025
2 checks passed
@helloyongyang helloyongyang deleted the vlm branch July 14, 2025 14:29
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 pull request introduces a new token reduction method, RandomPrune, which randomly prunes vision tokens for Llava-like models. The implementation uses PyTorch hooks to modify hidden states and related tensors during the forward pass. My review identified a couple of critical correctness issues, including a potential IndexError and a typo in a tensor operation, which could lead to runtime crashes or incorrect model behavior.

Comment on lines +43 to +45
pruning_paras['image_token_start_index'] = torch.where(token_indices)[
0
][0].item()

Choose a reason for hiding this comment

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

critical

This line can raise an IndexError if torch.where(token_indices)[0] returns an empty tensor, which happens when no IMAGE_TOKEN_INDEX is found in the input. This would crash the forward pass. You should add a check to ensure that at least one image token index is found before attempting to access it.

Comment on lines +55 to +58
image_token_idxs = (
input_ids[0] == pruning_paras['vision_token_index']
).nonzero(as_tuple=True)[0]
pruning_paras['image_token_start_index'] = image_token_idxs[0].item()

Choose a reason for hiding this comment

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

critical

This line can raise an IndexError if input_ids[0] == pruning_paras['vision_token_index'] returns an empty tensor, which happens when no vision_token_index is found in the input. This would crash the forward pass. You should add a check to ensure that at least one image token index is found before attempting to access it.

new_pe0 = position_embeddings[0][:, keep_indexs, :].clone()
new_pe1 = position_embeddings[1][:, keep_indexs, :].clone()
position_embeddings[0].resize_as_(new_pe0).copy_(new_pe0)
position_embeddings[1].resize_as_(new_pe0).copy_(new_pe1)

Choose a reason for hiding this comment

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

critical

There appears to be a typo here. position_embeddings[1] is being resized using the shape of new_pe0 instead of new_pe1. While their shapes might be identical in the current implementation, this is semantically incorrect and could lead to hard-to-debug errors if the shapes ever differ.

position_embeddings[1].resize_as_(new_pe1).copy_(new_pe1)

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