Repository navigation
Preserve registered Gemma prompt projection and expert reductions - #345
Closed
jonathan308 wants to merge 2 commits into
Closed
jonathan308 wants to merge 2 commits into
jonathan308 wants to merge 2 commits into
Conversation
This was referenced Oct 23, 2026
3 of 5 tasks
javiech1
pushed a commit
to javiech1/mlx-swift-lm
that referenced
this pull request
Oct 24, 2026
…) (Layr-Labs#345) **Port Qwen25VL MROPE fix (Layr-Labs#239) to Qwen2VL** Qwen2-VL was applying plain 1-D RoPE instead of 3-D multimodal positional encoding. Image tokens got sequential positions; generated tokens landed at inflated positions, causing spatial drift that grew with generation length. Text was correct but bounding boxes were wrong/short (visible in layout tasks like MinerU2.5). PR Layr-Labs#239 fixed this for Qwen2.5-VL. This is a direct port to `Qwen2VL.swift` — the spatial-position scheme is identical between models. Ported sites are marked `// Layr-Labs#239: Qwen25VL <symbol>`. Three coupled changes (matching Layr-Labs#239): - **MROPE section layout** via slice-replacement (`mropeCosSin` / `mropeSectionRaw`) - **`rope_deltas` reconstruction** for decode - **`_invFreq` underscore prefix** so the weight loader skips it Verified against the Python mlx-vlm reference: layout boxes now match coordinates exactly.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Registered Gemma prompt chunks can select different packed projection and expert reductions across Apple GPU generations. This draft adds an explicit model-local prompt scope that preserves singleton token reductions for supported BF16 affine4/8 group64 dense projections and each complete expert block.
The scope preserves selected routes and weights, custom-module fallbacks, ordinary SDK callers, verification frames, independent decode lanes, normalization and the vocabulary head. It broadcasts views of existing packed weights without a persistent duplicate bank. Provider activation is a separate child draft. This SDK draft targets its immediate parent #344.
Before
After
Validation: eight actual CPU integration tests cover dense projection bytes, routes, shared/expert composition, scope restoration, custom/dtype fallbacks and unchanged head behavior. One provider receipt test passes. Fresh 231-source SDK and 351-source Runtime compilation passes. Independent refactor review found no unresolved source issue. The five SDK files are the exact reviewed postimages transformed only by the required formatter; formatting is byte-idempotent and Swift parsing, fork ownership and test-layout checks pass.
Canonical focused suites:
Recorded CPU execution used a pinned isolated actual-source closure with an explicit CPU device. Review source SHA-256:
d348bfc0c806d697a111a906dab0557e2dfc9a80453aed503e4206bbd3e74323; independent review SHA-256:b659bbf703971074c6c60760472d7affa08252622b2b25c5c0b436381500546f.Limitations: a retained tiny CPU diagnostic shows existing rectangular normalization can differ from whole-layer tokenwise execution at 511/512 tokens. The production fix does not change normalization or claim whole-layer singleton equivalence. Actual-weight component probes, full-model cross-chip parity, allocation peaks and prompt throughput remain separate gates. This change does not implement Gemma vision/video transport or tensor parallelism. Final user acceptance requires tensor parallel for every included model and retention of every supported capability; text-only and phase-split results do not satisfy it.
Additional actual-weight diagnostic: six layer-zero shared-MLP widths (1, 8, 127, 128, 511, 512) ran on both M5 Max and M3 Ultra using the production arithmetic flags. Corrected full intermediate and post-normalization bytes match across chips for all six widths; the original wide path differs at 127/128/511/512. Original rectangular normalization is retained and its diagnostic token partition matches at the actual 2816-wide geometry. This is component evidence, not full-model or modality/TP qualification. Provider integration: d-inference #1569.