Skip to content

Preserve registered Gemma prompt projection and expert reductions - #345

Closed
jonathan308 wants to merge 2 commits into
fix/cluster-mimo-paged-historyfrom
fix/cluster-gemma-prefill-reductions
Closed

jonathan308 wants to merge 2 commits into
fix/cluster-mimo-paged-historyfrom
fix/cluster-gemma-prefill-reductions

Conversation

@jonathan308

@jonathan308 jonathan308 commented Oct 23, 2026 •

Copy link
Copy Markdown

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

flowchart TD
  A["Registered multi-token prompt"] --> B["Wide dense and sorted expert reductions"]
  B --> C["Architecture-dependent output bytes"]
  classDef default fill:#e8f0fe,stroke:#174ea6,color:#102a43;
  classDef failure fill:#fde2e2,stroke:#9b1c1c,color:#571111;
  class C failure;
Loading

After

flowchart TD
  A["Explicit eligible prompt scope"] --> B["Packed M=1 projection per token"]
  A --> C["Original complete expert call per token"]
  B --> D["Original rectangular normalization and head"]
  C --> D
  classDef default fill:#e8f0fe,stroke:#174ea6,color:#102a43;
  classDef success fill:#d7f0df,stroke:#176b39,color:#103b23;
  class B,C success;
Loading

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:

scripts/check-unit-test-files.sh
swift test -c release --filter 'Gemma4PrefillProjectionTests|Gemma4PrefillExpertTests'

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.

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant