Skip to content

fix: resolve the late XLA vs eager greedy divergences (Gemma3, Phi4MM vision, Gemma3n pin) - #3404

Merged
inureyes merged 7 commits into
mainfrom
fix/issue-3391-late-greedy-divergences
Nov 28, 2026
Merged

inureyes merged 7 commits into
mainfrom
fix/issue-3391-late-greedy-divergences

Conversation

@inureyes

Copy link
Copy Markdown
Member

Summary

Resolve the three late XLA versus eager greedy divergences from #3391 by measuring the top-2 logit margins on both sides at each divergence step, then documenting the near-ties and re-pinning the Gemma3n prefix-decode top1 with the measured margins recorded next to the pin.

Findings and what changed

  • Gemma3 (gemma-3-4b-it-4bit, test_image.png, -n 16): the first 15 tokens match; at the divergence step both arms rank the same top-2 set {2258, 3355}. Eager picks 2258 by 0.25 (exactly one bf16 ulp at a ~36-logit magnitude), XLA picks 3355 by 0.87. A documented cross-backend near-tie, not a defect. New probe test pinned_gemma3_greedy_margin_probe drives both loops the way the CLI does, records per-step margins, and asserts the top-2 candidate-set agreement at every step, so a real numeric divergence fails it.
  • Phi4MM vision (phi-4-multimodal-instruct): the bisect finds the path bit-identical at every stage (preprocessing, per-crop encoder hidden states, pooled and projected features, all max abs 0.0) and the CLI repro produces identical tokens on both backends, so the reported divergence does not reproduce on current main. New probe test pinned_phi4mm_vision_bisect_probe gates the stage parity a future loader change would break.
  • Gemma3n: the eager prefix-decode now ranks 236798 first, leading 854 by a 0.3125-logit margin at a ~14-logit magnitude, while the mlx-vlm 0.7.8 CPU oracle still ranks 854 by 0.75 and the XLA side ranks 236798 by 0.375. A cross-build, cross-device close-ranking flip; the pin moves from 854 to 236798 with the margins recorded in the comments at both assert sites. The re-pinned one-row test passes.

What changed

  • src/loading/vlm_gemma3_margin_tests.rs (new): the Gemma3 greedy margin probe, plus XlaPreparedPrefillLogits::decode_ragged_logits in src/lib/mlxcel-xla/src/batch.rs as the teacher-forced continuation shim.
  • src/loading/vlm_phi4mm_margin_tests.rs (new): the Phi4MM three-stage vision bisect probe.
  • src/models/gemma3n.rs: prefix-decode top1 re-pinned 854 to 236798, measured margins documented at both assert sites.

Test plan

  • Re-pinned Gemma3n one-row canonical test passes (1 passed, 0 failed).
  • Both new probes pass on the pinned checkpoints (Gemma3 margins dumped, Phi4MM all-stage max abs 0.0).
  • cargo check --features cuda,xla-iree --all-targets and cargo check --no-default-features --features xla-diagnostics --all-targets finish with no errors.
  • Phi4MM CLI greedy run produces identical 16 tokens on eager (SDPA deterministic) and XLA CUDA.

Closes #3391

Issue #3391 needs the top-2 logit margin measured on both sides at each
late XLA versus eager greedy divergence before deciding near-tie versus
defect. Two pinned probe tests record those margins and gate the parity
they measured.

pinned_gemma3_greedy_margin_probe (new
src/loading/vlm_gemma3_margin_tests.rs) drives the production eager
greedy loop and the XLA prepared-prefill loop the way the CLI does over
the pinned gemma-3-4b-it-4bit fixture, dumps the per-step top-2 margins
on both arms, and asserts the top-2 candidate-set agreement at every
step: a real numeric divergence fails it, a ranking flip inside a
near-tie set is documented. Measured on GB10: at the divergence step
both arms rank {2258, 3355}; eager picks 2258 by 0.25 (exactly one bf16
ulp at a ~36-logit magnitude) and XLA picks 3355 by 0.87, so the token
16 flip is a cross-backend near-tie, not a defect.

