-
-
Notifications
You must be signed in to change notification settings - Fork 20.4k
[BugFix] Fix 3D rope in transformers backend #35097
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
Changes from 4 commits
dce95b8
e75a62b
3836c06
c852324
cc0d738
c6f5eae
4d1efbf
e7596be
108074e
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 |
|---|---|---|
|
|
@@ -990,7 +990,7 @@ def _get_video_second_idx_glm4v( | |
| uniq.append(uniq[-1]) | ||
| frame_indices = uniq | ||
|
|
||
| full_second_idxs = [int(idx / video_fps) for idx in frame_indices] | ||
| full_second_idxs = [idx / video_fps for idx in frame_indices] | ||
|
Member
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. Is this change also necessary for GLM4.1V? I remember GLM4.1V use int for timestamp while GLM4.6V is float with decimal seconds:
Contributor
Author
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. in transformers we use the same timestamps format for all GLM models, so I am relying on it. Do you want to check-in with GLM authors, I can ask in slack?
Member
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. |
||
| timestamps_list = full_second_idxs[::2] | ||
| selected_timestamps = [] | ||
| for idx in range(0, len(timestamps_list)): | ||
|
|
@@ -1067,7 +1067,7 @@ def _get_video_second_idx_glm46v( | |
| uniq.append(uniq[-1]) | ||
|
|
||
| frame_indices = uniq | ||
| full_second_idxs = [int(idx / video_fps) for idx in frame_indices] | ||
| full_second_idxs = [idx / video_fps for idx in frame_indices] | ||
| timestamps_list = full_second_idxs[::2] | ||
| selected_timestamps = [] | ||
| for idx in range(len(timestamps_list)): | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -213,7 +213,7 @@ def apply( | |
| if "mm_token_type_ids" in processed_data | ||
| else "token_type_ids" | ||
| ) | ||
| mm_token_type_ids = processed_data.pop(token_type_key) | ||
| mm_token_type_ids = processed_data.get(token_type_key) | ||
|
|
||
| # We can infer vLLM style placeholder from token type ids, if we split | ||
| # it for each input `mm_data`. | ||
|
|
@@ -351,6 +351,7 @@ def embed_multimodal(self, **kwargs): | |
|
|
||
| num_image_patches = kwargs.pop("num_image_patches") | ||
| kwargs.pop("token_type_ids", None) # used only in `forward` | ||
| kwargs.pop("mm_token_type_ids", None) # used only in `model.get_rope_index` | ||
|
|
||
| if pixel_values is not None: | ||
| # ROCm: Force math SDP backend for vision encoder to avoid accuracy issues | ||
|
|
@@ -441,6 +442,7 @@ def get_mrope_input_positions( | |
| { | ||
| "image_grid_thw", | ||
| "video_grid_thw", | ||
| "mm_token_type_ids", | ||
| "second_per_grid_ts", | ||
| "audio_feature_lengths", | ||
| "use_audio_in_video", | ||
|
|
@@ -449,14 +451,15 @@ def get_mrope_input_positions( | |
| if any( | ||
| v | ||
| for k, v in kwargs.items() | ||
| if k not in {"image_grid_thw", "video_grid_thw"} | ||
| if k not in {"image_grid_thw", "mm_token_type_ids"} | ||
| ): | ||
| raise NotImplementedError( | ||
| "Transformers modeling backend only supports images." | ||
| ) | ||
|
|
||
| image_grid_thw = kwargs.get("image_grid_thw", []) | ||
| video_grid_thw = kwargs.get("video_grid_thw", []) | ||
| mm_token_type_ids = kwargs.get("mm_token_type_ids") | ||
|
|
||
| image_grid_thw = (torch.stack if image_grid_thw else torch.tensor)( | ||
| image_grid_thw | ||
|
|
@@ -465,10 +468,16 @@ def get_mrope_input_positions( | |
| video_grid_thw | ||
| ) | ||
|
|
||
| # In v4 this utility didn't accept any `kwargs`, thus we filter | ||
|
Member
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. I don't understand this comment. Will |
||
| kwargs = {} | ||
| if mm_token_type_ids: | ||
| kwargs["mm_token_type_ids"] = torch.cat(mm_token_type_ids) | ||
|
|
||
| mrope_positions, mrope_position_delta = self.model.get_rope_index( | ||
| input_ids=torch.tensor(input_tokens).unsqueeze(0), | ||
| image_grid_thw=image_grid_thw, | ||
| video_grid_thw=video_grid_thw, | ||
| **kwargs, | ||
| ) | ||
|
|
||
| mrope_positions = mrope_positions[:, 0] | ||
|
|
||
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.
cc @Isotr0py for this change