Skip to content

feat(xla): add Youtu-VL vision and MLA inference (#872) - #919

Merged
inureyes merged 1 commit into
mainfrom
feature/issue-872-youtu-vl-xla
Oct 16, 2026
Merged

inureyes merged 1 commit into
mainfrom
feature/issue-872-youtu-vl-xla

Conversation

@inureyes

@inureyes inureyes commented Jul 24, 2026 •

Copy link
Copy Markdown
Member

Summary

Implements the OpenXLA / IREE execution path for Youtu-VL (youtu_vl / YoutuVLForConditionalGeneration), covering SigLIP-style windowed vision encoding, PatchMerger 2x2 spatial reduction, <|image_pad|> token-feature replacement, and Multi-head Latent Attention (MLA) prefill/decode with compressed KV cache reuse:

  • Windowed SigLIP Vision & PatchMerger (mlxcel-xla::emitter::youtu_vl, youtu_vl_runtime):
    • Emits the 27-layer Youtu-VL vision tower (emit_youtu_vl_vision_mlir) with bilinear 2D position-embedding interpolation, merged-patch window partitioning (window_index / reverse_window_indices), full-attention layer overrides (fullatt_block_indexes = [7, 15, 23, 26]), and the 2x2 PatchMerger MLP into the language model hidden dimension.
    • Exposes stage-level diagnostic exports (patch_embeddings, position_embeddings, post_embed_hidden, window_packed_hidden, pre_merger_hidden, window_restored_hidden, merged_image_features) for boundary verification.
  • Prepared Multimodal Embeddings (multimodal::youtu_vl_prompt, host_preprocessor):
    • Expands <|vision_start|><|image_pad|><|vision_end|> placeholders to match merged visual token counts and replaces <|image_pad|> positions (151669) with projected visual features while preserving non-visual text embeddings bit-identically.
  • Multi-head Latent Attention (MLA) Decoder (mlxcel-xla::emitter::model, iree):
    • Emits MLA attention (emit_mla_attention) with q_a_proj -> q_a_layernorm -> q_b_proj, kv_a_proj_with_mqa -> kv_a_layernorm, per-head kv_b_proj key-nope and value latent expansion, interleaved RoPE (rope_interleave = true), and compressed KV cache slabs (kv_lora_rank = 512, qk_rope_head_dim = 64).
    • Defaults MLA checkpoints (config.mla.is_some()) to f32 carrier precision and disables f16-resident projection packing unless explicitly overridden via MLXCEL_XLA_PRECISION, keeping 40-layer chained latent contractions within the 2e-2 stage parity budget across GPU and CPU backends.
  • Reference Oracle & CLI Verification (xla_youtu_vl_reference_check, spike/openxla/youtu_vl_reference_oracle.py):
    • Adds end-to-end 27-stage actual-checkpoint verification comparing the production IREE runtime against the pinned tencent/Youtu-VL-4B-Instruct (8d30a0e49662a1d628a472b12df264dbcd768753) HuggingFace reference across vision, token replacement, prefill logits, MLA cache slices, 4-step greedy decode, and warm-cache reuse.

Verification

  • Unit & Contract Tests:
    • pytest spike/openxla/test_youtu_vl_reference_oracle.py (14 passed)
    • cargo test --features rocm -p mlxcel-xla --lib -- youtu_vl (12 passed)
    • cargo test --features rocm -p mlxcel-xla --lib -- mla_runtime_defaults (1 passed)
    • cargo test --features xla-diagnostics-rocm --lib -- youtu_vl (47 passed; 0 failed)
    • cargo clippy --all-targets --features xla-diagnostics-rocm -- -D warnings (0 warnings)
  • Actual-Checkpoint 27-Stage Oracle Gate (tencent/Youtu-VL-4B-Instruct @ 8d30a0e49662a1d628a472b12df264dbcd768753):
    • ROCm HIP (--device hip, gfx1151 AMD Radeon 8060S): "passed": true, "first_divergence": null across all 27 stages:
      • pixel_values, grid_thw, window_index, cu_seqlens, cu_window_seqlens, reverse_window_indices, expanded_token_ids, image_token_mask, non_visual_embeddings: max_absolute = 0.0
      • patch_embeddings: max_absolute = 6.914e-6 (<= 1e-3)
      • position_embeddings: max_absolute = 1.907e-6 (<= 1e-3)
      • post_embed_hidden: max_absolute = 6.795e-6 (<= 1e-3)
      • window_packed_hidden: max_absolute = 6.795e-6 (<= 1e-3)
      • pre_merger_hidden / window_restored_hidden: max_absolute = 5.479e-4 (<= 1e-2)
      • merged_image_features / replaced_Visual_embeddings / prepared_embeddings: max_absolute = 3.902e-3 (<= 2e-2)
      • prefill_logits / reuse_prefill_logits: max_absolute = 5.004e-3 (<= 2e-2)
      • selected_kv / reuse_selected_kv: max_absolute = 1.594e-3 (<= 2e-2)
      • greedy_tokens / reuse_greedy_tokens: [576, 2870, 21141, 261] (max_absolute = 0.0)
    • CPU (--device local-task): "passed": true, "first_divergence": null across all 27 stages (prepared_embeddings max_abs = 4.885e-3, prefill_logits max_abs = 4.118e-3, selected_kv max_abs = 1.987e-3, greedy_tokens [576, 2870, 21141, 261] max_abs = 0.0).

Closes #872

@inureyes inureyes added status:review Under review type:enhancement New features, capabilities, or significant additions priority:low Low priority area:models Model architectures, weights, loading, metadata area:inference Generation, sampling, decoding (incl. speculative, DRY) labels Jul 24, 2026
@inureyes
inureyes force-pushed the feature/issue-872-youtu-vl-xla branch from 206566c to 7886a65 Compare July 27, 2026 00:02
@inureyes

Copy link
Copy Markdown
Member Author

Rebased onto main@7fe1412d after #934 and force-pushed as 7886a657.

Post-rebase validation:

  • cargo fmt --all -- --check
  • git diff --check
  • cargo test --lib youtu_vl: 24 passed
  • cargo test -p mlxcel-xla --features iree --lib youtu_vl: 9 passed, 2 intentionally ignored
  • real 27-layer actual-schedule IREE CPU compile gate: 1 passed

The runtime now uses the explicit new_legacy_unqualified auxiliary-artifact path. That is intentional: #934 is only the versioned numeric-contract foundation, while the complete pinned ~10 GB HF-reference-versus-production numeric report is still pending. This PR therefore remains draft and must not be merged yet; #872 remains blocked by #932.

@inureyes
inureyes force-pushed the feature/issue-872-youtu-vl-xla branch from 7886a65 to e36cb34 Compare July 27, 2026 01:55
@inureyes
inureyes force-pushed the feature/issue-872-youtu-vl-xla branch from e36cb34 to 9171492 Compare July 27, 2026 02:24
@inureyes

Copy link
Copy Markdown
Member Author

Published the bounded Youtu-VL follow-up at 20c2bffd:

  • exact logical placeholder expansion with fail-closed partial-expansion checks;
  • stale safetensors index recovery by scanning every shard named by the index;
  • pinned HF spatial-merge/channel-fast patch layout and corrected oracle reconstruction;
  • fail-fast preprocessing/selected-vision comparisons with partial diagnostic artifacts;
  • explicit BF16 vision carrier boundaries using the shared numeric helpers.

Validation:

  • Rust Youtu suite: 12 passed, 1 environment-gated ignored
  • Python oracle contract: 13 passed
  • focused patch-layout, shard-resolution, and emitter tests passed
  • fmt, clippy, and git diff --check passed

The real pinned checkpoint now reaches the unchanged intermediate gate quickly.
Preprocessing and metadata are exact; patch projection passes
(max_absolute=0.015625), and layer 0 passes. The first selected failure is
layer.7.full at flat index 310: actual 0.3125, HF 0.28515625,
absolute=0.02734375, max_absolute=0.203125, 2,999 threshold mismatches.

An explicit BF16 contraction-input experiment produced byte-identical IREE
results and was removed. The LayerNorm decomposition matches HF exactly; the
remaining drift begins at the backend BF16 projection/attention contraction
schedule. No tolerance changed. The PR remains draft and fail-closed; language
KV/logits/tokens and lifecycle gates are not claimed.

@inureyes
inureyes force-pushed the feature/issue-872-youtu-vl-xla branch from 20c2bff to 4f8547f Compare October 16, 2026 06:02
@inureyes
inureyes marked this pull request as ready for review October 16, 2026 06:02
@inureyes inureyes changed the title feat: add Youtu-VL XLA vision and MLA inference (#872) feat(xla): add Youtu-VL vision and MLA inference (#872) Oct 16, 2026
Implements the OpenXLA/IREE execution path for Youtu-VL (`youtu_vl` / `YoutuVLForConditionalGeneration`), covering SigLIP-style windowed vision encoding, `PatchMerger` 2x2 spatial reduction, `<|image_pad|>` token-feature replacement, and Multi-head Latent Attention (MLA) prefill/decode with compressed KV cache reuse.

- Emits the 27-layer Youtu-VL SigLIP vision tower (`emit_youtu_vl_vision_mlir`) with bilinear 2D position-embedding interpolation, merged-patch window partitioning (`window_index` / `reverse_window_indices`), full-attention layer overrides (`fullatt_block_indexes`), and the 2x2 `PatchMerger` projection into language hidden size.
- Implements `YoutuVlVisionRuntime` (`src/lib/mlxcel-xla/src/youtu_vl_runtime.rs`) with shape-bucketed IREE vision compilation and intermediate stage diagnostics (`patch_embeddings`, `position_embeddings`, `post_embed_hidden`, `window_packed_hidden`, `pre_merger_hidden`, `window_restored_hidden`, `merged_image_features`).
- Emits MLA attention (`emit_mla_attention` in `src/lib/mlxcel-xla/src/emitter/model.rs`) with `q_a_proj` / `q_a_layernorm` / `q_b_proj`, `kv_a_proj_with_mqa` / `kv_a_layernorm`, per-head `kv_b_proj` key-nope and value latent expansion, interleaved rotary position embeddings (`rope_interleave`), and compressed KV cache storage (`kv_lora_rank` + `qk_rope_head_dim`).
- Preserves `f32` carrier precision and disables `f16`-resident projection packing by default for MLA checkpoints (`config.mla.is_some()`) so 40-layer chained latent contractions stay within the 2e-2 reference tolerance on GPU and CPU backends.
- Adds `xla_youtu_vl_reference_check`, `spike/openxla/youtu_vl_reference_oracle.py`, and `scripts/xla/validate_youtu_vl_reference.sh` for 27-stage actual-checkpoint verification against the pinned HuggingFace reference (`tencent/Youtu-VL-4B-Instruct` @ `8d30a0e49662a1d628a472b12df264dbcd768753`).

Closes #872
@inureyes
inureyes force-pushed the feature/issue-872-youtu-vl-xla branch from 4f8547f to 335993d Compare October 16, 2026 06:19
@inureyes inureyes added status:done Completed and removed status:review Under review labels Oct 16, 2026
@inureyes
inureyes merged commit cadc636 into main Oct 16, 2026
52 of 53 checks passed
@inureyes
inureyes deleted the feature/issue-872-youtu-vl-xla branch October 16, 2026 06:38
inureyes added a commit that referenced this pull request Oct 17, 2026
…loop (#2436)

Closes #2420

The `xla-iree` serve loop never sent `GenerateEvent::Prefill` and reported the logical (placeholder) prompt length, so a media body's `usage`, `/v1/messages/count_tokens` and the streaming `message_start` that #2415 holds for the prefill report all carried the placeholder count. This makes the loop match the batch scheduler and `docs/llama-server-compat.md`.

## Changes

- `ServeState` keeps one count, `effective_prefill_len` (the expanded prepared length for media, the prompt length for text), and a `prefill_reported` flag. `ServeState::report_prefill` sends `PrefillStats { prompt_tokens, cached_tokens: 0, processed, first_token: true, prompt_ms }` once, before the first `Token` or before `Done` when the engine finishes with no token (withheld EOS), and stamps the decode state's first-token instant as `sequence.rs` does.
- `Done`, the stop-string finish and the zero-budget media finish report the expanded length. The count route follows, since the XLA worker answers it with a one-token generation.
- Every XLA image family goes through `handle_preprocessed`, including the new Molmo (#918) and Youtu-VL (#919) producers, so they are covered too. So are the DeepStack (Qwen3-VL) and Phi4MM audio/image paths. The disaggregated coordinator stays out of scope, as the issue says.
- No doc change: `docs/llama-server-compat.md` has no XLA-specific statement, and its general promise now holds on this loop.

## Tests

The new and updated tests drive the real `admit`, preprocessing and `dispatch` path with a fake engine: text gets `Prefill(2)`, the image gets `Prefill(5)`, and Phi4MM audio and image each get `Prefill(6)`, every one sent before its `Token`, with `Done` reporting the same length. They also cover zero-budget image and audio, and a bare EOS finish. The fake Phi4MM producer now expands placeholders. Two existing assertions in this module were already failing on main because the error messages had changed (`structured` is now `constrained decoding`, and `refusing text fallback` is now `image resolution cardinality mismatch`). I updated them so the module runs green.

- Fix: `server::batch::xla_worker`, 15 passed (`--release --features cuda,xla-iree`, `--test-threads=1`, under gpu-lock).
- Revert check: with the production files restored to main, 9 failed. That is the 7 new or updated tests plus the 2 stale assertions.
- `cargo clippy --release --features cuda,xla-iree --lib --tests -- -D warnings`, `cargo check --release --features cuda,xla-iree --all-targets`, `cargo fmt --check` and the repository check scripts pass.

## Real checkpoint

`~/models/mlx/gemma-3-4b-it-4bit`, `MLXCEL_BACKEND=xla MLXCEL_XLA_DEVICE=cuda MLXCEL_XLA_CONTEXT_CAPACITY=512 mlxcel-server` on the GB10 (the default 256 capacity refuses a Gemma 3 image). I sent one body (`tests/fixtures/test_image.png` plus a short prompt) to `count_tokens`, non-streaming and streaming `/v1/messages`:

| Body | Build | count_tokens | usage.input_tokens | message_start.input_tokens |
|---|---|---|---|---|
| image | main cadc636, XLA | 18 | 18 | 18 |
| image | this PR, XLA | 277 | 277 | 277 |
| image | this PR, MLX path | 277 | 277 | 277 |
| text | both builds, XLA | 17 | 17 | 17 |

In every stream `message_start` was the first event, and it was sent once. Phi4MM audio was not run on a real checkpoint (none is present locally), so the fake-producer test covers it.
@inureyes inureyes added this to the 0.8 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) area:models Model architectures, weights, loading, metadata priority:low Low priority status:done Completed type:enhancement New features, capabilities, or significant additions

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat(xla): support Youtu-VL windowed vision and MLA inference

1 participant