The probe's teacher-forced continuation runs through a new
XlaPreparedPrefillLogits::decode_ragged_logits shim in
src/lib/mlxcel-xla/src/batch.rs, which exposes the engine's ragged
decode with full per-row logits for diagnostics.

pinned_phi4mm_vision_bisect_probe (new
src/loading/vlm_phi4mm_margin_tests.rs) bisects the Phi4MM vision path
stage by stage against eager (preprocessing, per-crop encoder hidden
states, pool and projection) and names the first diverging stage.
Measured: the path is bit-identical at every stage (max abs 0.0) and
the CLI repro produces identical tokens on both backends, so the
reported divergence does not reproduce on current main; the probe gates
that parity a future loader change would break.

Both tests are #[ignore]d (they need the pinned checkpoints plus MLX
and IREE CUDA) and run with --test-threads=1 under
--features cuda,xla-iree. Refs #3391.
The Gemma3n canonical pinned test
xla_canonical_diagnostics::pinned_e2b_intermediates_prefill_decode_and_greedy_match
failed its production-CUDA prefix-decode top1 pin: pinned 854, observed
236798. The old comment called it an unproven close-ranking
cross-device flip, which did not satisfy the issue's rule that a
re-pin requires a proven cause with the measured margin.

Measured on GB10 at the fc344c98 MLX pin: the eager prefix-decode ranks
236798 first, leading 854 by a 0.3125-logit margin at a ~14-logit
magnitude, the XLA side agrees on 236798 by 0.375, and the mlx-vlm
0.7.8 CPU oracle ranks 854 first by 0.75. A cross-build, cross-device
close-ranking flip: both top-2 sets are {854, 236798}, the ranking
flips with the toolchain and device, and the margins sit within a few
bf16 steps at that magnitude. The pin tracks the production CUDA
numerics.

Both assert sites in the two pinned tests now pin 236798 with the
measured margins recorded next to the pin. The re-pinned one-row test
passes (1 passed, 0 failed). Refs #3391.
@inureyes inureyes added status:review Under review type:bug Bug fixes, error corrections, or issue resolutions priority:medium Medium priority area:inference Generation, sampling, decoding (incl. speculative, DRY) labels Nov 24, 2026
@inureyes inureyes added the area:architecture Architecture and code structure changes label Nov 26, 2026
@inureyes
inureyes merged commit c22824d into main Nov 28, 2026
31 of 33 checks passed
@inureyes
inureyes deleted the fix/issue-3391-late-greedy-divergences branch November 28, 2026 10:28
inureyes added a commit that referenced this pull request Nov 28, 2026
PR #3404 merged after the folder split (#3506) and filed its report at the TECHNICAL_REPORTS/ root, which the layout check now rejects on main.
inureyes added a commit that referenced this pull request Nov 28, 2026
* docs(reports): generate an index for TECHNICAL_REPORTS

TECHNICAL_REPORTS/ held 980 files (490 en/ko pairs) with no index and no inbound link, so a report could not be found or mapped to its PR. A hand-kept table would drift on the next report, so the index is generated.

- scripts/ci/gen_reports_index.py writes TECHNICAL_REPORTS/README.md: one row per report stem, newest number first, with the PR link, the cleaned H1 title, the date (file-name suffix, else the header Date/Updated line) and en/ko links. --check exits 1 when the committed index is stale.
- tests/test_gen_reports_index.py covers the stale cases (new report, retitled report, hand edit, missing index), unpaired and pipe-bearing titles, and asserts the committed index is current and links every report file. It runs in verify-python-tooling and CI's python-tooling job, so a report PR that skips `make reports-index` fails there.
- Makefile gains reports-index and verify-reports-index; docs/README.md points at the directory.

TECHNICAL_REPORTS/ is gitignored, so the README is force-added like the reports.

Closes #1708

* docs: move the PR #3404 report into the 0.9 folder

PR #3404 merged after the folder split (#3506) and filed its report at the TECHNICAL_REPORTS/ root, which the layout check now rejects on main.
@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:medium Medium priority status:review Under review type:bug Bug fixes, error corrections, or issue resolutions

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix: resolve the late XLA vs eager greedy divergences (Gemma3, Phi4MM vision, Gemma3n pin)

1 participant