-
Notifications
You must be signed in to change notification settings - Fork 66
update fastervlm #398
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
update fastervlm #398
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -62,41 +62,36 @@ def pruning_hook(module, args, kwargs, pruning_paras): | |
| image_features = args[0] | ||
| image_attentions = pruning_paras['image_attentions'] | ||
|
|
||
| # image_attentions = image_attentions.max(dim=1)[0] # (B, N) = (1, 576) | ||
| image_attentions = image_attentions.mean(dim=1) # (B, N) = (1, 576) | ||
|
|
||
| B, N = image_features.shape[:2] | ||
| B, N, C = image_features.shape | ||
| visual_token_num = self.visual_token_num # T | ||
|
|
||
| # prune visual tokens by random scores | ||
| # token_weights = torch.rand(B, N, device=image_features.device) # (B, N) | ||
| # token_indices = torch.topk(token_weights, k=visual_token_num, dim=1)[1] # (B, T) | ||
| # token_indices = torch.sort(token_indices, dim=1)[0] # (B, T) | ||
|
|
||
| # prune visual tokens by attention scores | ||
| image_attentions = image_attentions.mean(dim=1) # (B, N) | ||
| token_indices = torch.topk(image_attentions, k=visual_token_num, dim=1)[1] # (B, T) | ||
| token_indices = torch.sort(token_indices, dim=1)[0] # (B, T) | ||
|
|
||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The removal of |
||
| # generate index mask | ||
| index_mask = torch.zeros(B, N, dtype=torch.bool, device=image_features.device) # (B, N) | ||
| index_mask.scatter_(1, token_indices, True) # (B, N) | ||
| index_masks = torch.zeros( | ||
| B, N, | ||
| dtype=torch.bool, | ||
| device=image_features.device | ||
| ) # (B, N) | ||
| index_masks.scatter_(1, token_indices, True) # (B, N) | ||
|
|
||
| pruning_paras['index_mask'] = index_mask | ||
| pruning_paras['image_attentions'] = image_attentions | ||
| pruning_paras['index_masks'] = index_masks | ||
|
|
||
|
Comment on lines
+80
to
81
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The processed |
||
| return (image_features,), kwargs | ||
|
|
||
| def get_image_mask_hook(module, args, kwargs, pruning_paras): | ||
| pruning_paras['image_mask'] = ( | ||
| pruning_paras['image_masks'] = ( | ||
| kwargs['input_ids'] == pruning_paras['image_token_index'] | ||
| ) # (B, len) | ||
|
|
||
| def prepare_inputs_for_llm_hook(module, args, kwargs, pruning_paras): | ||
|
|
||
| # Only batch size 1 is currently supported. | ||
| inputs_embeds = kwargs['inputs_embeds'] | ||
| image_mask = pruning_paras['image_mask'][0] | ||
| index_mask = pruning_paras['index_mask'][0] | ||
| image_mask = pruning_paras['image_masks'][0] | ||
| index_mask = pruning_paras['index_masks'][0] | ||
|
|
||
| B, L = inputs_embeds.shape[:2] | ||
| device = inputs_embeds.device | ||
|
|
@@ -123,7 +118,7 @@ def prepare_inputs_for_llm_hook(module, args, kwargs, pruning_paras): | |
| def prepare_inputs_hook(module, inputs, outputs, pruning_paras): | ||
|
|
||
| image_features = outputs | ||
| index_masks = pruning_paras['index_mask'] | ||
| index_masks = pruning_paras['index_masks'] | ||
| # image_attentions = pruning_paras['image_attentions'] | ||
| new_image_features = [] | ||
| for image_feature, index_mask in zip(image_features, index_masks): | ||
|
|
||
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.
Changing
image_features.shape[:2]toimage_features.shapeimplies thatimage_featuresis now strictly expected to be a 3-dimensional tensor. Ifimage_featurescould sometimes be 2-dimensional, this change would lead to aValueErrorduring unpacking. Please confirm thatimage_featureswill always have 3 dimensions (B, N, C) at this point in the execution.