-
Notifications
You must be signed in to change notification settings - Fork 383
code to patch for qwen3.5 grpo training #10957
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
Draft
leah1985
wants to merge
1
commit into
hiyouga:main
Choose a base branch
from
leah1985:leah/qwen35-grpo-exp
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,146 @@ | ||
| # Copyright 2024 The Qwen team, Alibaba Group and the HuggingFace Inc. team | ||
| # Copyright 2024 Bytedance Ltd. and/or its affiliates | ||
| # Adapted from: | ||
| # https://github.com/huggingface/transformers/blob/v5.4.0/src/transformers/models/qwen3_5/modeling_qwen3_5.py | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| """ | ||
| Standalone position ID computation for Qwen3.5-VL. | ||
|
|
||
| Qwen3.5 uses mRoPE like Qwen2/3-VL but with key differences: | ||
| - Uses mm_token_type_ids (0=text, 1=image, 2=video) instead of scanning for special tokens | ||
| - Position advances by max(H, W) / spatial_merge_size after each vision block (not T*H*W) | ||
| - get_vision_position_ids computes spatial positions with start_position offset | ||
| """ | ||
|
|
||
| import itertools | ||
| from typing import Optional | ||
|
|
||
| import torch | ||
| from transformers import ProcessorMixin | ||
|
|
||
|
|
||
| def get_vision_position_ids( | ||
| start_position: int, | ||
| grid_thw: torch.Tensor, | ||
| spatial_merge_size: int = 1, | ||
| device: Optional[torch.device] = None, | ||
| ) -> torch.Tensor: | ||
| """Compute 3D positional indices for vision tokens from a single image or video. | ||
|
|
||
| Args: | ||
| start_position: Offset added to all computed positional indices. | ||
| grid_thw: Tensor of shape (3,) — (T, H, W) grid of the vision feature. | ||
| spatial_merge_size: Factor by which H and W are reduced in the backbone. | ||
| device: Device for the output tensor. | ||
|
|
||
| Returns: | ||
| torch.LongTensor of shape (3, sequence_length): [temporal, height, width] positions. | ||
| """ | ||
| llm_grid_t = grid_thw[0].item() | ||
| llm_grid_h = grid_thw[1].item() // spatial_merge_size | ||
| llm_grid_w = grid_thw[2].item() // spatial_merge_size | ||
|
|
||
| image_seq_length = llm_grid_h * llm_grid_w * llm_grid_t | ||
| position_width = torch.arange(start_position, start_position + llm_grid_w, device=device).repeat( | ||
| llm_grid_h * llm_grid_t | ||
| ) | ||
| position_height = torch.arange(start_position, start_position + llm_grid_h, device=device).repeat_interleave( | ||
| llm_grid_w * llm_grid_t | ||
| ) | ||
| position_temporal = torch.full((image_seq_length,), start_position, device=device, dtype=torch.long) | ||
|
|
||
| return torch.stack([position_temporal, position_height, position_width], dim=0) | ||
|
|
||
|
|
||
| def get_rope_index( | ||
| processor: "ProcessorMixin", | ||
| input_ids: torch.Tensor, | ||
| mm_token_type_ids: torch.Tensor, | ||
| image_grid_thw: Optional[torch.Tensor] = None, | ||
| video_grid_thw: Optional[torch.Tensor] = None, | ||
| attention_mask: Optional[torch.Tensor] = None, | ||
| **kwargs, | ||
| ) -> torch.Tensor: | ||
| """Compute mRoPE position IDs for Qwen3.5, adapted from Qwen3_5Model.get_rope_index. | ||
|
|
||
| This is a standalone (non-method) version that works on single (unbatched) samples, | ||
| matching the interface used in EasyR1's dataset.py. | ||
|
|
||
| Args: | ||
| processor: The Qwen3.5 processor (used to get spatial_merge_size). | ||
| input_ids: 1D tensor of token IDs (seq_length,). | ||
| mm_token_type_ids: 1D tensor (seq_length,) — 0=text, 1=image, 2=video. | ||
| image_grid_thw: Tensor of shape (num_images, 3) or None. | ||
| video_grid_thw: Tensor of shape (num_videos, 3) or None. | ||
| attention_mask: 1D tensor (seq_length,) or None. | ||
|
|
||
| Returns: | ||
| torch.Tensor of shape (3, seq_length): mRoPE position IDs [temporal, height, width]. | ||
| """ | ||
| # Qwen3.5 splits video_grid_thw by temporal dimension (timestamps separate frames) | ||
| if video_grid_thw is not None: | ||
| video_grid_thw = torch.repeat_interleave(video_grid_thw, video_grid_thw[:, 0], dim=0) | ||
| video_grid_thw[:, 0] = 1 | ||
|
|
||
| spatial_merge_size = processor.image_processor.merge_size | ||
|
|
||
| position_ids = torch.zeros(3, len(input_ids), dtype=input_ids.dtype, device=input_ids.device) | ||
|
|
||
| if attention_mask is not None: | ||
| mask = attention_mask.bool() | ||
| input_ids_masked = input_ids[mask] | ||
| mm_types_masked = mm_token_type_ids[mask] | ||
| else: | ||
| input_ids_masked = input_ids | ||
| mm_types_masked = mm_token_type_ids | ||
|
|
||
| grid_iters = { | ||
| 1: iter(image_grid_thw) if image_grid_thw is not None else None, | ||
| 2: iter(video_grid_thw) if video_grid_thw is not None else None, | ||
| } | ||
|
|
||
| # Group consecutive tokens by modality type | ||
| input_type_group = [] | ||
| for key, group in itertools.groupby(enumerate(mm_types_masked.tolist()), lambda x: x[1]): | ||
| group = list(group) | ||
| start_index = group[0][0] | ||
| end_index = group[-1][0] + 1 | ||
| input_type_group.append((key, start_index, end_index)) | ||
|
|
||
| current_pos = 0 | ||
| llm_pos_ids_list = [] | ||
| for modality_type, start_idx, end_idx in input_type_group: | ||
| if modality_type == 0: # text | ||
| text_len = end_idx - start_idx | ||
| llm_pos_ids_list.append( | ||
| torch.arange(text_len, device=input_ids.device).view(1, -1).expand(3, -1) + current_pos | ||
| ) | ||
| current_pos += text_len | ||
| else: # image (1) or video (2) | ||
| grid_thw = next(grid_iters[modality_type]) | ||
| vision_pos = get_vision_position_ids( | ||
| current_pos, grid_thw, spatial_merge_size, device=input_ids.device | ||
| ) | ||
| llm_pos_ids_list.append(vision_pos) | ||
| # Qwen3.5 advances position by max(H, W) / spatial_merge_size (NOT T*H*W) | ||
| current_pos += max(grid_thw[1].item(), grid_thw[2].item()) // spatial_merge_size | ||
|
|
||
| llm_positions = torch.cat(llm_pos_ids_list, dim=1).reshape(3, -1) | ||
| if attention_mask is not None: | ||
| position_ids[..., attention_mask.bool()] = llm_positions.to(position_ids.device) | ||
| else: | ||
| position_ids = llm_positions.to(position_ids.device) | ||
|
|
||
| return position_ids | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 code assumes that
grid_iters[modality_type]is an iterator. However, ifimage_grid_thworvideo_grid_thwisNone, the corresponding entry ingrid_itersis initialized toNone(see lines 110-111). Callingnext(None)will raise aTypeError. You should check if the iterator exists before callingnextor ensure that the input tensors are provided if the modality is present inmm_token_type_ids.