Skip to content

Commit 4437f63

Browse files
ShareLerliu-zichen
authored andcommitted
(fix): Fix the position_ids issue for qwen_vl when uses megatron
1 parent 7393296 commit 4437f63

2 files changed

Lines changed: 23 additions & 1 deletion

File tree

mcore_adapter/src/mcore_adapter/models/qwen2_5_vl/modeling_qwen2_5_vl.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,19 @@ def forward(
324324
**kwargs,
325325
) -> "torch.Tensor":
326326
force_vit_image = kwargs.pop("force_vit_image", False)
327-
force_vit_video = kwargs.pop("force_vit_video", False)
327+
force_vit_video = kwargs.pop("force_vit_video", False)
328+
329+
if position_ids is not None:
330+
expected_shape = (3, input_ids.shape[0], input_ids.shape[1]) # (3, batch, seq_len)
331+
if position_ids.shape != expected_shape:
332+
if position_ids.shape == (input_ids.shape[0], input_ids.shape[1]):
333+
position_ids, _ = self.get_rope_index(
334+
input_ids, image_grid_thw, video_grid_thw, second_per_grid_ts, attention_mask
335+
)
336+
else:
337+
raise ValueError(f"Unexpected position_ids shape: {position_ids.shape}, "
338+
f"expected: {expected_shape} or {(input_ids.shape[0], input_ids.shape[1])}")
339+
328340
if position_ids is None and input_ids is not None:
329341
position_ids, _ = self.get_rope_index(
330342
input_ids, image_grid_thw, video_grid_thw, second_per_grid_ts, attention_mask

mcore_adapter/src/mcore_adapter/models/qwen2_vl/modeling_qwen2_vl.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,16 @@ def forward(
303303
) -> "torch.Tensor":
304304
force_vit_image = kwargs.pop("force_vit_image", False)
305305
force_vit_video = kwargs.pop("force_vit_video", False)
306+
307+
if position_ids is not None:
308+
expected_shape = (3, input_ids.shape[0], input_ids.shape[1]) # (3, batch, seq_len)
309+
if position_ids.shape != expected_shape:
310+
if position_ids.shape == (input_ids.shape[0], input_ids.shape[1]):
311+
position_ids, _ = self.get_rope_index(input_ids, image_grid_thw, video_grid_thw)
312+
else:
313+
raise ValueError(f"Unexpected position_ids shape: {position_ids.shape}, "
314+
f"expected: {expected_shape} or {(input_ids.shape[0], input_ids.shape[1])}")
315+
306316
if position_ids is None and input_ids is not None:
307317
position_ids, _ = self.get_rope_index(input_ids, image_grid_thw, video_grid_thw)
308318

0 commit comments

Comments
 (0)