Repository navigation
Conversation
Video frames are padded up to a multiple of temporal_patch_size, but smart_resize checked the budget with a round()-based t_bar (which can under-count odd frame counts) and computed beta with the unpadded num_frames, so the padded output could exceed max_pixels. Use ceil() for t_bar (restoring the semantics of huggingface#42083, unintentionally changed by the modularization in huggingface#47573) and compute both beta values from t_bar. Hand-edited the two glm4v sources and propagated via `python utils/checkers.py copies,modular_conversion --fix` to all affected qwen-like processors. Add regression tests for odd-frame budget enforcement and even-frame/image no-op behavior. Fixes huggingface#50871
|
Thank you for your contribution 🤗! CI Security Gate — waiting for maintainer approvalCI is approved automatically only for authors who already have a contribution merged in this repository. A maintainer will review this PR and approve CI to run on it. |
|
|
||
| if t_bar * h_bar * w_bar > max_pixels: | ||
| beta = math.sqrt((num_frames * height * width) / max_pixels) | ||
| beta = math.sqrt((t_bar * height * width) / max_pixels) |
There was a problem hiding this comment.
are we sure the budget needs t-bar since the H/W are used as raw values? Do you have a ref to original implem to check with?
There was a problem hiding this comment.
Yes — the mix is intentional and matches the original Qwen implementation. Reference: the official qwen-vl-utils in QwenLM/Qwen3-VL, vision_process.py:
- L430-L432: frames are padded up with
ceil_by_factor(len(image_list), FRAME_FACTOR)(last frame repeated). - L448-L451: the per-frame budget is derived from the padded tensor shape:
nframes, _, height, width = video.shapethenmax_pixels = ... total_pixels / nframes * FRAME_FACTOR .... - L73-L76:
beta = math.sqrt((height * width) / max_pixels)uses raw H/W, combined withfloor_by_factor.
So upstream divides the total budget by the padded frame count first, then computes beta from raw H/W; this PR is the algebraic equivalent for the 3D formulation (beta = sqrt(t_bar * H * W / total_budget)).
Raw H/W in beta is safe by design: floor rounding only shrinks the output, so beta computed from raw values is already conservative. The frame count is the only dimension that gets padded up, so it is the only one that must use the padded value — otherwise t_bar * h_bar * w_bar > max_pixels still holds after resizing (the 57-frame repro in the description).
Regarding the other affected families: all seven processors share the exact same frame-padding code (if pad := -num_frames % temporal_patch_size, last frame repeated) and the same smart_resize source (glm4v, propagated via copies/modular), so the same inconsistency applies to each of them. For the GLM family there is no separate upstream to compare against — the official zai-org/GLM-V inference code uses AutoProcessor.from_pretrained directly, i.e. the transformers processor is the reference implementation, and it follows the qwen-format design referenced above.
| def test_smart_resize_odd_frames_processor_output_fits_budget(self): | ||
| size = {"longest_edge": 20480, "shortest_edge": 4096} | ||
| for video_processing_class in self.video_processor_list: | ||
| encoded = self._process_frames(video_processing_class, 5, size, frame_size=64) | ||
| # 5 frames pad to 6 (3 temporal groups); the 32x32 output gives a 2x2 patch grid. | ||
| self.assertEqual(list(encoded.shape), [12, 3 * 2 * 16 * 16]) | ||
|
|
There was a problem hiding this comment.
can we keep only tests that call into processor instead of testing helper fns?
There was a problem hiding this comment.
Done — reworked the tests to go through the processor only. The odd-frame cases now assert the encoded output shapes (including a CI-friendly downscale case equivalent to the 57-frame report), and the even-frame guard checks processor outputs are unchanged.
Per review feedback, drop the tests calling the smart_resize helper directly and cover the same behavior via processor outputs: odd-frame budget enforcement (5-frame 64x64 and a CI-friendly downscale case) and an even-frame no-op guard.
|
[For maintainers] Suggested jobs to run (before merge) run-slow: cohere_compass, cosmos3_edge, glm46v, glm4v, glmga, hy_v4_vl, qwen3_vl |
What does this PR do?
Fixes #50871
Video processors pad the frame count up to a multiple of
temporal_patch_size(e.g. 57 → 58, 5 → 6), butsmart_resizesizes the spatial dimensions against the unpadded frame count. As a result, the final padded output can exceedmax_pixels(size.longest_edge), which breaks downstream consumers that pre-allocate buffers from that budget (e.g. vLLM's encoder cache:24882 > 24576embedding tokens for a 57-frame 720x1280 video with a 50331648-pixel budget).Two issues in
smart_resize:t_bar = round(num_frames / temporal_factor) * temporal_factorcan under-count odd frame counts (round(5 / 2) * 2 = 4, while padding always pads up to 6).ceilwas originally introduced in fix qwen2vl/qwen3vl video processor temporal padding when num_frames%temporal_patch_size!=1 #42083 together with the frame-padding fix; it appears to have been unintentionally changed back toroundduring the modularization in Modularize qwen-format vision processors #47573 (no review discussion mentions this change). This PR restores theceilsemantics.betacomputations use the unpaddednum_frames, so even with a correctt_barthe downscale factor is too small andt_bar * h_bar * w_bar > max_pixelsstill holds after resizing. Both branches now computebetafromt_bar.Both changes are needed together: with
ceilonly, the 5-frame repro below still returns 64x64 (beta ≈ 1); with thebetafix only,t_bar = 4under-counts and the over-budget branch never triggers.As suggested in #50871 (comment) ("prob affects more models ... Feel free to open a PR fixing all at once"), this fixes all affected qwen-like processors in one PR.
How the change is propagated
Only two hand-written sources are edited:
glm4v/image_processing_glm4v.py(source of thesmart_resizechain)glm4v/image_processing_pil_glm4v.py(hand-written twin with no sync mechanism, inherited by the glm46v/glmga/cosmos3_edge PIL processors)The remaining 13 files are regenerated with:
python utils/checkers.py copies,modular_conversion --fix
(
copiessyncsglm4v/video_processing_glm4v.pyvia the# Copied frommarker; the modular generator inlinessmart_resizetransitively into qwen3_vl, glmga, glm46v, cosmos3_edge, and — on a second run, since they import from the generated qwen3_vl file — cohere_compass and hy_v4_vl.) Read-onlycopiesandmodular_conversionchecks pass.Affected families: qwen3_vl, glm4v, glm46v, glmga, cosmos3_edge, cohere_compass, hy_v4_vl. The 2D per-frame-budget variants (qwen2_vl, video_llama_3, etc.) have no temporal padding and are unaffected. Image inputs are unaffected: there
num_frames == temporal_factor, sot_bar == num_framesand the change is a no-op (covered by a test).Verified behavior
max_pixels=20480max_pixels=50331648Tests
Added to
tests/models/qwen3_vl/test_video_processing_qwen3_vl.py:test_smart_resize_odd_frames_respects_max_pixels— the 5-frame and 57-frame repros abovetest_smart_resize_odd_frames_processor_output_fits_budget— end-to-end processor output shape for the 5-frame casetest_smart_resize_even_frames_and_images_unchanged— guards that even-frame and image behavior is untouchedqwen3_vlandglm4vvideo/image processing suites pass locally (64 passed). Other affected families' suites were also run; the only failures reproduce identically on unmodifiedmain(local environment issues, unrelated to this change).Out of scope / follow-up
The
cap_pixels_per_framepath computesmax_pixels = pixels_per_frame * num_frameswith the unpadded count before callingsmart_resize. With this fix the output is guaranteed to fit that (slightly under-counted) budget, but the budget itself arguably should also use the padded count. Happy to address it here or in a follow-up — would appreciate a maintainer's opinion (@zucchini-nlp).Before submitting checklist
smart_resizecomputesbetawith unpaddednum_frames, so padded output exceedsmax_pixels(size.longest_edge) #50871copies,modular_conversion) pass