Skip to content

fix(xla): expand Youtu-VL MLA latents in the transposed weight layout - #3393

Merged
inureyes merged 4 commits into
mainfrom
fix/issue-3364-youtu-vl-xla-repeat
Nov 24, 2026
Merged

inureyes merged 4 commits into
mainfrom
fix/issue-3364-youtu-vl-xla-repeat

Conversation

@inureyes

@inureyes inureyes commented Nov 24, 2026 •

Copy link
Copy Markdown
Member

Summary

MLXCEL_BACKEND=xla mlxcel generate on Youtu-VL repeated token 35 (D) for image and text-only prompts on cuda and local-task. Root cause: the single-sequence session (IreeLlama) uploads projections transposed to [in, out] since #2859, but mla_expand_latent still reshaped kv_b_proj as the checkpoint's [n_q, nope + v, kv_lora_rank], scrambling every head's keys and values. #872's MLA landed before #2859, so it was correct then. The oracle gate and the server use the batch engine (IreeRaggedLlama), which keeps the checkpoint [out, in] layout, so both stayed correct.

  • The MLA latent expansion now splits and contracts kv_b on the axes of the layout the graph declares (OutIn: [n_q, d, r] on axis 2; InOut: [r, n_q, d] on axis 0). The output shapes are unchanged.
  • The Youtu-VL oracle gate now also loads the production XlaInferenceSession that the CLI builds. On the image prompt its greedy tokens must equal the HF greedy_tokens. On a text-only prompt they must equal the batch engine's (new greedy_from_tokens: slot token prefill, then ragged decode). validate_youtu_vl_reference.sh passes MLXCEL_XLA_DEVICE to the run. The 27-stage comparison is unchanged.
  • Emitter unit test mla_latent_expansion_follows_the_projection_layout pins the per-layout reshape.

Same regression class: StarCoder2

Review audited every emitter consumer of a weight the single-sequence loader transposes under InOut (#2859) without going through linear_proj. One more site was broken: StarCoder2's mlp.c_fc.weight was pushed as WeightSpec::Whole while the emitter declares it through take_weight, so starcoder2-3b-4bit failed to load on XLA (expected shape 3072x12288, actual shape 12288x3072 on local-task, expected f16 but have f32 on cuda). It now goes through push_proj like c_proj; XLA cuda, XLA local-task and eager give the same 12 tokens. The new weights::tests::every_family_declares_weights_the_way_the_loader_uploads_them emits prefill, decode and ragged graphs for every bundled asset config plus Youtu-VL and Mamba2 in both layouts and both precisions and checks each declared weight against what the loader uploads; it failed on StarCoder2 before the fix. The full audit table is in the review comment.

Test plan

  • Gate on cuda with main's emitter: production smoke fails with [35, 35, 35, 35] vs HF [576, 2870, 5658, 261]
  • Gate with the fix, cuda and local-task: 27 stages pass, first_divergence=null, production image [576, 2870, 5658, 261], text-only [9495, 13, 128001, 198]
  • CLI on youtu-vl-4b-instruct, --temp 0 -n 16, eager with MLXCEL_SDPA_DETERMINISTIC=1: XLA cuda and local-task token ids equal eager for the image prompt (837 2870 371 261 6673 11 15589 2600 324 10282 19030 2467 13 2613 516 750), a chat text prompt (837 6849 324 7664 371 9495 13) and a --no-chat-template text prompt (9495 13)
  • mlxcel-server eager, XLA cuda and XLA local-task return identical text for the image chat, text chat and /v1/completions requests
  • cargo test -p mlxcel-xla --lib emitter::tests::mla_, cargo check --features cuda,xla-iree --all-targets, cargo check --no-default-features --features xla-diagnostics --all-targets, python3 scripts/insert_apache_header.py --check

Closes #3364

The single-sequence OpenXLA session uploads projection weights transposed to `[in, out]` (#2859), but the MLA latent expansion still reshaped `kv_b_proj` as the checkpoint's `[n_q, nope + v, kv_lora_rank]`. Every head's keys and values were scrambled, so `MLXCEL_BACKEND=xla mlxcel generate` on Youtu-VL repeated token 35 for image and text-only prompts. The continuous-batching engine keeps the checkpoint layout, which is why the HF oracle gate (and the server) stayed correct. The expansion now splits and contracts `kv_b` on the axes of the layout the graph declares.

The Youtu-VL oracle gate now also loads the production session the CLI builds and requires its greedy tokens to match the HF reference on the image prompt and the gated batch engine on a text-only prompt. An emitter unit test pins the per-layout reshape.

Closes #3364
@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
The loader pushed StarCoder2's `mlp.c_fc.weight` as a `Whole` spec while the emitter declares it through `take_weight`, so the single-sequence `InOut` session failed at prefill (`expected shape 3072x12288, actual shape 12288x3072` on local-task, f16 vs f32 on cuda). This is the same #2859 regression class as the Youtu-VL MLA latent and the Phi4MM LoRA fixes. `c_fc` now goes through `push_proj` like `c_proj`.

A new unit test emits prefill, decode and ragged graphs for every bundled family plus Youtu-VL and Mamba2, in both layouts and in f32 and f16, and checks that each declared weight argument has the shape and element type the loader uploads for its spec. It fails on StarCoder2 without this change.

On starcoder2-3b-4bit, `--temp 0 -n 12` now gives the same 12 token ids on XLA cuda, XLA local-task and eager.

Refs #3364
@inureyes

Copy link
Copy Markdown
Member Author

Implementation Review Summary

Intent

Fix the Youtu-VL single-sequence (InOut) MLA latent expansion and add a production-session smoke stage to the oracle gate (#3364).

Findings Addressed

Remaining Items

Audit of InOut consumers

Every Dense projection consumer outside linear_proj was checked: q/k/v/o, gate/up/down, fused Rows (Phi3, Molmo, Molmo2), MLA q_a/q_b/kv_a/o, the head (tied and untied), SSM in_proj/out_proj, and packed and native operands. All of them go through linear_proj, linear_seq_proj, gate_up_proj or the layout branch in take_weight. MLA kv_b is the only direct reshape on a projection, and this PR fixes it. MoE experts, router and shared expert, the SSM conv weights, and the Phi4MM LoRA pairs (on main) are Whole specs, which are never transposed, and their declarations do not change with the layout. Gemma3n and the vision towers pin OutIn and use their own loaders. The new declaration test passed on the configs of 18 real checkpoints, including molmo-7b, molmo2-4b, mixtral, qwen1.5-moe, qwen3-moe, olmoe, gemma 1/2/3, exaone, granite, internlm3, mimo, mamba2 and youtu-vl. It failed only on StarCoder2.

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

mla_latent_expansion_follows_the_projection_layout fails against the pre-fix model.rs ("InOut must expand kv_b on its transposed axes"). On GB10, Youtu-VL XLA cuda gives the same tokens as eager for the issue's text prompt. StarCoder2-3b gives the same 12 tokens on XLA cuda, XLA local-task and eager. cargo test -p mlxcel-xla --lib -- weights:: passes, and so do clippy -D warnings and fmt.

@inureyes inureyes added status:done Completed area:architecture Architecture and code structure changes and removed status:review Under review labels Nov 24, 2026
@inureyes
inureyes marked this pull request as ready for review November 24, 2026 06:36
@inureyes
inureyes merged commit 4e24a12 into main Nov 24, 2026
33 checks passed
@inureyes
inureyes deleted the fix/issue-3364-youtu-vl-xla-repeat branch November 24, 2026 07:19
@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:architecture Architecture and code structure changes 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: Youtu-VL production XLA path repeats one token

1 participant