Skip to content

Commit f257f08

Browse files
committed
fix qwen2vl video processor t padding
1 parent f91ae0f commit f257f08

File tree

3 files changed

+26
-5
lines changed

3 files changed

+26
-5
lines changed

src/transformers/models/qwen2_vl/video_processing_qwen2_vl.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -232,9 +232,10 @@ def _preprocess(
232232
patches = stacked_videos
233233

234234
# Check that videos have `num_frames` divisible by `temporal_patch_size`
235-
if patches.shape[1] % temporal_patch_size != 0:
236-
repeats = patches[:, -1:].repeat(1, self.temporal_patch_size - 1, 1, 1, 1)
237-
patches = torch.cat([patches, repeats], dim=1)
235+
T = patches.shape[1]
236+
if pad := -T % temporal_patch_size:
237+
repeats = patches[:, -1:].expand(-1, pad, -1, -1, -1)
238+
patches = torch.cat((patches, repeats), dim=1)
238239

239240
batch_size, grid_t, channel = patches.shape[:3]
240241
grid_t = grid_t // temporal_patch_size

tests/models/qwen2_vl/test_video_processing_qwen2_vl.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,3 +343,22 @@ def test_call_sample_frames(self):
343343

344344
# Assign back the actual num frames in tester
345345
self.video_processor_tester.num_frames = prev_num_frames
346+
347+
def test_num_frames_equal_temporal_patch_size_plus_two(self):
348+
for video_processing_class in self.video_processor_list:
349+
video_processor_dict = self.video_processor_dict.copy()
350+
video_processor_dict["size"] = {"longest_edge": 5 * 28 * 28, "shortest_edge": 28 * 28}
351+
video_processor_dict["do_sample_frames"] = False
352+
temporal_patch_size = 3
353+
video_processor_dict["temporal_patch_size"] = temporal_patch_size
354+
video_processing = video_processing_class(**video_processor_dict)
355+
356+
n, w, h = 5, 28, 28
357+
video_inputs = [(np.random.randint(0, 256, (h, w, 3), dtype=np.uint8)) for _ in range(n)]
358+
359+
video_processed = video_processing(video_inputs, return_tensors="pt")
360+
encoded_videos = video_processed[self.input_name]
361+
self.assertEqual(list(encoded_videos.shape), [8, temporal_patch_size * 3 * 14 * 14])
362+
363+
video_grid_thw = video_processed["video_grid_thw"]
364+
self.assertEqual(video_grid_thw.tolist(), [[2, 2, 2]])

tests/models/qwen3_vl/test_video_processing_qwen3_vl.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -352,15 +352,16 @@ def test_num_frames_equal_temporal_patch_size_plus_two(self):
352352
video_processor_dict = self.video_processor_dict.copy()
353353
video_processor_dict["size"] = {"longest_edge": 5 * 32 * 32, "shortest_edge": 32 * 32}
354354
video_processor_dict["do_sample_frames"] = False
355-
video_processor_dict["temporal_patch_size"] = 3
355+
temporal_patch_size = 3
356+
video_processor_dict["temporal_patch_size"] = temporal_patch_size
356357
video_processing = video_processing_class(**video_processor_dict)
357358

358359
n, w, h = 5, 32, 32
359360
video_inputs = [(np.random.randint(0, 256, (h, w, 3), dtype=np.uint8)) for _ in range(n)]
360361

361362
video_processed = video_processing(video_inputs, return_tensors="pt")
362363
encoded_videos = video_processed[self.input_name]
363-
self.assertEqual(list(encoded_videos.shape), [8, 2304])
364+
self.assertEqual(list(encoded_videos.shape), [8, temporal_patch_size * 3 * 16 * 16])
364365

365366
video_grid_thw = video_processed["video_grid_thw"]
366367
self.assertEqual(video_grid_thw.tolist(), [[2, 2, 2]])

0 commit comments

Comments
 (0)