Skip to content

Fix video smart_resize to compute the pixel budget with padded frame counts - #51155

Open
11098489 wants to merge 2 commits into
huggingface:mainfrom
11098489:fix-video-smart-resize-padded-frames
Open

11098489 wants to merge 2 commits into
huggingface:mainfrom
11098489:fix-video-smart-resize-padded-frames

Conversation

@11098489

@11098489 11098489 commented Nov 24, 2026 •

Copy link
Copy Markdown

🤗 Transformers CI

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), but smart_resize sizes the spatial dimensions against the unpadded frame count. As a result, the final padded output can exceed max_pixels (size.longest_edge), which breaks downstream consumers that pre-allocate buffers from that budget (e.g. vLLM's encoder cache: 24882 > 24576 embedding tokens for a 57-frame 720x1280 video with a 50331648-pixel budget).

Two issues in smart_resize:

  1. t_bar = round(num_frames / temporal_factor) * temporal_factor can under-count odd frame counts (round(5 / 2) * 2 = 4, while padding always pads up to 6). ceil was 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 to round during the modularization in Modularize qwen-format vision processors #47573 (no review discussion mentions this change). This PR restores the ceil semantics.
  2. Both beta computations use the unpadded num_frames, so even with a correct t_bar the downscale factor is too small and t_bar * h_bar * w_bar > max_pixels still holds after resizing. Both branches now compute beta from t_bar.

Both changes are needed together: with ceil only, the 5-frame repro below still returns 64x64 (beta ≈ 1); with the beta fix only, t_bar = 4 under-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 the smart_resize chain)
  • 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

(copies syncs glm4v/video_processing_glm4v.py via the # Copied from marker; the modular generator inlines smart_resize transitively 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-only copies and modular_conversion checks 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, so t_bar == num_frames and the change is a no-op (covered by a test).

Verified behavior

Case Before After
5 frames, 64x64, max_pixels=20480 64x64 → 6·64·64 = 24576 > 20480 32x32 → 6144 ≤ 20480
57 frames, 720x1280, max_pixels=50331648 704x1248 → 50958336 > 50331648 (24882 tokens) 672x1216 → 47394816 ≤ 50331648 (23142 tokens)
Sweep: odd frames 3..767 × 4 resolutions (1532 cases) 34 budget violations 0 violations
Even frame counts / image path — identical outputs (no behavior change)

Tests

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 above
  • test_smart_resize_odd_frames_processor_output_fits_budget — end-to-end processor output shape for the 5-frame case
  • test_smart_resize_even_frames_and_images_unchanged — guards that even-frame and image behavior is untouched

qwen3_vl and glm4v video/image processing suites pass locally (64 passed). Other affected families' suites were also run; the only failures reproduce identically on unmodified main (local environment issues, unrelated to this change).

Out of scope / follow-up

The cap_pixels_per_frame path computes max_pixels = pixels_per_frame * num_frames with the unpadded count before calling smart_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

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
@github-actions

Copy link
Copy Markdown
Contributor

Thank you for your contribution 🤗!

CI Security Gate — waiting for maintainer approval

CI 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)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.shape then max_pixels = ... total_pixels / nframes * FRAME_FACTOR ....
  • L73-L76: beta = math.sqrt((height * width) / max_pixels) uses raw H/W, combined with floor_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.

Comment on lines +469 to +475
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])

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we keep only tests that call into processor instead of testing helper fns?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.
@github-actions

Copy link
Copy Markdown
Contributor

[For maintainers] Suggested jobs to run (before merge)

run-slow: cohere_compass, cosmos3_edge, glm46v, glm4v, glmga, hy_v4_vl, qwen3_vl

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Qwen3-VL video smart_resize computes beta with unpadded num_frames, so padded output exceeds max_pixels (size.longest_edge)

2 participants