Skip to content

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

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

11098489 wants to merge 1 commit 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.

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

1 participant