Skip to content

fix(xla): size CLI image requests from the processed media - #3388

Merged
inureyes merged 4 commits into
mainfrom
fix/issue-3361-xla-image-sizing
Nov 24, 2026
Merged

inureyes merged 4 commits into
mainfrom
fix/issue-3361-xla-image-sizing

Conversation

@inureyes

@inureyes inureyes commented Nov 24, 2026 •

Copy link
Copy Markdown
Member

Summary

The OpenXLA CLI now prepares a request's images (or Phi4MM media) before it compiles its one context shape. It sizes the shape and admission from PreparedPrefill::sequence_len, the expanded prompt the family preprocessor actually produced. The serve worker already routes on that same length. The per-family worst-case floor is no longer added on top, so Qwen2-VL stops compiling 32768 for a 64-token image, and Gemma3, Molmo v1 and Phi4MM no longer get a 256 shape they cannot fit.

  • multimodal::xla_image_prefill holds the one rule: PreparedImagePrefill, prepare_image_prefill (DeepStack first) and xla_request_positions. The CLI, XlaBackendSession::generate_images_greedy and the server preprocess stage all use it.
  • backend::xla::create_prepared_xla_session builds the request-sized session and takes over the preprocessor that prepared the images, so vision weights load once.
  • xla_image_context_floor now only picks the server's startup buckets and guards an unsized session. Family vision limits still fail closed, and the Qwen3-VL 512-patch IREE vision bound is documented in the module docs and in docs/environment-variables.md.

Verification (GB10, XLA cuda, tests/fixtures/test_image.png, -n 16 --temp 0, eager with MLXCEL_SDPA_DETERMINISTIC=1)

Model Old shape New shape (prompt) Tokens vs eager
qwen2-vl-2b-instruct-4bit 32768 (OOM) 256 (89) 11/11 match
qwen2.5-vl-3b-instruct-4bit 32768 (OOM), pinned 256 refused 256 (89), pinned 256 admitted 16/16 match
qwen3-vl-2b-instruct-4bit 256 256 (78) 16/16 match
gemma-3-4b-it-4bit 256, refused 512 (272) 15/16, known token-16 divergence
molmo-7b-d-0924-4bit 256, refused 512 (325) 16/16 match
youtu-vl-4b-instruct 256 256 (74) admitted; output broken by #3364
phi-4-multimodal-instruct 256, refused 1024 (552) 16/16 match with #3380's fix applied locally; on main prefill fails with the #3362 LoRA transpose
llava-interleave-qwen-0.5b-bf16 1024 1024 (742) 16/16 match

Each XLA run matched the same binary pinned at MLXCEL_XLA_CONTEXT_CAPACITY=1024. llava-1.5-7b-4bit cannot load its XLA image preprocessor on main (text_config.hidden_size missing), and that is unrelated to sizing.

After review: the Qwen2-VL/Qwen2.5-VL 256-patch vision cap (QWEN2_VL_PATCH_BUCKETS) is documented next to the Qwen3-VL 512 and Youtu-VL 256 caps and pinned by a refusal test; CLI preparation_time excludes the one-time vision weight load and compile, as before; the unpinned Phi4MM producer is bounded by min(window, context ladder top); a two-image test pins multi-image sizing.

  • Per-family expansion and sizing unit tests (multimodal::xla_image_prefill::tests, including the large-image refusals for Qwen2-VL, Qwen3-VL and Youtu-VL and a two-image case), server xla_preprocess tests, CLI sizing tests (18 pass)
  • cargo check --features cuda,xla-iree --all-targets, cargo check --no-default-features --features xla-diagnostics --all-targets, cargo clippy --features cuda,xla-iree --lib --bins -D warnings, scripts/insert_apache_header.py --check

Closes #3361

Since #2856 the OpenXLA CLI added a per-family worst-case image expansion to the text prompt before compiling its one context shape. Qwen2-VL and Qwen2.5-VL added the 16384-token maximum-pixel floor, so a 64-token 224x224 image compiled a 32768 shape that ran out of CUDA memory and was refused under MLXCEL_XLA_CONTEXT_CAPACITY=256. Gemma3, Molmo v1 and Phi4MM had no floor and compiled a 256 shape their expanded prompt (272, 325, 552 tokens) could not fit.

The CLI now decodes and prepares the images (or the Phi4MM media) before it builds the session and sizes the shape and admission from PreparedPrefill::sequence_len, the expanded length the serve worker already routes on. The new multimodal::xla_image_prefill module holds that rule: PreparedImagePrefill and prepare_image_prefill (DeepStack first) are shared by the CLI, XlaBackendSession and the server preprocess stage. backend::xla::create_prepared_xla_session takes over the preprocessor that prepared the request so vision weights load once. The worst-case floor now only chooses the server's startup buckets and guards an unsized session. Family vision limits stay fail-closed, including the Qwen3-VL 512-patch IREE vision bound, which is documented.

Verified on GB10 XLA cuda against eager for Qwen2-VL, Qwen2.5-VL (also pinned 256), Qwen3-VL, Gemma3, Molmo v1, Youtu-VL, Phi4MM and LLaVA-interleave; per-family expansion unit tests pin the sizing.

Refs #3361
@inureyes inureyes added status:review Under review type:bug Bug fixes, error corrections, or issue resolutions priority:high High priority area:inference Generation, sampling, decoding (incl. speculative, DRY) labels Nov 24, 2026
@inureyes

Copy link
Copy Markdown
Member Author

Implementation Review Summary

Intent

Size OpenXLA CLI image requests (and Phi4MM media) from the prompt the family preprocessor actually produced, sharing one rule with the serve worker, so no per-family worst case is added on top (#3361, including the Phi4MM comment).

Findings Addressed

No CRITICAL or HIGH findings, so no fixes were pushed.

Remaining Items

  • qwen2_vl_sizes_a_large_image_from_its_resized_grid asserts a 4096 shape for a 2048x1536 image, but the Qwen2-VL IREE vision path packs all images into at most 256 patches (QWEN2_VL_PATCH_BUCKETS), so production refuses that image with "exceeding qualified capacity 256". The module docs list the Qwen3-VL 512 and Youtu-VL 256 caps but not this one. Suggest asserting the refusal bound instead and adding Qwen2-VL to the module docs. (MEDIUM)
  • CLI preparation_time now covers load_xla_image_preprocessor (vision weight load and IREE vision compile), which used to sit in untimed session creation, so the reported image-request throughput includes one-time load cost. Timing only prepare_image_prefill keeps the old accounting. (MEDIUM)
  • No multi-image sizing test. The shared dispatch test uses one image. Sizing is correct by construction (sequence_len covers every block, and the Qwen-VL and Youtu-VL patch caps are packed totals), but a two-image case would pin it. (LOW)
  • The XlaBackend::create_session comment still says the request-sized path "includes the image expansion in its hint". (LOW)
  • Unpinned, the Phi4MM producer is bounded by the checkpoint window, not min(window, ladder top). A prompt between 32768 and the window computes text embeddings before the resolver refuses it. This is CLI-only and bounded by the image count limit, dynamic_hd, and the audio policy, and it fails closed. (LOW)

Verification

  • All stated requirements implemented
  • No placeholder/mock code remaining
  • Integrated into project code flow
  • Project conventions followed
  • Existing modules reused where applicable
  • No unintended structural changes
  • Tests pass

Server path: the stage now calls the shared prepare_image_prefill with the same DeepStack-first order and the same error.to_string() messages. model_worker still picks startup buckets from xla_image_context_floor, so routing and admission are unchanged. CLI: images are decoded and prepared once, and the preprocessor is handed to create_prepared_xla_session, so vision weights load once. Phi4MM maps to no session preprocessor, so it has no double load either. Fail-closed: a pinned capacity bounds the Phi4MM producer, and an unpinned shape comes from the resolver, which never returns a capacity below required (it errors instead), and admission bails at zero room. The Gemma3n audio path is moved but behaves the same: it is sized from the text prompt and prepared against s.context_capacity().

Narrow tests: cargo test --profile test-fast --features cuda,xla-iree --lib -- xla_image_prefill xla_preprocess --test-threads=1, 17 passed.

Apply the review findings on the request-sized XLA image path. The Qwen2-VL test asserted a 4096 shape for a 2048x1536 image that production refuses at the 256-patch vision bucket (QWEN2_VL_PATCH_BUCKETS), so it now asserts that refusal bound, and the module docs and environment-variables.md list the Qwen2-VL and Qwen2.5-VL cap next to the Qwen3-VL and Youtu-VL ones. A two-image Qwen2.5-VL test pins that sequence_len counts both expansions.

The CLI preparation time no longer includes the one-time vision weight load and compile, so reported image throughput accounting matches the earlier behavior. The unpinned Phi4MM producer is bounded by the top of the context ladder, so an over-ladder prompt is refused before embeddings are computed. The stale create_session comment is corrected.

Refs #3361
@inureyes inureyes added status:done Completed and removed status:review Under review labels Nov 24, 2026
@inureyes
inureyes marked this pull request as ready for review November 24, 2026 04:17
@inureyes inureyes closed this Nov 24, 2026
@inureyes
inureyes deleted the fix/issue-3361-xla-image-sizing branch November 24, 2026 06:35
@inureyes
inureyes restored the fix/issue-3361-xla-image-sizing branch November 24, 2026 06:35
@inureyes inureyes reopened this Nov 24, 2026
@inureyes
inureyes merged commit f250c72 into main Nov 24, 2026
33 checks passed
@inureyes
inureyes deleted the fix/issue-3361-xla-image-sizing branch November 24, 2026 07:02
@inureyes inureyes added this to the 0.9 milestone Nov 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:inference Generation, sampling, decoding (incl. speculative, DRY) priority:high High priority status:done Completed type:bug Bug fixes, error corrections, or issue resolutions

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix: size OpenXLA CLI image requests from the processed media

1 participant